<template>
  <div class="business-container">
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form ref="queryForm" :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item prop="nboName">
            <el-input v-model="queryForm.nboName" clearable placeholder="商机标题" @keyup.enter.native="queryData" />
          </el-form-item>

          <el-form-item prop="custName">
            <el-input
              v-model="queryForm.custName"
              clearable
              placeholder="客户名称"
              size="small"
              @keyup.enter.native="queryData" />
          </el-form-item>

          <el-form-item prop="nboType">
            <el-select v-model="queryForm.nboType" clearable placeholder="商机类别">
              <el-option v-for="dict in nboTypeOptions" :key="dict.key" :label="dict.value" :value="dict.key" />
            </el-select>
          </el-form-item>
          <el-form-item prop="saleName">
            <el-input
              v-model="queryForm.saleName"
              clearable
              placeholder="归属人员"
              size="small"
              @keyup.enter.native="queryData" />
          </el-form-item>

          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>

      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">新增项目</el-button>
        <el-button icon="el-icon-refresh" type="primary" @click="handleTransfer">转移项目</el-button>
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">创建任务</el-button>
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">创建工单</el-button>
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">创建合同</el-button>
        <!--        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">删除</el-button>-->
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <table-tool :check-list.sync="checkList" :columns="columns" />
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list" :height="height" @selection-change="setSelectRows">
      <el-table-column align="center" show-overflow-tooltip 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" />

      <el-table-column align="center" label="操作" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">跟进</el-button>
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <!--          <el-button type="text" @click="handleDelete(row)">删除</el-button>-->
        </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" />
    <edit ref="edit" @fetch-data="fetchData" />
    <transfer ref="transfer" @fetch-data="fetchData" />
  </div>
</template>

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

  export default {
    name: 'Business',
    components: { Edit, Transfer, TableTool },
    data() {
      return {
        height: this.$baseTableHeight(2),
        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: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          nboName: undefined,
          custName: undefined,
          nboType: undefined,
          saleName: undefined,
        },
        nboTypeOptions: [],
      }
    },
    computed: {
      finallyColumns() {
        return this.columns.filter((item) => this.checkList.includes(item.label))
      },
    },
    created() {
      this.fetchData()
      this.getDicts('proj-nbo-type').then((response) => {
        this.nboTypeOptions = response.data.values || []
      })
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleTransfer(row) {
        this.$refs['transfer'].open(row)
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await businessApi.doDelete({ ids: [row.id] })
            this.$baseMessage(msg, 'success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id)
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await businessApi.doDelete({ ids })
              this.$baseMessage(msg, 'success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await businessApi.getList(this.queryForm)
        const { list, total } = data
        this.list = list
        this.total = total
        this.listLoading = false
      },
    },
  }
</script>
