<!-- 当新旧质保单不一致时, 需要用户选择处理方式, 在这个组件内处理通过confirm回调来通知父组件完成 -->
<template>
  <div>
    <el-row>
      <el-alert title="质保单处理结果与历史质保单不一致，请选择处理方式" type="warning" />
    </el-row>
    <el-row :gutter="20" style="margin-top: 20px;">
      <el-col :span="12">
        <el-table :data="oldResult" style="width: 100%">
          <el-table-column prop="attrKey" label="属性名称" />
          <el-table-column prop="attrValue" label="属性值" />
        </el-table>
      </el-col>
      <el-col :span="12">
        <el-table :data="newResult" style="width: 100%">
          <el-table-column prop="attrKey" label="属性名称" />
          <el-table-column prop="attrValue" label="属性值" />
        </el-table>
      </el-col>
    </el-row>
    <el-row style="margin-top: 20px;">
      <el-col>
        <el-radio-group v-model="uploadQualityCertificateForm.qualityCertificateType">
          <el-radio v-for="item in options" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-col>
    </el-row>
    <el-button type="primary" @click="handleConfirm">确认</el-button>
  </div>
</template>

<script>
import { addBomItem } from '@/api/wms/bomItem'
import { addBom } from '@/api/wms/bom'
import { updateRawMaterial, addRawMaterial, getRawMaterial } from '@/api/wms/rawMaterial'
import { updatePurchasePlanDetail } from '@/api/wms/purchasePlanDetail'

export default {
  name: 'Merger',
  props: {
    oldResult: {
      type: Array,
      required: true
    },
    newResult: {
      type: Array,
      required: true
    },
    info: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      options: [
        {
          label: '使用旧的质保单',
          value: 1
        },
        {
          label: '使用新的质保单',
          value: 2
        },
        {
          label: '创建新物料',
          value: 3
        }
      ],
      uploadQualityCertificateForm: {
        qualityCertificateType: 1
      }
    }
  },
  methods: {
    async handleConfirm() {
      if (!this.uploadQualityCertificateForm.qualityCertificateType) {
        this.$modal.msgError('请选择处理方式');
        return;
      }
      this.$emit('confirm', {
        status: 'start',
      })
      try {
        if (this.uploadQualityCertificateForm.qualityCertificateType === 1) {
          // 什么都不需要做,直接完成就可以
          console.log('使用旧的质保单');
        } 
        else if (this.uploadQualityCertificateForm.qualityCertificateType === 2) {
          // 创建一个新的BOM
          const bom = await addBom({
            bomName: 'N' + new Date().getTime(),
            bomCode: 'N' + new Date().getTime(),
          })
          // 将BOMID赋值给对应的materialId
          await updateRawMaterial({
            rawMaterialId: this.info.rawMaterialId,
            bomId: bom.data.bomId,
          })
          // 逐项创建BOMItem
          for (let i = 0; i < this.newResult.length; i++) {
            await addBomItem({
              bomId: bom.data.bomId,
              attrKey: this.newResult[i].attrKey,
              attrValue: this.newResult[i].attrValue,
            })
          }
          this.$store.dispatch('category/getBomMap');
          this.$store.dispatch('category/getRawMaterialMap');
        } else if (this.uploadQualityCertificateForm.qualityCertificateType === 3) {
          console.log('创建新物料');
          // 创建一个新的BOM
          const bom = await addBom({
            bomName: 'N' + new Date().getTime(),
            bomCode: 'N' + new Date().getTime(),
          })
          // 逐项创建BOMItem
          for (let i = 0; i < this.newResult.length; i++) {
            await addBomItem({
              bomId: bom.data.bomId,
              attrKey: this.newResult[i].attrKey,
              attrValue: this.newResult[i].attrValue,
            })
          }
          // 创建一个新的物料,使用原有物料的信息, code使用时间戳
          const { rawMaterialId, rawMaterialCode, ...rawMaterial } = (await getRawMaterial(this.info.rawMaterialId)).data
          const newMaterial = await addRawMaterial({
            ...rawMaterial,
            bomId: bom.data.bomId,
            rawMaterialCode: 'N' + new Date().getTime(),
          })
          // 修改采购单据赋值为新的物料
          await updatePurchasePlanDetail({
            detailId: this.info.detailId,
            rawMaterialId: newMaterial.data.rawMaterialId,
          })
          this.$store.dispatch('category/getBomMap');
          this.$store.dispatch('category/getRawMaterialMap');
        }

        this.$emit('confirm', {
          status: 'success',
        })
      } catch {
        this.$emit('confirm', {
          status: 'error',
        })
      }
    }
  }
}
</script>