<template>
  <div id="addOder">
    <el-form :model="basicForm" :rules="basicRules" ref="basic" label-width="auto">
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item label="调出库存组织">
            <el-select clearable size="small" v-model="basicForm.deliveryInventoryOrg" :disabled="sonDisable" @focus="chooseRefer('ORG_PARAM', true, '调出库存组织')" style="width: 200px">
              <el-option v-for="item in chuOrgOptions" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
            <el-form-item label="订单类型">
              <el-select v-model="basicForm.billType" :disabled="sonDisable" size="small" style="width: 200px" clearable>
                <el-option v-for="dict in dict.type.sys_allot_billtype" :key="dict.value" :label="dict.label" :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="单据号">
              <el-input
                v-model="basicForm.code"
                size="small"
                :disabled="sonDisable"
                clearable
                style="width: 200px"
              />
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="单据日期">
              <el-date-picker
                v-model="basicForm.billDate"
                size="small"
                :disabled="sonDisable"
                type="date"
                clearable
                value-format="yyyy-MM-dd"
                style="width: 200px">
              </el-date-picker>
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="调拨方式">
              <el-select v-model="basicForm.allotType" disabled size="small" style="width: 200px" clearable>
                <el-option v-for="dict in dict.type.sys_to_type" :key="dict.value" :label="dict.label" :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="调拨出库单号">
              <el-input
                v-model="basicForm.deliveryCode"
                size="small"
                :disabled="sonDisable"
                clearable
                style="width: 200px"
              />
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="调拨入库单号">
              <el-input
                v-model="basicForm.storageCode"
                size="small"
                :disabled="sonDisable"
                clearable
                style="width: 200px"
              />
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="调入库存组织">
              <el-select clearable size="small" v-model="basicForm.storageInventoryOrg" :disabled="sonDisable" @focus="chooseRefer('ORG_PARAM', true, '调入库存组织')" style="width: 200px">
                <el-option v-for="item in ruOrgOptions" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="调出业务员">
              <el-select clearable size="small" v-model="basicForm.businessPersonal" :disabled="sonDisable" @focus="chooseRefer('CONTACTS_PARAM', true, '调出业务员')" style="width: 200px">
                <el-option v-for="item in manOptions" :key="item.id" :label="item.name" :value="item.code" />
              </el-select>
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="调出部门">
              <el-select clearable size="small" v-model="basicForm.deliveryDept" :disabled="sonDisable" @focus="chooseRefer('DEPT_PARAM', true, '调出部门', basicForm.deliveryInventoryOrg)" style="width: 200px">
                <el-option v-for="item in deptOptions" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="币种">
              <el-select clearable size="small" v-model="basicForm.currency" :disabled="sonDisable" @focus="chooseRefer('CURRENCY_PARAM', true, '币种')" style="width: 200px">
                <el-option v-for="item in currencyOptions" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="折本汇率">
              <el-input-number
                v-model="basicForm.rate"
                size="small"
                disabled
                :precision="2" 
                :step="0.1" 
                :max="10"
                style="width: 200px"
              ></el-input-number>
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="在途归属">
              <el-input
                v-model="basicForm.onRouteAffilliation"
                size="small"
                disabled
                clearable
                style="width: 200px"
              />
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="总数量">
              <el-input
                v-model="basicForm.qty"
                size="small"
                :disabled="sonDisable"
                clearable
                style="width: 200px"
              />
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="结算路径">
              <el-input
                v-model="basicForm.accountPath"
                size="small"
                :disabled="sonDisable"
                clearable
                style="width: 200px"
              />
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="客户">
              <el-input
                v-model="basicForm.customer"
                size="small"
                :disabled="sonDisable"
                clearable
                style="width: 200px"
              />
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="调入仓库">
              <el-select clearable size="small" v-model="basicForm.storageWarehouse" :disabled="sonDisable" @focus="chooseRefer('WAREHOUSE_PARAM', true, '调入仓库', basicForm.storageInventoryOrg, 'N', 'N')" style="width: 200px">
                <el-option v-for="item in ruHouseOptions" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="调出仓库">
              <el-select clearable size="small" v-model="basicForm.deliveryWarehouse" :disabled="sonDisable" @focus="chooseRefer('WAREHOUSE_PARAM', true, '调出仓库')" style="width: 200px">
                <el-option v-for="item in chuHouseOptions" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="物流项目组">
              <el-select v-model="basicForm.materialProject" :disabled="sonDisable" size="small" style="width: 200px" clearable>
                <el-option v-for="dict in dict.type.sys_project_group" :key="dict.value" :label="dict.label" :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
         </el-col>
        <el-col :span="1.5">
            <el-form-item label="利润中心">
              <el-select clearable size="small" v-model="basicForm.liacenter" :disabled="sonDisable" @focus="chooseTreeRefer('LIACENTER_PARAM', false, '利润中心')" style="width: 200px">
                <el-option v-for="item in liacenterOptions" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item label="单据状态">
            <el-select disabled v-model="basicForm.status" size="small" style="width: 200px" clearable>
              <el-option v-for="dict in dict.type.sys_status" :key="dict.value" :label="dict.label" :value="dict.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
            <el-form-item label="备注">
              <el-input
                v-model="basicForm.remark"
                size="small"
                :disabled="sonDisable"
                clearable
                style="width: 200px"
              />
            </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="20">
          <el-form-item label="同步NC结果">
              <el-input
                v-model="basicForm.isSendWms"
                type="textarea"
                :rows="3"
                size="small"
                disabled
                clearable
              />
          </el-form-item>
        </el-col>
      </el-row>

      <el-tabs style="margin-top: 10px;" type="border-card">
        <el-tab-pane label="物料信息">
          <div class="btn_grooup">
            <el-button type="primary" size="small" v-if="!sonDisable" @click="addLine">增行</el-button>
          </div>
          <el-table
          :data="materialInfo" 
          fit
          max-height="480"
          >
          <el-table-column show-overflow-tooltip label="行号" align="center" prop="rowno"/>
          <el-table-column show-overflow-tooltip label="采购需求单单号" align="center" prop="demandCode" width="200px"/>
          <el-table-column show-overflow-tooltip label="物料编码" align="center" prop="materialCode" width="230px">
            <template slot-scope="scope">
              <el-form-item class="hang">
                <el-input clearable :disabled="sonDisable" size="small" v-model="scope.row.materialCode" @focus="chooseMaterial(scope.$index)">
                  <el-button size="small" :disabled="sonDisable" slot="append" icon="el-icon-more" @click="chooseMaterial(scope.$index)"></el-button>
                </el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="物料名称" align="center" prop="materialName" width="200px"/>
          <el-table-column show-overflow-tooltip label="调拨出库单号" align="center" prop="deliveryCode" width="200px"/>
          <el-table-column show-overflow-tooltip label="调拨入库单号" align="center" prop="storageCode" width="200px"/>
          <el-table-column show-overflow-tooltip label="规格" align="center" prop="specification"/>
          <el-table-column show-overflow-tooltip label="生产厂家/代理人" align="center" prop="manufacturer" width="200px"/>
          <el-table-column show-overflow-tooltip label="产地" align="center" prop="originPlaceName"/>
          <el-table-column show-overflow-tooltip label="型号" align="center" prop="model"/>
          <el-table-column show-overflow-tooltip label="单位" align="center" prop="unitName"/>
          <!-- <el-table-column show-overflow-tooltip label="调出仓库" align="center" prop="deliveryWarehouseName"/> -->
          <el-table-column show-overflow-tooltip label="数量" align="center" prop="qty" width="150px">
            <template slot-scope="scope">
              <el-form-item class="hang">
                <el-input :readonly="sonDisable" clearable size="small" v-model="scope.row.qty" @input="getSL(scope)"/>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="主单位" align="center" prop="mainUnitName"/>
          <el-table-column show-overflow-tooltip label="换算率" align="center" prop="equation"/>
          <el-table-column show-overflow-tooltip label="主数量" align="center" prop="mainQty" width="150px">
            <template slot-scope="scope">
              <el-form-item class="hang">
                <el-input :readonly="sonDisable" clearable size="small" v-model="scope.row.mainQty" @input="getZSL(scope)"/>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="税率" align="center" prop="rate"/>
          <el-table-column show-overflow-tooltip label="调出货位" align="center" prop="deliveryAllocationName" width="230px">
            <template slot-scope="scope">
              <el-form-item class="hang">
                <el-input clearable :disabled="sonDisable" size="small" v-model="scope.row.deliveryAllocationName" @focus="chooseMxHW(scope.$index, 'ALLOCATION_PARAM', true, '调出货位', basicForm.deliveryWarehouse)">
                  <el-button size="small" :disabled="sonDisable" slot="append" icon="el-icon-more" @click="chooseMxHW(scope.$index, 'ALLOCATION_PARAM', true, '调出货位', basicForm.deliveryWarehouse)"></el-button>
                </el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="调入部门" align="center" prop="storageDeptName" width="230px">
            <template slot-scope="scope">
              <el-form-item class="hang">
                <el-input clearable :disabled="sonDisable" size="small" v-model="scope.row.storageDeptName" @focus="chooseMxBM(scope.$index, 'DEPT_PARAM', true, '调入部门', basicForm.storageInventoryOrg)">
                  <el-button size="small" :disabled="sonDisable" slot="append" icon="el-icon-more" @click="chooseMxBM(scope.$index, 'DEPT_PARAM', true, '调入部门', basicForm.storageInventoryOrg)"></el-button>
                </el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="调入货位" align="center" prop="storageAllocationName" width="230px">
            <template slot-scope="scope">
              <el-form-item class="hang">
                <el-input clearable :disabled="sonDisable" size="small" v-model="scope.row.storageAllocationName" @focus="chooseMxHW(scope.$index, 'ALLOCATION_PARAM', true, '调入货位', basicForm.storageWarehouse)">
                  <el-button size="small" :disabled="sonDisable" slot="append" icon="el-icon-more" @click="chooseMxHW(scope.$index, 'ALLOCATION_PARAM', true, '调入货位', basicForm.storageWarehouse)"></el-button>
                </el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="批次号" align="center" prop="patchNo" width="150px"/>
          <el-table-column show-overflow-tooltip label="产品批号" align="center" prop="producBatch" width="230px">
            <template slot-scope="scope">
              <el-form-item class="hang">
                <el-input readonly size="small" v-model="scope.row.producBatch">
                  <el-button size="small" :disabled="sonDisable" slot="append" icon="el-icon-more" @click="chooseBatch(scope.$index)"></el-button>
                </el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="生产日期" align="center" prop="manufactureDate" width="230px">
            <template slot-scope="scope">
              <el-form-item class="hang">
                <el-date-picker
                  v-model="scope.row.manufactureDate"
                  :readonly="sonDisable"
                  type="date"
                  size="small"
                  value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="有效期至/失效日期" align="center" prop="periodEndDate" width="150px"/>
          <el-table-column show-overflow-tooltip label="调拨日期" align="center" prop="allotDate" width="230px">
            <template slot-scope="scope">
              <el-form-item class="hang">
                <el-date-picker
                  v-model="scope.row.allotDate"
                  :readonly="sonDisable"
                  type="date"
                  size="small"
                  :picker-options="pickerOptionsEnd"
                  value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="批准文号" align="center" prop="ratifyCode" width="200px"/>
          <el-table-column show-overflow-tooltip label="注册证号" align="center" prop="registration" width="200px"/>
          <el-table-column show-overflow-tooltip label="商品名" align="center" prop="productCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="通用名" align="center" prop="commonCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="剂型" align="center" prop="drug"/>
          <el-table-column show-overflow-tooltip label="调入调出结算规则明细" align="center" prop="ruleDetail" width="200px"/>
          <el-table-column show-overflow-tooltip label="上市许可持有人" align="center" prop="marketingApprovalPersonal" width="200px"/>
          <el-table-column show-overflow-tooltip label="生产许可证号/经营许可证号/备案凭证号" align="center" prop="production" width="260px"/>
          <el-table-column show-overflow-tooltip label="pi码" align="center" prop="pi">
            <template slot-scope="scope">
              <el-form-item class="hang">
                <el-input :readonly="sonDisable" size="small" v-model="scope.row.pi"/>
              </el-form-item>
            </template>
          </el-table-column>
          <!-- <el-table-column show-overflow-tooltip label="无税金额" align="center" prop="code"/> -->
          <!-- <el-table-column show-overflow-tooltip label="备注" align="center" prop="code"/> -->
          <el-table-column show-overflow-tooltip label="类别" align="center" prop="classify" width="150px"/>
          <el-table-column show-overflow-tooltip label="客户物料码" align="center" prop="customerLogistic" width="150px"/>
          <el-table-column show-overflow-tooltip label="客户物料名称" align="center" prop="customerLogisticName" width="150px"/>
          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            >
            <template slot-scope="scope">
              <el-button type="text" size="small" :disabled="sonDisable" @click="delLine(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
          </el-table>
        </el-tab-pane>

        <el-tab-pane label="收发货信息">
          <el-table
          :data="receiveInfo" 
          fit
          max-height="480"
          >
          <el-table-column show-overflow-tooltip label="行号" align="center" prop="rowno"/>
          <el-table-column show-overflow-tooltip label="调拨出库单号" align="center" prop="deliveryCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="调拨入库单号" align="center" prop="storageCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="生产厂家/代理人" align="center" prop="manufacturer" width="150px"/>
          <el-table-column show-overflow-tooltip label="产地" align="center" prop="originPlace" width="150px"/>
          <el-table-column show-overflow-tooltip label="类别" align="center" prop="classify" width="150px"/>
          <el-table-column show-overflow-tooltip label="调出仓库" align="center" prop="deliveryWarehouseName" width="150px"/>
          <el-table-column show-overflow-tooltip label="调入仓库" align="center" prop="storageWarehouseName" width="150px"/>
          <!-- <el-table-column show-overflow-tooltip label="收货利润中心" align="center" prop="code"/>
          <el-table-column show-overflow-tooltip label="发货利润中心" align="center" prop="code"/> -->
          <el-table-column show-overflow-tooltip label="调入业务员" align="center" prop="businessPersonalName" width="150px"/>
          <el-table-column show-overflow-tooltip label="产品批号" align="center" prop="productBatch" width="150px"/>
          <el-table-column show-overflow-tooltip label="生产日期" align="center" prop="manufactureDate" width="150px"/>
          <el-table-column show-overflow-tooltip label="有效期至/失效日期" align="center" prop="periodEndDate" width="150px"/>
          <el-table-column show-overflow-tooltip label="批准文号" align="center" prop="ratifyCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="注册证号" align="center" prop="registration" width="150px"/>
          <el-table-column show-overflow-tooltip label="商品名" align="center" prop="productCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="通用名" align="center" prop="commonCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="剂型" align="center" prop="drug" width="150px"/>
          <el-table-column show-overflow-tooltip label="收货客户" align="center" prop="customerName" width="150px"/>
          <el-table-column show-overflow-tooltip label="收货地址" align="center" prop="addressaddress" width="150px"/>
          <el-table-column show-overflow-tooltip label="收货地区" align="center" prop="area" width="150px"/>
          <el-table-column show-overflow-tooltip label="收货地点" align="center" prop="place" width="150px"/>
          <el-table-column show-overflow-tooltip label="计划发货日期" align="center" prop="deliveryDate" width="150px"/>
          <el-table-column show-overflow-tooltip label="计划到货日期" align="center" prop="strageDate" width="150px"/>
          <el-table-column show-overflow-tooltip label="运输方式" align="center" prop="transportation" width="150px"/>
          <el-table-column show-overflow-tooltip label="备注" align="center" prop="remark" width="150px"/>
          <el-table-column show-overflow-tooltip label="客户物料编码" align="center" prop="customerLogistic" width="150px"/>
          <el-table-column show-overflow-tooltip label="客户物料名称" align="center" prop="customerLogisticName" width="150px"/>
          </el-table>
        </el-tab-pane>

        <el-tab-pane label="价格费用">
          <el-table
          :data="priceList" 
          fit
          max-height="480"
          >
          <el-table-column show-overflow-tooltip label="行号" align="center" prop="rowno"/>
          <el-table-column show-overflow-tooltip label="调拨出库单号" align="center" prop="deliveryCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="调拨入库单号" align="center" prop="storageCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="生产厂家/代理人" align="center" prop="manufacturer" width="150px"/>
          <el-table-column show-overflow-tooltip label="产地" align="center" prop="originPlace" width="150px"/>
          <el-table-column show-overflow-tooltip label="类别" align="center" prop="classify" width="150px"/>
          <!-- <el-table-column show-overflow-tooltip label="税率" align="center" prop="code"/> -->
          <!-- <el-table-column show-overflow-tooltip label="无税单价" align="center" prop="code"/> -->
          <!-- <el-table-column show-overflow-tooltip label="含税单价" align="center" prop="code"/> -->
          <el-table-column show-overflow-tooltip label="产品批号" align="center" prop="productBatch" width="150px"/>
          <el-table-column show-overflow-tooltip label="生产日期" align="center" prop="manufactureDate" width="150px"/>
          <el-table-column show-overflow-tooltip label="有效期至/失效日期" align="center" prop="periodEndDate" width="150px"/>
          <el-table-column show-overflow-tooltip label="批准文号" align="center" prop="ratifyCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="注册证号" align="center" prop="registration" width="150px"/>
          <el-table-column show-overflow-tooltip label="商品名" align="center" prop="productCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="通用名" align="center" prop="commonCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="剂型" align="center" prop="drug" width="150px"/>
          <!-- <el-table-column show-overflow-tooltip label="询价无税价" align="center" prop="code"/> -->
          <!-- <el-table-column show-overflow-tooltip label="询价含税价" align="center" prop="code"/> -->
          <!-- <el-table-column show-overflow-tooltip label="税额" align="center" prop="code"/> -->
          <!-- <el-table-column show-overflow-tooltip label="加价率" align="center" prop="code"/> -->
          <!-- <el-table-column show-overflow-tooltip label="无税金额" align="center" prop="code"/> -->
          <!-- <el-table-column show-overflow-tooltip label="价税合计" align="center" prop="code"/> -->
          <el-table-column show-overflow-tooltip label="客户物料码" align="center" prop="customerLogistic" width="150px"/>
          <el-table-column show-overflow-tooltip label="客户物料名称" align="center" prop="customerLogisticName" width="150px"/>
          </el-table>
        </el-tab-pane>

        <el-tab-pane label="执行结果">
          <el-table
          :data="resultList" 
          fit
          max-height="480"
          >
          <el-table-column show-overflow-tooltip label="行号" align="center" prop="rowno"/>
          <el-table-column show-overflow-tooltip label="调拨出库单号" align="center" prop="deliveryCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="调拨入库单号" align="center" prop="storageCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="生产厂家/代理人" align="center" prop="manufacturer" width="150px"/>
          <el-table-column show-overflow-tooltip label="产地" align="center" prop="originPlace" width="150px"/>
          <el-table-column show-overflow-tooltip label="类别" align="center" prop="classify" width="150px"/>
          <el-table-column show-overflow-tooltip label="产品批号" align="center" prop="productBatch" width="150px"/>
          <el-table-column show-overflow-tooltip label="生产日期" align="center" prop="manufactureDate" width="150px"/>
          <el-table-column show-overflow-tooltip label="有效期至/失效日期" align="center" prop="periodEndDate" width="150px"/>
          <el-table-column show-overflow-tooltip label="批准文号" align="center" prop="ratifyCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="注册证号" align="center" prop="registration" width="150px"/>
          <el-table-column show-overflow-tooltip label="商品名" align="center" prop="productCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="通用名" align="center" prop="commonCode" width="150px"/>
          <el-table-column show-overflow-tooltip label="剂型" align="center" prop="drug" width="150px"/>
          <!-- <el-table-column show-overflow-tooltip label="已调出主数量" align="center" prop="code"/>
          <el-table-column show-overflow-tooltip label="已调入主数量" align="center" prop="code"/> -->
          <!-- <el-table-column show-overflow-tooltip label="调拨在途主数量" align="center" prop="code"/> -->
          <!-- <el-table-column show-overflow-tooltip label="已发货主数量" align="center" prop="code"/> -->
          <!-- <el-table-column show-overflow-tooltip label="途损主数量" align="center" prop="code"/> -->
          <!-- <el-table-column show-overflow-tooltip label="累计退货主数量" align="center" prop="code"/> -->
          <!-- <el-table-column show-overflow-tooltip label="调出与调入已结算主数量" align="center" prop="code"/> -->
          <!-- <el-table-column show-overflow-tooltip label="调出与调入已结算金额" align="center" prop="code"/> -->
          <el-table-column show-overflow-tooltip label="客户物料码" align="center" prop="customerLogistic" width="150px"/>
          <el-table-column show-overflow-tooltip label="客户物料名称" align="center" prop="customerLogisticName" width="150px"/>
          </el-table>
        </el-tab-pane>
      </el-tabs>

      <el-divider content-position="left">表尾信息</el-divider>
      <el-row :gutter="10">
        <el-col :span="1.5">
            <el-form-item label="制单人">
              <el-input
                v-model="basicForm.createByName"
                size="small"
                disabled
                clearable
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
        <el-col :span="1.5">
            <el-form-item label="制单日期">
              <el-input
                v-model="basicForm.createTime"
                size="small"
                disabled
                clearable
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
        <!-- <el-col :span="1.5">
            <el-form-item label="审批人">
              <el-input
                v-model="basicForm.code"
                size="small"
                disabled
                clearable
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
        <el-col :span="1.5">
            <el-form-item label="审批日期">
              <el-input
                v-model="basicForm.code"
                size="small"
                disabled
                clearable
                style="width: 200px"
              />
            </el-form-item>
          </el-col> -->
      </el-row>

      <el-divider content-position="left">审计信息</el-divider>
      <el-row :gutter="10">
        <el-col :span="1.5">
            <el-form-item label="创建人">
              <el-input
                v-model="basicForm.createByName"
                size="small"
                disabled
                clearable
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
        <el-col :span="1.5">
            <el-form-item label="创建时间">
              <el-input
                v-model="basicForm.createTime"
                size="small"
                disabled
                clearable
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
        <el-col :span="1.5">
            <el-form-item label="最后修改人">
              <el-input
                v-model="basicForm.updateByName"
                size="small"
                disabled
                clearable
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
        <el-col :span="1.5">
            <el-form-item label="最后修改时间">
              <el-input
                v-model="basicForm.updateTime"
                size="small"
                disabled
                clearable
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
      </el-row>
    </el-form>

    <div class="btn_group">
      <!-- <el-col :span="1.5">
        <el-button type="primary" size="small" plain @click="copy" v-if="sonPageStu == 'check'">复制</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="primary" size="small" plain @click="save" v-if="sonPageStu == 'add' || sonPageStu == 'edit'">保存</el-button>
      </el-col>
      <el-col :span="1.5" style="margin: 0 10px;">
        <el-button type="primary" size="small" plain @click="submit" v-if="sonPageStu == 'check' && (row.status == '0' || row.status == '3')">提交</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button size="small" plain @click="back">返回</el-button>
      </el-col>
    </div>

    <Refers ref="refer" @doSubmit="selectionsToInput" :single="true"/>
    <TreeRefers ref="tree" @doSubmit="selectionsToInput2" :single="true"/>
    <popDialog ref="materialRefer" @doSubmit="selectMaterial" :single="true" />
    <batchRefers ref="batchRefer" @doSubmit="selectBatch" :single="true"/>
  </div>
