<template>
  <el-card shadow="never">
    <ele-pro-table
      ref="table"
      :columns="columns"
      :datasource="list"
      height="calc(100vh - 350px)"
      class="dict-table"
      :need-page="false"
    >
      <!-- 表头工具栏 -->
      <template v-slot:toolbar>
        <el-button type="primary" @click="openEdit('add', {})">添加</el-button>
        <el-button type="primary" @click="save">确认修改</el-button>
      </template>
      <template v-slot:content="{ row }">
        <el-link
          type="primary"
          :underline="false"
          @click="openEdit('view', row)"
        >
          {{ row.content }}</el-link
        >
      </template>

      <!-- 操作列 -->
      <template v-slot:action="{ row, $index }">
        <el-link
          type="primary"
          :underline="false"
          icon="el-icon-edit"
          @click="openEdit('edit', row, $index)"
        >
          修改
        </el-link>

        <el-popconfirm
          class="ele-action"
          title="确定要删除此信息吗？"
          @confirm="handleBankDel(row, $index)"
        >
          <template v-slot:reference>
            <el-link type="danger" :underline="false" icon="el-icon-delete">
              删除
            </el-link>
          </template>
        </el-popconfirm>
      </template>
    </ele-pro-table>
    <AddFollowDialog ref="addFollowDialogRef" @done="done"></AddFollowDialog>
  </el-card>
</template>

<script>
  import AddFollowDialog from './addFollowDialog.vue';
  import { contactUpdate } from '@/api/saleManage/contact';

  import { contactDetail } from '@/api/saleManage/contact';

  export default {
    components: {
      AddFollowDialog
    },

    data() {
      return {
        key: '',
        list: [],
        form: {},
        delVisible: false,
        columns: [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'content',
            label: '跟进内容',
            slot: 'content',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'stageName',
            label: '跟进阶段',
            slot: 'stageName',
            showOverflowTooltip: true,
            minWidth: 100
          },
          {
            prop: 'agreement',
            label: '达成共识',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'linkName',
            label: '客户联系人名称',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'nextPlan',
            label: '下一步计划',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'followupTime',
            label: '跟进时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },

          {
            columnKey: 'action',
            label: '操作',
            width: 230,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true,
            fixed: 'right'
          }
        ]
      };
    },

    methods: {
      openEdit(type, row, index) {
        this.$refs.addFollowDialogRef.open(
          type,
          row,
          this.form.linkList,
          index
        );
        this.$refs.addFollowDialogRef.$refs.form &&
          this.$refs.addFollowDialogRef.$refs.form.clearValidate();
      },

      async init(row) {
        this.form = row;
        this.list = row.base.opportunityFollowupList;
      },
      handleBankDel(row, index) {
        this.list.splice(index, 1);
        // this.save();
      },
      done({ data, type, index }) {
        data.linkId = data.linkId.toString();
        if (type == 'add') {
          this.list.unshift(data);
        } else {
          this.$set(this.list, index, data);
        }
      },
      save() {
        console.log(this.form);

        this.form.base.opportunityFollowupList = this.list;
        contactUpdate(this.form)
          .then((res) => {
            this.$message.success('修改成功');

            contactDetail(this.form.base.id).then((res) => {
              this.list = res.base.opportunityFollowupList;
            });

        
            this.$emit('success');
          })
          .catch((e) => {
            //this.loading = false;
          });
      }
    }
  };
</script>

<style lang="scss" scoped>
  .tree_col {
    border: 1px solid #eee;
    padding: 10px 0;
    box-sizing: border-box;
    height: 500px;
    overflow: auto;
  }

  .table_col {
    padding-left: 10px;

    ::v-deep .el-table th.el-table__cell {
      background: #f2f2f2;
    }
  }

  .pagination {
    text-align: right;
    padding: 10px 0;
  }

  .btns {
    text-align: center;
    padding: 10px 0;
  }

  .topsearch {
    margin-bottom: 15px;
  }
</style>
