<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>
        <el-button
          size="small"
          type="primary"
          class="ele-btn-icon"
          @click="save"
          v-if="isDrawer"
        >
          保存
        </el-button>
      </template>
      <template v-slot:linkId="scope">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.linkId'"
          :rules="{
            required: true,
            message: '请选择',
            trigger: 'change'
          }"
        >
          <el-select
            v-model="scope.row.linkId"
            placeholder="请选择"
            @change="changePersonel(scope.row, $event)"
          >
            <el-option
              v-for="item in linkNameOptions"
              :key="item.id"
              :label="item.linkName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </template>
      <template v-slot:linkPost="scope">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.linkPost'"
        >
          <el-input
            v-model="scope.row.linkPost"
            disabled
            placeholder="请输入"
          ></el-input>
        </el-form-item>
      </template>
      <template v-slot:linkMobilePhone="scope">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.linkMobilePhone'"
        >
          <el-input
            v-model="scope.row.linkMobilePhone"
            disabled
            placeholder="请输入"
          ></el-input>
        </el-form-item>
      </template>
      <template v-slot:goal="scope">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.goal'"
          :rules="{
            required: true,
            message: '请输入',
            trigger: 'change'
          }"
        >
          <el-input v-model="scope.row.goal" placeholder="请输入"></el-input>
        </el-form-item>
      </template>
      <template v-slot:influenceCode="scope">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.influenceCode'"
          :rules="{
            required: true,
            message: '请选择',
            trigger: 'change'
          }"
        >
          <DictSelection
            dictName="影响力"
            clearable
            v-model="scope.row.influenceCode"
          >
          </DictSelection>
        </el-form-item>
      </template>
      <template v-slot:attitudeCode="scope">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.attitudeCode'"
          :rules="{
            required: true,
            message: '请选择',
            trigger: 'change'
          }"
        >
          <DictSelection
            dictName="态度"
            clearable
            v-model="scope.row.attitudeCode"
          >
          </DictSelection>
        </el-form-item>
      </template>
      <template v-slot:headerLinkId="{ column }">
        <span class="is-required">{{ column.label }}</span>
      </template>
      <template v-slot:headerGoal="{ column }">
        <span class="is-required">{{ column.label }}</span>
      </template>
      <template v-slot:headerInfluenceCode="{ column }">
        <span class="is-required">{{ column.label }}</span>
      </template>
      <template v-slot:headerAttitudeCode="{ 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>
  import { getcontactlink } from '@/api/saleManage/businessFollow';
  import dictMixins from '@/mixins/dictMixins';
  export default {
    props: {
      delDetailIds: Array,
      isDrawer: {
        type: Boolean,
        default: false
      },
      contactId: {
        type: String,
        default: ''
      }
    },
    mixins: [dictMixins],
    data() {
      const defaultForm = {
        key: null,
        linkId: '',
        linkPost: '',
        linkMobilePhone: '',
        goal: '',
        influenceCode: '',
        influenceName: '',
        attitudeCode: '',
        attitudeName: ''
      };
      return {
        newContactId: '',
        linkNameOptions: [],
        defaultForm,
        form: {
          datasource: []
        },
        rules: {},
        columns: [
          {
            width: 45,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            fixed: 'left'
          },

          {
            prop: 'linkId',
            label: '姓名',
            slot: 'linkId',
            headerSlot: 'headerLinkId',
            align: 'center'
          },
          {
            prop: 'linkPost',
            label: '职位',
            slot: 'linkPost',
            align: 'center'
          },
          {
            prop: 'linkMobilePhone',
            label: '联系方式',
            slot: 'linkMobilePhone',
            align: 'center'
          },
          {
            prop: 'goal',
            label: '项目目标/期望',
            slot: 'goal',
            headerSlot: 'headerGoal',
            align: 'center'
          },
          {
            prop: 'influenceCode',
            label: '影响力',
            slot: 'influenceCode',
            headerSlot: 'headerInfluenceCode',
            align: 'center'
          },
          {
            prop: 'attitudeCode',
            label: '态度',
            slot: 'attitudeCode',
            headerSlot: 'headerAttitudeCode',
            align: 'center'
          },
          {
            width: 100,
            columnKey: 'action',
            label: '操作',
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ]
      };
    },
    computed: {
      canHandl() {
        return this.form.datasource.length;
      }
    },
    watch: {
     
    },
    methods: {
      //初始数据
      async getContactIdInfo(contactId) {
        const data = await getcontactlink({ contactId});
        if (data && data?.length) {
          this.linkNameOptions = data;
        }
      },
      //修改回显
      putTableValue(data, contactId) {
        if (contactId) {
          this.getContactIdInfo(contactId);
        }
        if (data && data?.length) {
          this.form.datasource = data;
        }
      },
      // 返回列表数据
      getTableValue() {
        let _datasource = this.form.datasource || [];
        if (_datasource.length === 0) return;
        _datasource.forEach((v, index) => {
          if (_datasource[index].influenceCode) {
            _datasource[index].influenceName = this.getDictValue(
              '影响力',
              _datasource[index].influenceCode
            );
          }
          if (_datasource[index].attitudeCode) {
            _datasource[index].attitudeName = this.getDictValue(
              '态度',
              _datasource[index].attitudeCode
            );
          }
        });
        return _datasource;
      },
      //选择关键人
      changePersonel(row, val) {
        
        let selectVal = this.linkNameOptions.find((v) => v.id === val);
        row.linkPost = selectVal.post;
        row.linkMobilePhone = selectVal.mobilePhone|| selectVal.phone;
      },
      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() {
        this.$emit('getContactId', (id) => {
          if (!id) {
            return this.$message.error('请先选择客户');
          }
          this.getContactIdInfo(id);
          let item = JSON.parse(JSON.stringify(this.defaultForm));
          item.key = this.form.datasource.length + 1;
          this.form.datasource.push(item);
        });
      },
      save() {
        this.$emit('save');
      },
      validateForm(callback) {
        //开始表单校验
        this.$refs.form.validate((valid) => {
          callback(valid);
        });
      }
    }
  };
</script>
<style lang="scss" scoped>
  .time-form .el-form-item {
    margin-bottom: 0 !important;
  }
</style>
