<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="字段名称" prop="fieldName">
        <el-input
          v-model="queryParams.fieldName"
          placeholder="请输入字段名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="内容名称" prop="contentName">
        <el-input
          v-model="queryParams.contentName"
          placeholder="请输入内容名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!-- <el-form-item label="结果预览" prop="resultPreview">
        <el-input
          v-model="queryParams.resultPreview"
          placeholder="请输入结果预览"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->

      <!-- <el-form-item label="内容备注信息" prop="contentRemark">
        <el-input
          v-model="queryParams.contentRemark"
          placeholder="请输入内容备注信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manage:labelfieldcontent:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:labelfieldcontent:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:labelfieldcontent:remove']"
          >删除</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="labelfieldcontentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column
        label="画像标签和知识库字段表id"
        align="center"
        prop="fieldId"
      /> -->
      <el-table-column label="字段名称" align="center" prop="fieldName" />
      <el-table-column label="内容名称" align="center" prop="contentName" />
      <el-table-column label="内容编码" align="center" prop="contentCode" />
      <el-table-column label="结果预览" align="center" prop="resultPreview" />
      <el-table-column label="内容排序" align="center" prop="contentSort" />
      <el-table-column
        label="内容备注信息"
        align="center"
        prop="contentRemark"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="画像字段标识" align="center" prop="fieldMark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:labelfieldcontent:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manage:labelfieldcontent:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加标签字段内容信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      v-if="open"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="110px"
        :inline="true"
      >
        <el-form-item label="字段信息" prop="fieldId">
          <el-select
            v-model="form.fieldId"
            placeholder="请选择字段信息"
            style="width: 260px"
            clearable
            @change="change"
          >
            <el-option
              v-for="item in labelfieldList"
              :key="item.id"
              :label="item.fieldName"
              :value="item.id"
            >
            </el-option>
          </el-select> </el-form-item
        ><br />
        <div v-for="(aitem, index) in form.fieldContentList" :key="index">
          <el-form-item
            label="内容名称"
            :rules="rules.fieldContentList.contentName"
            :prop="`fieldContentList.${index}.contentName`"
          >
            <el-input
              v-model="aitem.contentName"
              placeholder="请输入内容名称"
              style="width: 260px"
            />
          </el-form-item>
          <el-form-item
            label="结果预览"
            :rules="rules.fieldContentList.resultPreview"
            :prop="`fieldContentList.${index}.resultPreview`"
          >
            <el-input
              v-model="aitem.resultPreview"
              placeholder="请输入结果预览"
              style="width: 260px"
            />
          </el-form-item>
          <el-form-item
            label="内容排序"
            :rules="rules.fieldContentList.contentSort"
            :prop="`fieldContentList.${index}.contentSort`"
          >
            <el-input-number
              v-model="aitem.contentSort"
              controls-position="right"
              :min="0"
              placeholder="请输入内容排序"
              style="width: 260px"
            />
          </el-form-item>

          <el-form-item
            label="画像字段标识"
            :rules="rules.fieldContentList.fieldMark"
            :prop="`fieldContentList.${index}.fieldMark`"
          >
            <el-input
              style="width: 260px"
              v-model="aitem.fieldMark"
              placeholder="请输入画像字段标识,例如:${content}"
            />
          </el-form-item>
          <el-form-item label="内容备注信息" prop="contentRemark">
            <el-input
              type="textarea"
              v-model="aitem.contentRemark"
              placeholder="请输入内容备注信息"
              style="width: 260px"
            />
          </el-form-item>
          <el-button
            type="primary"
            plain
            icon="el-icon-circle-plus-outline"
            style="margin-top: 9px; margin-left: 6px"
            v-if="index == 0"
            @click="addnurseClassifyitem"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            plain
            style="margin-top: 9px; margin-left: 6px"
            v-if="index != 0"
            @click="delnurseClassifyitem(index)"
          ></el-button>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--修改标签字段内容信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="openup"
      width="1200px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rulesup"
        label-width="110px"
        :inline="true"
      >
        <el-form-item label="字段信息" prop="fieldId">
          <el-select
            v-model="form.fieldId"
            placeholder="请选择字段信息"
            style="width: 260px"
            clearable
            @change="change"
          >
            <el-option
              v-for="item in labelfieldList"
              :key="item.id"
              :label="item.fieldName"
              :value="item.id"
            >
            </el-option>
          </el-select> </el-form-item
        ><br />

        <el-form-item label="内容名称" prop="contentName">
          <el-input
            v-model="form.contentName"
            placeholder="请输入内容名称"
            style="width: 260px"
          />
        </el-form-item>
        <el-form-item label="结果预览" prop="resultPreview">
          <el-input
            v-model="form.resultPreview"
            placeholder="请输入结果预览"
            style="width: 260px"
          />
        </el-form-item>
        <el-form-item label="内容排序" prop="contentSort">
          <el-input-number
            v-model="form.contentSort"
            controls-position="right"
            :min="0"
            placeholder="请输入内容排序"
            style="width: 260px"
          />
        </el-form-item>

        <el-form-item label="画像字段标识" prop="fieldMark">
          <el-input
            style="width: 260px"
            v-model="form.fieldMark"
            placeholder="请输入画像字段标识,例如:${content}"
          />
        </el-form-item>
        <el-form-item label="内容备注信息" prop="contentRemark">
          <el-input
            type="textarea"
            v-model="form.contentRemark"
            placeholder="请输入内容备注信息"
            style="width: 260px"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelup">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLabelfieldcontent, getLabelfieldcontent, delLabelfieldcontent, addLabelfieldcontent, updateLabelfieldcontent, labelFieldInfoList } from "@/api/manage/labelfieldcontent";

