<!--
@name: 收货入库-数据采集
@author: 李恒
@time: 2022-05-31 13:45:10
-->
<template>
  <a-modal :visible='showModalTag' :width='1024' okText='确认' title='数据采集'
           @cancel="$emit('update:showModalTag', false)" @ok='modalSubmit'>
    <p-form-model ref='formTargetModal' :label-col='{ span: 8 }' :model='form' :wrapper-col='{ span: 16 }'
                  class='plg-grid-from'>
      <a-row>
        <a-col :span="16">
          <p-form-model-item label='单号' :label-col='{ span: 4 }' :wrapper-col='{ span: 20 }'>
            <p-input v-model='selectRecord.asnHeaderId' disabled/>
          </p-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span='8'>
          <p-form-model-item label='商品编号'>
            <p-input v-model='selectRecord.itemNum' disabled/>
          </p-form-model-item>
        </a-col>
        <a-col :span='8'>
          <p-form-model-item label='商品名称'>
            <p-input v-model='selectRecord.itemName' disabled/>
          </p-form-model-item>
        </a-col>
        <a-col :span='8'>
          <p-form-model-item label='包装数量'>
            <p-input v-model='selectRecord.conversionRate' disabled/>
          </p-form-model-item>
        </a-col>
        <a-col :span='8'>
          <p-form-model-item label='批号'>
            <p-input v-model='selectRecord.manufacturerLotNum' disabled/>
          </p-form-model-item>
        </a-col>
        <a-col :span='8'>
          <p-form-model-item label='生产日期'>
            <p-input v-model='selectRecord.batchProductionDate' disabled/>
          </p-form-model-item>
        </a-col>
        <a-col :span='8'>
          <p-form-model-item label='有效期至'>
            <p-input v-model='selectRecord.batchExpiredDate' disabled/>
          </p-form-model-item>
        </a-col>
        <a-col :span='8'>
          <p-form-model-item label='未收数量'>
            <p-input v-model='selectRecord.pieceQty' disabled/>
          </p-form-model-item>
        </a-col>
        <a-col :span='8'>
          <p-form-model-item label='扫描件数'>
            <p-input :value='scanCaseQty' disabled/>
          </p-form-model-item>
        </a-col>
        <a-col :span='8'>
          <p-form-model-item label='扫描数量'>
            <p-input :value='scanQty' disabled/>
          </p-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span='8'>
          <p-form-model-item label='扫描方向'>
            <p-select v-model='scanFlag' :options="scanFlagOptions"/>
          </p-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span='8'>
          <p-form-model-item label='容器号'>
            <p-input v-model='containerCode' placeholder="请扫描容器号"/>
          </p-form-model-item>
        </a-col>
        <!-- 销退时录入监管码 -->
        <a-col :span='8' v-if="businessType === 'salesReturn'">
          <p-form-model-item label='监管码'>
            <p-input v-model='supervisionCode' placeholder="请扫描监管码"/>
          </p-form-model-item>
        </a-col>
        <!-- 包材录入数量 -->
        <a-col :span='8' v-if="businessType === 'packaging' && scanFlag === 'IN'">
          <p-form-model-item label='件数'>
            <p-input-number v-model='inputScanCaseQty' style="width: 100%" :min="0" :precision="0"
                            placeholder="请输入存放包材件数"/>
          </p-form-model-item>
        </a-col>
        <a-col :span='8'>
          <p-form-model-item :wrapper-col='{ span: 20,offset:4 }'>
            <p-button type='primary' @click="saveCollection">确认</p-button>
          </p-form-model-item>
        </a-col>
      </a-row>
    </p-form-model>
    <a-row :gutter="20">
      <a-col :span='12'>
        <p-table :data-source.sync='containerDataSource'
                 bordered
                 :hasRowCount="false"
                 :pagination="false"
                 :rowSelectType="false"
                 @clickRow="clickRowContainer"
                 row-key='id'>
          <p-table-column data-index='containerCode' title='容器'/>
          <p-table-column data-index='caseQty' title='件数'/>
          <p-table-column data-index='receiveQty' title='数量'/>
        </p-table>
      </a-col>
      <a-col :span='12'>
        <p-table v-if="businessType !== 'packaging'"
                 :data-source.sync='regulatoryDataSource'
                 bordered
                 :hasRowCount="false"
                 :pagination="false"
                 :rowSelectType="false"
                 row-key='id'>
          <p-table-column data-index='supervisionCode' title='监管码'/>
          <p-table-column data-index='pieceQty' title='数量'/>
        </p-table>
      </a-col>
    </a-row>
  </a-modal>
