<!--
 * @Author: liuzhenlin 461480418@qq.ocm
 * @Date: 2023-01-05 11:28:51
 * @LastEditors: liuzhenlin
 * @LastEditTime: 2023-01-11 15:09:48
 * @FilePath: \订单全流程管理系统\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="contractCode">
            <el-input
              v-model="queryForm.contractCode"
              clearable
              placeholder="合同编号"
              @keyup.enter.native="queryData" />
          </el-form-item>
          <el-form-item prop="contractName">
            <el-input
              v-model="queryForm.contractName"
              clearable
              placeholder="合同名称"
              @keyup.enter.native="queryData" />
          </el-form-item>
          <el-form-item prop="custId">
            <el-input v-model="queryForm.custName" clearable placeholder="客户名称" @keyup.enter.native="queryData" />
          </el-form-item>
          <el-form-item prop="custId">
            <el-input v-model="queryForm.nboName" clearable placeholder="项目名称" @keyup.enter.native="queryData" />
          </el-form-item>
          <el-form-item prop="custId">
            <el-select v-model="queryForm.approStatus" clearable placeholder="审批状态">
              <el-option v-for="item in approStatusOption" :key="item.id" :label="item.label" :value="item.id" />
            </el-select>
          </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 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"
        :min-width="item.width"
        :prop="item.prop"
        show-overflow-tooltip
        :sortable="item.sortable">
        <template #default="{ row }">
          <el-button v-if="item.prop === 'custName'" class="link-button" type="text" @click="handleCustDetail(row)">
            {{ row.custName }}
          </el-button>
          <span v-else-if="item.prop === 'contractAmount'">
            {{ formatPrice(row.contractAmount) }}
          </span>
          <span v-else-if="item.label === '合同时间'">
            {{ parseTime(row.contractStartTime, '{y}-{m}-{d}') }}~{{ parseTime(row.contractEndTime, '{y}-{m}-{d}') }}
          </span>
          <el-button
            v-else-if="item.prop === 'contractName'"
            style="font-size: 14px"
            type="text"
            @click="handleContractDetail(row)">
            {{ row.contractName }}
          </el-button>
          <span v-else>{{ row[item.prop] }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="120px">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
          <el-button type="text" @click="handleTransfer(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" @contractSave="contractSave" />
    <!-- 转移合同 -->
    <Transfer ref="transfer" :contract-id="contractId" @transferSave="contractSave" />
  </div>
</template>

<script>
  import to from 'await-to-js'
  import contractApi from '@/api/contract'
  import Edit from './components/Edit'
  import TableTool from '@/components/table/TableTool'
  import Transfer from './components/Transfer'
  export default {
    name: 'OpenSea',
    components: {
      Edit,
      TableTool,
      Transfer,
    },
    data() {
      return {
        height: this.$baseTableHeight(2),
        approStatusOption: [
          { id: 1, label: '审批中' },
          { id: 2, label: '已拒绝' },
          { id: 3, label: '已通过' },
        ],
        contractId: 0, //当前合同id
        listLoading: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        list: [],
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          contractCode: '', // 合同编号
          contractName: '', //合同名称
          custName: '', // 客户名称  ()
          nboName: '', //项目名称
          approStatus: '', //审批状态
        },
        selectRows: [], //选择的表格数据
        industryOptions: [], //客户行业
        levelOptions: [], //客户级别
        // 自定义列表
        checkList: [],
        columns: [
          {
            label: '合同编号',
            width: '160px',
            prop: 'contractCode',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '合同名称',
            width: '280px',
            prop: 'contractName',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '客户名称',
            width: '280px',
            prop: 'custName',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '项目名称',
            width: '280px',
            prop: 'nboName',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '合同类型',
            width: '100px',
            prop: 'contractType',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '合同金额',
            width: '120px',
            prop: 'contractAmount',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '合同时间',
            width: '180px',
            prop: 'distributorName',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '审批状态',
            width: '100px',
            prop: 'approStatus',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '销售工程师',
            width: '120px',
            prop: 'inchargeName',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '公司签约人',
            width: '120px',
            prop: 'signatoryName',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '客户签约人',
            width: '120px',
            prop: 'custSignatoryName',
            sortable: false,
            disableCheck: false,
          },
          {
            label: '经销商/代理商',
            width: '120px',
            prop: 'distributorName',
            sortable: false,
            disableCheck: false,
          },
        ],
      }
    },
    computed: {
      finallyColumns() {
        return this.columns.filter((item) => this.checkList.includes(item.label))
      },
    },
    mounted() {
      this.queryData()
    },
    methods: {
      async queryData() {
        this.listLoading = true
        const params = { ...this.queryForm }
        const [err, res] = await to(contractApi.getList(params))
        if (err) return (this.listLoading = false)
        this.list = res.data.list || []
        this.total = res.data.total
        this.listLoading = false
      },
      reset() {
        this.queryForm = {
          pageNum: 1,
          pageSize: 10,
          custCode: '', // 客户编码
          custName: '', //客户名称
          custIndustry: '', // 客户行业  ()
          custLevel: '', //客户级别
        }
        this.queryData()
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.queryData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.queryData()
      },
      setSelectRows(val) {
        this.selectRows = val.map((item) => item.id)
      },
      // 客户编辑
      async handleEdit(row = null) {
        row ? this.$refs.edit.init(row.id) : this.$refs.edit.init()
      },
      // 合同详情
      handleContractDetail(row) {
        this.$router.push({
          path: '/contract/detail',
          query: {
            id: row.id,
          },
        })
      },
      // 客户详情
      handleCustDetail(row) {
        this.$router.push({
          path: '/customer/detail',
          query: {
            id: row.custId,
          },
        })
      },
      // 转移合同
      handleTransfer(row) {
        this.contractId = row.id
        this.$refs.transfer.open()
      },
      // 合同删除
      handleDelete(row = null) {
        let ids = row ? [row.id] : this.selectRows
        if (!ids[0]) {
          return
        }
        this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            const [err, res] = await to(contractApi.delContract({ id: ids }))
            if (err) return
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
              this.queryData()
            }
          })
          .catch(() => {})
      },
      contractSave() {
        this.queryData()
      },
    },
  }
</script>

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