<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"
        :toolkit="[]"
        :datasource="datasource"
        cache-key="customerReturnOrderDialog"
        class="time-form">
        <!--////-->
      </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";


export default {
  name: "customerReturnOrderDetail",
  components: {bpmDetail},
  props: ['row'],
  data() {
    return {
      title: '客户退货处理单',
      outboundDetailsDialogFlag: false,
      loading: false,

      datasource: [],
      warehousingMaterialList: [],
      materialCodeReqList: [],
      metaList: [],
      currentRow: {},
      form: {},
      rules: {}

    }
  },
  computed: {
    columns() {
      let basicFields = [
        {
          width: 45,
          type: 'index',
          columnKey: 'index',
          align: 'center',
          fixed: 'left'
        },
        {
          minWidth: 120,
          prop: 'name',
          label: '名称',
          align: 'center',
          slot: 'name'
        },
        {
          minWidth: 120,
          prop: 'categoryCode',
          label: '编码',
          align: 'center',
          slot: 'categoryCode'
        },
        // {
        //   minWidth: 120,
        //   prop: 'modelType',
        //   label: '型号',
        //   align: 'center',
        //   slot: 'modelType'
        // },
        {
          minWidth: 120,
          prop: 'specification',
          label: '规格',
          align: 'center',
          slot: 'specification'
        },
        {
          minWidth: 80,
          prop: 'packageCount',
          label: '包装数量',
          align: 'center',
          slot: 'packageCount',
          headerSlot: 'headerPackingCount'
        },
        {
          minWidth: 80,
          prop: 'packingUnit',
          label: '包装单位',
          align: 'center',
          slot: 'packingUnit'
        },
        {
          minWidth: 80,
          prop: 'measurementCount',
          label: '计量数量',
          align: 'center',
          slot: 'measurementCount',
          headerSlot: 'headerCount'
        },
        {
          minWidth: 80,
          prop: 'measuringUnit',
          label: '计量单位',
          align: 'center',
          slot: 'measuringUnit'
        },
        {
          minWidth: 80,
          prop: 'weight',
          label: '重量',
          align: 'center',
          slot: 'weight',
          headerSlot: 'headerWeight'
        },
        {
          minWidth: 80,
          prop: 'weightUtil',
          label: '重量单位',
          align: 'center',
          slot: 'weightUtil'
        },
        {
          minWidth: 80,
          prop: 'carveCode',
          label: '刻码',
          slot: 'carveCode',
          align: 'center',
          headerSlot: 'headerEngrave'
        },
        {
          minWidth: 140,
          prop: 'reason',
          label: '客户退货理由',
          align: 'center',
          slot: 'reason',
          headerSlot: 'headerReason'
        },
        {
          minWidth: 140,
          prop: 'feedbackDate',
          label: '客户反馈日期',
          align: 'center',
          slot: 'feedbackDate'
        },
        {
          minWidth: 140,
          prop: 'sendDate',
          label: '客户发出日期',
          slot: 'sendDate',
          align: 'center',
          headerSlot: 'headerSendDate'
        },
        {
          minWidth: 140,
          prop: 'ifPackageOk',
          label: '包装完好与否',
          align: 'center',
          slot: 'ifPackageOk',
          headerSlot: 'headerIfPackageOk'
        },
        {
          minWidth: 140,
          prop: 'unpackUserName',
          label: '拆包装责任人',
          align: 'center',
          slot: 'unpackUserName',
          headerSlot: 'headerUnpackUserName'
        },
        {
          minWidth: 250,
          prop: 'ifReasonMatch',
          label: '退货原因是否与退货信息一致',
          align: 'center',
          slot: 'ifReasonMatch',
          headerSlot: 'headerIfReasonMatch'
        },
        {
          minWidth: 140,
          prop: 'reason',
          label: '客户退货理由',
          align: 'center',
          slot: 'reason',
          headerSlot: 'headerReason'
        },
        {
          minWidth: 250,
          prop: 'unqualifiedReasonAnalysis',
          label: '不合格原因分析',
          align: 'center',
          slot: 'unqualifiedReasonAnalysis',
          headerSlot: 'headerUnqualifiedReasonAnalysis'
        },
        {
          minWidth: 250,
          prop: 'unqualifiedTypeName',
          label: '不合格类型',
          align: 'center',
          slot: 'unqualifiedTypeName',
          headerSlot: 'headerUnqualifiedType'
        },
        {
          minWidth: 250,
          prop: 'handleOpinion',
          label: '对不合格品的处理意见',
          align: 'center',
          slot: 'handleOpinion',
          headerSlot: 'headerHandleOpinion'
        },

        {
          minWidth: 130,
          prop: 'inOrOut',
          label: '内部或外部',
          align: 'center',
          slot: 'inOrOut',
          formatter: (_row, _column, cellValue) => {
            return _row.inOrOut == 1 ? '内部' : '外部';
          }
        },
        {
          minWidth: 250,
          prop: 'outerInfo',
          label: '外协单位',
          align: 'center',
          slot: 'outerInfo',
        },
        {
          minWidth: 250,
          prop: 'improveCorrect',
          label: '不合格原因是否改善到位',
          align: 'center',
          slot: 'improveCorrect',
          headerSlot: 'headerImproveCorrect'
        },
        {
          minWidth: 250,
          prop: 'improveOpinion',
          label: '对改善后产品处理意见',
          align: 'center',
          slot: 'improveOpinion',
          headerSlot: 'headerImproveOpinion'
        }
      ]
      this.activeComp = this.taskDefinitionKey
      return basicFields
    }
  },
  mounted() {
    this.init('', this.row)
  },
  methods: {

    init(type, row = {}) {
      this.dialogType = 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>
