<template>
  <el-form ref="form" :model="form" :rules="rules">
    <ele-pro-table
      ref="table"
      :needPage="false"
      :columns="columns"
      :datasource="form.datasource"
      cache-key="systemRoleTable17"
      class="time-form"
      :toolkit="[]"
    >
      <!-- 表头工具栏 -->
      <template v-slot:toolbar>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          class="ele-btn-icon"
          @click="handlAdd"
        >
          新增
        </el-button>
      </template>
      <template v-slot:name="scope">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.name'"
          :rules="{
            required: true,
            message: '请输入',
            trigger: 'change'
          }"
        >
          <el-input v-model="scope.row.name" placeholder="请输入"></el-input>
        </el-form-item>
      </template>
      <template v-slot:analysis="scope">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.analysis'"
          :rules="{
            required: true,
            message: '请输入',
            trigger: 'change'
          }"
        >
          <el-input
            v-model="scope.row.analysis"
            type="textarea"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
      </template>
      <template v-slot:headerName="{ column }">
        <span class="is-required">{{ column.label }}</span>
      </template>
      <template v-slot:headerAnalysis="{ column }">
        <span class="is-required">{{ column.label }}</span>
      </template>

      <!-- 操作列 -->
      <template v-slot:action="{ row }">
        <el-popconfirm
          class="ele-action"
          title="确定要删除吗？"
          @confirm="remove(row)"
        >
          <template v-slot:reference>
            <el-link type="danger" :underline="false" icon="el-icon-delete">
              删除
            </el-link>
          </template>
        </el-popconfirm>
      </template>
    </ele-pro-table>
  </el-form>
</template>
<script>
  export default {
    props: {
      delDetailIds: Array
    },
    data() {
      const defaultForm = {
        key: null,
        name: '',
        analysis: ''
      };
      return {
        defaultForm,
        form: {
          datasource: []
        },
        rules: {},
        columns: [
          {
            width: 45,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            fixed: 'left'
          },

          {
            width: 300,
            prop: 'name',
            label: '竞争对手名称',
            slot: 'name',
            headerSlot:'headerName',
            align: 'center',

          },
          {
            prop: 'analysis',
            label: '竞品分析',
            slot: 'analysis',
            headerSlot:'headerAnalysis',
            align: 'center',
          },
          {
            width: 100,
            columnKey: 'action',
            label: '操作',
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ]
      };
    },
    computed: {
      canHandl() {
        return this.form.datasource.length;
      }
    },
    methods: {
      // 返回列表数据
      getTableValue() {
        return this.form.datasource;
      },
      //修改回显
      putTableValue(data) {
        if (data && data?.length) {
          this.form.datasource = data;
        }
      },
      remove(row) {
        let index = this.form.datasource.findIndex((n) => n.key == row.key);
        if (index !== -1) {
          this.form.datasource.splice(index, 1);
          this.setSort();
          if (row.id) {
            this.delDetailIds.push(row.id);
          }
        }
      },
      // 清空表格
      restTable() {
        this.form.datasource = [];
      },
      // 重新排序
      setSort() {
        this.form.datasource.forEach((n, index) => {
          n.key = index + 1;
        });
      },
      // 添加
      handlAdd() {
        let item = JSON.parse(JSON.stringify(this.defaultForm));
        item.key = this.form.datasource.length + 1;
        this.form.datasource.push(item);
      },

      validateForm(callback) {
        //开始表单校验
        this.$refs.form.validate((valid) => {
          callback(valid);
        });
      }
    }
  };
</script>
<style lang="scss" scoped>
  .time-form .el-form-item {
    margin-bottom: 0 !important;
  }
</style>