</template>

<script>
import {saveCollection, getCollection, getSupervisionCodes} from './api'

export default {
  props: {
    showModalTag: {
      type: Boolean,
      default: false
    },
    selectRecord: {
      type: Object,
      default: () => ({})
    },
    selectBill: {
      type: Object,
      default: () => ({})
    }
  },
  computed: {
    // 扫描件数
    scanCaseQty() {
      return this.containerDataSource.reduce((pre, cur) => pre + (cur.caseQty * 1 || 0), 0)
    },
    // 扫描数量
    scanQty() {
      return this.containerDataSource.reduce((pre, cur) => pre + (cur.receiveQty * 1 || 0), 0)
    },
    // 是否监管码
    isSupervise() {

    },
    // 业务类型
    businessType() {
      const {rcvType,isSupervise} = this.selectBill
      if(isSupervise === 0 || ['19', '20'].includes(rcvType)){
        return 'packaging'
      }
      // 根据收货类型、是否监管码来控制业务类型
      if (['16', '17'].includes(rcvType)) {
        // 生产入库
        return 'produce'
      }
      if (['18'].includes(rcvType)) {
        // 销退入库
        return 'salesReturn'
      }
    }
  },
  data() {
    return {
      form: {},
      // 扫描方向
      scanFlag: 'IN',
      // 容器号
      containerCode: '',
      // 监管码
      supervisionCode: '',
      // 存入数量
      inputScanCaseQty: 0,
      // 容器数据
      containerDataSource: [],
      // 监管码数据
      regulatoryDataSource: [],
      // 字典
      scanFlagOptions: [
        {label: '扫入', value: 'IN'},
        {label: '扫出', value: 'OUT'},
      ]
    }
  },
  watch: {
    showModalTag: {
      handler(nVal) {
        if (nVal) {
          this.containerDataSource = []
          this.regulatoryDataSource = []
          this.getCollection()
        }
      },
      immediate: true
    }
  },
  methods: {
    onReset() {
      this.$refs.formTargetModal.resetFields()
      this.form = {}
    },
    onSubmit() {
      this.listParams = {...this.form}
    },
    getCollection() {
      const {asnLineId} = this.selectRecord
      if (!asnLineId) return
      const params = {
        asnLineId
      }
      getCollection(params).then(res => {
        if (res.success) {
          this.containerDataSource = res.data
        } else {
          this.$message.error(res.message)
        }
      })
    },
    saveCollection() {
      const businessType = this.businessType
      const scanFlag = this.scanFlag
      const containerCode = this.containerCode
      const {rcvType: billType} = this.selectBill
      const {mtlOwnerId, mtlOwnerCode, batchExpiredDate, batchProductionDate} = this.selectRecord
      if (!billType) return this.$message.error('获取汇总单据订单类型失败')
      let supervisionCode = this.supervisionCode
      let scanCaseQty = this.scanCaseQty
      // 销退录入监管码
      if (businessType === 'salesReturn') {
        if (!supervisionCode) return this.$message.warn('请扫描监管码')
        scanCaseQty = 1
      }
      // 包材入库录入数量
      if (businessType === 'packaging' && scanFlag === 'IN') {
        scanCaseQty = this.inputScanCaseQty
        if (!scanCaseQty) return this.$message.warn('请输入扫描件数')
      }
      const params = {
        ...this.selectRecord,
        expiredDate: batchExpiredDate,
        productionDate: batchProductionDate,
        cargoOwnerId: mtlOwnerId,
        cargoOwnerCode: mtlOwnerCode,
        supervisionCode,

        scanCaseQty,
        scanQty: this.scanQty,


        billType,
        scanFlag,
        containerCode
      }
      saveCollection(params).then(res => {
        if (res.success) {
          // 保存完成,更新数据
          this.getCollection()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 选择容器号查监管码
    clickRowContainer(form, rowKey, record) {
      if (this.businessType === 'packaging') return
      const {containerCode} = record
      getSupervisionCodes({containerCode}).then(res => {
        if (res.success) {
          this.regulatoryDataSource = res.data
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 弹出框提交
    modalSubmit() {
      const containerDataSource = this.containerDataSource
      // 发送数据
      this.$emit('change', {containerDataSource})
      this.$emit('update:showModalTag', false)
    }
  }
}
</script>