<template>
  <div>
    <el-dialog
      :title="title + ' - ' + warehousing.orderNo"
      :visible.sync="opened"
      width="1280px"
    >
      <el-tabs v-model="activeName" type="card" @tab-click="handleTabs">
        <el-tab-pane :label="edit ? '货物修改' : '货物入仓'" name="first">
          <el-form ref="form" :model="form" :rules="formRules" label-width="80px">
            <el-descriptions border :column="2">
              <el-descriptions-item>
                <template slot="label"><span style="color: red">*</span> 中文品名</template>
                {{ warehousing.prodTitleZh }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label"><span style="color: red">*</span> 英文品名</template>
                {{ warehousing.prodTitleEn }}
              </el-descriptions-item>
              <el-descriptions-item label="品牌">
                <template slot="label"><span style="color: red">*</span> 品牌</template>
                <span v-if="false">{{ form.brand ? brand : '无' }}</span>
                <el-form-item required label="" prop="brand" label-width="0">
                  <el-select
                    v-model="form.brand"
                    placeholder="可修改"
                    filterable
                    @change="handleBrandChange"
                    clearable>
                    <el-option
                      v-for="item in brandList"
                      :key="item.id"
                      :label="item.titleZh"
                      :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-descriptions-item>
              <el-descriptions-item label="是否备案">
                <template slot="label"><span style="color: red">*</span> 是否备案</template>
                {{ isBeian }}
              </el-descriptions-item>
              <el-descriptions-item label="收费模式">
                <template slot="label"><span style="color: red">*</span> 收费模式</template>
                {{ feeType }}
              </el-descriptions-item>
              <el-descriptions-item label="填单参数">
                箱数：
                <el-input size="mini" v-if="edit" v-model="warehousing.num" style="display: inline-block;width: 100px"></el-input>
                <span v-else>{{ warehousing.num }}</span><br>
                体积：
                <el-input size="mini" v-if="edit" v-model="warehousing.volume" style="display: inline-block;width: 100px"></el-input>
                <span v-else>{{ warehousing.volume }}</span>m³<br>
                重量：
                <el-input size="mini" v-if="edit" v-model="warehousing.weight" style="display: inline-block;width: 100px"></el-input>
                <span v-else>{{ warehousing.weight }}</span>Kg
              </el-descriptions-item>
            </el-descriptions>
            <el-form-item label="材质" style="margin-top: 20px">
              <dict-selector :type="DICT_TYPE.ECW_PRODUCT_MATERIAL" v-model="form.material"></dict-selector>
            </el-form-item>
            <el-form-item label="入仓时间" v-if="!edit">
              <el-date-picker v-model="form.inTime" type="datetime" placeholder="请选择入仓时间" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
          </el-form>

          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>入仓记录</span>
              <el-button style="float: right;margin-left: 15px" size="mini" type="primary" icon="el-icon-minus" circle @click="handleDelete"></el-button>
              <el-button style="float: right" size="mini" type="primary" icon="el-icon-plus" circle @click="handleAdd()"></el-button>
            </div>
            <el-form ref="tableForm" :model="tableData" :rules="tableFormRules" size="mini">
              <el-table
                :data="tableData"
                style="width: 100%">
                <el-table-column label="箱数" width="150px">
                  <template v-slot:header>
                    <span style="color: red">*</span> 箱数
                  </template>
                  <template v-slot="{r,c,$index}">
                    <el-form-item :rules="tableFormRules.cartonsNum" :prop="$index + '.cartonsNum'">
                      <span v-if="tableData[$index].id && !edit">
                        {{ tableData[$index].cartonsNum }}
                        {{ getDictDataLabel(DICT_TYPE.ECW_PACKAGING_TYPE, tableData[$index].unit) }}
                      </span>
                      <el-input v-else v-model="tableData[$index].cartonsNum" placeholder="">
                        <span slot="append">{{ getDictDataLabel(DICT_TYPE.ECW_PACKAGING_TYPE, tableData[$index].unit) }}</span>
                      </el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="包装类型" width="100px">
                  <template v-slot:header>
                    <span style="color: red">*</span> 包装类型
                  </template>
                  <template v-slot="{r,c,$index}">
                    <el-form-item>
                      <span v-if="tableData[$index].id && !edit">{{ getDictDataLabel(DICT_TYPE.ECW_PACKAGING_TYPE, tableData[$index].unit) }}</span>
                      <dict-selector v-else :type="DICT_TYPE.ECW_PACKAGING_TYPE" v-model="tableData[$index].unit"></dict-selector>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="长(cm)">
                  <template v-slot="{r,c,$index}">
                    <el-form-item :rules="tableFormRules.boxGauge1" :prop="!tableData[$index].id && $index + '.boxGauge1'">
                      <span v-if="tableData[$index].id && !edit">{{ tableData[$index].boxGauge ? tableData[$index].boxGauge.split('*')[0] : '' }}</span>
                      <el-input v-else v-model.number="tableData[$index].boxGauge1" placeholder="" @blur="handleVolume($index)"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="宽(cm)">
                  <template v-slot="{r,c,$index}">
                    <el-form-item :rules="tableFormRules.boxGauge2" :prop="!tableData[$index].id && $index + '.boxGauge2'">
                      <span v-if="tableData[$index].id && !edit">{{ tableData[$index].boxGauge ? tableData[$index].boxGauge.split('*')[1] : '' }}</span>
                      <el-input v-else v-model.number="tableData[$index].boxGauge2" placeholder="" @blur="handleVolume($index)"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="高(cm)">
                  <template v-slot="{r,c,$index}">
                    <el-form-item :rules="tableFormRules.boxGauge3" :prop="!tableData[$index].id && $index + '.boxGauge3'">
                      <span v-if="tableData[$index].id && !edit">{{ tableData[$index].boxGauge ? tableData[$index].boxGauge.split('*')[2] : '' }}</span>
                      <el-input v-else v-model.number="tableData[$index].boxGauge3" placeholder="" @blur="handleVolume($index)"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="体积(m³)">
                  <template v-slot:header>
                    <span style="color: red">*</span> 体积(m³)
                  </template>
                  <template v-slot="{r,c,$index}">
                    <el-form-item :rules="tableFormRules.volume" :prop="$index + '.volume'">
                      <span v-if="tableData[$index].id && !edit">{{ tableData[$index].volume }}</span>
                      <el-input v-else v-model="tableData[$index].volume" placeholder="" type="number"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="重量(Kg)">
                  <template v-slot:header>
                    <span style="color: red">*</span> 重量(Kg)
                  </template>
                  <template v-slot="{r,c,$index}">
                    <el-form-item :rules="tableFormRules.weight" :prop="$index + '.weight'">
                      <span v-if="tableData[$index].id && !edit">{{ tableData[$index].weight }}</span>
                      <el-input v-else v-model="tableData[$index].weight" placeholder="" type="number"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="数量" width="130px">
                  <template v-slot:header>
                    <span style="color: red">*</span> 数量
                  </template>
                  <template v-slot="{r,c,$index}">
                    <el-form-item :rules="tableFormRules.quantityAll" :prop="$index + '.quantityAll'">
                      <span v-if="tableData[$index].id && !edit">{{ tableData[$index].quantityAll }}</span>
                      <el-input v-else v-model.number="tableData[$index].quantityAll" placeholder="">
                        <template slot="append">个</template>
                      </el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="快递单号">
                  <template v-slot="{r,c,$index}">
                    <el-form-item>
                      <span v-if="tableData[$index].id && !edit">{{ tableData[$index].expressNo }}</span>
                      <el-input v-else v-model="tableData[$index].expressNo" placeholder=""></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
              </el-table>

            </el-form>
          </el-card>
        </el-tab-pane>

        <el-tab-pane label="添加新品名" name="second" v-if="!edit">
          <el-form ref="form1" :model="form1" :rules="formRules" label-width="80px">
            <el-descriptions border :column="2">
              <el-descriptions-item>
                <template slot="label"><span style="color: red">*</span> 中文品名</template>
                <el-form-item required label="" prop="prodId" label-width="0">
                  <product-selector v-model="form1.prodId" @change="onProductChange"/>
                </el-form-item>
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label"><span style="color: red">*</span> 英文品名</template>
                <el-form-item required label="" prop="prodId" label-width="0">
                  <product-selector lang="En" v-model="form1.prodId" @change="onProductChange"/>
                </el-form-item>
              </el-descriptions-item>
              <el-descriptions-item label="品牌">
                <template slot="label"><span style="color: red">*</span> 品牌</template>
                <el-form-item required label="" prop="brand" label-width="0">
                  <el-select
                    v-model="form1.brand"
                    placeholder="可修改"
                    filterable
                    @change="handleBrandChange"
                    clearable>
                    <el-option
                      v-for="item in brandList"
                      :key="item.id"
                      :label="item.titleZh"
                      :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-descriptions-item>
              <el-descriptions-item label="是否备案">
                <template slot="label"><span style="color: red">*</span> 是否备案</template>
                {{ isBeian1 }}
              </el-descriptions-item>
              <el-descriptions-item label="收费模式">
                <template slot="label"><span style="color: red">*</span> 收费模式</template>
                {{ feeType1 }}
              </el-descriptions-item>
              <el-descriptions-item label="填单参数">非填单货物</el-descriptions-item>
            </el-descriptions>
            <el-form-item label="材质" style="margin-top: 20px">
              <dict-selector :type="DICT_TYPE.ECW_PRODUCT_MATERIAL" v-model="form1.material"></dict-selector>
            </el-form-item>
            <el-form-item label="入仓时间">
              <el-date-picker v-model="form1.inTime" type="datetime" placeholder="请选择入仓时间" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
          </el-form>

          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>入仓记录</span>
              <el-button style="float: right;margin-left: 15px" size="mini" type="primary" icon="el-icon-minus" circle @click="handleDelete(1)"></el-button>
              <el-button style="float: right" size="mini" type="primary" icon="el-icon-plus" circle @click="handleAdd(1)"></el-button>
            </div>
            <el-form ref="tableForm1" :model="tableData1" :rules="tableFormRules" size="mini">
              <el-table
                :data="tableData1"
                style="width: 100%">
                <el-table-column label="箱数" width="150px">
                  <template v-slot:header>
                    <span style="color: red">*</span> 箱数
                  </template>
                  <template v-slot="{r,c,$index}">
                    <el-form-item  :rules="tableFormRules.cartonsNum" :prop="$index + '.cartonsNum'">
                      <span v-if="tableData1[$index].id">
                        {{ tableData1[$index].cartonsNum }}
                        {{ getDictDataLabel(DICT_TYPE.ECW_PACKAGING_TYPE, tableData1[$index].unit) }}
                      </span>
                      <el-input v-else v-model="tableData1[$index].cartonsNum" placeholder="">
                        <span slot="append">{{ getDictDataLabel(DICT_TYPE.ECW_PACKAGING_TYPE, tableData1[$index].unit) }}</span>
                      </el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="包装类型" width="100px">
                  <template v-slot:header>
                    <span style="color: red">*</span> 包装类型
                  </template>
                  <template v-slot="{r,c,$index}">
                    <el-form-item>
                      <span v-if="tableData1[$index].id">{{ getDictDataLabel(DICT_TYPE.ECW_PACKAGING_TYPE, tableData1[$index].unit) }}</span>
                      <dict-selector v-else :type="DICT_TYPE.ECW_PACKAGING_TYPE" v-model="tableData1[$index].unit"></dict-selector>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="长(cm)">
                  <template v-slot="{r,c,$index}">
                    <el-form-item :rules="tableFormRules.boxGauge1" :prop="$index + '.boxGauge1'">
                      <span v-if="tableData1[$index].id">{{ tableData1[$index].boxGauge ? tableData1[$index].boxGauge.split('*')[0] : '' }}</span>
                      <el-input v-else v-model.number="tableData1[$index].boxGauge1" placeholder="" @blur="handleVolume($index, 1)"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="宽(cm)">
                  <template v-slot="{r,c,$index}">
                    <el-form-item :rules="tableFormRules.boxGauge2" :prop="$index + '.boxGauge2'">
                      <span v-if="tableData1[$index].id">{{ tableData1[$index].boxGauge ? tableData1[$index].boxGauge.split('*')[1] : '' }}</span>
                      <el-input v-else v-model.number="tableData1[$index].boxGauge2" placeholder="" @blur="handleVolume($index, 1)"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="高(cm)">
                  <template v-slot="{r,c,$index}">
                    <el-form-item :rules="tableFormRules.boxGauge3" :prop="$index + '.boxGauge3'">
                      <span v-if="tableData1[$index].id">{{ tableData1[$index].boxGauge ? tableData1[$index].boxGauge.split('*')[2] : '' }}</span>
                      <el-input v-else v-model.number="tableData1[$index].boxGauge3" placeholder="" @blur="handleVolume($index, 1)"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="体积(m³)">
                  <template v-slot:header>
                    <span style="color: red">*</span> 体积(m³)
                  </template>
                  <template v-slot="{r,c,$index}">
                    <el-form-item :rules="tableFormRules.volume" :prop="$index + '.volume'">
                      <span v-if="tableData1[$index].id">{{ tableData1[$index].volume }}</span>
                      <el-input v-else v-model.number="tableData1[$index].volume" placeholder=""></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="重量(Kg)">
                  <template v-slot:header>
                    <span style="color: red">*</span> 重量(Kg)
                  </template>
                  <template v-slot="{r,c,$index}">
                    <el-form-item :rules="tableFormRules.weight" :prop="$index + '.weight'">
                      <span v-if="tableData1[$index].id">{{ tableData1[$index].weight }}</span>
                      <el-input v-else v-model.number="tableData1[$index].weight" placeholder=""></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="数量" width="130px">
                  <template v-slot:header>
                    <span style="color: red">*</span> 数量
                  </template>
                  <template v-slot="{r,c,$index}">
                    <el-form-item :rules="tableFormRules.quantityAll" :prop="$index + '.quantityAll'">
                      <span v-if="tableData1[$index].id">{{ tableData1[$index].quantityAll }}</span>
                      <el-input v-else v-model.number="tableData1[$index].quantityAll" placeholder="">
                        <template slot="append">个</template>
                      </el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="快递单号">
                  <template v-slot="{r,c,$index}">
                    <el-form-item>
                      <span v-if="tableData1[$index].id">{{ tableData1[$index].expressNo }}</span>
                      <el-input v-else v-model="tableData1[$index].expressNo" placeholder=""></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
              </el-table>
            </el-form>
          </el-card>

        </el-tab-pane>
      </el-tabs>

      <div v-if="edit">
        <h2>审批流程</h2>
        <work-flow xmlkey="free_apply" v-model="selectedUsers"  />
        <!--        <div>选择的用户：{{selectedUsers}}</div>-->
      </div>

      <span slot="footer">
        <template v-if="!isEditing">
          <el-button @click="opened = false">关 闭</el-button>
          <el-button type="primary" @click="handleSubmit()">{{ edit ? '确认修改' : '提 交' }}</el-button>
        </template>
        <template v-else>
          <el-button type="primary" @click="$router.push('/bpm/process-instance/detail?id=' + formId)">审核中</el-button>
          <el-button type="primary" @click="handleCancelProcessInstance">取消审核</el-button>
          <el-button @click="opened = false">返回</el-button>
        </template>
        <template v-else>

        </template>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import dictSelector from "@/components/DictSelector"
import {DICT_TYPE, getDictDataLabel} from "@/utils/dict"
import {getWarehouseLastUpdateApprovalInfo, orderWarehouseIn, orderWarehouseInUpdateApply} from '@/api/ecw/order'
import { getFeeTypeByOrderProduct, getProductBrankPage } from '@/api/ecw/productBrank'
import WorkFlow from "@/components/WorkFlow"
import ProductSelector from "@/components/ProductSelector"
import {cancelProcessInstance} from "@/api/bpm/processInstance"

export default {
  name: 'editDialog',

  components: {
    dictSelector,
    WorkFlow,
    ProductSelector
  },

  props: {
    orderId: {
      type: Number,
      default: undefined
    },
    visible: {
      type: Boolean,
      default: false
    },
    // 入仓修改
    edit: {
      type: Boolean,
      default: false
    },
    warehousing: {
      type: Object,
      default: () => {
        return {
          material: undefined,
          inTime: undefined,
          orderWarehouseInBackItemDoList: []
        }
      }
    }
  },

  mounted() {
    getProductBrankPage({pageSize: 100000}).then(r => {
      this.brandList = r.data.list.map(e => {
        if(e.id){
          e.id = e.id.toString()
        }
        return e
      })
    })
  },

  data() {
    return {
      DICT_TYPE,
      getDictDataLabel,
      opened: false,
      brandList: [],
      formRules: {
        prodId: [{required: true, message: this.$t("请选择品名"), trigger: "change"}],
        brand: [{required: true, message: this.$t("请选择品牌"), trigger: "change"}],
      },
      form: {
        "brand": "",
        // "brandType": 0,
        // "inTime": "",
        // "material": "",
        // "orderId": 0,
        // "orderItemId": 0,
        // "orderNo": "",
        "orderWarehouseInItemDoList": [],
        "prodAttrIds": "",
        "prodId": '',
        // "prodTitleEn": "",
        // "prodTitleZh": "",
        // "prodType": 0,
        // "type": 0,
        feeType: 0,
        recordMode: undefined
      },
      tableFormRules: {
        cartonsNum: [{required: true, message: this.$t("箱数不能为空"), trigger: "change"}],
        boxGauge1: [{required: true, message: this.$t("货物长不能为空"), trigger: "change"}],
        boxGauge2: [{required: true, message: this.$t("货物宽不能为空"), trigger: "change"}],
        boxGauge3: [{required: true, message: this.$t("货物高不能为空"), trigger: "change"}],
        volume: [{required: true, message: this.$t("体积不能为空"), trigger: "change"}],
        weight: [{required: true, message: this.$t("重量不能为空"), trigger: "change"}],
        quantityAll: [{required: true, message: "数量不能为空", trigger: "change"}]
      },
      form1: {
        "brand": undefined,
        "brandType": 0,
        "inTime": "",
        "material": "",
        "orderId": 0,
        "orderItemId": 0,
        "orderNo": "",
        "orderWarehouseInItemDoList": [],
        "prodAttrIds": "",
        "prodId": '',
        "prodTitleEn": "",
        "prodTitleZh": "",
        "prodType": 0,
        "type": 1,
        feeType: 0,
        recordMode: undefined
      },
      // orderItem: {},

      selectedUsers: undefined,

      activeName: 'first',

      // 入仓修改正在审核中
      isEditing: false,
      // 审批业务id
      formId: ''
    };
  },

  watch: {
    visible(val) {
      if (val) {
        this.opened = true
        if (this.warehousing) {
          // this.orderItem = this.warehousing
          this.inTime = this.warehousing.inTime
        }
      } else {
      }
    },
    opened(val) {
      if (val) {
        this.form.material = this.warehousing.material
        if (this.warehousing.brand && this.warehousing.brand !== 0){
          this.form.brand = this.warehousing.brand.toString() || ''
        }
        this.handleBrandChange(this.form.brand)
        getWarehouseLastUpdateApprovalInfo({
          orderItemId: this.warehousing.orderItemId,
          orderId: this.warehousing.orderId
        }).then(r => {
          if (r.data && r.data.status === 1 && r.data.type === 1){
            this.isEditing = true
            this.formId = r.data.formId
          }
        })
      } else {
        this.$emit('update:visible', false)
      }
    },
    warehousing(newVal, oldVal){
      if(newVal.orderItemId !== oldVal.orderItemId){
        this.form.orderWarehouseInItemDoList = []
        this.form1.orderWarehouseInItemDoList = []
        this.activeName = 'first'
        if (!this.edit){
          this.handleAdd()
          this.handleAdd(1)
        }
      }
    }
  },

  methods: {
    handleCancelProcessInstance(){
      this.$prompt('请输入取消原因？', "取消流程", {
        type: 'warning',
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /^[\s\S]*.*\S[\s\S]*$/, // 判断非空，且非空格
        inputErrorMessage: "取消原因不能为空",
      }).then(({ value }) => {
        cancelProcessInstance(this.formId, value).then(() => {
          this.opened = false
          this.$modal.msgSuccess("取消成功");
        })
      })
    },
    onProductChange(product){
      this.form1.prodTitleZh = product.titleZh
      this.form1.prodTitleEn = product.titleEn
    },
    handleTabs(){},
    handleBrandChange(v){
      getFeeTypeByOrderProduct({
        brandId: parseInt(v),
        productId: this.warehousing.prodId,
        orderId: this.orderId
      }).then(r => {
        if(r.code === 0){
          (this.activeName === "first" ? this.form : this.form1).feeType = parseInt(r.data.feeType);
          (this.activeName === "first" ? this.form : this.form1).recordMode = parseInt(r.data.recordMode)
        }
      })
    },
    handleVolume(index, val){
      const {boxGauge1, boxGauge2, boxGauge3} = (val === 1 ? this.tableData1 : this.tableData)[index]
      let result = ''
      if (boxGauge1 && boxGauge2 && boxGauge3) {
        result = ((boxGauge1 * boxGauge2 * boxGauge3) / 1000000)?.toFixed(2) || ''
      } else {
        result = ''
      }
      if (result === '0.00') result = '0.01';
      (val === 1 ? this.tableData1 : this.tableData)[index].volume = result
    },
    handleSubmit() {
      const orderWarehouseInItemDoList = [
        ...this.warehousing.orderWarehouseInBackItemDoList,
        ...this.form.orderWarehouseInItemDoList.map(e => {
          return {
            ...e,
            boxGauge: e.boxGauge1 + '*' + e.boxGauge2 + '*' + e.boxGauge3
          }
        })
      ]
      if (this.activeName !== "first"){
        // 添加非填单货物
        this.$refs['form1'].validate(valid => {
          this.$refs['tableForm1'].validate(valid1 => {
            if (!valid || !valid1) {
              return
            }
            return orderWarehouseIn({
              ...this.form1,
              brandType: this.warehousing.brandType,
              orderId: this.warehousing.orderId,
              orderItemId: undefined,
              orderNo: this.warehousing.orderNo,
              orderWarehouseInItemDoList: this.form1.orderWarehouseInItemDoList.map(e => {
                return {
                  ...e,
                  boxGauge: e.boxGauge1 + '*' + e.boxGauge2 + '*' + e.boxGauge3
                }
              })
            }).then(r => {
              if (r.data) {
                this.$message.success('新增入仓成功')
                this.handleClose()
              } else {
                this.$message.success('新增入仓失败')
              }
            })
          })
        })
      } else {
        this.$refs['form'].validate(valid => {
          this.$refs['tableForm'].validate(valid1 => {
            if (!valid || !valid1) {
              return
            }
            if (this.edit) {
              // 入仓修改
              return orderWarehouseInUpdateApply({
                ...this.form,
                brandType: this.warehousing.brandType,
                orderId: this.warehousing.orderId,
                orderItemId: this.warehousing.orderItemId,
                orderNo: this.warehousing.orderNo,
                num: this.warehousing.num,
                volume: this.warehousing.volume?.toFixed(2) || '',
                weight: this.warehousing.weight?.toFixed(2) || '',
                prodId: this.warehousing.prodId,
                "orderWarehouseInUpdateItemDoList": this.tableData.map(e => {
                  return {
                    ...e,
                    boxGauge: e.boxGauge1 + '*' + e.boxGauge2 + '*' + e.boxGauge3
                  }
                }),
                copyUserId: this.selectedUsers
              }).then(r => {
                if (r.data) {
                  this.$message.success('入仓修改发起成功')
                  this.handleClose()
                } else {
                  this.$message.success('入仓修改发起失败')
                }
              })
            } else {
              // 首次入仓
              return orderWarehouseIn({
                ...this.form,
                brandType: this.warehousing.brandType,
                orderId: this.warehousing.orderId,
                orderItemId: this.warehousing.orderItemId,
                orderNo: this.warehousing.orderNo,
                orderWarehouseInItemDoList
              }).then(r => {
                if (r.data) {
                  this.$message.success('入仓成功')
                  this.handleClose()
                } else {
                  this.$message.success('入仓失败')
                }
              })
            }
          })
        })

      }
    },
    handleClose() {
      this.form.orderWarehouseInItemDoList = []
      this.opened = false
    },
    handleDelete(val) {
      (val === 1 ? this.form1 : this.form).orderWarehouseInItemDoList.pop()
    },
    handleAdd(val = 0) {
      let cartonsNum = '';
      if (val !== 1) {
        let hasCartonsNum = 0
        this.tableData.forEach(e => {
          hasCartonsNum += e.cartonsNum
        })
        cartonsNum = this.warehousing.num - hasCartonsNum
      }
      const form = val === 1 ? this.form1 : this.form
      form.orderWarehouseInItemDoList.push({
        "boxGauge1": "",
        "boxGauge2": "",
        "boxGauge3": "",
        "cartonsNum": cartonsNum > 0 ? cartonsNum : '',
        "expressNo": "",
        "quantityAll": "",
        "unit": "1",
        "volume": '',
        "weight": ''
      })
    }
  },

  computed: {
    title() {
      return this.$route.name
    },
    brandObject(){
      return this.brandList.find(e => e.id === this.form.brand) || ''
    },
    brandObject1(){
      return this.brandList.find(e => e.id === this.form1.brand) || ''
    },
    tableData() {
      if (!this.edit){
        return [...this.warehousing.orderWarehouseInBackItemDoList, ...this.form.orderWarehouseInItemDoList]
      }

      // 入仓修改的时候
      this.form.orderWarehouseInItemDoList = this.warehousing.orderWarehouseInBackItemDoList
      return this.form.orderWarehouseInItemDoList.map(e => {
        const boxGauge = e.boxGauge
        if (boxGauge && boxGauge.length > 0) {
          const boxGaugeList = boxGauge.split('*')
          const [boxGauge1, boxGauge2, boxGauge3] = boxGaugeList
          return {...e, boxGauge1, boxGauge2, boxGauge3}
        }
        return e
      })
    },
    tableData1() {
      return this.form1.orderWarehouseInItemDoList
    },
    isBeian(){
      if (this.form.recordMode !== undefined){
        return ['无备案', '有备案', '中性'][this.form.recordMode]
      }
      if (this.brandObject.filing){
        return ['无备案', '有备案', '中性'][this.brandObject.filing]
      } else {
        return ''
      }
    },
    isBeian1(){
      if (this.form1.recordMode !== undefined){
        return ['无备案', '有备案', '中性'][this.form1.recordMode]
      }
      if (this.brandObject1.filing){
        return ['无备案', '有备案', '中性'][this.brandObject1.filing]
      } else {
        return ''
      }
    },
    /**
     * 收费模式
     * @returns {string} 无牌价0，有牌价1，中性品牌价2
     */
    feeType(){
      const feeType = parseInt(this.form.feeType)
      if (feeType >= 0 && feeType < 3) {
        return ['无牌价', '有牌价', '中性品牌价'][feeType]
      }
      return ''
    },
    feeType1(){
      const feeType = parseInt(this.form1.feeType)
      if (feeType >= 0 && feeType < 3) {
        return ['无牌价', '有牌价', '中性品牌价'][feeType]
      }
      return ''
    },
    brand(){
      if (!!this.form.brand){
        const brand = this.brandList.find(e => e.id === this.form.brand)
        if (brand) {
          return brand.titleZh
        }
      }
      return ''
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-table__cell {
  padding-bottom: 0;
  .cell {
    .el-form-item {
      margin-bottom: 16px;
    }

    .el-form-item__error {
      z-index: 1;
      padding-top: 0;
      top: 32px;
    }
  }
}
</style>