export default {
  name: "Labelfieldcontent",
  data() {

    var validateInput = (rule, value, callback) => {
      if (!value) {
        callback(new Error("画像字段标识不能为空"));
      }
      else if (!value.startsWith('$') || /[\u4e00-\u9fa50-9]/.test(value)) {
        callback(new Error('画像字段标识格式有误,请以$开头,例如${content}'));
      } else {
        callback();
      }
      // const regex =/^\$/;
      // // const regex  =/^\$[^u4e00-u9fa50-9]/
      //  if (!value) {
      //     callback(new Error("画像字段标识不能为空"));
      //   }else if (!regex.test(value)){
      //   callback(new Error('画像字段标识格式有误，请以$开头'));
      //   }else{
      //   callback();
      //   }
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 标签字段内容信息表格数据
      labelfieldcontentList: [],
      labelfieldList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openup: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fieldId: null,
        fieldName: null,
        contentName: null,
        contentCode: null,
        resultPreview: null,
        contentSort: null,
        contentRemark: null,
        fieldMark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fieldId: [
          { required: true, message: "请选择字段信息", trigger: "blur" }
        ],
        fieldContentList: {
          contentName: [
            {
              required: true,
              message: "请输入内容名称",
              trigger: "blur",
            },
          ],
          resultPreview: [
            {
              required: true,
              message: "请输入结果预览",
              trigger: "blur",
            },
          ],
          contentSort: [
            {
              required: true,
              message: "请输入内容排序",
              trigger: "blur",
            },
          ],
          fieldMark: [
            {

              required: true,
              trigger: "blur",
              // validator: validateInput
            },
            // { pattern:/^$W*$/, message: '以$开头且不能输入汉字和数字的字符串', trigger: 'blur' }
          ],

        },
      },
      rulesup: {
        fieldId: [
          { required: true, message: "画像标签和知识库字段表id不能为空", trigger: "blur" }
        ],
        contentName: [
          {
            required: true,
            message: "请输入内容名称",
            trigger: "blur",
          },
        ],
        resultPreview: [
          {
            required: true,
            message: "请输入结果预览",
            trigger: "blur",
          },
        ],
        contentSort: [
          {
            required: true,
            message: "请输入内容排序",
            trigger: "blur",
          },
        ],
        fieldMark: [
          {
            required: true,
            // message: "请输入画像字段表示",
            trigger: "blur",
            // validator: validateInput
          },
        ],

      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询标签字段内容信息列表 */
    getList() {
      this.loading = true;
      listLabelfieldcontent(this.queryParams).then(response => {
        this.labelfieldcontentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 字段信息
    getlabel() {
      this.loading = true;
      labelFieldInfoList().then(response => {
        this.labelfieldList = response.data;
        // this.total = response.total;
        this.loading = false;
      });

    },
    change(e) {
      this.form.fieldName = this.labelfieldList.find(f => f.id == e).fieldName
      console.log(e)

    },

    addnurseClassifyitem() {
      var obj = {
        contentName: null,
        resultPreview: null,
        contentSort: undefined,
        fieldMark: null,
        contentRemark: null,
      };
      if (this.form.fieldContentList.length == 5) {
        this.$message.error("最多批量添加5条");
      } else {
        this.form.fieldContentList.push(obj);
        console.log(this.form)
      }
    },
    //添加时删除item
    delnurseClassifyitem(index) {
      this.form.fieldContentList.splice(index, 1);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 修改取消
    cancelup() {
      this.openup = false,
        this.reset();

    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fieldId: null,
        fieldName: null,
        contentName: null,
        contentCode: null,
        resultPreview: null,
        contentSort: undefined,
        contentRemark: null,
        fieldMark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form = {
        fieldContentList: [
          {
            contentName: null,
            resultPreview: null,
            contentSort: undefined,
            fieldMark: null,
            contentRemark: null,
          },
        ],
      };
      this.getlabel();
      this.title = "添加标签字段内容信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.getlabel();
      getLabelfieldcontent(id).then(response => {
        this.form = response.data;
        this.openup = true;
        this.title = "修改标签字段内容信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLabelfieldcontent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openup = false;
              this.getList();
            });
          } else {
            addLabelfieldcontent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除标签字段内容信息编号为"' + ids + '"的数据项？').then(function () {
        return delLabelfieldcontent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manage/labelfieldcontent/export', {
        ...this.queryParams
      }, `labelfieldcontent_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
</style>
