<template>
  <div>
    <el-form :rules="rules" ref="formRef" :model="form">
      <header-title title="退货物品明细" size="16px"></header-title>

      <ele-pro-table
        ref="table"
        :needPage="false"
        :columns="columns"
        :datasource="datasource"
        @columns-change="handleColumnChange"
        :cache-key="cacheKeyUrl"
        class="time-form"
      >
        <template v-slot:technicalDrawings="scope">
          <el-form-item
            style="margin-bottom: 20px"
            :prop="'detailList.' + scope.$index + '.technicalDrawings'"
          >
            <fileMain
              v-model="scope.row.technicalDrawings"
              type="view"
            ></fileMain>
          </el-form-item>
        </template>
      </ele-pro-table>
    </el-form>
  </div>
</template>

<script>
  import {
    getOutInBySourceBizNoAPI,
    saleReturnGetByIdAPI
  } from '@/api/saleManage/returnGoods';
  import { deepClone } from '@/utils';
  import bpmDetail from '@/views/bpm/processInstance/detail.vue';
  import { reviewStatusEnum } from '@/enum/dict';
  import fileMain from '@/components/addDoc/index.vue';
  import dictMixins from '@/mixins/dictMixins';
  import tabMixins from '@/mixins/tableColumnsMixin';

  export default {
    mixins: [dictMixins, tabMixins],
    name: 'customerReturnOrderDetail',
    components: { bpmDetail, fileMain },
    props: ['row'],
    data() {
      return {
        title: '客户退货处理单',
        outboundDetailsDialogFlag: false,
        loading: false,
        cacheKeyUrl: 'eos-62240a29-saleManage-customerReturnOrderDetail',
        datasource: [],
        warehousingMaterialList: [],
        materialCodeReqList: [],
        metaList: [],
        currentRow: {},
        form: {},
        rules: {}
      };
    },
    computed: {
      columns() {
        let basicFields = [
          {
            width: 45,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            fixed: 'left'
          },
          {
            minWidth: 160,
            prop: 'productCode',
            label: '编码',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            minWidth: 120,
            prop: 'productName',
            label: '名称',
            showOverflowTooltip: true,
            align: 'center'
          },

          {
            minWidth: 160,
            prop: 'batchNo',
            label: '批次号',
            showOverflowTooltip: true,
            slot: 'batchNo',
            align: 'center'
          },
          {
            minWidth: 160,
            prop: 'barcodes',
            label: '发货条码',
            showOverflowTooltip: true,
            slot: 'barcodes',
            align: 'center'
          },
          {
            minWidth: 120,
            prop: 'materielDesignation',
            label: '物料代号',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            minWidth: 120,
            prop: 'clientCode',
            label: '客户代号',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            minWidth: 120,
            prop: 'engrave',
            label: '刻码',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            minWidth: 160,
            prop: 'packageNo',
            align: 'center',
            label: '包装编码',
            showOverflowTooltip: true
          },
          {
            minWidth: 100,
            prop: 'packingQuantity',
            align: 'center',
            label: '包装数量',
            showOverflowTooltip: true
          },
          {
            minWidth: 120,
            prop: 'packingUnit',
            align: 'center',
            label: '包装单位',
            showOverflowTooltip: true
          },
          {
            minWidth: 100,
            prop: 'measurementCount',
            label: '计量数量',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            minWidth: 150,
            prop: 'measurementUnit',
            label: '计量单位',
            showOverflowTooltip: true,
            align: 'center'
          },
          // {
          //   width: 100,
          //   prop: 'singlePrice',
          //   label: '单价',
          //   slot: 'singlePrice',
          //   align: 'center'
          // },
          // {
          //   width: 100,
          //   prop: 'totalPrice',
          //   label: '合计',
          //   align: 'center'
          // },

          {
            minWidth: 80,
            prop: 'weight',
            label: '重量',
            slot: 'weight',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            minWidth: 100,
            prop: 'weightUnit',
            label: '重量单位',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            width: 240,
            prop: 'technologyRouteName',
            label: '工艺路线',
            slot: 'technologyRouteName',
            align: 'center'
          },
          {
            width: 240,
            prop: 'technicalDrawings',
            label: '技术图纸',
            slot: 'technicalDrawings',
            align: 'center'
          },
          {
            prop: 'provenance',
            label: '产地',
            slot: 'provenance',
            align: 'center',
            // show:this.contractBookType==2,
            minWidth: 200,
            showOverflowTooltip: true,
            formatter: (row, column) => {
              return row.provenance && row.provenance.length
                ? row.provenance
                    .map((item) => this.getDictValue('产地', item))
                    .join(',')
                : '';
            }
          },
          {
            minWidth: 140,
            prop: 'reason',
            label: '客户退货理由',
            align: 'center',
            slot: 'reason',
            headerSlot: 'headerReason'
          },
          {
            minWidth: 180,
            prop: 'feedbackDate',
            label: '客户反馈日期',
            align: 'center',
            slot: 'feedbackDate'
          },
          {
            minWidth: 180,
            prop: 'sendDate',
            label: '客户发出日期',
            slot: 'sendDate',
            align: 'center',
            headerSlot: 'headerSendDate'
          },
          {
            minWidth: 140,
            prop: 'ifPackageOk',
            label: '包装完好与否',
            align: 'center',
            formatter: (_row, _column, cellValue) => {
              return cellValue == 1 ? '是' : cellValue === 0 ? '否' : '';
            }
          },
          {
            minWidth: 140,
            prop: 'unpackUserName',
            label: '拆包装责任人',
            align: 'center'
          },
          {
            minWidth: 250,
            prop: 'ifReasonMatch',
            label: '退货原因是否与退货信息一致',
            align: 'center',
            formatter: (_row, _column, cellValue) => {
              return cellValue == 1 ? '是' : cellValue === 0 ? '否' : '';
            }
          },
          {
            minWidth: 140,
            prop: 'reason',
            label: '客户退货理由',
            align: 'center'
          },
          {
            minWidth: 250,
            prop: 'unqualifiedReasonAnalysis',
            label: '不合格原因分析',
            align: 'center'
          },
          {
            minWidth: 250,
            prop: 'unqualifiedTypeName',
            label: '不合格类型',
            align: 'center'
          },
          {
            minWidth: 250,
            prop: 'handleOpinion',
            label: '对不合格品的处理意见',
            align: 'center'
          },

          {
            minWidth: 130,
            prop: 'inOrOut',
            label: '内部或外部',
            align: 'center',
            slot: 'inOrOut',
            formatter: (_row, _column, cellValue) => {
              return cellValue == 1 ? '内部' : cellValue === 2 ? '外部' : '';
            }
          },
          {
            minWidth: 250,
            prop: 'outerInfo',
            label: '外协单位',
            align: 'center'
          },
          {
            minWidth: 250,
            prop: 'improveCorrect',
            label: '不合格原因是否改善到位',
            align: 'center'
          },
          {
            minWidth: 250,
            prop: 'improveOpinion',
            label: '对改善后产品处理意见',
            align: 'center'
          },
          {
            minWidth: 120,
            prop: 'repairStatus',
            label: '返修状态',
            align: 'center',
            fixed: 'right',
            formatter: (_row, _column, cellValue) => {
              return cellValue == 1
                ? '待排产'
                : cellValue === 2
                ? '已排产'
                : cellValue === 3
                ? '生产中'
                : cellValue === 4
                ? '入库中'
                : cellValue === 5
                ? '已入库'
                : '';
            }
          }
        ];

        this.activeComp = this.taskDefinitionKey;
        return basicFields;
      }
    },
    created() {
      this.requestDict('产地');
    },
    mounted() {
      this.init('', this.row);
    },
    methods: {
      init(type, row = {}) {
        this.dialogType = type;
        console.log(type);
        this.currentRow = row;
        if (type !== 'add') {
          this.getReturnInfo(row);
        }
      },
      async getReturnInfo(row) {
        if (!row.handleReceiptId) return;
        this.form = await saleReturnGetByIdAPI(row.handleReceiptId);

        //拼接信息
        const data = await getOutInBySourceBizNoAPI(row.sendNo);
        this.infoData = deepClone(data);

        //出库物料明细
        this.warehousingMaterialList = this.infoData?.outInDetailVOList || [];

        // 出库包装明细
        if (this.warehousingMaterialList.length) {
          this.materialCodeReqList = [];
          this.metaList = [];

          this.warehousingMaterialList.forEach((item) => {
            if (item.outInDetailRecordVOList?.length) {
              item.outInDetailRecordVOList.forEach((i) => {
                const newItem = {
                  ...i,
                  outInCode: this.infoData.bizNo,
                  outInId: item.outInId,
                  measurementCount: i.totalCount,
                  outboundDetailId: i.id,
                  outboundType: 1,
                  packageCount: i.packingCount,
                  packingUnit: i.packingUnit,
                  weight: i.weight,
                  specification: item.specification,
                  name: i.name,
                  carveCode: i.carveCode,
                  categoryCode: i.categoryCode,
                  id: ''
                };
                this.materialCodeReqList.push(newItem);

                if (i.outInDetailRecordMaterialDetailVOList?.length) {
                  i.outInDetailRecordMaterialDetailVOList.forEach((detail) => {
                    const newDetail = {
                      ...detail,
                      outInId: item.outInId,
                      outInCode: item.outInCode,
                      measurementCount: 1,
                      outboundDetailId: detail.id,
                      outboundType: 2,
                      packageCount: detail.packingCount,
                      packingUnit: detail.packingUnit,
                      weight: detail.weight,
                      weightUtil: detail.weightUnit,
                      specification: item.specification,
                      categoryCode: detail.assetCode,
                      name: detail.assetName,
                      carveCode: detail.carveCode,
                      id: ''
                    };
                    this.metaList.push(newDetail);
                  });
                }
              });
            }
          });
        }

        // this.form.detailList.forEach(item => {
        //   const findItem = item.outboundType === 1 ? this.materialCodeReqList.find(i => i.outboundDetailId === item.outboundDetailId) : this.metaList.find(i => i.outboundDetailId === item.outboundDetailId) || {}
        //   item.name = findItem.name
        //   item.categoryCode = findItem.categoryCode
        //   item.specification = findItem.specification
        //   item.weightUtil = findItem.weightUtil
        //   item.packingUnit = findItem.packingUnit
        //   item.measuringUnit = findItem.measuringUnit
        // })

        this.datasource = this.form.detailList;
      }
    }
  };
</script>
<style scoped lang="scss"></style>
