<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <div class="ele-border-lighter form-content" v-loading="loading">
        <opportunity-search @search="reload"> </opportunity-search>

        <!-- 数据表格 -->
        <ele-pro-table
          ref="table"
          :columns="columns"
          :datasource="datasource"
          height="calc(100vh - 325px)"
          full-height="calc(100vh - 116px)"
          tool-class="ele-toolbar-form"
          :selection.sync="selection"
          cache-key="eomContactPageTable"
        >
          <!-- 表头工具栏 -->
          <template v-slot:toolbar>
            <el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              @click="openEdit('add', {})"
            >
              新建
            </el-button>
            <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:name="{ row }">
            <el-link type="primary" :underline="false" @click="openDetail(row)">
              {{ row.name }}</el-link
            >
          </template>

          <!-- 操作列 -->
          <template v-slot:action="{ row }">
            <el-link
              type="primary"
              :underline="false"
              icon="el-icon-edit"
              @click="handleFollow(row)"
              v-if="[0, 3].includes(row.approvalStatus)"
            >
              跟进
            </el-link>
            <el-link
              type="primary"
              :underline="false"
              icon="el-icon-plus"
              v-if="[2].includes(row.approvalStatus)&& row.hasQuote!=1"
              @click="addQuotation('add', row)"
            >
              生成报价
            </el-link>
            <el-link
              type="primary"
              :underline="false"
              icon="el-icon-edit"
              v-if="[0, 3].includes(row.approvalStatus)"
              @click="openEdit('edit', row)"
            >
              修改
            </el-link>
            <el-link
              type="primary"
              :underline="false"
              icon="el-icon-plus"
              @click="sub(row)"
              v-if="[0, 3].includes(row.approvalStatus)"
            >
              提交
            </el-link>
            <el-link
              v-if="row.status === 2"
              type="primary"
              :underline="false"
              icon="el-icon-check"
              @click="enableOption(row)"
            >
              启用
            </el-link>
            <el-link
              v-if="row.status === 1"
              type="primary"
              :underline="false"
              icon="el-icon-close"
              @click="disableOption(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"
                  v-if="[0, 3].includes(row.approvalStatus)"
                >
                  删除
                </el-link>
              </template>
            </el-popconfirm>
          </template>
        </ele-pro-table>
      </div>
    </el-card>

    <add-opportunity-dialog
      ref="addContactDialogRef"
      :categoryTreeList="treeList"
      @done="reload"
    ></add-opportunity-dialog>
    <addQuotation ref="quotationRef" @done="reload"></addQuotation>

    <opportunity-detail-dialog
      ref="contactDetailDialogRef"
    ></opportunity-detail-dialog>
    <follow-list ref="followDialogRef"></follow-list>
    <!-- 多选删除弹窗 -->
    <pop-modal
      :visible.sync="delVisible"
      content="是否确定删除？"
      @done="commitBtn"
    />
  </div>
</template>

