<template>
  <el-form ref="form" >
    <ele-pro-table
      ref="table"
      :needPage="false"
      :columns="columns"
      :toolkit="[]"
      :datasource="form.datasource"
      cache-key="systemRoleTable17"
      class="time-form"
    >
      <!-- 表头工具栏 -->
      <template v-slot:toolbar>
        <div class="headbox">
          <div class="pricebox">
            <span class="amount">总计：{{ allPrice }}元</span>
            <span class="amount" v-if="isDiscountTotalPrice"
              >优惠后总金额：{{ form.discountTotalPrice }}元</span
            >
          </div>
        </div>
      </template>
    </ele-pro-table>
  </el-form>
</template>
<script>
  import dictMixins from '@/mixins/dictMixins';

  import fileMain from '@/components/addDoc/index.vue';
  import {contactQueryByCategoryIdsAPI} from "@/api/bpm/components/supplierManage/contact";



  export default {
    mixins: [dictMixins],
    components: {
      fileMain
    },
    props: {
      isDiscountTotalPrice: {
        default: false,
        type: Boolean
      },

      isDiscount: {
        //折让
        type: Boolean,
        default: true
      },
      isCustomerMark: {
        //客户代号必填
        type: Boolean,
        default: false
      },
      pageName: {
        default: ''
      },
      isChangeCount: {
        //默认计算
        type: Boolean,
        default: true
      },
      contractBookType: {
        //合同类型 1销售 2采购
        type: [String, Number],
        default: 1
      },
    },
    data() {
      return {
        allPrice: 0,

        form: {
        discountTotalPrice: 0,
          datasource: []
        },

        columns: [
          {
            width: 45,
            type: 'index',
            columnKey: 'index',
            align: 'center',
          },
          {
            width: 280,
            prop: 'productName',
            label: '名称',
            slot: 'productName',
            headerSlot: 'headerProductName',
            align: 'center',
          },
          {
            width: 120,
            prop: 'productCode',
            label: '编码',
            slot: 'productCode',
            align: 'center',
          },
          {
            width: 200,
            prop: 'productCategoryName',
            label: '类型',
            slot: 'productCategoryName',
            align: 'center',
          },
          {
            width: 120,
            prop: 'specification',
            label: '规格',
            slot: 'specification',
            align: 'center'
          },
          {
            width: 200,
            prop: 'customerMark',
            label: '客户代号',
            slot: 'customerMark',
            headerSlot: 'headerCustomerMark',
            align: 'center',
            show: this.contractBookType == 1
          },
          {
            width: 200,
            prop: 'supplierMark',
            label: '供应商代号',
            slot: 'supplierMark',
            headerSlot: 'headerSupplierMark',
            align: 'center',
            show: this.contractBookType == 2
          },
          {
            minWidth: 120,
            prop: 'entrustedEnterpriseId',
            label: '受托企业',
            slot: 'entrustedEnterpriseId',
            show: this.isCustomerMark,
            align: 'center',
            showOverflowTooltip: true,
            formatter: (row, column) => {
              return (
                this.supplierObj&&this.supplierObj[row.productId]?.find(
                  (item) => item.id === row.entrustedEnterpriseId
                )?.name || ''
              );
            }
          },
          {
            width: 150,
            prop: 'totalCount',
            label: '数量',
            slot: 'totalCount',
            headerSlot: 'headerTotalCount',
            align: 'center'
          },

          {
            width: 120,
            prop: 'measuringUnit',
            label: '计量单位',
            slot: 'measuringUnit',
            align: 'center'
          },
          {
            minWidth: 110,
            prop: 'sendTotalCount',
            label: '已发货数量',
            slot: 'sendTotalCount',
            show: this.pageName == 'send',
            align: 'center'
          },
          {
            minWidth: 110,
            prop: 'notSendTotalCount',
            label: '未发货数量',
            slot: 'notSendTotalCount',
            show: this.pageName == 'send',
            align: 'center'
          },

          {
            width: 120,
            prop: 'singleWeight',
            label: '单重',
            slot: 'singleWeight',
            headerSlot: 'headerSingleWeight',
            align: 'center'
          },
          {
            width: 120,
            prop: 'totalWeight',
            label: '总重',
            slot: 'totalWeight',
            align: 'center'
          },
          {
            width: 120,
            prop: 'weightUnit',
            label: '重量单位',
            slot: 'weightUnit',
            align: 'center'
          },

          {
            width: 160,
            prop: 'pricingWay',
            label: '计价方式',
            slot: 'pricingWay',
            align: 'center',
            formatter: (row, column) => {
              return row.pricingWay == 1 ? '按数量计费' : row.pricingWay == 2 ? '按重量计费':'';

            }
          },
          {
            width: 200,
            prop: 'singlePrice',
            label: '单价',
            slot: 'singlePrice',
            headerSlot: 'headerSinglePrice',
            align: 'center'
          },
          {
            width: 160,
            prop: 'taxRate',
            label: '税率',
            slot: 'taxRate',
            align: 'center'
          },
          {
            width: 160,
            prop: 'discountSinglePrice',
            label: '折让单价',
            align: 'center',
            show: this.isDiscount,
            formatter: (_row, _column, cellValue) => {
              return _row.discountSinglePrice
                ? Number(_row.discountSinglePrice).toFixed(2)
                : '';
            }
          },
          {
            width: 120,
            prop: 'totalPrice',
            label: '合计',
            slot: 'totalPrice',
            align: 'center'
          },
          {
            width: 160,
            prop: 'discountTotalPrice',
            label: '折让合计',
            align: 'center',
            show: this.isDiscount,
            formatter: (_row, _column, cellValue) => {
              return _row.discountTotalPrice
                ? Number(_row.discountTotalPrice).toFixed(2)
                : '';
            }
          },
          {
            width: 160,
            prop: 'productBrand',
            label: '牌号',
            slot: 'productBrand',
            align: 'center'
          },
          {
            width: 120,
            prop: 'modelType',
            label: '型号',
            slot: 'modelType',
            align: 'center'
          },

          {
            width: 120,
            prop: 'imgCode',
            align: 'center',
            label: '图号/件号',
            showOverflowTooltip: true
          },
          {
            width: 120,
            prop: 'produceType',
            align: 'center',
            label: '生产类型',
            showOverflowTooltip: true,
            formatter: (row, column) => {
              return row?.produceType
                ?.map((item) => {
                  return this.getDictValue('生产类型', item);
                })
                ?.toString();
            }
          },
          {
            width: 120,
            prop: 'approvalNumber',
            align: 'center',
            label: '批准文号',
            showOverflowTooltip: true
          },
          {
            width: 120,
            prop: 'packingSpecification',
            align: 'center',
            label: '包装规格',
            showOverflowTooltip: true
          },

          {
            width: 160,
            prop: 'customerExpectDeliveryDeadline',
            label: this.contractBookType == 1 ? '客户期望交期' : '交付日期',
            slot: 'customerExpectDeliveryDeadline',
            headerSlot: 'headerCustomerExpectDeliveryDeadline',
            align: 'center'
          },
          {
            width: 160,
            prop: 'produceDeliveryDeadline',
            label: '生产交付交期',
            slot: 'produceDeliveryDeadline',
            headerSlot: 'headerProduceDeliveryDeadline',
            show: this.contractBookType == 1,
            align: 'center'
          },
          {
            width: 200,
            prop: 'guaranteePeriod',
            label: '质保期',
            slot: 'guaranteePeriod',
            headerSlot: 'headerCustomerExpectDeliveryDeadline',
            align: 'center'
          },
          {
            width: 200,
            prop: 'guaranteePeriodDeadline',
            label: '质保期截止日期',
            slot: 'guaranteePeriodDeadline',
            align: 'center'
          },

          {
            width: 220,
            prop: 'customerReqFiles',
            label: '客户需求',
            slot: 'customerReqFiles',
            align: 'center'
          },
          {
            width: 130,
            prop: 'technicalAnswerName',
            label: '技术答疑人',
            slot: 'technicalAnswerName',
            align: 'center'
          },
          {
            width: 220,
            prop: 'technicalParams',
            label: '技术参数',
            slot: 'technicalParams',
            align: 'center'
          },
          {
            width: 240,
            prop: 'technicalDrawings',
            label: '技术图纸',
            slot: 'technicalDrawings',
            align: 'center'
          },
          {
            width: 240,
            prop: 'technologyRouteName',
            label: '工艺路线',
            slot: 'technologyRouteName',
            align: 'center'
          },
          {
            width: 240,
            prop: 'industryArtFiles',
            label: '工艺附件',
            slot: 'industryArtFiles',
            align: 'center'
          },
          {
            width: 240,
            prop: 'otherFiles',
            label: '其他附件',
            slot: 'otherFiles',
            align: 'center'
          },
          {
            width: 220,
            prop: 'remark',
            label: '备注',
            slot: 'remark',
            align: 'center'
          }
        ]
      };
    },
    computed: {},

    created() {
      this.requestDict('生产类型');
    },
    methods: {
      async getSupplierObj(productList, queryName) {
        try {
          let categoryIds = productList.map((item) => item[queryName]);
          return await contactQueryByCategoryIdsAPI({
            categoryIds,
            isQueryEE: 1
          });
        } catch (e) {
          return Promise.resolve({});
        }
      },

      // 返回列表数据
      getTableValue() {
        let comitDatasource = this.form.datasource;
        if (comitDatasource.length === 0) return [];
        comitDatasource.forEach((v) => {
          if (v.guaranteePeriodUnitCode) {
            v.guaranteePeriodUnitName = this.getDictValue(
              '保质期单位',
              v.guaranteePeriodUnitCode
            );
          }
          v.technicalDrawings = v.technicalDrawings ? v.technicalDrawings : [];
          v.customerReqFiles = v.customerReqFiles || [];
          v.industryArtFiles = v.industryArtFiles || [];
          v.otherFiles = v.otherFiles || [];
        });
        return comitDatasource;
      },

      //修改回显
      async putTableValue(data) {
        console.log(data,'dasdas')
        let productList =
          (data &&
            (data.quoteProductList || data.productList || data.detailList)) ||
          [];
        if (productList) {
          this.form.datasource = productList;
          this.allPrice = data.totalAmount || data.totalPrice||data?.contractVO?.totalPrice;
          if (this.isDiscountTotalPrice) {
            this.form.discountTotalPrice = data.payAmount||data.discountTotalPrice||data?.contractVO?.discountTotalPrice;
          }
          this.supplierObj = await this.getSupplierObj(
            productList,
            'productId'
          );
          this.$refs.table.reload();
        }
      }
    }
  };
</script>
<style lang="scss" scoped>
  .headbox {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .amount {
      font-size: 14px;
      font-weight: bold;
      padding-right: 30px;
    }
  }

  .time-form .el-form-item {
    margin-bottom: 0 !important;
  }

  ::v-deep .period {
    display: flex;

    .borderleftnone {
      .el-input--medium .el-input__inner {
        border-top-right-radius: 0;
        border-bottom-right-radius: 0;
      }
    }

    .borderrightnone {
      .el-input--medium .el-input__inner {
        border-top-left-radius: 0;
        border-bottom-left-radius: 0;
      }
    }
  }

  ::v-deep .time-form tbody > tr:hover > td {
    background-color: transparent !important;
  }

  ::v-deep .time-form .el-table tr {
    background-color: #ffffff;
  }

  .pricebox {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    font-weight: bold;
  }
</style>
