<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <headerTitle title="退货信息"></headerTitle>
      <el-row>
        <el-col :span="12">
          <el-form-item
            label="退货类型："
            style="margin-bottom: 16px">
            {{ form.returnSourceType==2?'受托收货单退货':'销售发货单退货' }}
          </el-form-item>
          <el-form-item
            label="客户名称："
            prop="contactName"
            style="margin-bottom: 16px"
          >
            {{ form.contactName }}
          </el-form-item>
          <el-form-item
            label="退货方式："
            style="margin-bottom: 16px">
            {{ form.typeName }}
          </el-form-item>
          <el-form-item
            label="赔偿金额："
            prop="redressAmount"
            style="margin-bottom: 16px"
            v-show="['30', '50'].includes(form.type)"
          >
            {{ form.redressAmount }}元
          </el-form-item>
          <el-form-item
            label="赔偿总金额："
            prop="totalAmount"
            style="margin-bottom: 16px"
          >
            {{ form.totalAmount }}元
          </el-form-item>
          <el-form-item
            label="备注："
            prop="remark"
            style="margin-bottom: 16px"
          >
            {{ form.remark }}
          </el-form-item>
          <el-form-item
            label="创建时间："
            prop="createTime"
            style="margin-bottom: 16px"
          >
            {{ form.createTime }}
          </el-form-item>
          <el-form-item
            label="制单人："
            prop="makerName"
            style="margin-bottom: 16px"
          >
            {{ form.makerName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="退货编码："
            prop="returnNo"
            style="margin-bottom: 16px"
          >
            {{ form.returnNo }}
          </el-form-item>
          <el-form-item
            v-if="form.returnSourceType==2"
            label="受托收货编码："
            prop="entrustedReceiveCode"
            style="margin-bottom: 16px"
          >
            {{ form.entrustedReceiveCode }}
          </el-form-item>
          <el-form-item
            v-else
            label="发货编码："
            prop="sendNo"
            style="margin-bottom: 16px"
          >
            {{ form.sendNo }}
          </el-form-item>

          <el-form-item prop="orderNo" label="订单编码：">
            {{ form.orderNo }}
          </el-form-item>

          <el-form-item
            label="审核状态："
            prop="reviewStatus"
            style="margin-bottom: 16px"
          >
            {{ reviewStatusEnum[form.reviewStatus]?.label }}
          </el-form-item>
          <el-form-item
            label="退货附件："
            prop="returnFiles"
            style="margin-bottom: 16px"
          >
            <div
              v-if="detailData.returnFiles && detailData.returnFiles?.length"
            >
              <el-link
                v-for="link in detailData.returnFiles"
                :key="link.id"
                type="primary"
                :underline="false"
                @click="downloadFile(link)"
              >
                {{ link.name }}
              </el-link
              >
            </div>
          </el-form-item>
          <el-form-item
            label="回执附件："
            prop="sendFiles"
            style="margin-bottom: 16px"
          >
            <div
              v-if="detailData.repliedFiles && detailData.repliedFiles?.length"
            >
              <el-link
                v-for="link in detailData.repliedFiles"
                :key="link.id"
                type="primary"
                :underline="false"
                @click="downloadFile(link)"
              >
                {{ link.name }}
              </el-link
              >
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <headerTitle
      title="产品清单"
      v-show="
        taskDefinitionKey == 'starter' ||
        taskDefinitionKey == 'deptLeader' ||
        ['10', '20', '30'].includes(form.type)
      "
    ></headerTitle>
    <ele-pro-table
      ref="table"
      :needPage="false"
      :columns="competAnalysisListcolumns"
      :toolkit="[]"
      :datasource="detailData.productList"
      row-key="id"
    >
      <!-- <template v-slot:toolbar>
        <div class="headbox">
        <span class="amount">总计：{{detailData.totalAmount}}元</span>
        <span class="amount">应付金额：{{detailData.payAmount}}元</span>
      </div>
      </template> -->
      <template v-slot:technicalDrawings="{ row }">
        <div v-if="row.technicalDrawings && row.technicalDrawings?.length">
          <el-link
            v-for="link in row.technicalDrawings"
            :key="link.id"
            type="primary"
            :underline="false"
            @click="downloadFile(link)"
          >
            {{ link.name }}</el-link
          >
        </div>
      </template>
    </ele-pro-table>
  </div>
</template>

<script>
  import { getReturnSaleOrderrecordDetail } from '@/api/bpm/components/saleManage/saleorder';
  import { getFile } from '@/api/system/file';
  import dictMixins from '@/mixins/dictMixins';
  import { reviewStatusEnum } from '@/enum/dict';


  export default {
    mixins: [dictMixins],
    components: {

    },
    data() {
      return {
        codeData: [],
        reviewStatusEnum,
        visible: false,
        detailId: '',
        title: '详情',
        row: {},
        activeName: 'base',
        form: {
          orderFiles: []
        },
        rules: {},
        detailData: {},

        competAnalysisListcolumns: [
          {
            width: 45,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            fixed: 'left'
          },
          {
            width: 200,
            prop: 'productName',
            label: '名称',
            slot: 'productName'
          },
          {
            width: 120,
            prop: 'productCode',
            label: '编码',
            slot: 'productCode'
          },
          {
            width: 200,
            prop: 'productCategoryName',
            label: '类型',
            slot: 'productCategoryName'
          },
          {
            width: 160,
            prop: 'productBrand',
            label: '牌号',
            slot: 'productBrand'
          },
          {
            width: 120,
            prop: 'modelType',
            label: '型号',
            slot: 'modelType'
          },
          {
            width: 120,
            prop: 'specification',
            label: '规格',
            slot: 'specification'
          },
          {
            width: 200,
            prop: 'customerMark',
            label: '客户代号',
            slot: 'customerMark'
          },
          {
            width: 200,
            prop: 'warehouseName',
            label: '仓库名称',
            slot: 'warehouseName'
          },
          {
            width: 120,
            prop: 'sendTotalCount',
            label: '发货数量',
            slot: 'sendTotalCount'
          },

          {
            width: 120,
            prop: 'totalCount',
            label: '退货数量',
            slot: 'totalCount'
          },
          {
            width: 80,
            prop: 'measuringUnit',
            label: '计量单位',
            slot: 'measuringUnit'
          },
          {
            width: 120,
            prop: 'singleWeight',
            label: '单重',
            slot: 'singleWeight',
            align: "center"
          },
          {
            width: 200,
            prop: 'receiveTotalWeight',
            label: '收货总重',
            slot: 'receiveTotalWeight',
            align: "center",
            headerSlot: 'headerSendTotalWeight'
          },
          {
            width: 200,
            prop: 'sendTotalWeight',
            label: '发货总重',
            slot: 'sendTotalWeight',
            align: "center"
          },
          {
            width: 100,
            prop: 'increaseTotalWeight',
            label: '增重重量',
            slot: 'increaseTotalWeight',
            align: "center"
          },
          {
            width: 160,
            prop: 'singlePrice',
            label: '单价',
            slot: 'singlePrice'
          },
          {
            width: 160,
            prop: 'discountSinglePrice',
            label: '折让单价',
            slot: 'discountSinglePrice'
          },
          {
            width: 120,
            prop: 'totalPrice',
            label: '合计',
            slot: 'totalPrice'
          },
          {
            width: 120,
            prop: 'discountTotalPrice',
            label: '折让合计',
            slot: 'discountTotalPrice'
          },

          {
            width: 200,
            prop: 'returnReason',
            label: '退货原因',
            slot: 'returnReason'
          },
          {
            width: 120,
            prop: 'deliveryDays',
            label: '交期(天)',
            slot: 'deliveryDays'
          },
          {
            width: 120,
            prop: 'technicalAnswerName',
            label: '技术答疑人',
            slot: 'technicalAnswerName'
          },
          {
            width: 220,
            prop: 'technicalParams',
            label: '技术参数',
            slot: 'technicalParams'
          },
          {
            width: 240,
            prop: 'technicalDrawings',
            label: '技术图纸',
            slot: 'technicalDrawings',
            formatter: (_row, _column, cellValue) => {
              return _row.guaranteePeriod + _row.guaranteePeriodUnitName;
            }
          },
          {
            width: 220,
            prop: 'remark',
            label: '备注',
            slot: 'remark'
          }
        ]
      };
    },
    props: {
      businessId: {
        default: ''
      },
      taskDefinitionKey: {
        default: ''
      }
    },
    created() {
      this.getDetailData(this.businessId);
    },
    methods: {
      downloadFile(file) {
        getFile({ objectName: file.storePath }, file.name);
      },
      async getTableValue() {
        try {
          // 表单验证通过，执行保存操作
          this.loading = true;
          return this.form;
        } catch (error) {
          console.log(error);
          // 表单验证未通过，不执行保存操作
        }
      },

      getValidate() {
        return Promise.all([
          new Promise((resolve, reject) => {
            this.$refs.inventoryTabledetailRef.validateForm((valid) => {
              if (!valid) {
                reject(false);
              } else {
                resolve(true);
              }
            });
          })
        ]);
      },
      async getDetailData(id) {
        this.loading = true;
        const data = await getReturnSaleOrderrecordDetail(id);
        this.loading = false;
        if (data) {
          this.detailData = data;
          this.form = data;
          this.$nextTick(() => {
            this.$refs.inventoryTabledetailRef &&
              this.$refs.inventoryTabledetailRef.putTableValue(data);
          });
        }
      }
    }
  };
</script>

<style scoped lang="scss">
  .ele-dialog-form {
    .el-form-item {
      margin-bottom: 10px;
    }
  }

  .headbox {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    .amount {
      font-size: 14px;
      font-weight: bold;
      margin-right: 20px;
    }
  }
</style>
