<!--
 * @Author: liuzhenlin 461480418@qq.ocm
 * @Date: 2023-01-05 11:28:51
 * @LastEditors: wanglj
 * @LastEditTime: 2023-03-22 14:53:09
 * @FilePath: \opms_frontend\src\views\contract\index.vue
-->
<template>
  <div class="contract-container">
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form :inline="true" label-width="0px" :model="queryForm" @submit.native.prevent>
          <el-form-item prop="inchargeName">
            <el-input v-model="queryForm.inchargeName" clearable placeholder="对接人" @keyup.enter.native="queryData" />
          </el-form-item>
          <el-form-item prop="name">
            <el-input v-model="queryForm.name" clearable placeholder="联系人" @keyup.enter.native="queryData" />
          </el-form-item>
          <el-form-item prop="unit">
            <el-input v-model="queryForm.unit" clearable placeholder="单位" @keyup.enter.native="queryData" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
      <vab-query-form-left-panel :span="12">
        <el-button v-permissions="['contract:manage:add']" icon="el-icon-plus" type="primary" @click="handleEdit()">
          新建
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <table-tool :columns="columns" :show-columns.sync="showColumns" table-type="contractTable" />
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table ref="table" v-loading="listLoading" :data="list" :height="height">
      <el-table-column
        v-for="(item, index) in showColumns"
        :key="index"
        align="center"
        :label="item.label"
        :min-width="item.width"
        :prop="item.prop"
        show-overflow-tooltip
        :sortable="item.sortable">
        <template #default="{ row }">
          <el-button v-if="item.prop === 'code'" class="link-button" type="text" @click="handleDetail(row)">
            {{ row.code }}
          </el-button>
          <span v-else-if="item.label === '日期时间'">
            {{ parseTime(row.consultTime, '{y}-{m}-{d}') }}
          </span>
          <span v-else>{{ row[item.prop] }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="140px">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleFollowUp(scope.row)">跟进</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange" />
    <Edit ref="edit" @consultSave="consultSave" />
    <FollowUp ref="followUp" @consultSave="consultSave" />
  </div>
</template>

<script>
  import to from 'await-to-js'
  import consultApi from '@/api/consult'
  import Edit from './components/Edit'
  import FollowUp from './components/FollowUp'
  import TableTool from '@/components/table/TableTool'

  export default {
    name: 'Consult',
    components: {
      Edit,
      FollowUp,
      TableTool,
    },
    data() {
      return {
        height: this.$baseTableHeight(2),
        listLoading: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        list: [],
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          name: '', // 联系人
          inchargeName: '', //对接人
          unit: '', //单位
        },
        selectRows: [], //选择的表格数据
        contractOptions: {}, //合同类型
        productLineOptions: {}, //产品线
        lines: [],
        // 自定义列表
        showColumns: [],
        columns: [
          {
            label: '序号',
            width: '160px',
            prop: 'code',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '日期时间',
            width: '100px',
            prop: 'consultTime',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '所在省',
            width: '200px',
            prop: 'province',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '所在市',
            width: '100px',
            prop: 'city',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '单位名称',
            width: '280px',
            prop: 'unit',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '联系人',
            prop: 'name',
            width: '140px',
          },
          {
            label: '对接人',
            width: '120px',
            prop: 'inchargeName',
            sortable: false,
            disableCheck: false,
          },
        ],
      }
    },
    watch: {
      showColumns: function () {
        this.$nextTick(() => this.$refs.table.doLayout())
      },
    },
    activated() {
      this.queryData()
    },
    mounted() {
      this.queryData()
      this.getOptions()
    },
    methods: {
      getOptions() {
        Promise.all([this.getDicts('contract_type'), this.getDicts('sys_product_line')])
          .then(([contract, productLine]) => {
            this.contractOptions = {}
            contract.data.values.filter((i) => {
              this.contractOptions[i.key] = i.value
            })
            this.productLineOptions = {}
            productLine.data.values.filter((i) => {
              this.productLineOptions[i.key] = i.value
            })
            this.lines = productLine.data.values
          })
          .catch((err) => console.log(err))
      },
      async queryData() {
        this.listLoading = true
        const params = { ...this.queryForm }
        const [err, res] = await to(consultApi.list(params))
        if (err) return (this.listLoading = false)
        this.list = res.data.list || []
        this.total = res.data.total
        this.listLoading = false
        this.$nextTick(() => this.$refs.table.doLayout())
      },
      reset() {
        this.queryForm = {
          pageNum: 1,
          pageSize: 10,
          name: '', // 联系人
          inchargeName: '', //对接人
          unit: '', //单位
        }
        this.queryData()
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.queryData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.queryData()
      },
      async handleEdit(row = null) {
        row ? this.$refs.edit.init(row.id) : this.$refs.edit.init()
      },
      async handleFollowUp(row = null) {
        row ? this.$refs.followUp.init(row.id) : this.$refs.followUp.init()
      },
      handleDetail(row) {
        this.$router.push({
          path: '/consult/detail',
          query: {
            id: row.id,
          },
        })
      },
      handleDelete(row) {
        let ids = [row.id]
        this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            const [err, res] = await to(consultApi.delete({ id: ids }))
            if (err) return
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
              this.queryData()
            }
          })
          .catch(() => {})
      },
      consultSave(createProj) {
        this.queryData()
        if (createProj) {
          this.$router.push({
            path: '/opportunity/business',
          })
        }
      },
    },
  }
</script>

<style lang="scss" scoped>
  $base: '.contract-container';
</style>
