<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="字段类型"
            label-width="100px"
            prop="fieldType"
          >
            <el-select v-model="queryParams.fieldType" placeholder="请选择字段类型" clearable>
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
      <!-- <el-form-item label="字段编码" prop="fieldCode">
        <el-input
          v-model="queryParams.fieldCode"
          placeholder="请输入字段编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段排序" prop="fieldSort">
        <el-input
          v-model="queryParams.fieldSort"
          placeholder="请输入字段排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段备注信息" prop="fieldRemark">
        <el-input
          v-model="queryParams.fieldRemark"
          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:labelfieldinfo: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:labelfieldinfo: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:labelfieldinfo:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="labelfieldinfodata"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="字段名称" align="center" prop="fieldName" />
      <el-table-column label="字段编码" align="center" prop="fieldCode" />
      <el-table-column
        label="字段类型"
        align="center"
        prop="fieldType"
      >
      <template slot-scope="scope">
        {{scope.row.fieldType=="PORTRAIT_LABEL_FIELD"?'画像标签':''}}
        {{scope.row.fieldType=="KNOWLEDGE_FIELD"?'知识库字段':''}}
      </template>
      </el-table-column>
      <el-table-column label="字段排序" align="center" prop="fieldSort" />
      <el-table-column label="字段备注信息" align="center" prop="fieldRemark" :show-overflow-tooltip="true"/>
      <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:labelfieldinfo:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manage:labelfieldinfo: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="750px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        :inline="true"
      >
        <div v-for="(aitem, index) in form.labelFieldInfoList" :key="index">
          <el-form-item
            label="字段名称"
            :rules="rules.labelFieldInfoList.fieldName"
            :prop="`labelFieldInfoList.${index}.fieldName`"
          >
            <el-input v-model="aitem.fieldName" placeholder="请输入字段名称"   style="width: 210px"/>
          </el-form-item>
          <el-form-item
            label="字段类型"
            :rules="rules.labelFieldInfoList.fieldType"
            :prop="`labelFieldInfoList.${index}.fieldType`"
          >
            <el-select v-model="aitem.fieldType" placeholder="请选择字段类型" clearable    style="width: 210px">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="字段排序"
            :rules="rules.labelFieldInfoList.fieldSort"
            :prop="`labelFieldInfoList.${index}.fieldSort`"
          >
          <el-input-number
            v-model="aitem.fieldSort"
            controls-position="right"
            :min="0"
            placeholder="请输入字段排序"
            style="width: 210px"
          />
            <!-- <el-input v-model="aitem.fieldSort" placeholder="请输入字段排序" oninput="value=value.replace(/[^\d]/g,'')"
              maxlength="5"/> -->
          </el-form-item>
          <el-form-item
            label="字段备注信息"
            prop="fieldRemark"

          >
            <el-input
              style="width: 210px"
              v-model="aitem.fieldRemark"
              placeholder="请输入字段备注信息"
              type="textarea"
            />
          </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="titleup" :visible.sync="openup" width="750px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rulesup"
        label-width="100px"
        :inline="true"
      >
          <el-form-item
            label="字段名称"
            prop="fieldName"
          >
            <el-input v-model="form.fieldName" placeholder="请输入字段名称"   style="width: 210px"/>
          </el-form-item>
          <el-form-item
            label="字段类型"
            prop="fieldType"
          >
            <el-select v-model="form.fieldType" placeholder="请选择字段类型" clearable    style="width: 210px">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="字段排序"
            prop="fieldSort"
          >
          <el-input-number
            v-model="form.fieldSort"
            controls-position="right"
            :min="0"
            placeholder="请输入字段排序"
            style="width: 210px"
          />

          </el-form-item>
          <el-form-item
            label="字段备注信息"
            prop="fieldRemark"

          >
            <el-input
              style="width: 210px"
              v-model="form.fieldRemark"
              placeholder="请输入字段备注信息"
              type="textarea"
            />
          </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 { listLabelfieldinfo, getLabelfieldinfo, delLabelfieldinfo, addLabelfieldinfo, updateLabelfieldinfo } from "@/api/manage/labelfieldinfo";

export default {
  name: "Labelfieldinfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 标签字段信息表格数据
      labelfieldinfodata: [],
      // 弹出层标题
      title: "",
      titleup:"",
      // 是否显示弹出层
      open: false,
      openup:false,//修改
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fieldName: null,
        fieldType: null,
        fieldType: null,
        fieldSort: null,
        fieldRemark: null,
      },
      options: [{
        value: 'PORTRAIT_LABEL_FIELD',
        label: '画像标签'
      }, {
        value: 'KNOWLEDGE_FIELD',
        label: '知识库'
      },],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        labelFieldInfoList: {
          fieldType: [
            {
              required: true,
              message: "请选择字段类型",
              trigger: "blur",
            },
          ],
          fieldName: [
            {
              required: true,
              message: "请输入字段名称",
              trigger: "blur",
            },
          ],
          fieldSort: [
            {
              required: true,
              message: "请输入字段排序",
              trigger: "blur",
            },
          ],
          fieldRemark: [
            {
              required: true,
              message: "请输入字段备注信息",
              trigger: "blur",
            },
          ],

        },
      },
      rulesup:{
      fieldType: [
            {
              required: true,
              message: "请选择字段类型",
              trigger: "blur",
            },
          ],
          fieldName: [
            {
              required: true,
              message: "请输入字段名称",
              trigger: "blur",
            },
          ],
          fieldSort: [
            {
              required: true,
              message: "请输入字段排序",
              trigger: "blur",
            },
          ],
          // fieldRemark: [
          //   {
          //     required: true,
          //     message: "请输入字段备注信息",
          //     trigger: "blur",
          //   },
          // ],

          },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询标签字段信息列表 */
    getList() {
      this.loading = true;
      listLabelfieldinfo(this.queryParams).then(response => {
        this.labelfieldinfodata = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //添加时新增item
    addnurseClassifyitem() {
      var obj = {
        fieldType: null,
        fieldName: null,
        fieldRemark: null,
        fieldSort: undefined,

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

    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fieldName: null,
        fieldType: null,
        fieldType: null,
        fieldSort: undefined,
        fieldRemark: 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.form = {
        labelFieldInfoList: [
          {
            fieldType: null,
            fieldName: null,
            fieldRemark: null,
            fieldSort: undefined,
          },
        ],
      };
      this.open = true;
      this.title = "添加标签字段信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLabelfieldinfo(id).then(response => {
        this.form = response.data;
        this.openup = true;

        // this.title = "修改标签字段信息";
        this.titleup = "修改标签字段信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLabelfieldinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openup = false;
              this.getList();
            });
          } else {
            addLabelfieldinfo(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 delLabelfieldinfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manage/labelfieldinfo/export', {
        ...this.queryParams
      }, `labelfieldinfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
</style>