<script>
  import opportunitySearch from './components/opportunitySearch.vue';
  import addOpportunityDialog from './components/addOpportunityDialog.vue';
  import addQuotation from '@/views/saleManage/quotation/components/addDialog.vue';

  import opportunityDetailDialog from './components/opportunityDetailDialog.vue';
  import followList from './components/followList.vue';
  import popModal from '@/components/pop-modal';
  import {
    getTableList,
    submit,
    deleteInformation,
    updateStatus
  } from '@/api/saleManage/businessOpportunity';
  import dictMixins from '@/mixins/dictMixins';
  import { reviewStatus } from '@/enum/dict';

  export default {
    mixins: [dictMixins],
    components: {
      opportunitySearch,
      followList,
      popModal,
      addOpportunityDialog,
      opportunityDetailDialog,
      addQuotation
    },
    data() {
      return {
        selection: [],
        delVisible: false,
        // 加载状态
        loading: false,
        columns: [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center'
          },
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'contactName',
            label: '客户名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'productNames',
            label: '产品名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'name',
            label: '商机名称',
            align: 'center',
            slot: 'name',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'sourceName',
            label: '商机来源',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'stageName',
            label: '商机阶段',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'budget',
            label: '预算',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140,
            formatter: (_row, _column, cellValue) => {
              return _row.budget ? _row.budget + '万元' : '';
            }
          },
          {
            prop: 'winRate',
            label: '赢单率',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140,
            formatter: (_row, _column, cellValue) => {
              return _row.winRate? _row.winRate + '%': '';
            }
          },
          {
            prop: 'expectedClosingDate',
            label: '预计结单日期',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140,
            formatter: (_row, _column, cellValue) => {
              return _row.expectedClosingDate?.substr(0, 10) || '';
            }
          },
          {
            prop: 'remark',
            label: '备注',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 120
          },
          {
            prop: 'responsibleName',
            label: '负责人名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 120
          },

          {
            prop: 'status',
            label: '启用状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return _row.status === 1 ? '启用' : '禁用';
            }
          },
          {
            prop: 'approvalStatus',
            label: '审核状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return reviewStatus[_row.approvalStatus];
            }
          },
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 160,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 300,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true,
            fixed: 'right'
          }
        ],
        current: {},
        curNodeData: {},
        treeList: [],
        treeLoading: false,
        formData: {},
        rootTreeId: null,
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        showEdit: true
      };
    },
    computed: {},
    created() {
      this.requestDict('客户状态');
    },
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return getTableList({
          pageNum: page,
          size: limit,
          ...where
        });
      },

      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where });
      },
      openEdit(type, row) {
        this.current = row;
        this.showEdit = true;
        this.$refs.addContactDialogRef.open(type, row, row.id);
        this.$refs.addContactDialogRef.$refs.form &&
          this.$refs.addContactDialogRef.$refs.form.clearValidate();
      },
      addQuotation(type,row) {
        this.$refs.quotationRef.open(type, row, row.id);
        this.$refs.quotationRef.$refs.form &&
          this.$refs.quotationRef.$refs.form.clearValidate();
      },
      //跟进
      handleFollow(row) {
        this.current = row;
        this.$refs.followDialogRef.open(row);
      },
      //批量删除
      allDelBtn() {
        if (this.selection.length === 0) return;
        let flag =  this.selection.some(item=>[1,2].includes(item.approvalStatus))
        if(flag) return this.$message.warning('抱歉已审核、审核中的数据不能删除,请检查')
        this.delVisible = true;
      },

      commitBtn() {
        const dataId = this.selection.map((v) => v.id);
        deleteInformation(dataId).then((res) => {
          this.$message.success('删除成功！');
          this.reload();
        });
      },
      remove(row) {
        deleteInformation([row.id]).then((res) => {
          this.$message.success('删除成功！');
          this.reload();
        });
      },
      /** 启用客户 */
      enableOption(row) {
        updateStatus(row.id, 1)
          .then((msg) => {
            this.$message.success('启用成功');
            this.reload();
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      /** 禁用客户 */
      disableOption(row) {
        updateStatus(row.id, 2)
          .then((msg) => {
            this.$message.success('禁用成功');
            this.reload();
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      sub(res) {
        submit({
          businessId: res.id
        })
          .then((res) => {
            this.$message.success('提交成功');
            this.reload();
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      openDetail(row) {
        this.$refs.contactDetailDialogRef.open(row);
      }
    }
  };
</script>

<style lang="scss" scoped>
  :deep(.el-link--inner) {
    margin-left: 0px !important;
  }

  .sys-organization-list {
    height: calc(100vh - 264px);
    box-sizing: border-box;
    border-width: 1px;
    border-style: solid;
    overflow: auto;
  }
  .sys-organization-list :deep(.el-tree-node__content) {
    height: 40px;
    & > .el-tree-node__expand-icon {
      margin-left: 10px;
    }
  }
</style>
