<template>
  <div>
    <el-card style="margin: 5px;margin-top: -20px">
      <el-form :model="formData" ref="dataForm" label-width="100px" style="margin-bottom: -15px">
        <el-button-group style="display: flex;margin: 0px 0 10px 80%; height: 35px">
          <el-button
              type="primary"
              @click.native="saveDeviceReceiveOrder()"
          >草稿保存
          </el-button>
          <el-button
              type="primary"
              @click.native="submitAudit()"
          >提交审核
          </el-button>
        </el-button-group>

        <el-row>
          <el-col :span="10">
            <el-form-item class="query-form-item" label="登记记录号:">
              <el-input v-model="formData.orderId"
                        auto-complete="off"
                        :disabled="true"
                        style="width: 90%"
                        clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item class="query-form-item" label="登记时间:">
              <el-input v-model="formData.createTime"
                        auto-complete="off"
                        :disabled="true"
                        style="width: 90%"
                        clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item class="query-form-item" label="登记部门:">
              <el-select v-model="formData.fromDeptCode" placeholder="请选择登记部门信息"
                         :disabled="detailList.length >0 && this.paramValue == 0 "
                         style="width: 90%;"
                         disabled
                         clearable>
                <el-option
                    v-for="item in deptList"
                    :key="item.name"
                    :label="item.name"
                    :value="item.code">
                  <span style="float: left">{{ item.name }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.parentName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="10" v-if="paramValue!=2">
            <el-form-item class="query-form-item" label="设备来源:">
              <el-select v-model="formData.type" placeholder="请选择设备来源信息"
                         :disabled="detailList.length >0"
                         style="width: 90%"
                         clearable>
                <el-option label="库存设备" :value=1></el-option>
                <el-option label="科室设备" :value=2></el-option>
                <el-option label="其他" :value=3></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="15">
            <el-form-item class="query-form-item" label="扫码录入:">
              <el-input
                  id="inputer"
                  @focus="getInputFocus($event)"
                  @keypress.enter.native="enterKey($event)"
                  ref='inputRef'
                  style="ime-mode:disabled"
                  type="tel"
                  v-model="formData.code"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button
                type="primary"
                size="mini"
                @click.native.stop="addCode()"
                style="margin-left: 15px"
            >扫码添加
            </el-button>
          </el-col>
          <el-col :span="4">
            <el-button-group style="display: flex;margin-right: 20px;float: right;">
              <el-button type="primary" @click.native.stop="selectInvProduct(true)" :loading="loading">选入设备
              </el-button>
            </el-button-group>
          </el-col>
        </el-row>


        <el-table v-loading="loading" :data="detailList" style="width: 100%;margin-top: 20px;"
                  highlight-current-row
                  border max-height="300" height="300">
          <el-table-column label="序号" type="index" width="50"></el-table-column>
          <!--          <el-table-column label="条码" width="180" prop="originCode" show-overflow-tooltip></el-table-column>-->
          <el-table-column width="150" label="设备名称" prop="productName" show-overflow-tooltip></el-table-column>
          <el-table-column width="150" label="规格型号" prop="ggxh" show-overflow-tooltip></el-table-column>
          <el-table-column width="150" label="DI/物资编码" prop="nameCode" show-overflow-tooltip></el-table-column>
          <el-table-column width="150" label="批次号" prop="batchNo" v-if="paramValue != 2" show-overflow-tooltip></el-table-column>
          <el-table-column width="150" label="生产日期" prop="productionDate" v-if="paramValue != 2" show-overflow-tooltip></el-table-column>
          <el-table-column width="150" label="失效日期" prop="expireDate" v-if="paramValue != 2" show-overflow-tooltip></el-table-column>
          <el-table-column width="120" label="批次号" v-if="paramValue == 2">
            <template slot-scope="scope">
              <el-input v-model="scope.row.batchNo"
                        :disabled="scope.row.id != selectedIndex"
                        ref='inputBatchNoRef'
                        placeholder="请输入批次号"></el-input>
              <!--@input="scope.row.batchNo =scope.row.batchNo.replace(/[\W]/g, '')"-->
            </template>
          </el-table-column>
          <el-table-column width="120" label="生产日期" v-if="paramValue == 2">
            <template slot-scope="scope">

              <el-input v-model="scope.row.productionDate" auto-complete="off"
                        oninput="if(value.length>6)value=value.slice(0,6)"
                        :disabled="scope.row.id != selectedIndex"
                        type="number"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column width="120" label="失效日期" v-if="paramValue == 2">
            <template slot-scope="scope">

              <el-input v-model="scope.row.expireDate" auto-complete="off"
                        oninput="if(value.length>6)value=value.slice(0,6)"
                        :disabled="scope.row.id != selectedIndex"
                        type="number"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="生产企业" prop="ylqxzcrbarmc" show-overflow-tooltip></el-table-column>
          <el-table-column label="注册/备案号" prop="zczbhhzbapzbh" show-overflow-tooltip></el-table-column>
          <el-table-column label="供应商" prop="supName" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click.stop="true"
                         @click.native="deleteOrderDetail(scope.$index, scope.row)">删除
              </el-button>
              <el-button type="text" size="small" v-if="scope.row.id == selectedIndex && paramValue == 1"
                         @click.stop="true" @click.native="updateOrderDetail(scope.row)">保存
              </el-button>
              <el-button type="text" size="small" v-if="scope.row.id != selectedIndex && paramValue == 1"
                         @click.stop="true" @click.native="uploadOrderDetail(scope.row)">编辑
              </el-button>
            </template>
          </el-table-column>
        </el-table>

      </el-form>
    </el-card>


    <el-dialog
        title="库存产品录入"
        :visible.sync="selectInvProductVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="80%"
        v-if="selectInvProductVisible"
        :append-to-body='true'
    >
      <el-form label-width="100px" :model="filterQuery" size="mini" v-show="showSearch">
        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="DI/物资编码:">
              <el-input v-model="filterQuery.nameCode" style="width: 90%" placeholder="请输入DI/物资编码"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="设备名称:">
              <el-input v-model="filterQuery.cpmctymc" style="width: 90%" placeholder="请输入设备名称"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="规格型号:">
              <el-input v-model="filterQuery.ggxh" style="width: 90%" placeholder="请输入规格型号" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="生产企业:">
              <el-input v-model="filterQuery.manufactory" style="width: 90%" placeholder="请输入生产企业"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="注册/备案号:">
              <el-input v-model="filterQuery.zczbhhzbapzbh" style="width: 90%" placeholder="请输入注册/备案号"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="供应商:">
              <el-input v-model="filterQuery.supName" style="width: 90%" placeholder="请输入供应商"
                        clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="仓库:">
              <el-select v-model="filterQuery.invCode" placeholder="请选择设备来源信息" style="width: 90%" clearable>
                <el-option
                    v-for="item in invList"
                    :key="item.name"
                    :label="item.name"
                    :value="item.code">
                  <span style="float: left">{{ item.name }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.code }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="top-right-btn">
        <el-button-group>
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="queryInvProductsList">查询</el-button>
          <el-button type="primary" icon="search" @click="combine">确定</el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>


      <el-table v-loading="loading" :data="productList" style="width: 100%" highlight-current-row="false"
                :row-key="getRowKeys"
                @selection-change="handleSelectionChange" border
                ref="multipleTable">
        <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="设备名称" prop="cpmctymc" width="200"></el-table-column>
        <el-table-column label="规格型号" prop="ggxh" width="200"></el-table-column>
        <el-table-column label="DI/物资编码" prop="nameCode" width="120"></el-table-column>
        <el-table-column label="UDI码" prop="code" width="350"></el-table-column>
        <el-table-column label="批次号" prop="batchNo" width="120"></el-table-column>
        <el-table-column label="生产日期" prop="produceDate" width="120"></el-table-column>
        <el-table-column label="失效日期" prop="expireDate" width="120"></el-table-column>
        <el-table-column label="序列号" prop="serialNo" width="150"></el-table-column>
        <el-table-column label="生产企业" prop="manufactory" width="200"></el-table-column>
        <el-table-column label="注册/备案号" prop="zczbhhzbapzbh" width="200"></el-table-column>
        <el-table-column label="供应商" prop="supName" width="260"></el-table-column>
      </el-table>

      <pagination
          v-show="productTotal>0"
          :total="productTotal"
          :limit.sync="filterQuery.limit"
          :page.sync="filterQuery.page"
          @pagination="queryInvProducts"
      ></pagination>
    </el-dialog>

    <el-dialog
        title="设备登记-物资字典录入"
        :visible.sync="selectProductVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="85%"
        v-if="selectProductVisible"
        :append-to-body='true'
    >
      <el-form label-width="100px" :model="filterProductQuery" size="mini" v-show="showSearch">
        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="DI/物资编码:">
              <el-input v-model="filterProductQuery.nameCode" style="width: 90%" placeholder="请输入DI/物资编码"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="设备名称:">
              <el-input v-model="filterProductQuery.cpmctymc" style="width: 90%" placeholder="请输入设备名称"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="规格型号:">
              <el-input v-model="filterProductQuery.ggxh" style="width: 90%" placeholder="请输入规格型号"
                        clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="生产企业:">
              <el-input v-model="filterProductQuery.manufactory" style="width: 90%" placeholder="请输入生产企业"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="注册/备案号:">
              <el-input v-model="filterProductQuery.zczbhhzbapzbh" style="width: 90%" placeholder="请输入注册/备案号"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="供应商:">
              <el-input v-model="filterProductQuery.companyName" style="width: 90%" placeholder="请输入供应商"
                        clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="top-right-btn">
        <el-button-group>
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onProductReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="queryProductsList">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="combine">添加</el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>
      <el-table v-loading="loading" :data="udiProductList" style="width: 100%" highlight-current-row border
                :row-key="getRowKeys"
                @selection-change="handleSelectionChange" ref="multipleTable">
        <el-table-column type="selection" :reserve-selection="true" width="55">
        </el-table-column>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="设备名称" prop="cpmctymc" width="300"></el-table-column>
        <el-table-column label="规格型号" prop="ggxh" width="300"></el-table-column>
        <el-table-column label="DI/物资编码" prop="nameCode" width="300"></el-table-column>
        <el-table-column label="生产企业" prop="manufactory" width="300"></el-table-column>
        <el-table-column label="注册/备案号" prop="zczbhhzbapzbh" width="300"></el-table-column>
        <el-table-column label="供应商" prop="companyName" width="300"></el-table-column>
      </el-table>

      <pagination
          v-show="udiProductTotal>0"
          :total="udiProductTotal"
          :limit.sync="filterProductQuery.limit"
          :page.sync="filterProductQuery.page"
          @pagination="queryProductsList"
      ></pagination>
    </el-dialog>


    <el-dialog
        title="设备登记-科室设备录入"
        :visible.sync="selectDeviceVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="85%"
        v-if="selectDeviceVisible"
        :append-to-body='true'
    >
      <el-form label-width="100px" :model="filterDeviceQuery" size="mini" v-show="showSearch">
        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="DI/物资编码:">
              <el-input v-model="filterDeviceQuery.nameCode" style="width: 90%" placeholder="请输入DI/物资编码"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="设备名称:">
              <el-input v-model="filterDeviceQuery.deviceName" placeholder="请输入设备名称" style="width: 90%"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="规格型号:">
              <el-input v-model="filterDeviceQuery.ggxh" style="width: 90%" placeholder="请输入规格型号"
                        clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="生产企业:">
              <el-input v-model="filterDeviceQuery.manufactory" style="width: 90%" placeholder="请输入生产企业"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="注册/备案号:">
              <el-input v-model="filterDeviceQuery.zczbhhzbapzbh" style="width: 90%" placeholder="请输入注册/备案号"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="供应商:">
              <el-input v-model="filterDeviceQuery.supName" placeholder="请输入供应商" style="width: 90%"
                        clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="top-right-btn">
        <el-button-group>
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onDeviceReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="queryDeviceList">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="combine">添加</el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>
      <el-table v-loading="loading" :data="deviceList" style="width: 100%" highlight-current-row border
                :row-key="getRowKeys"
                @selection-change="handleSelectionChange" ref="multipleTable">
        <el-table-column type="selection" :reserve-selection="true" width="55">
        </el-table-column>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="登记部门" prop="deptName" width="180"></el-table-column>
        <el-table-column label="设备名称" prop="deviceName" width="200"></el-table-column>
        <el-table-column label="规格型号" prop="ggxh" width="300"></el-table-column>
        <el-table-column label="DI/物资编码" prop="nameCode" width="300"></el-table-column>
        <el-table-column label="生产企业" prop="manufactory" width="300"></el-table-column>
        <el-table-column label="注册/备案号" prop="zczbhhzbapzbh" width="300"></el-table-column>
        <el-table-column label="供应商" prop="supName" width="300"></el-table-column>
      </el-table>

      <pagination
          v-show="udiDeviceTotal>0"
          :total="udiDeviceTotal"
          :limit.sync="filterDeviceQuery.limit"
          :page.sync="filterDeviceQuery.page"
          @pagination="queryProductsList"
      ></pagination>
    </el-dialog>


  </div>
</template>

<script>
import {getDeptListByUser} from "@/api/auth/authDept";
import {filterSubAll, findInvByUser} from "@/api/system/invSubWarehouse";
import {getInvSpaceList} from "@/api/inventory/invSpace";
import {
  addDeviceReceiveOrder,
  addDeviceReceiveOrderDetail,
  updateDeviceReceiveOrder,
  updateDeviceReceiveOrderDetail,
  getDeviceReceiveOrderDetailList,
  deleteDeviceReceiveOrderDetail,
  checkCode,
  getConfig, selectProduct, submitAudit
} from "@/api/dev/deviceReceiveOrder";
import {getInvMAProducts} from "@/api/inventory/invPorduct";
import {isBlank} from "@/utils/strUtil";
import {listDeptUser} from "@/api/system/deptUser";
import {getDeptDeviceList} from "@/api/dev/deptDevice";

export default {
  props: {
    deviceReceiveOrder: {
      type: Object,
      required: true,
    },
    closeDialog: {
      type: Function,
      required: true,
    },
  },

  data() {
    return {
      filterQuery: {
        nameCode: null,
        cpmctymc: null,
        batchNo: null,
        invCode: null,
        // cplb: "设备",    //todo 由后台或者前端加上，只查询设备
        invSpaceCode: null,
        page: 1,
        limit: 10
      },
      productList: [],
      udiProductList: [],
      productTotal: 0,
      udiProductTotal: 0,
      deptList: [],
      invList: [],
      spaceList: [],
      formData: {
        id: null,
        orderId: null,
        createTime: null,
        fromDeptCode: this.$store.getters.locDeptCode,
        fromInvCode: null,
        fromInvSpaceCode: null,
        receiveUser: null,
        status: 0,
        code: null,
      },
      filterProductQuery: {
        nameCode: null,
        cpmctymc: null,
        batchNo: null,
        invCode: null,
        // cplb: "设备",    //todo 由后台或者前端加上，只查询设备
        invSpaceCode: null,
        page: 1,
        limit: 10
      },
      detailList: [],
      paramValue: null,
      loading: false,
      selectProductVisible: false,
      scanText: "扫码录入：",
      currentIndex: -1,
      selectInvProductVisible: false,
      multiProduct: [],
      userList: [],
      productInvList: [],
      productSpaceList: [],
      currentRow: {},
      selectedIndex: null,
      showSearch: true,
      selectDeviceVisible: false,
      filterDeviceQuery: {
        nameCode: null,
        cpmctymc: null,
        batchNo: null,
        invCode: null,
        invSpaceCode: null,
        page: 1,
        limit: 10
      },
      udiDeviceTotal: 0,
      deviceList: [],
    }
  },
  methods: {
    getInvList() {
      filterSubAll().then((res) => {
        if (res.code === 20000) {
          this.invList = res.data || [];
          if (!isBlank(this.deviceReceiveOrder.fromInvSpaceCode)) {
            this.formData.fromInvSpaceCode = this.deviceReceiveOrder.fromInvSpaceCode;
            this.invChange();
          }
        }
      });
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    deptChange() {
      //查询此部门下的仓库和用户
      this.formData.fromInvCode = null;
      this.formData.fromInvSpaceCode = null;
      this.formData.receiveUser = null;
      this.invList = [];
      this.spaceList = [];
      this.userList = [];
      this.getInvList();
    },
    getDeptList() {
      getDeptListByUser().then((res) => {
        this.deptList = res.data || [];
        if (!isBlank(this.deviceReceiveOrder.fromInvCode)) {
          this.formData.fromInvCode = this.deviceReceiveOrder.fromInvCode;
          this.getInvList();
        }
      });
    },
    invChange() {
      this.formData.invSpaceCode = null;
      this.spaceList = [];
      let params = {invWarehouseCode: this.formData.fromInvCode, status: 1};
      getInvSpaceList(params).then((res) => {
        if (!isBlank(this.deviceReceiveOrder.fromInvSpaceCode)) {
          this.formData.fromInvSpaceCode = this.deviceReceiveOrder.fromInvSpaceCode;
        }
        this.spaceList = res.data.list || [];
      })
      this.formData.fromInvSpaceCode = "";
    },
    selectInvProduct(obj) {
      if (this.formData.type == 1) {
        //库存
        let message = this.verifyOrder(obj);
        if (!isBlank(message)) {
          this.$message.warning(message);
          return;
        }
        //保存或更新设备登记仓库信息
        this.saveOrder();
        this.onReset();
        this.selectInvProductVisible = true;
        this.getInvList();
      } else if (this.formData.type == 2) {
        //科室
        //保存或更新设备登记仓库信息
        this.saveOrder();
        this.onDeviceReset();
        this.selectDeviceVisible = true;
      } else if (this.formData.type == 3) {
        //物资
        //保存或更新设备登记仓库信息
        this.saveOrder();
        this.onProductReset();
        this.selectProductVisible = true;
      }
    },
    saveMAOrder() {
      this.saveOrder();
      this.closeDialog();
    },
    saveOrder(code) {
      if (!isBlank(this.formData.orderId)) {
        //更新单据
        updateDeviceReceiveOrder(this.formData).then((res) => {
        }).catch((error) => {
          this.$message.error("更新单据信息失败！");
        })
      } else {
        addDeviceReceiveOrder(this.formData).then((res) => {
          if (res.code === 20000) {
            this.formData.id = res.data.id;
            this.formData.orderId = res.data.orderId;
            this.formData.createTime = res.data.createTime;
            if (!isBlank(code)) {
              //扫码添加设备登记记录详情
              if (!isBlank(this.formData.code)) {
                this.addScanCode();
              }
            }
          } else {
            this.$message.error(res.message);
          }
        });
      }
    },

    saveDeviceReceiveOrder() {
      if (this.paramValue == 0) {
        var message = this.verifyOrder()
      } else {
        var message = this.verifyProductOrder()
      }
      if (isBlank(message)) {
        this.saveOrder();
        this.closeDialog();
      } else {
        this.$message.warning(message);
      }
    },

    getInputFocus(event) {
      event.currentTarget.select();
    },
    enterKey(event) {
      this.addCode();
      this.$refs.inputRef.focus();
      this.$refs.inputRef.select();
    },
    addCode() {
      if(isBlank(typeof this.formData)){
        this.$message.warning("请先选择来源类型");
        return;
      }

      let message = this.verifyOrder(1);
      if (!isBlank(message)) {
        this.$message.warning(message);
        return;
      }

      if (isBlank(this.formData.code)) {
        this.$message.warning("请扫描或输入UDI码");
        return;
      }

      if (isBlank(this.formData.orderId)) {
        this.saveOrder(this.formData.code);
      } else {
        this.addScanCode();
      }

    },
    verifyOrder(obj) {
      if (isBlank(this.formData.fromDeptCode)) {
        return "请选择登记部门！";
      } else if (isBlank(obj) && (this.detailList == null || this.detailList.length == 0)) {
        return "请选择设备！";
      }
    },
    verifyProductOrder() {
      if (this.detailList == null || this.detailList.length == 0) {
        return "请选择设备！";
      }
    },
    submitAudit() {
      let params = {id: this.formData.id};
      submitAudit(params).then((res) => {
        if (res.code === 20000) {
          this.$message.success("提交成功！");
          this.closeDialog();
        } else {
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.$message.error(error.message);
      });
    },
    addScanCode() {
      //校验条码是否符合规则
      let checkParams = {
        code: this.formData.code
      };
      checkCode(checkParams).then((res) => {
        if (res.code === 20000) {
          this.addDeviceReceiveOrderDetail();
        } else {
          if (res.code === 3) {
            this.$message.error(res.message);
          } else if (res.code === 7) {
            this.$confirm(res.message + "，是否继续添加？", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              this.addDeviceReceiveOrderDetail();
            }).catch(() => {
            });
          }
        }
      }).catch((error) => {
      });
    },
    addDeviceReceiveOrderDetail() {
      //扫码添加库存养护记录详情
      let params = {
        type: this.formData.type,
        orderIdFk: this.formData.orderId,
        code: this.formData.code
      }
      addDeviceReceiveOrderDetail(params).then((res) => {
        if (res.code === 20000) {
          this.$message.success("添加成功！");
          this.getList();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    deleteOrderDetail(index, row) {
      if (row.count === 1) {
        this.$confirm('此操作将删除此产品登记记录，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = {id: row.id};
          deleteDeviceReceiveOrderDetail(params).then((res) => {
            if (res.code === 20000) {
              this.$message.success("删除成功");
              this.getList();
            }
          })
        }).catch(() => {
        });
      } else {
        let params = {id: row.id};
        deleteDeviceReceiveOrderDetail(params).then((res) => {
          if (res.code === 20000) {
            this.$message.success("删除成功");
            this.getList();
          }
        })
      }
    },
    uploadOrderDetail(val) {
      this.currentRow = val;
      this.selectedIndex = val.id;
      console.log(this.selectedIndex)
    },
    saveOrderDetail(val) {

    },
    getList() {
      let params = {orderIdFk: this.formData.orderId};
      getDeviceReceiveOrderDetailList(params).then((res) => {
        if (res.code === 20000) {
          this.detailList = res.data.list || [];
        }
      })
    },
    updateOrderDetail(row) {
      updateDeviceReceiveOrderDetail(row).then((res) => {
        if (res.code === 20000) {
          this.$message.success("保存成功！")
          this.selectedIndex = null
          this.getList();
        }
      })
    },
    editOrderDetail(index, row) {
      this.currentIndex = index;
    },
    onReset() {
      this.filterQuery = {
        nameCode: null,
        cpmctymc: null,
        batchNo: null,
        invCode: this.formData.fromInvCode,
        // cplb: "设备",
        invSpaceCode: null,
        page: 1,
        limit: 10
      };
      this.multiProduct = [];
      this.queryInvProducts();
    },
    onProductReset() {
      this.filterProductQuery = {
        nameCode: null,
        cpmctymc: null,
        batchNo: null,
        // cplb: "设备",
        invSpaceCode: null,
        page: 1,
        limit: 10
      };
      this.multiProduct = [];
      this.queryProductsList();
    },
    onDeviceReset() {
      this.filterDeviceQuery = {
        nameCode: null,
        cpmctymc: null,
        batchNo: null,
        invSpaceCode: null,
        page: 1,
        limit: 10
      };
      this.multiProduct = [];
      this.queryDeviceList();
    },
    handleCurrentChange(val) {
      this.filterQuery.page = val;
      this.queryInvProducts();
    },
    queryInvProductsList() {
      this.filterQuery.page = 1;
      this.queryInvProducts();
    },
    queryInvProducts() {
      getInvMAProducts(this.filterQuery).then((res) => {
        if (res.code === 20000) {
          this.productList = res.data.list || [];
          this.productTotal = res.data.total || 0;
        } else {
          this.$message.error(res.message);
          this.productList = [];
          this.productTotal = 0;
        }
      }).catch((error) => {
        this.$message.error(error.message);
        this.productList = [];
        this.productTotal = 0;
      });
    },
    queryProductsList() {
      selectProduct(this.filterProductQuery).then((res) => {
        if (res.code === 20000) {
          this.udiProductList = res.data.list || [];
          this.udiProductTotal = res.data.total || 0;
        } else {
          this.$message.error(res.message);
          this.udiProductList = [];
          this.udiProductTotal = 0;
        }
      }).catch((error) => {
        this.$message.error(error.message);
        this.udiProductList = [];
        this.udiProductTotal = 0;
      });
    },
    combine() {
      if (this.multiProduct.length === 0) {
        this.$message.error("请选择登记产品！");
        return;
      }
      let invProductIds = [];
        this.multiProduct.forEach(item => {
          invProductIds.push(item.id);
        });
      let params = {
        type: this.formData.type,
        orderIdFk: this.formData.orderId,
        invProductIds: invProductIds,
        invCode: this.formData.fromInvCode,
        invSpaceCode: this.formData.fromInvSpaceCode
      };
      addDeviceReceiveOrderDetail(params).then((res) => {
        if (res.code === 20000) {
          this.$message.success("录入成功！");
          this.selectInvProductVisible = false;
          this.selectProductVisible = false;
          this.selectDeviceVisible = false
          this.getList();
        } else {
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.$message.error(error.message);
      })
    },
    getRowKeys(row) {
      return row.id;
    },
    handleSelectionChange(selectedRows) {
      this.multiProduct = [];
      selectedRows.forEach(item => {
        this.multiProduct.push(item);
      });
    },
    queryDeviceList() {
      getDeptDeviceList(this.filterDeviceQuery).then((res) => {
        if (res.code === 20000) {
          this.deviceList = res.data.list || [];
          this.udiDeviceTotal = res.data.total || 0;
        } else {
          this.$message.error(res.message);
          this.deviceList = [];
          this.udiDeviceTotal = 0;
        }
      }).catch((error) => {
        this.$message.error(error.message);
        this.deviceList = [];
        this.udiDeviceTotal = 0;
      });
    }
  },
  created() {
    //查询配置项
    getConfig().then((res) => {
      if (res.code == 20000 && !isBlank(res.data.paramValue)) {
        this.paramValue = res.data.paramValue
      }
    })
    this.getDeptList();
    this.deptChange();
    if (!(isBlank(this.deviceReceiveOrder.orderId))) {
      this.formData.orderId = this.deviceReceiveOrder.orderId;
      this.formData.id = this.deviceReceiveOrder.id;
      this.formData.createTime = this.deviceReceiveOrder.createTime;
      this.formData.fromDeptCode = this.deviceReceiveOrder.fromDeptCode;
      this.formData.fromInvCode = this.deviceReceiveOrder.fromInvCode;
      this.formData.fromInvSpaceCode = this.deviceReceiveOrder.fromInvSpaceCode;
      this.formData.receiveUser = this.deviceReceiveOrder.receiveUser;
      this.formData.type = this.deviceReceiveOrder.type
      this.getList();
      this.getDeptUserList();
    }
  },
};
</script>

<style scoped>


#inputer {
  width: 100%;
  min-height: 30px;
  background-color: white;
  border: #d0d0d0;
  border-style: solid;
  border-width: 0.1px;
  color: #4a4a4a;

}

#inputer:focus {
  width: 100%;
  min-height: 30px;
  background-color: white;
  border: #0080FF;
  border-style: solid;
  border-width: 0.1px;
  color: #4a4a4a;
}

#inputer1 {
  width: 100%;
  min-height: 30px;
  background-color: white;
  border: #d0d0d0;
  border-style: solid;
  border-width: 0.1px;
  color: #4a4a4a;

}

#inputer1:focus {
  width: 100%;
  min-height: 30px;
  background-color: white;
  border: #0080FF;
  border-style: solid;
  border-width: 0.1px;
  color: #4a4a4a;
}
</style>

