<template>
  <el-dialog append-to-body :title="title" :visible.sync="innerVisible">
    <el-row>
      <el-col :span="24">
        <div style="float: right">
          <el-button size="mini" type="primary" @click="handleAdd">新建项目</el-button>
          <el-dropdown style="margin-left: 10px" trigger="click">
            <el-button icon="el-icon-more" size="mini" />
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item icon="el-icon-upload2">导入</el-dropdown-item>
              <el-dropdown-item icon="el-icon-download">导出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-input
          v-model="queryForm.nboName"
          clearable
          placeholder="项目名称"
          style="width: 30%; margin-right: 10px"
          suffix-icon="el-icon-search" />
        <!--        <span>显示：</span>-->
        <!--        <el-radio-group v-model="queryForm.type">-->
        <!--          <el-radio-button label="全部客户" />-->
        <!--          <el-radio-button label="我负责的客户" />-->
        <!--        </el-radio-group>-->
        <table-tool :check-list.sync="checkList" :columns="columns" style="float: right" />
      </el-col>
    </el-row>
    <el-table ref="table" v-loading="listLoading" :data="list" @selection-change="setSelectRows">
      <el-table-column align="center" type="selection" />
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        show-overflow-tooltip
        :sortable="item.sortable"
        :width="item.width">
        <template #default="{ row }">
          <span v-if="item.prop === 'custStatus'">
            {{ row.custStatus == 10 ? '正常' : '异常' }}
          </span>
          <span v-else>{{ row[item.prop] }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange" />
    <span slot="footer">
      <el-button size="mini" type="primary" @click="save">保存</el-button>
      <el-button size="mini" @click="innerVisible = false">取消</el-button>
    </span>
    <!-- 新建项目弹窗 -->
    <business-edit ref="businessEdit" />
  </el-dialog>
</template>

<script>
  import businessApi from '@/api/proj/business'
  import TableTool from '@/components/table/TableTool'
  import BusinessEdit from '@/views/proj/business/components/BusinessEdit'

  export default {
    name: 'SelectBusiness',
    components: {
      TableTool,
      BusinessEdit,
    },
    props: {
      title: {
        type: String,
        default: '选择',
      },
      multiple: Boolean,
      queryParams: {
        type: Object,
        default() {
          return {}
        },
      },
    },
    data() {
      return {
        innerVisible: false,
        queryForm: {
          nboName: '',
          type: '全部客户',
          pageNum: 1,
          pageSize: 10,
        },
        checkList: [],
        columns: [
          {
            label: '商机标题',
            width: 'auto',
            prop: 'nboName',
            sortable: true,
            disableCheck: true,
          },
          {
            label: '关联客户',
            width: 'auto',
            prop: 'custName',
          },
          {
            label: '审批状态',
            width: 'auto',
            prop: 'approStatus',
          },
          {
            label: '商机状态',
            width: 'auto',
            prop: 'nboPhase',
          },
          {
            label: '商机类别',
            width: 'auto',
            prop: 'nboType',
          },
          {
            label: '商机金额',
            width: 'auto',
            prop: 'nboBudget',
          },
          {
            label: '最后跟进时间',
            width: 'auto',
            prop: 'finalFollowTime',
          },
          {
            label: '下次跟进时间',
            width: 'auto',
            prop: 'nextFollowTime',
          },
        ],
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: [],
      }
    },
    computed: {
      finallyColumns() {
        return this.columns.filter((item) => this.checkList.includes(item.label))
      },
    },
    mounted() {
      this.fetchData()
    },
    methods: {
      open() {
        this.innerVisible = true
      },
      save() {
        this.innerVisible = false
        this.$emit('save', this.selectRows)
      },
      handleAdd() {
        this.$refs.businessEdit.showEdit()
      },
      async fetchData() {
        this.listLoading = true
        let query = Object.assign(this.queryForm, this.queryParams)
        const {
          data: { list, total },
        } = await businessApi.getList(query)
        this.list = list
        this.total = total
        this.listLoading = false
      },
      setSelectRows(val) {
        if (!this.multiple && val.length === this.list.length) {
          // 返回单条数据情况下-控制全选情况下单选第一条数据
          if (this.selectRows.length === 1) {
            this.$refs.table.clearSelection()
            return
          }
          this.$refs.table.clearSelection()
          this.$refs.table.toggleRowSelection(val.shift(), true)
        } else if (!this.multiple && val.length > 1) {
          // 返回单条数据情况下-控制选择当前点击数据
          this.$refs.table.clearSelection()
          this.$refs.table.toggleRowSelection(val.pop(), true)
        } else {
          this.selectRows = val
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
    },
  }
</script>

<style scoped></style>