</template>

<script>
import {addOrder, getOrderDetail, editOrder, submitOrder} from '@/api/purchase/transferOrder.js'
import Refers from '@/components/Refers/refers.vue'
// 用于回显参照框数据
import { getRefer } from '@/api/purchase/basic.js'
import TreeRefers from '@/components/Refers/treeRefer.vue'
// 明细行选择物料参照
import popDialog from '@/components/PopDialog/index.vue'
// 明细行选择批次号
import batchRefers from '@/components/Refers/batchRefer.vue'

export default {
  name: 'addOder',
  dicts: ['sys_allot_billtype', 'sys_project_group', 'sys_status', 'sys_to_type'],
  props: ['pageStu','row', 'disable'],
  components: {
    Refers,
    TreeRefers,
    popDialog,
    batchRefers
  },
  model: {
    prop: 'isList',
    event: 'jugislist'
  },
  data() {
    return {
      // 不能直接改变props传来的值
      sonPageStu: this.pageStu,
      sonDisable: this.disable,
      basicForm: {
        deliveryInventoryOrg: '',
        deliveryInventoryOrgCode: '',
        deliveryInventoryOrgName: '',
        billType: '',
        code: '',
        billDate: '',
        allotType: '',
        deliveryCode: '',
        storageCode: '',
        storageInventoryOrg: '',
        storageInventory: '',
        storageInventoryOrgName: '',
        businessPersonal: '',
        businessPersonalName: '',
        deliveryDept: '',
        deliveryDeptName: '',
        currency: '',
        currencyName: '',
        rate: 1,
        onRouteAffilliation: '',
        qty: '',
        accountPath: '',
        customer: '',
        customerName: '',
        deliveryWarehouse: '',
        deliveryWarehouseCode: '',
        deliveryWarehouseName: '',
        storageWarehouse: '',
        storageWarehouseName: '',
        materialProject: '',
        liacenter: '',
        liacenterName: '',
        sourceIdentification: '',
        isSendWms: '',
        status: '0',
        tenantId: '',
        revision: '',
        createByName: '',
        updateByName: '',
        delFlag: '',
      },
      referCondition: {
        type: '',
        isPage: true,
        title: '',
        // 仓库编码
        stordocId: ''
      },
      referConditionMx: {
        orgCode: '',
        materialCode: '',
        unitCode: '',
        warehouseCode: ''
      },
      options: [{
        value: 'Y', label: '是',
      }, {
        value: 'N', label: '否'
      }],
      tableIndex: null,
      chuOrgOptions: [],
      ruOrgOptions:[],
      manOptions: [],
      deptOptions: [],
      currencyOptions: [],
      ruHouseOptions: [],
      chuHouseOptions: [],
      liacenterOptions: [],
      basicRules: {},
      materialInfo: [],
      receiveInfo: [],
      priceList: [],
      resultList: [],
      pickerOptionsEnd: {
        disabledDate: (time) => {
          let beginDateVal = this.basicForm.billDate;
          if (beginDateVal) {
            return (
              time.getTime() <
              new Date(beginDateVal).getTime()
            );
          }
        }
      }
    }
  },
  created() {
    if(this.pageStu == 'check') {
      console.log('数据', this.row)
      this.getDetails(this.row)
    } else if(this.pageStu == 'edit') {
      this.getDetails(this.row)
    }
  },
  methods: {
    // 输入数量或者主数量同步改变
    getZSL(scope) {
      scope.row.qty = scope.row.mainQty
    },
    getSL(scope) {
      scope.row.mainQty = scope.row.qty
    },
    copy() {

    },
    submit() {
      this.basicForm.stAllotMaterialList = this.materialInfo
      this.basicForm.stAllotReceiveDeliverList = this.receiveInfo
      this.basicForm.stAllotReceiveExecuteList = this.priceList
      this.basicForm.stAllotReceiveExpenseList = this.resultList
      submitOrder(this.basicForm).then(res => {
        if (res.code === 200) {
          this.$modal.msgSuccess("提交成功");
          this.back()
        }
      })
    },
    save() {
      if(this.pageStu == 'add') {
        this.basicForm.stAllotMaterialList = this.materialInfo
        this.basicForm.stAllotReceiveDeliverList = this.receiveInfo
        this.basicForm.stAllotReceiveExecuteList = this.priceList
        this.basicForm.stAllotReceiveExpenseList = this.resultList
        addOrder(this.basicForm).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess("保存成功");
            this.back()
          }
        })
      } else if (this.pageStu == 'edit') {
        this.basicForm.stAllotMaterialList = this.materialInfo
        this.basicForm.stAllotReceiveDeliverList = this.receiveInfo
        this.basicForm.stAllotReceiveExecuteList = this.priceList
        this.basicForm.stAllotReceiveExpenseList = this.resultList
        editOrder(this.basicForm).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess("编辑成功");
            this.back()
          }
        })
      }
    },
    back() {
      this.$emit('jugislist', true)
      let queryParams = {
        pageNum: 1,
        pageSize: 5
      }
      this.$emit('refresh', queryParams)
    },
    // 如果需要回显则调用详情接口
    getDetails(row) {
      getOrderDetail(row.id).then(res => {
        if (res.code === 200) {
          this.basicForm = res.data
          this.materialInfo = res.data.stAllotMaterialList
          this.receiveInfo = res.data.stAllotReceiveDeliverList
          this.priceList = res.data.stAllotReceiveExecuteList
          this.resultList = res.data.stAllotReceiveExpenseList
          if(this.basicForm.deliveryInventoryOrg) { this.reBackRefer('ORG_PARAM', this.basicForm.deliveryInventoryOrg, '调出库存组织') }
          if(this.basicForm.storageInventoryOrg) { this.reBackRefer('ORG_PARAM', this.basicForm.storageInventoryOrg, '调入库存组织') }
          if(this.basicForm.businessPersonal) { this.reBackRefer('CONTACTS_PARAM', this.basicForm.businessPersonal) }
          if(this.basicForm.deliveryDept) { this.reBackRefer('DEPT_PARAM', this.basicForm.deliveryDept) }
          if(this.basicForm.currency) { this.reBackRefer('CURRENCY_PARAM', this.basicForm.currency) }
          if(this.basicForm.storageWarehouse) { this.reBackRefer('WAREHOUSE_PARAM', this.basicForm.storageWarehouse, '调入仓库') }
          if(this.basicForm.deliveryWarehouse) { this.reBackRefer('WAREHOUSE_PARAM', this.basicForm.deliveryWarehouse, '调出仓库') }
          if(this.basicForm.liacenter) { this.reBackRefer('LIACENTER_PARAM', this.basicForm.liacenter) }
        }
      })
    },
    // 回显参照框
    reBackRefer(type, id, title) {
      getRefer({ type: type, id: id, title: title }).then(res => {
        if (type == 'ORG_PARAM' && title == '调出库存组织') {
          this.chuOrgOptions = res.rows
        }
        if (type == 'ORG_PARAM' && title == '调入库存组织') {
          this.ruOrgOptions = res.rows
        }
        if (type == 'CONTACTS_PARAM') {
          this.manOptions = res.rows
        }
        if (type == 'DEPT_PARAM') {
          this.deptOptions = res.rows
        }
        if (type == 'CURRENCY_PARAM') {
          this.currencyOptions = res.rows
        }
        if (type == 'LIACENTER_PARAM') {
          this.liacenterOptions = res.rows
        }
        if (type == 'WAREHOUSE_PARAM' && title == '调入仓库') {
          this.ruHouseOptions = res.rows
        }
        if (type == 'WAREHOUSE_PARAM' && title == '调出仓库') {
          this.chuHouseOptions = res.rows
        }
        if (type == 'UNIT_PARAM') {
          this.materialInfo[this.tableIndex].unitCode = res.rows[0].code
        }
      })
    },
    addLine() {
      const newLine = {
        rowno: null,
        demandCode: null,
        material: null,
        materialCode: null,
        materialName: null,
        deliveryCode: null,
        storageCode: null,
        specification: null,
        manufacturer: null,
        originPlace: null,
        originPlaceName: null,
        model: null,
        unit: null,
        unitCode: null,
        unitName: null,
        deliveryWarehouseName: null,
        qty: null,
        mainUnit: null,
        mainUnitName: null,
        equation: '1/1',
        mainQty: null,
        rate: '0.00',
        storageWarehouseName: null,
        deliveryAllocationName: null,
        deliveryAllocation: null,
        storageDept: null,
        storageDeptName: null,
        storageAllocationName: null,
        storageAllocation: null,
        patchNo: null,
        producBatch: null,
        manufactureDate: null,
        periodEndDate: null,
        allotDate: null,
        ratifyCode: null,
        registration: null,
        productCode: null,
        commonCode: null,
        drug: null,
        ruleDetail: null,
        marketingApprovalPersonal: null,
        production: null,
        pi: null,
        classify: null,
        customerLogistic: null,
        customerLogisticName: null
      }
      this.materialInfo.push(newLine)
    },
    delLine(index) {
      console.log('删除行:', index)
      this.materialInfo.splice(index, 1)
    },
    chooseRefer(type, isPage, title, pkOrg, isDirectStore, gubFlag) {
      this.referCondition.type = type
      this.referCondition.isPage = isPage
      this.referCondition.title = title
      this.referCondition.pkOrg = pkOrg
      this.referCondition.isDirectStore = isDirectStore
      this.referCondition.gubFlag = gubFlag
      this.$refs.refer.init(this.referCondition)
    },
    selectionsToInput(selection) {
      if (this.referCondition.title == '调出库存组织') {
        this.chuOrgOptions = selection
        this.basicForm.deliveryInventoryOrg = selection[0].id
        this.basicForm.deliveryInventoryOrgCode = selection[0].code
        this.basicForm.deliveryInventoryOrgName = selection[0].name
      }
      if (this.referCondition.title == '调入库存组织') {
        this.ruOrgOptions = selection
        this.basicForm.storageInventoryOrg = selection[0].id
        this.basicForm.storageInventory = selection[0].code
        this.basicForm.storageInventoryOrgName = selection[0].name
        this.basicForm.onRouteAffilliation = selection[0].name
        // 选择调入库存组织时判断调拨方式
        if (this.basicForm.storageInventoryOrg == this.basicForm.deliveryInventoryOrg) {
          this.basicForm.allotType = '4'
        } else {
          this.basicForm.allotType = '2'
        }
      }
      if (this.referCondition.title == '调出业务员') {
        this.manOptions = selection
        this.basicForm.businessPersonal = selection[0].code
        this.basicForm.businessPersonalName = selection[0].name
      }
      if (this.referCondition.title == '调出部门') {
        this.deptOptions = selection
        this.basicForm.deliveryDept = selection[0].id
        this.basicForm.deliveryDeptName = selection[0].name
      }
      if (this.referCondition.title == '币种') {
        this.currencyOptions = selection
        this.basicForm.currency = selection[0].id
        this.basicForm.currencyName = selection[0].name
      }
      if (this.referCondition.title == '调入仓库') {
        this.ruHouseOptions = selection
        this.basicForm.storageWarehouse = selection[0].id
        this.basicForm.storageWarehouseName = selection[0].name
      }
      if (this.referCondition.title == '调出仓库') {
        this.chuHouseOptions = selection
        this.basicForm.deliveryWarehouse = selection[0].id
        this.basicForm.deliveryWarehouseCode = selection[0].code
        this.basicForm.deliveryWarehouseName = selection[0].name
      }
      if (this.referCondition.title == '调出货位') {
        this.materialInfo[this.tableIndex].deliveryAllocationName = selection[0].name
        this.materialInfo[this.tableIndex].deliveryAllocation = selection[0].id
      }
      if (this.referCondition.title == '调入货位') {
        this.materialInfo[this.tableIndex].storageAllocationName = selection[0].name
        this.materialInfo[this.tableIndex].storageAllocation = selection[0].id
      }
      if (this.referCondition.title == '调入部门') {
        this.materialInfo[this.tableIndex].storageDeptName = selection[0].name
        this.materialInfo[this.tableIndex].storageDept = selection[0].id
      }
    },
    chooseTreeRefer(type, isPage, title) {
      this.referCondition.type = type
      this.referCondition.isPage = isPage
      this.referCondition.title = title
      this.$refs.tree.init(this.referCondition)
    },
    selectionsToInput2(selection) {
      this.liacenterOptions.push(selection)
      this.basicForm.liacenter = selection.id
      this.basicForm.liacenterName = selection.name
    },
    // 明细行选择物料编码带出数据
    chooseMaterial(index) {
      console.log("🚀 ~ file: add.vue:790 ~ chooseMaterial ~ index:", index)
      this.tableIndex = index
      this.$refs.materialRefer.init()
    },
    selectMaterial(selection) {
      console.log('选中的物料', selection)
      this.materialInfo[this.tableIndex].material = selection[0].id
      this.materialInfo[this.tableIndex].materialCode = selection[0].code
      this.materialInfo[this.tableIndex].materialName = selection[0].name
      this.materialInfo[this.tableIndex].specification = selection[0].specification
      this.materialInfo[this.tableIndex].model = selection[0].model
      this.materialInfo[this.tableIndex].originPlace = selection[0].originPlace
      this.materialInfo[this.tableIndex].originPlaceName = selection[0].originPlaceName
      this.materialInfo[this.tableIndex].unit = selection[0].unitId
      this.materialInfo[this.tableIndex].mainUnit = selection[0].unitId
      this.materialInfo[this.tableIndex].unitName = selection[0].unitIdName
      this.materialInfo[this.tableIndex].mainUnitName = selection[0].unitIdName
      this.materialInfo[this.tableIndex].manufacturer = selection[0].manufacturerIdName
      this.materialInfo[this.tableIndex].marketingApprovalPersonal = selection[0].registrant
      this.materialInfo[this.tableIndex].production = selection[0].productionPermit
      // 根据物料单位id查询单位code
      this.reBackRefer('UNIT_PARAM', selection[0].unitId)
    },
    // 明细行选择批次号
    chooseBatch(index) {
      this.tableIndex = index
      this.referConditionMx.orgCode = this.basicForm.deliveryInventoryOrgCode
      this.referConditionMx.materialCode = this.materialInfo[this.tableIndex].materialCode
      this.referConditionMx.unitCode = this.materialInfo[this.tableIndex].unitCode
      this.referConditionMx.warehouseCode = this.basicForm.deliveryWarehouseCode
      this.$refs.batchRefer.init(this.referConditionMx)
    },
    selectBatch(selection) {
      console.log('选中的批次号', selection)
    },
    // 明细行选择货位
    chooseMxHW(index, type, isPage, title, stordocId) {
      this.tableIndex = index
      this.referCondition.type = type
      this.referCondition.isPage = isPage
      this.referCondition.title = title
      this.referCondition.stordocId = stordocId
      this.$refs.refer.init(this.referCondition)
    },
    // 明细行选择调入部门
    chooseMxBM(index, type, isPage, title, pkOrg) {
      this.tableIndex = index
      this.referCondition.type = type
      this.referCondition.isPage = isPage
      this.referCondition.title = title
      this.referCondition.pkOrg = pkOrg
      this.$refs.refer.init(this.referCondition)
    },
  }
}
</script>

<style lang="scss" scoped>
.btn_group {
  width: 100%;
  margin: 20px 0;
  display: flex;
  justify-content: center;
} 
.btn_grooup {
  margin-bottom: 10px;
  display: flex;
  justify-content: flex-end;
}
.hang {
  margin: auto;
}
.hang ::v-deep .el-form-item__content{
  margin-left: 0px !important;
}
</style>