<template>
  <el-dialog
    title="选择报价单"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    top="5vh"
    :close-on-press-escape="false"
    append-to-body
    width="70%"
  >
    <div class="ele-border-lighter form-content" v-loading="loading">
      <search-quotation @search="reload"></search-quotation>

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        height="calc(100vh - 350px)"
        full-height="calc(100vh - 116px)"
        tool-class="ele-toolbar-form"
        @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>
    </div>
    <div class="btns">
      <el-button type="primary" size="small" @click="selected"
      >生成合同
      </el-button
      >
      <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import searchQuotation from '@/views/saleManage/quotation/components/searchQuotation.vue';

import {getTableList, getDetail, isHasGeneratedContractAPI} from '@/api/saleManage/quotation';
import dictMixins from '@/mixins/dictMixins';

export default {
  mixins: [dictMixins],
  components: {
    searchQuotation
  },
  data() {
    return {
      radio: null,
      visible: false,
      loading: false,
      columns: [
        {
          action: 'action',
          slot: 'action',
          align: 'center',
          label: '选择'
        },
        {
          columnKey: 'index',
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'contactName',
          label: '询价方名称',
          align: 'center',
          slot: 'contactName',
          showOverflowTooltip: true,
          minWidth: 200
        },
        {
          prop: 'contactLinkName',
          label: '询价方联系人',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 140
        },
        {
          prop: 'contactTel',
          label: '询价方联系电话',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 140
        },
        {
          prop: 'quoteName',
          label: '报价方名称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200
        },
        {
          prop: 'quoteLinkName',
          label: '报价方联系人',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 140
        },

        {
          prop: 'quoteTel',
          label: '报价方联系电话',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 140
        },
        {
          prop: 'deliveryDate',
          label: '交货日期',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100
        },
        {
          prop: 'settlementModeName',
          label: '付款方式',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100
        },

        {
          prop: 'taxRate',
          label: '税率',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 140,
          formatter: (_row, _column, cellValue) => {
            return _row.taxRate + '%';
          }
        },
        {
          prop: 'totalPrice',
          label: '总金额',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100
        },
        {
          prop: 'createTime',
          label: '创建时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 170,
          formatter: (_row, _column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        }
      ]
    };
  },
  computed: {},
  created() {
  },
  methods: {
    open() {
      this.visible = true;
    },

    /* 表格数据源 */
    datasource({page, limit, where, order}) {
      return getTableList({
        pageNum: page,
        size: limit,
        ...where,
        status: 2

      });
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({page: 1, where});
    },
    // 单击获取id
    cellClick(row) {
      this.current = row;
      this.radio = row.id;
    },
    handleClose() {
      this.visible = false;
      this.current = null;
    },

    async selected() {
      if (!this.current) {
        return this.$message.warning('请至少选择一条数据');
      }
      let data = await this.addGenerateContract();

      if (data) {
        this.$emit('changeParent', {data, sourceCode: this.current.code});

        this.handleClose();
      }
    },
    //新增合同
    async addGenerateContract() {
     //报价单是否生成过合同
      let res = await isHasGeneratedContractAPI(this.current.id)
      if(res) this.$message.warning('该报价单已生成过合同，请注意')
      //获取详情
      const data = await getDetail(this.current.id);
      data.quoteProductList.forEach(item => {
        this.$set(item, 'discountTotalPrice', Number(item.totalPrice))
        this.$set(item, 'discountSinglePrice', Number(item.singlePrice))
      })
      let obj = {
        contractVO: {
          categoryId: '',
          categoryName: '',
          contractEndDate: '',
          contractFile: [],
          contractName: data.contactName,
          contractNo: '',
          contractNumber: '',
          contractStartDate: '',
          createDept: '',
          createTime: '',
          createUsername: '',
          enabled: '',
          id: '',
          settlementMode: data.settlementMode,
          settlementModeName: data.settlementModeName,
          opportunityId: '',
          partaAddress: data.contactAddress,
          partaEmail: data.contactEmail,
          partaFax: data.contactFax,
          partaId: data.contactId,
          partaLinkId: data.contactLinkId,
          partaLinkName: data.contactLinkName,
          partaName: data.contactName,
          partaTel: data.contactTel,
          partaUnifiedSocialCreditCode: '',
          partbAddress: data.quoteAddress,
          partbEmail: data.quoteEmail,
          partbFax: data.quoteFax,
          partbId: data.quoteCompanyId,
          partbLinkId: data.quoteLinkId,
          partbLinkName: data.quoteLinkName,
          partbName: data.quoteName,
          partbTel: data.quoteTel,
          partbUnifiedSocialCreditCode: '',
          receiptPaymentType: '',
          reviewerId: '',
          reviewerName: '',
          reviewerTime: '',
          sourceId: this.current.id,
          sourceType: 1,
          sourceName: data.code,
          status: '',
          totalPrice: data.totalPrice,
          discountTotalPrice: data.totalPrice,
          type: '1',
          typeName: '销售合同',
          updateUsername: ''
        },
        receiptPaymentList: [],
        productList: data.quoteProductList
      };
      return obj;
    }
  }
};
</script>

<style lang="scss" scoped>
.btns {
  text-align: center;
  padding: 10px 0;
}
</style>
