<template>
  <!-- 新增 预装箱单-成衣-->
  <div id="addAdvancePacking">
    <a-drawer
      :title="(editDecide == 'add')?'新增预装箱单-成衣':'编辑预装箱单-成衣'"
      width="89%"
      placement="right"
      :closable="true"
      :visible="visible"
      @close="handleCancel"
    >
      <!-- 主表信息 填写 -->
      <a-card :bordered="true">
        <div class="table-page-search-wrapper">
          <!-- :rules="validatorRules" -->
          <a-form-model layout="inline" ref="form" :model="addAdpacking">
            <a-row :gutter="24">
              <a-col :md="6" :sm="8">
                <a-form-model-item label="单据号" prop="documentNo">
                  {{addAdpacking.documentNo | ellipsis}}
                  <!-- <a-input placeholder="自动生成" v-model="addAdpacking.orderNumber" readOnly></a-input> -->
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="8">
                <a-form-model-item label="订单号" prop="orderNumber">
                  {{addAdpacking.orderNumber | ellipsis}}
                  <!-- <a-input placeholder="自动生成" v-model="addAdpacking.orderNumber" readOnly></a-input> -->
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="8">
                <a-form-model-item label="集装箱代号" prop="containerCode">
                  {{addAdpacking.containerCode | ellipsis}}
                  <!-- <a-input placeholder="请输入集装箱代号" v-model="addAdpacking.containerCode" readOnly></a-input> -->
                </a-form-model-item>
              </a-col>

              <a-col :md="6" :sm="8">
                <a-form-model-item label="集装箱号" prop="containerNumber">
                  {{addAdpacking.containerNumber | ellipsis}}
                  <!-- <a-input placeholder="请输入集装箱号" v-model="addAdpacking.containerNumber" readOnly></a-input> -->
                </a-form-model-item>
              </a-col>

              <!-- <a-col :md="6" :sm="8">
                <a-form-model-item label="预发货日期" prop="u8Remarks">
                  <j-date placeholder="请选择预发货日期" v-model="addAdpacking.preDeliveryDate" style="width:100%"></j-date>
                </a-form-model-item>
              </a-col> -->
              <a-col :md="6" :sm="8">
                <a-form-model-item label="u8备注" prop="u8Remarks">
                  <a-input placeholder="请输入u8备注" v-model="addAdpacking.u8Remarks"></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="8">
                <a-form-model-item label="款号" prop="itemNumber">
                  {{addAdpacking.itemNumber | ellipsis}}
                  <!-- <a-input placeholder="选择子表自动生成" v-model="addAdpacking.itemNumber"></a-input> -->
                </a-form-model-item>
              </a-col>

              <a-col :md="6" :sm="8">
                <a-form-model-item label="中文品名" prop="productName">
                  {{addAdpacking.productName | ellipsis}}
                  <!-- <a-input placeholder="选择子表自动生成" v-model="addAdpacking.productName"></a-input> -->
                </a-form-model-item>
              </a-col>

              <a-col :md="6" :sm="8">
                <a-form-model-item label="英文品名" prop="productName">
                  {{addAdpacking.englishProductName | ellipsis}}
                  <!-- <a-input placeholder="选择子表自动生成" v-model="addAdpacking.englishProductName"></a-input> -->
                </a-form-model-item>
              </a-col>

              <a-col :md="6" :sm="8">
                <a-form-model-item label="尺码范围" prop="sizeRange">
                  {{addAdpacking.sizeRange | ellipsis}}
                  <!-- <a-input placeholder="选择子表自动生成" v-model="addAdpacking.sizeRange" readOnly></a-input> -->
                </a-form-model-item>
              </a-col>

              <a-col :md="6" :sm="8">
                <a-form-model-item label="客户" prop="customer">
                  {{addAdpacking.customer  | ellipsis }}
                  <!-- <a-input placeholder="选择子表自动生成" v-model="addAdpacking.customer"></a-input> -->
                </a-form-model-item>
              </a-col>

              

              <!-- <a-col :md="6" :sm="8">
                <a-form-model-item label="成衣工厂" prop="garmentFactory">
                  <a-input placeholder="选择子表自动生成" v-model="addAdpacking.garmentFactory"></a-input>
                </a-form-model-item>
              </a-col> -->
              <a-col :md="6" :sm="8">
                <a-form-model-item label="总箱数" prop="totalBoxes">
                  {{addAdpacking.totalBoxes | ellipsis }}
                  <!-- <a-input placeholder="自动生成" v-model="addAdpacking.totalBoxes" readOnly></a-input> -->
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="8">
                <a-form-model-item label="总净重" prop="totalNetWeight">
                  {{addAdpacking.totalNetWeight | ellipsis}}
                  <!-- <a-input placeholder="自动生成" v-model="addAdpacking.totalNetWeight" readOnly></a-input> -->
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="8">
                <a-form-model-item label="总毛重" prop="totalGrossWeight">
                  {{addAdpacking.totalGrossWeight  | ellipsis}}
                  <!-- <a-input placeholder="自动生成" v-model="addAdpacking.totalGrossWeight" readOnly></a-input> -->
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="8">
                <a-form-model-item label="总体积" prop="totalVolume">
                  {{addAdpacking.totalVolume | ellipsis}}
                  <!-- <a-input placeholder="自动生成" v-model="addAdpacking.totalVolume" readOnly></a-input> -->
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="8">
                <a-form-model-item label="总价" prop="totalPrice">
                  {{addAdpacking.totalPrice  | ellipsis}}
                  <!-- <a-input placeholder="自动生成" v-model="addAdpacking.totalPrice" readOnly></a-input> -->
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="8">
                <a-form-model-item label="总件数" prop="totalNumber">
                  {{addAdpacking.total  | ellipsis}}
                  <!-- <a-input placeholder="自动生成" v-model="addAdpacking.total" readOnly></a-input> -->
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </div>
      </a-card>

      <!-- 参照发运明细 増行-->
      <a-card :bordered="false" style="margin:10px 0 60px 0;">

        <!-- 子表 ipagination :rules="validatorRules"-->
        <a-spin :spinning="confirmLoading">
          <a-form-model ref="formRef">
            <j-vxe-table
              ref="vTable"
              toolbar
              row-number
              row-selection
              drag-sort
              keep-source
              :height="350"
              :loading="loading"
              :dataSource="addAdpacking.syPreAssembledPackingListItemList"
              :columns="columns"
              :alwaysEdit=true
              :bordered=true
              :scroll="{ x: 1500 }"
              style="margin-top: 5px;"
              :toolbarConfig="toolbarConfig"
              @valueChange="handleValueChange"
            >
            <template v-slot:action="props">
              <a @click="copy(props)">复制</a>
              <a-divider type="vertical"/>
              <a-popconfirm title="确定删除吗？" @confirm="handleDelete(props)">
                <a>删除</a>
              </a-popconfirm>
            </template>
            <template v-slot:toolbarSuffix>
              <a-button type="primary" @click="referShipmentDetails" icon="ordered-list">参照出运明细</a-button>
            </template>
            </j-vxe-table>
          </a-form-model>
        </a-spin>
      </a-card>

      <!-- 页面底部提交取消 -->
      <div
        :style="{
          position: 'absolute',
          right: 0,
          bottom: 0,
          width: '100%',
          borderTop: '1px solid #e9e9e9',
          padding: '10px 16px',
          background: '#fff',
          textAlign: 'right',
          zIndex: 999
        }"
      >
        <a-popconfirm title="确定放弃编辑？" @confirm="handleCancel" okText="确定" cancelText="取消">
          <a-button :style="{ marginRight: '8px' }">取消</a-button>
        </a-popconfirm>
        <a-button type="primary" @click="myHandSave" :loading="btnLoading" v-preventReClick="2000">
          保存
        </a-button>
        <a-button type="primary" @click="myHandSubmit" style="margin-left: 8px">
          保存并提交
        </a-button>
      </div>
    </a-drawer>

    <!-- 参照发运明细弹框 -->
    <referShipmentDetails-modal
      ref="referShipmentDetailsModal"
      @bao="getSon"
      @ok="modalFormOk"
    ></referShipmentDetails-modal>

  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JEllipsis from '@/components/jeecg/JEllipsis'
