<template>
  <el-dialog
    title="选择商机"
    custom-class="ele-dialog-form long-dialog-form"
    :visible.sync="opportunityDialogFlag"
    :before-close="handleClose"
    :close-on-click-modal="false"
    top="5vh"
    :close-on-press-escape="false"
    append-to-body
    width="80%"
  >
    <el-card shadow="never">
      <search @search="reload"></search>
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        row-key="id"
        height="calc(100vh - 500px)"
        class="dict-table"
        @cell-click="cellClick"
      >
        <!-- 表头工具栏 -->
        <template v-slot:action="{ row }">
          <el-radio class="radio" v-model="radio" :label="row.id"><i></i></el-radio>
        </template>
      </ele-pro-table>
    </el-card>

    <div class="btns" slot="footer">
      <el-button type="primary" size="small" @click="selected">选择</el-button>
      <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import search from './opportunitySearch.vue';
import {getTableList} from "@/api/bpm/components/saleManage/quotation";

export default {
  components: {
    search
  },
  props: {
    opportunityDialogFlag: {
      default: false,
      type: Boolean
    },
  },
  data() {
    return {
      currentIndex: null,
      columns: [
        {
          columnKey: 'index',
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          action: 'action',
          slot: 'action',
          align: 'center',
          label: '选择'
        },
        {
          prop: 'contactName',
          label: '客户名称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200
        },
        {
          prop: 'name',
          label: '商机名称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 140
        },
        {
          prop: 'sourceName',
          label: '商机来源',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 150
        },
        {
          prop: 'stageName',
          label: '商机阶段',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 130
        },

        {
          prop: 'budget',
          label: '预算',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 130,
          formatter: (_row, _column, cellValue) => {
            return _row.budget ? _row.budget + '万元' : '';
          }
        },
        {
          prop: 'winRate',
          label: '赢单率',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 130,
          formatter: (_row, _column, cellValue) => {
            return _row.winRate ? _row.winRate + '%' : '';
          }
        },
        {
          prop: 'expectedClosingDate',
          label: '预计结单日期',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 130
        },
        {
          prop: 'responsibleName',
          label: '负责人名称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 130
        }
      ],

      radio: null
    };
  },

  methods: {
    init(item = {}, currentIndex = '') {
      this.currentIndex = currentIndex;
      // if (item.id) {
      //   this.radio = item.id;
      // }
    },
    /* 表格数据源 */
    datasource({page, limit, where, order}) {
      return getTableList({
        pageNum: page,
        size: limit,
        ...where,
        status: 1,
        approvalStatus: 2,

      });
    },

    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({pageNum: 1, where: where});
    },

    handleNodeClick(data, node) {
      this.reload({categoryId: data.id});
    },

    // 单击获取id
    cellClick(row) {
      this.current = row;
      this.radio = row.id;
    },
    handleClose() {
      this.$emit('update:opportunityDialogFlag', false)
    },

    selected() {
      if (!this.current) {
        return this.$message.warning('请至少选择一条数据');
      }
      this.$emit('changeParent', this.current, this.currentIndex);
      this.handleClose();
    }
  }
};
</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>
