<template>
  <ele-modal
    custom-class="ele-dialog-form long-dialog-form"
    :centered="true"
    :visible.sync="addOrEditDialogFlag"
    :title="'跟进记录'"
    :append-to-body="false"
    :close-on-click-modal="false"
    width="70%"
    :before-close="cancel"
    :maxable="true"
    :resizable="true"
  >
    <AddFollowDialog22
      ref="addFollowDialogRef"
      @done="reload"
    ></AddFollowDialog22>
    <ele-pro-table
      ref="table"
      :columns="columns"
      :datasource="datasource"
      height="calc(100vh - 350px)"
      class="dict-table"
      :initLoad="false"
      :selection.sync="selection"
      row-key="id"
    >
      <!-- <template v-slot:content="{ row }">
        <el-link type="primary" :underline="false" @click="openDetail(row)">
          {{ row.content }}</el-link
        >
      </template> -->
      <template v-slot:files="{ row }">
        <fileMain v-model="row.files" type="view"></fileMain>
      </template>

      <template v-slot:imgs="{ row }">
         <el-button v-if="row.imgs && row.imgs.length > 0" type="primary" @click="viewImgs(row)">查看</el-button>
      </template>

      <template v-slot:toolbar v-if="showAction">
        <el-button
          size="small"
          type="danger"
          el-icon-delete
          class="ele-btn-icon"
          @click="allDelBtn"
          :disabled="selection?.length === 0"
        >
          批量删除
        </el-button>
      </template>

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

        <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>
    <div slot="footer">
      <el-button @click="cancel">返回</el-button>
    </div>

    <ele-modal
      custom-class="ele-dialog-form long-dialog-form"
      :centered="true"
      :visible.sync="imgVisible"
      :title="'照片'"
      :append-to-body="true"
      :close-on-click-modal="true"
      width="70%"
      :before-close="viewImgs"
      :maxable="true"
      :resizable="true"
    >
      <div class="ele-border-lighter sys-organization-list">
        <img style="width: 160px; margin-right: 10px; margin-bottom: 10px;" v-for="(item, index) in rowImgs" :key="index" :src="item" alt="" srcset="">
      </div>
    </ele-modal>

    <pop-el-modal
      :popVisible.sync="delVisible"
      content="是否确定删除？"
      @done="commitBtn"
    />
  </ele-modal>
</template>
<script>
  import dictMixins from '@/mixins/dictMixins';
  // import fileMain from '@/components/addDoc/index.vue';
  import popElModal from '@/components/pop-el-modal';
  import {
    getTableList,
    deleteInformation
  } from '@/api/saleManage/businessFollow';

  import AddFollowDialog22 from './addFollowDialog22.vue';
  export default {
    mixins: [dictMixins],
    components: { AddFollowDialog22, popElModal },

    computed: {
      columns() {
        let section = {
          width: 45,
          type: 'selection',
          columnKey: 'selection',
          align: 'center',
          reserveSelection: true
        };
        let arr = [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'contactName',
            align: 'center',
            label: '客户名称',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'opportunityName',
            label: '商机名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'content',
            align: 'center',
            label: '跟进内容',
            slot: 'content',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            align: 'center',
            prop: 'stageName',
            label: '跟进阶段',
            slot: 'stageName',
            showOverflowTooltip: true,
            minWidth: 100
          },
          {
            prop: 'agreement',
            align: 'center',
            label: '达成共识',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            align: 'center',
            prop: 'linkName',
            label: '客户联系人名称',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            align: 'center',
            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);
            }
          },
          {
            prop: 'files',
            label: '附件',
            align: 'center',
            slot: 'files',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'imgs',
            label: '照片',
            align: 'center',
            slot: 'imgs',
            showOverflowTooltip: true,
            minWidth: 110
          }
        ];

        let action = {
          columnKey: 'action',
          label: '操作',
          width: 180,
          align: 'center',
          resizable: false,
          slot: 'action',
          showOverflowTooltip: true,
          fixed: 'right'
        };
        if (this.showAction) {
          arr.push(action);
          arr.unshift(section);
        }

        return arr;
      }
    },

    data() {
      return {
        addOrEditDialogFlag: false,
        showAction: false,
        current: {},
        delVisible: false,
        selection: [],
        imgVisible: false,
        rowImgs: []
      };
    },
    mounted() {},
    created() {},
    methods: {
      open(params, type) {
        console.log(params);

        this.current = params;
        this.showAction = type === 'view' ? false : true;
        console.log(params);

        this.addOrEditDialogFlag = true;
        this.$nextTick(() => {
          this.reload(params);
        });
      },
      viewImgs(row) {
        this.rowImgs = row.imgs;
        this.imgVisible = !this.imgVisible
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return getTableList({
          pageNum: page,
          size: limit,
          ...where
        });
      },

      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ pageNum: 1, where: where });
        this.$parent.reload();
      },
      //关闭弹窗
      cancel() {
        this.addOrEditDialogFlag = false;
      },
      allDelBtn() {
        if (this.selection?.length === 0) return;
        this.delVisible = true;
      },
      openEdit(type, row) {
        this.$refs.addFollowDialogRef.open(type, row, this.current);
        this.$refs.addFollowDialogRef.$refs.form &&
          this.$refs.addFollowDialogRef.$refs.form.clearValidate();
      },
      remove(row) {
        console.log(row); 
        deleteInformation([row.id]).then((res) => {
          this.$message.success('删除成功！');
          this.reload();
        });
      },
      commitBtn() {
        console.log(this.selection);
        const dataId = this.selection.map((v) => v.id);
        deleteInformation(dataId).then((res) => {
          this.selection = [];
          this.$message.success('删除成功！');
          this.reload();
        });
      }
    }
  };
</script>
<style scoped lang="scss"></style>