import moment from 'moment'

import ReferShipmentDetailsModal from '@views/advance-packingList/referShipmentDetailsModal.vue'

import { addAdvPac, editById } from '@api/document/advance-packingList.js'
import { queryShippingDetails } from '@api/document/advance-packingList.js'
import { JVXETypes } from '@/components/jeecg/JVxeTable'
import { pushIfNotExist, randomNumber, randomUUID } from '@/utils/util'
import { getDotConfig, getDotValidExpress } from '@/utils/myutil'

export default {
  name: 'AddAdvancePacking', // 新增预装箱单
  mixins: [JeecgListMixin],
  components: { JEllipsis, moment, ReferShipmentDetailsModal }, // 参照发运明细弹框
  data() {
    let ellipsis = (v, l = 20) => <j-ellipsis value={v} length={l} /> // 省略

    return {
      toolbarConfig: {
          // add 新增按钮；remove 删除按钮；clearSelection 清空选择按钮
          btn: []
        },
      btnLoading:false,//按钮等待状态
      // 表头
      columns: [
        {
          title: '账套',
          key: 'acSetNo',
          width: 120,
          // fixed: 'left',
          type: JVXETypes.normal,
        },
        {
          title: '成衣工厂',
          key: 'garmentFactory',
          width: 120,
          // fixed: 'left',
          type: JVXETypes.normal,
        },
        {
          title: 'HOD',
          key: 'hod',
          width: 120,
          // fixed: 'left',
          type: JVXETypes.normal,
        },
        {
          title: 'STYLE NO.',
          key: 'styleNo',
          width: 120,
          type: JVXETypes.normal,
        },
        {
          title: 'PO NO.',
          key: 'poNo',
          width: 120,
          type: JVXETypes.normal,
        },

        {
          title: 'ITEM NO./SKU NO./UPC NO./PACKS CODE',
          key: 'itemCode',
          width: 340,
          type: JVXETypes.input,
        },
        {
          title: '分销点/DC/LABEL',
          key: 'distributionPoint',
          width: 180,
          type: JVXETypes.normal,
        },

        {
          title: 'PREPACK SKU',
          key: 'prepackSku',
          width: 140,
          type: JVXETypes.input,
          insertAfter:true,

        },

        {
          title: '起始箱号',
          key: 'startingBoxNumber',
          width: 120,
          type: JVXETypes.inputNumber ,
        },

        {
          title: '结束箱号',
          key: 'endCaseNumber',
          width: 140,
          type: JVXETypes.inputNumber ,
        },

        {
          title: '颜色(中英文)',
          key: 'colour',
          width: 140,
          type: JVXETypes.input ,
        },
        {
          title: '配码--根据U8订单来显示',
          key: 'withCode',
          width: 240,
          type: JVXETypes.normal ,
        },
        {
          title: '计划尺码数量',
          key: 'planSize',
          width: 240,
          type: JVXETypes.normal ,
        },
        {
          title: '计划装箱数量',
          key: 'planQuantity',
          width: 240,
          type: JVXETypes.normal ,
        },
        {
          title: '件数/箱',
          key: 'piecesBox',
          width: 90,
          type: JVXETypes.normal ,
        },
        {
          title: '箱数',
          key: 'boxNumber',
          width: 140,
          type: JVXETypes.normal ,
        },

        {
          title: '总件数',
          key: 'total',          
          width: 140,
          type: JVXETypes.normal,
        },
        {
          title: '净重/箱',
          key: 'netWeight',
          width: 90,
          type: JVXETypes.inputNumber,
        },
        {
          title: '总净重',
          key: 'totalNetWeight',
          width: 140,
          type: JVXETypes.normal,
        },

        {
          title: '毛重/箱',
          key: 'grossWeight',
          width: 120,
          type: JVXETypes.inputNumber,
        },
        {
          title: '总毛重',
          key: 'totalGrossWeight',
          width: 140,
          type: JVXETypes.normal,
        },
        {
          title: '外箱长度',
          key: 'outerBoxLength',
          width: 120,
          type: JVXETypes.inputNumber
        },

        {
          title: '外箱宽度',
          key: 'outerBoxWidth',
          width: 140,
          type: JVXETypes.inputNumber,
        },
        {
          title: '外箱高度',
          key: 'outerBoxHeight',
          type: JVXETypes.inputNumber ,
          width: 140,
        },

        {
          title: '总体积',
          key: 'totalVolume',
          type: JVXETypes.normal ,
          width: 140,
          
        },
        {
          title: '净净重',
          key: 'netWeightToo',
          width: 120,
          type: JVXETypes.inputNumber,
        },
        {
          title: '单价',
          key: 'unitPrice',
          width: 120,
          type: JVXETypes.normal,
        },
        {
          title: '总价',
          key: 'totalPrice',          
          width: 120,
          type: JVXETypes.normal,
        //    validateRules: [{ required: true, message: '${title}不能为空' },
        //     {pattern: /^\d+(\.\d{1,2})?$/, message: '小数点位数不能超过2位' }],
        },
        {
          title: '备注',
          key: 'memo',          
          width: 120,
          type: JVXETypes.normal,
        //    validateRules: [{ required: true, message: '${title}不能为空' },
        //     {pattern: /^\d+(\.\d{1,2})?$/, message: '小数点位数不能超过2位' }],
        },
        {
          title: '操作',
          key: 'action',
          type: JVXETypes.slot,
          fixed: 'right',
          width: 160,
          align: 'center',
            slotName: 'action',
        }
      ],
      row:{},
      loading: false, // 表格加载
      visible: false,
      editDecide: 'add',
      dateFormat: 'YYYY-MM-DD',
      addAdpacking: {
        syPreAssembledPackingListItemList:[]
      }, //主表信息
      confirmLoading: false,
      msgFormSon: 'test', // 子组件传来数据的变量
      // state: '0', // 单据状态初始未【仅保存】
      // pushState: '0' // 推送状态初始未【仅保存】

      // 待确定还有哪些必填信息 ----------------------------
      // validatorRules: {
      //   orderNumber: [{ required: true, message: '订单号不能为空', trigger: 'blur' }],
      //   styleNum: [{ required: true, message: '款号不能为空', trigger: 'blur' }],
      //   name: [{ required: true, message: '品名不能为空', trigger: 'blur' }]
      // },
      sizeFields:[], // 尺码字段
      dotConfig:{}, // 小数点配置
      judge : 0 ,
    }
  },
  // 接收父组件查询方法
  props: {
    fatherList: {
      type: Function,
      default: null
    }
  },

  methods: {
    getSon(val) {
      // console.log('勾选发运明细数据', val)
      // for (var i in val) {
      //   val[i]['orderQuantity'] = val[i].quantity
      //   val[i]['orderRemainingQuantity'] = val[i].surplusNum
      //   val[i]['salesUnitPrice'] = val[i].unitPriceIncludingTax
      //   val[i]['customerOrder'] = val[i].customerOrderNumber
      //   val[i]['shipmentQuantity'] = val[i].surplusNum //订单剩余数量 赋值给发货数量
      // }
      this.addAdpacking.syPreAssembledPackingListItemList = [];
      this.setHeadCalField();
      var selectData = val[0];      
      queryShippingDetails({id:selectData.id,groupId:selectData.groupId}).then(res => {
          if (res.success) {
            // 放在前面，有小数位数设置
            this.setHeadData(val[0], res.result);
            this.loadSizeTables(res.result.sizeTables);          
            var changedData = this.changeData(val[0], res.result);
            this.addAdpacking.syPreAssembledPackingListItemList.push(changedData);
          }else{
            this.$message.error("获取发运明细详情失败");
          }
        })
    },

    loadSizeTables(sizeTable){
      var newColums = this.columns.filter(item=>{
        return item.isSize != true;
      });            

      this.sizeFields = [];
      if (sizeTable != null){
        var i=0;
        // 找到插入位置点
        for (; i<newColums.length;i++){
          if (newColums[i].insertAfter){
            break;
          }
        }
        // 插入数据
        for (var j=0;j<sizeTable.length;j++){
          this.sizeFields.push('size-'+sizeTable[j].size);
          var validExpress = getDotValidExpress(this.dotConfig.quantity);
          var field = {
              title: sizeTable[j].size,
              key: 'size-'+sizeTable[j].size,
              type: JVXETypes.inputNumber ,
              width: 80,
              isSize:true,
              validateRules: [validExpress]
          };
          
          newColums.splice(i+1+j,0,field);
        }
      }
      
      this.columns = newColums;  
    },
    // 参照发运明细
    referShipmentDetails() {
      // console.log('打开参照发运明细')
      this.$refs.referShipmentDetailsModal.referShipDetModVis = true
    },
    myHandSave(){
      this.addAdpacking.status=0;
      this.saveAdd(); 

    },
   async myHandSubmit(){
      this.addAdpacking.status=1;
      this.saveAdd(); 
    },
    //  保存
   async saveAdd() {
      var newObj = this.addAdpacking;
      if (this.sizeFields.length == 0){
        this.$message.error('当前记录没有尺码字段，无法保存');
        return;
      }
      // if (newObj.totalBoxes == 0){
      //   this.$message.error('总箱数为0，无法保存');
      //   return;
      // }
      await this.JudgeVluabled()
      //尺码赋值
      for(var i =0;i<newObj.syPreAssembledPackingListItemList.length;i++){
        var dataTable = newObj.syPreAssembledPackingListItemList[i]
        for (var j=0; j<dataTable.sizeTables.length; j++){
          var sizeTable = dataTable.sizeTables[j];
          var field = 'size-'+sizeTable.size;
         dataTable.sizeTables[j].proportion =  dataTable[field];
        }
      }
      // 日期格式转换
      if (newObj.preDeliveryDate != "" && newObj.preDeliveryDate != undefined){
        newObj.preDeliveryDate = newObj.preDeliveryDate+" 00:00:00";
      }
      // 推送状态、状态赋默认值。新增状态赋值
      if (newObj.id == undefined || newObj.id == ""){
          newObj.push_status = 0;
      }
      if(this.judge == 0){
        if(this.editDecide === 'edit'){
          editById(newObj).then(res => {
          this.btnLoading = true
          if (res.success) {
            if(newObj.status == '1'){
              this.$message.success('编辑并提交成功')
            }else {
              this.$message.success('编辑成功')
            }
             this.close()
             this.btnLoading = false
              //清空信息
             newObj = {syPreAssembledPackingListItemList:[]};
             this.row = res.result

             this.fatherList() // 调用父组件 查询方法
            // this.msgFormSon = {}  清空子表信息
           }else{
             this.$message.error(res.message)
              this.btnLoading = false
          }
         })
       }else {
         addAdvPac(newObj).then(res => {
          this.btnLoading = true
          if (res.success) {
             if(newObj.status == '1'){
              this.$message.success('新增并提交成功')
             }else {
              this.$message.success('新增成功')
             }
             this.close()
             this.btnLoading = false
             //清空信息
             newObj = {syPreAssembledPackingListItemList:[]};
             this.row = res.result
             this.fatherList() // 调用父组件 查询方法
          // this.msgFormSon = {}  清空子表信息
         }else{
          this.$message.error(res.message)
           this.btnLoading = false
        }
      })
      }
      }
      
      
      

      // const that = this
      // // 触发表单验证
      // this.$refs.form.validate(valid => {
      //   if (valid) {
      //     that.confirmLoading = true
      //   }
      // })
    },
   JudgeVluabled(){
          this.judge = 0
           for (var i=0; i<this.addAdpacking.syPreAssembledPackingListItemList.length;i++){
            var tableRow = this.addAdpacking.syPreAssembledPackingListItemList[i];
             tableRow = JSON.parse(JSON.stringify(tableRow))
            var allSizeSum = this.getAllSizeSum(tableRow);
            if (allSizeSum == 0){
              this.$message.error('第'+(i+1)+'行所有尺码数量为0，无法保存');
              this.judge += 1
              return;
            }
            if(tableRow.startingBoxNumber > tableRow.endCaseNumber) {
              this.$message.error('第'+(i+1)+'行起始箱号大于结束箱号，无法保存');
              this.judge += 1
              return;
            }
            //是否未填
            var required = [
            {key:'startingBoxNumber',value:'起始箱号'},
            {key:'endCaseNumber',value:'结束箱号'},
            {key:'netWeight',value:'净重/箱'},
            {key:'grossWeight',value:'毛重/箱'},
            {key:'outerBoxLength',value:'外箱长度'},
            {key:'outerBoxHeight',value:'外箱高度'},
            {key:'outerBoxWidth',value:'外箱宽度'},
            // {key:'netWeightToo',value:'净净重'},
            ]
             for(var j=0 ; j<required.length;j++){
                if(tableRow[required[j].key] == null || tableRow[required[j].key] == "" || tableRow[required[j].key] == undefined){
                  this.$message.error('第'+(i+1)+'行'+required[j].value+'无值，无法保存');
                  this.judge+=1
                  return
                }
             }
             

             //是否为整
             var integer = [
               {key:'startingBoxNumber',value:'起始箱号'},
               {key:'endCaseNumber',value:'结束箱号'},
               {key:'piecesBox',value:'件数/箱'},
             ]
             for(var v=0 ; v<integer.length;v++){
                if(!Number.isInteger(Number(tableRow[integer[v].key]))){
                  this.$message.error('第'+(i+1)+'行'+integer[v].value+'不为整数，无法保存');
                  this.judge+=1
                  return
                }
             }

            //判断小数位数
            var Several = [
               {key:'netWeight',value:'净重/箱'},
               {key:'grossWeight',value:'毛重/箱'},
               {key:'netWeightToo',value:'净净重'}
             ]
            for(var s=0 ; s<Several.length;s++){
                var position =  tableRow[Several[s].key].toString().indexOf('.')  + 1;   //小数点的位置
                var digit = tableRow[Several[s].key].toString().length - position;  //小数的位值
                if(digit > 2 && position!==0 ){
                 this.$message.error('第'+(i+1)+'行'+Several[s].value+'小数超过两位，无法保存');
                this.judge+=1
                 return 
              }
             }   
           }
      
    },
    // 设置表头信息
   setHeadData(referData, mData){
      var itemData = {};
      if (mData.syPreAssembledPackingListItemList != null && mData.syPreAssembledPackingListItemList.size>0){
        itemData = mData.syPreAssembledPackingListItemList[0];
      }
      // 设置件数小数位数
      this.dotConfig = getDotConfig(itemData.masterMetering);
      this.addAdpacking.syDeclarationElementsId = mData.syDeclarationElementsId;    
      this.addAdpacking.elementsId = mData.elementsId
      this.addAdpacking.itemNumber = referData.itemNumber;
      // this.addAdpacking.productName = referData.chineseName;
      this.addAdpacking.productName = mData.productName;
      this.addAdpacking.englishProductName = mData.englishProductName;
      this.addAdpacking.u8Remarks = mData.u8Remarks;
      this.addAdpacking.sizeRange = "";
      if (mData.sizeTables != null){
        if (mData.sizeTables.length == 1){
            this.addAdpacking.sizeRange = mData.sizeTables[0].size;
        }else if (mData.sizeTables.length>1){
            this.addAdpacking.sizeRange = mData.sizeTables[0].size + " ~ " + mData.sizeTables[mData.sizeTables.length-1].size;
        }
      }
      this.addAdpacking.customer = mData.customer;
      this.addAdpacking.ompoId = mData.ompoId
      this.addAdpacking.salesman = mData.salesman
      this.addAdpacking.customerAbbreviation = mData.customerAbbreviation
      this.addAdpacking.spurOrSubOrder = mData.spurOrSubOrder
      this.addAdpacking.garmentFactory = mData.garmentFactory;
      this.addAdpacking.orderNumber = mData.orderNumber
    },
    // 发运明细详情成预装箱记录:mData-主表记录，itemData-子表记录
    changeData(referData, mData) {      
      var itemData = {};
      if (mData.syPreAssembledPackingListItemList != null && mData.syPreAssembledPackingListItemList.length>0){
        itemData = mData.syPreAssembledPackingListItemList[0];
      }
      var addrow = {
        syDeclarationElementsId:itemData.syDeclarationElementsId,
        syDeclarationElementsItemId:itemData.syDeclarationElementsItemId,
        acSetNo: itemData.acSetNo,
        garmentFactory: itemData.garmentFactory,
        hod: itemData.hod,
        styleNo:itemData.styleNo,
        poNo:itemData.poNo,
        itemCode:'',
        distributionPoint:referData.distributionPoint,
        prepackSku: '',
        size:"",
        startingBoxNumber: '',
        endCaseNumber: '',
        colour: itemData.colour,
        withCode: itemData.withCode,
        piecesBox: 0,
        boxNumber: '',
        total: '',
        netWeight: '',
        totalNetWeight: '',
        grossWeight: '',
        totalGrossWeight: '',
        outerBoxLength: '',
        outerBoxWidth: '',
        outerBoxHeight: '',
        totalVolume: '',
        netWeightToo: '',
        unitPrice: itemData.unitPrice,
        totalPrice: '',
        smallPo: referData.smallPo,
        inventoryName:referData.inventoryName,
        spurOrSubOrder:referData.purOrSubOrder,
        factoryUnitPrice:referData.factoryUnitPrice,
        customerAbbreviation:referData.customerAbbreviation,
        sizeTables : itemData.sizeTables,
        planSize : itemData.planSize,
        planQuantity:itemData.planQuantity,
        supplierCode:itemData.supplierCode,
        supplier:itemData.supplier,
        ompoAccount:itemData.ompoAccount,
        ompoId:itemData.ompoId,
        ompoIdItem:itemData.ompoIdItem,
        ymoney:itemData.ymoney
      }
      return addrow;
    },

    //  操作按钮 删除
    handleDelete(index) {
       var ret = this.addAdpacking.syPreAssembledPackingListItemList.splice(index.rowIndex, 1);
      this.setHeadCalField();
      return ret;
    },
    // 复制
    copy(record) {
      var newRow = JSON.parse(JSON.stringify(record.row));
      newRow.isAdd = 1
      this.addAdpacking.syPreAssembledPackingListItemList.push(newRow);
      this.setHeadCalField();
    },

    // 抽屉 取消
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.visible = false;
      this.btnLoading = false
      this.editDecide = 'add'
      this.addAdpacking = {syPreAssembledPackingListItemList:[]};
    },
    aa() {},
    modalFormOk() {
      this.$refs.referShipmentDetailsModal.referShipmentDetailsModVis = true
    },
    // 表格字段值变动时触发
    handleValueChange(event) {
      if (event.row.startingBoxNumber != undefined && event.row.startingBoxNumber != "" &&
      event.row.endCaseNumber != undefined && event.row.endCaseNumber != ""){
        event.row.boxNumber = event.row.endCaseNumber*1-event.row.startingBoxNumber*1+1;
      }
      var allSizeSum = this.getAllSizeSum(event.row);
      event.row.piecesBox = allSizeSum;
      event.row.total = (event.row.piecesBox*event.row.boxNumber);
      if (event.row.unitPrice != undefined && event.row.unitPrice != ""){
        event.row.totalPrice = (event.row.unitPrice*event.row.total).toFixed(2);
      }
      if (event.row.netWeight != "" && event.row.netWeight != undefined){
        event.row.totalNetWeight = (event.row.netWeight * event.row.boxNumber).toFixed(2);
      }
      if (event.row.grossWeight != "" && event.row.grossWeight != undefined){
        event.row.totalGrossWeight = (event.row.grossWeight * event.row.boxNumber).toFixed(2);
      }
      if (event.row.outerBoxLength != "" && event.row.outerBoxLength != undefined &&
      event.row.outerBoxWidth != "" && event.row.outerBoxWidth != undefined &&
      event.row.outerBoxHeight != "" && event.row.outerBoxHeight != undefined){
        event.row.totalVolume = ((event.row.outerBoxLength * event.row.outerBoxWidth * event.row.outerBoxHeight * event.row.boxNumber)/1000000).toFixed(3);
      }
      this.$forceUpdate()
      this.setHeadCalField();
      },
    // 获取不同尺码数量之和
    getAllSizeSum(record){
      var ret = 0;
      for (var i=0; i<this.sizeFields.length; i++){
        if (record[this.sizeFields[i]] != undefined && record[this.sizeFields[i]] != ""){
          ret += record[this.sizeFields[i]]*1;
        }
      }
      return ret;
    },
    // 设置表头计算值,直接设置表头值对象不能刷新，把表头对象换了就可以了
    setHeadCalField(){
      var headData = JSON.parse(JSON.stringify(this.addAdpacking));
      var totalBoxes = 0;
      var totalNetWeight = 0;
      var totalGrossWeight = 0;
      var totalVolume = 0;
      var totalPrice = 0;
      var total = 0;

      for (var i=0; i<headData.syPreAssembledPackingListItemList.length; i++){
        var rowData = headData.syPreAssembledPackingListItemList[i];
        totalBoxes += rowData.boxNumber;
        if (rowData.totalNetWeight != undefined && rowData.totalNetWeight != ""){
          totalNetWeight += rowData.totalNetWeight*1;
        }
        if (rowData.totalGrossWeight != undefined && rowData.totalGrossWeight != ""){
          totalGrossWeight += rowData.totalGrossWeight*1;
        }
        if (rowData.totalVolume != undefined && rowData.totalVolume != ""){
          totalVolume += rowData.totalVolume*1;
        }
        if (rowData.totalPrice != undefined && rowData.totalPrice != ""){
          totalPrice += rowData.totalPrice*1;
        }
        if (rowData.total != undefined && rowData.total != ""){
          total += rowData.total*1;
        }
      }
      headData.totalBoxes = totalBoxes.toFixed(0);
      headData.totalNetWeight = totalNetWeight;
      headData.totalGrossWeight = totalGrossWeight;
      headData.totalVolume = totalVolume.toFixed(3);
      headData.totalPrice = totalPrice.toFixed(2); 
      headData.total = total.toFixed(0)
      this.addAdpacking = headData;
    },
  },
  computed: {},
  mounted() {}
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
/deep/ .ant-table-thead > tr > th {
  text-align: center;
  // font-weight: 700;
}
/deep/ .ant-table-tbody {
  text-align: center;
}
// /deep/ th.replacecolor {
//   background-color: #ccc;
// }
// 抽屉里的card样式
/deep/ .ant-drawer-content {
  background-color: #f0f2f5;
}
/deep/ .ant-drawer-body {
  padding: 10px;
}
</style>
