<template>
  <div>
    <el-card style="margin: 5px;">
      <el-form
        :model="orderFormData"
        :rules="formRules"
        ref="dataForm"
        label-width="110px"
        style="margin-bottom: -15px"
      >
        <el-button-group
          style="display: flex; margin: 0px 0 15px 75%; height: 35px"
        >
          <el-button
            size="mini"
            type="primary"
            v-if="documentShow"
            @click.native="OptionalDocument()"
          >选入预验收库单
          </el-button>
          <el-button size="mini" type="primary" @click.native="saveOrder()"
          >草稿保存
          </el-button>
          <el-button size="mini" type="primary" @click.native="submit()"
                     v-if="(viewType != 5 && viewType != 6)&&(curAction.checkWebNew != 2  || viewType !=1)"
          >立即提交
          </el-button>
          <el-button size="mini" type="primary" @click.native="submit(1)"
                     v-if="curAction.checkWebNew > 1  && viewType == 1"
          >待配货提交
          </el-button>

          <el-button size="mini" type="primary" @click.native="submit(1)"
                     v-if="viewType == 5"
          >完成验收
          </el-button>

          <el-button size="mini" type="primary" @click.native="submit(1)"
                     v-if=" viewType == 6"
          >完成验收
          </el-button>

        </el-button-group>
        <el-row>
          <el-col :span="11">
            <el-form-item prop="corpOrderId" label="单据号:">
              <el-input
                v-model="orderFormData.corpOrderId"
                auto-complete="off"
                style="width: 90%"
                clearable
                :disabled="true"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="createTime" label="单据时间:">
              <el-date-picker
                v-model="orderFormData.createTime"
                type="datetime"
                placeholder="日期"
                clearable
                value-format="yyyy-MM-dd HH:mm:ss"
                :disabled="true"
                style="width: 90%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

          <el-col :span="11">
            <el-form-item prop="invCode" label="当前仓库:">
              <el-select
                v-model="orderFormData.invCode"
                placeholder="当前仓库"
                style="width: 90%"
                :disabled="corpOrderIdDisabled"
                @change="changeInv"
                filterable
              >
                <el-option
                  v-for="item in curInvOptions"
                  :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.warehouseName
                    }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item prop="action" label="单据类型:">
              <el-select
                v-model="orderFormData.action"
                placeholder="请选择单据类型"
                :disabled="corpOrderIdDisabled"
                style="width: 90%"
                @change="actionChange"
              >
                <el-option
                  v-for="item in busTypeOptions"
                  :key="item.name"
                  :label="item.name"
                  :value="item.action"
                >
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item
              prop="fromCorp"
              label="往来信息:"
              v-if="
                curAction.corpType === 0 ||
                curAction.corpType === 2 ||
                (curAction.corpType === 1 && !curAction.genUnit)
              "
            >
              <el-select
                v-model="orderFormData.fromCorp"
                filterable
                remote
                clearable
                reserve-keyword
                style="width: 90%"
                placeholder="请输入往来信息"
                :remote-method="findMethod"
                :loading="loading"
                :disabled="corpOrderIdDisabled"
              >
                <el-option
                  v-for="item in fromOptions"
                  :key="item.erpId"
                  :label="item.name"
                  :value="item.erpId"
                >
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              prop="fromCorp"
              v-if="curAction.corpType == 3"
              label="往来信息:"
            >
              <el-select
                v-model="orderFormData.fromInvCode"
                placeholder="请选择往来仓库"
                :disabled="corpOrderIdDisabled"
                filterable
                clearable
                style="width: 90%"
              >
                <el-option
                  v-for="item in fromInvOptions"
                  :key="item.code"
                  :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-form-item
              prop="fromCorp"
              label="往来信息:"
              v-if="curAction.corpType == 1 && curAction.genUnit"
            >
              <el-input
                v-model="orderFormData.fromCorp"
                auto-complete="off"
                clearable
                style="width: 90%"
                :disabled="corpOrderIdDisabled"
                placeholder="请输入病人住院号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="remark" label="备注:">
              <el-input
                v-model="orderFormData.remark"
                auto-complete="off"
                clearable
                style="width: 90%"
                placeholder="请输入备注信息"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="documentShow">
          <el-col :span="11">
            <el-form-item prop="acceptance" label="预验收单据:">
              <el-select
                style="width: 90%"
                v-model="checkPreInArray"
                :disabled="corpOrderIdDisabled"
                multiple
                placeholder="请选入单据"
              >
                <el-option
                  v-for="(item, index) in checkPreInArray"
                  :key="item.index"
                  :label="item"
                  :value="item"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="curSpaceShow">
          <el-col :span="11">
            <el-form-item prop="curSpaceCode" label="当前货位:">
              <el-select
                v-model="orderFormData.curSpaceCode"
                placeholder="当前货位"
                style="width: 90%"
                :disabled="corpOrderIdDisabled"
                filterable
              >
                <el-option
                  v-for="item in curSpaceOptions"
                  :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-row v-if="preInSpaceShow">
          <!--          <el-col :span="11">-->
          <!--            <el-form-item prop="checkPreInInvCode" label="预验收出库仓库:">-->
          <!--              <el-select-->
          <!--                v-model="orderFormData.checkPreInInvCode"-->
          <!--                placeholder="预验收仓库"-->
          <!--                style="width: 90%"-->
          <!--                :disabled="corpOrderIdDisabled"-->
          <!--                @change="changePreInInv"-->
          <!--                filterable-->
          <!--              >-->
          <!--                <el-option-->
          <!--                  v-for="item in curPreInInvOptions"-->
          <!--                  :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-col :span="11">
            <el-form-item prop="checkPreInSpaceCode" label="预验收出库货位:">
              <el-select
                v-model="orderFormData.checkPreInSpaceCode"
                placeholder="预验收货位"
                style="width: 90%"
                :disabled="corpOrderIdDisabled"
                filterable
              >
                <el-option
                  v-for="item in curPreInSpaceOptions"
                  :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-row v-if="preSpaceShow">

          <!--          <el-col :span="11">-->
          <!--            <el-form-item prop="checkPreInInvCode" label="预验收仓库:">-->
          <!--              <el-select-->
          <!--                v-model="orderFormData.checkPreInInvCode"-->
          <!--                placeholder="预验收仓库"-->
          <!--                style="width: 90%"-->
          <!--                :disabled="corpOrderIdDisabled"-->
          <!--                @change="changePreInInv"-->
          <!--                filterable-->
          <!--              >-->
          <!--                <el-option-->
          <!--                  v-for="item in curPreInInvOptions"-->
          <!--                  :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-col :span="11">
            <el-form-item prop="preCurSpaceCode" label="寄售出库货位:">
              <el-select
                v-model="orderFormData.preCurSpaceCode"
                placeholder="寄售出库货位"
                style="width: 90%"
                :disabled="corpOrderIdDisabled"
                filterable
              >
                <el-option
                  v-for="item in curPreSpaceOptions"
                  :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-row v-if="errView == 1">
          <el-col :span="20">
            <el-form-item prop="errMsg" label="异常信息:">
              <el-input v-model="orderFormData.errMsg" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="viewType != 1">
          <el-col :span="20">
            <el-form-item prop="code" label="扫码录入校验:">
              <el-input
                id="inputer"
                @focus="getInputFocus($event)"
                @keypress.enter.native="enterKey($event)"
                ref="inputRef"
                style="ime-mode: disabled"
                type="tel"
                v-model="codeFormData.code"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-button
              type="primary"
              size="mini"
              @click.native.stop="addCode()"
              style="margin-left: 15px"
            >添加
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-tabs type="border-card" style="margin: 5px">
      <el-tab-pane label="业务详情" v-if="bizShow && viewBizVisible || (viewType ==5 || viewType==6)">
        <biz-detail-panel
          :idQuery="orderFormData"
          :refreshPanel="refreshCodesPanel"
          :refreshOrder="refreshOrder"
          :viewType="viewType"
          :curBustype="curAction"
          v-if="isBizDetailAlive"
        ></biz-detail-panel>
      </el-tab-pane>
      <el-tab-pane label="扫码详情" v-if="viewCodeVisible">
        <codeDetailPanel
          :idQuery="orderFormData"
          :refreshPanel="refreshCodesPanel"
          :viewType="viewType"
          v-if="isCodeDetailAlive"
        ></codeDetailPanel>
      </el-tab-pane>
      <el-tab-pane label="扫码明细" v-if="viewCodeDetailVisible">
        <codesPanel
          :idQuery="orderFormData"
          :refreshPanel="refreshCodesPanel"
          v-if="isCodeAlive"
          :viewType="viewType"
          :selectRlTitle="selectRlTitle"
          :editTitle="editTitle"
          :selectUnitTitle="selectUnitTitle"
        ></codesPanel>
      </el-tab-pane>
    </el-tabs>

    <el-dialog
      :title="editTitle"
      :visible.sync="editOriginCodeVisible"
      append-to-body
      width="70%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      v-if="editOriginCodeVisible"
    >
      <editCodeDialog
        :closeCodeDialog="closeCodeDialog"
        :repeatAddCode="repeatAddCode"
        :codeDetail="codeFormData"
        editType="1"
      >
      </editCodeDialog>
    </el-dialog>
    <!-- 选入预验收库单 -->
    <el-dialog
      title="选入预验收库单"
      :visible.sync="selectorder"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="70%"
      v-if="selectorder"
      append-to-body
      @close="closeDialogtwo"
    >
      <dialog-create-order-select
        @giveselectbox="giveselectbox"
        :closeDialogtwo="closeDialogtwo"
        :checkPreInOrders="checkPreInArray"
      >
      </dialog-create-order-select>
    </el-dialog>

    <!--    绑定供应商-->
    <el-dialog
      :title="selectUnitTitle"
      :visible.sync="dialogTableVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      v-if="dialogTableVisible"
      width="80%"
      append-to-body
    >
      <DialogSelectUnit
        :codeId="codeFormData"
        v-on:selectSupUnit="selectSupUnit"
      ></DialogSelectUnit>
    </el-dialog>


    <!--    绑定产品-->
    <el-dialog
      :title="selectRlTitle"
      :visible.sync="selectRlVisible"
      width="80%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      v-if="selectRlVisible"
      append-to-body
    >
      <selectRlDialog
        :curRow="codeFormData"
        :type="1"
        :fromCorp="orderFormData.fromCorp"
        v-on:selectBindRl="selectBindRl"
      ></selectRlDialog>
    </el-dialog>


    <el-dialog
      title="请指定当前产品所在货位"
      :visible.sync="selectSpaceVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      v-if="selectSpaceVisible"
      width="80%"
      append-to-body
    >
      <DialogSelectSpace
        :spaceCodeList="spaceCodeList"
        v-on:selectSpaceBind="selectSpaceBind"
      ></DialogSelectSpace>
    </el-dialog>


  </div>
</template>

<script>
import {
  submitBiz,
  addOrderWeb,
  submitCodes,
  submitOrderWeb,
  saveOrderWeb,
  updateCodeBindSup,
  enterCodeWeb, submitAllocateBiz,
} from "../../api/inout/order";
import {filterSubAll, findByFrom, findInvByUser} from "../../api/system/invSubWarehouse";
import {parseTime} from "../../utils/coTools";
import {getBasicUnitMaintains} from "../../api/basic/basicUnitMaintain";
import editCodeDialog from "./DialogEditCode";
import DialogCreateOrderSelect from "./DialogCreateOrderSelect";

import codesPanel from "./PanelCreateOrderCodes";
import codeDetailPanel from "./PanelCreateOrderCodeDetail";
import bizDetailPanel from "./PanelCreateOrderBizDetail";
import A from "../../plugins/KeyScaner";
import {findByInvUser} from "@/api/basic/busType";
import {getInvSpaceList} from "@/api/inventory/invSpace";
import DialogSelectUnit from "./DialogSelectUnit";
import selectRlDialog from "./DialogSelectRl";
import DialogSelectSpace from "./DialogSelectSpace";
import {isBlank} from "@/utils/strUtil";

export default {
  name: "ioCreateOrder",
  props: {
    closeDialog: {
      type: Function,
      required: true,
    },
    orderQuery: {
      type: Object,
      required: true,
    },
    errView: {
      type: Object,
      required: true,
    },
    viewType: {
      //1、新增业务单据页面，2.新增扫码单据页面，3、正常单据处理页面,4.带配货页面,5:有三期验收,6:无三期验收
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      //单据相关
      orderFormData: {
        billNo: null,
        corpOrderId: null,
        action: null,
        invCode: null,
        fromCorp: null,
        fromCorpName: null,
        fromInvCode: null,
        remark: null,
        fromType: 2,
        errMsg: null,
        checkPreInOrders: null,
        checkPreInInvCode: null,
        checkPreInSpaceCode: null,
        curSpaceCode: null,
        preSpaceCode: null,
        preCurSpaceCode: null,  //单据

      },
      checkPreInArray: [],
      errView: 0,
      //选入相关
      selectorder: false,
      //扫码相关
      codeFormData: {
        code: "",
        batchNo: null,
        produceDate: null,
        expireDate: null,
        serialNo: null,
        relId: null,
      },

      curInvOptions: [],
      busTypeOptions: [],
      fromInvOptions: [],


      curPreInInvOptions: [],    //预验收仓库
      curPreInSpaceOptions: [],  //预验收货位
      curPreSpaceOptions: [],    //寄售货位
      curSpaceOptions: [], //当前货位

      curAction: {
        corpType: 0,
        genUnit: false,
        checkWebNew: null
      },
      fromOptions: [],
      isCodeAlive: true,
      bizShow: true,
      isBizDetailAlive: true,
      isCodeDetailAlive: true,
      editCodeVisible: false,
      actionEnable: false,
      editOriginCodeVisible: false,
      curId: null,
      storageList: [],

      formRules: {
        corpOrderId: [
          {required: true, message: "请输入单据号", trigger: "blur"},
        ],
        createTime: [
          {required: true, message: "请选择创建时间", trigger: "blur"},
        ],
        invCode: [
          {required: true, message: "请选择当前仓库", trigger: "change"},
        ],
        action: [
          {required: true, message: "请选择单据类型", trigger: "change"},
        ],
        acceptance: [
          {required: true, message: "请选入预验收单据", trigger: "blur"},
        ],
      },
      isScan: true,
      scanText: "扫码录入：",
      corpOrderIdDisabled: false,
      isSupVesion: false,
      loading: false,
      index: null,
      formLoading: false,
      formVisible: false,
      deleteLoading: false,
      orderNo: null,
      fromStorageOptions: [],
      curRow: null,
      dialogTableVisible: false,
      selectRlVisible: false,
      selectSpaceVisible: false,
      sitcomScan: false,
      selectRlTitle: "绑定产品",
      selectUnitTitle: "绑定供应商",
      sictomText: "",
      originCode: "",
      checkSuccess: false,
      codeDetail: null,
      editTitle: "编辑条码",
      fromDeptOptions: [],
      enableDept: false,

      viewBizVisible: false,
      viewCodeVisible: false,
      viewCodeDetailVisible: false,
      documentShow: false,      //控制预验收选入单据界面
      curSpaceShow: false,     //正常出入库选入出库界面
      preInSpaceShow: false,   //是否显示选择预验收界面
      preSpaceShow: false,     //寄售，预验收仓库界面

      spaceCodeList: [],
    };
  },
  components: {
    codesPanel,
    codeDetailPanel,
    bizDetailPanel,
    editCodeDialog,
    DialogCreateOrderSelect,
    DialogSelectUnit, DialogSelectSpace, selectRlDialog
  },
  methods: {
    //获取用户仓库列表
    findInvListByUser() {
      this.curInvOptions = [];
      findInvByUser()
        .then((response) => {
          this.curInvOptions = response.data || [];
          if (
            this.curInvOptions != null &&
            this.curInvOptions.length > 0 &&
            this.orderFormData.invWarehouseCode == null
          ) {
            //this.orderFormData.invCode = this.curInvOptions[0].code;
            this.getBusTypeByInv();
            this.findCurSpaceList(this.orderFormData.invCode);
          }
        })
        .catch(() => {
        });
    },
    //接收预验单传值
    giveselectbox(val) {

      this.checkPreInArray = JSON.parse(JSON.stringify(val));
      if (this.checkPreInArray != null) {
        this.orderFormData.checkPreInOrders = this.checkPreInArray.join(",");
      }
      // this.orderFormData.checkPreInOrders = JSON.parse(JSON.stringify(val));
    },
    //仓库改变
    changeInv(row) {
      this.orderFormData.action = null;
      this.orderFormData.checkPreInOrders = null;
      this.checkPreInArray = [];
      if (this.documentShow) {
        this.documentShow = false;
      }

      this.curSpaceShow = false;
      this.preInSpaceShow = false;
      this.preSpaceShow = false;
      this.orderFormData.curSpaceCode = false;
      this.orderFormData.checkPreInSpaceCode = false;
      this.orderFormData.preCurSpaceCode = false;
      this.getBusTypeByInv();
      this.findCurSpaceList(row);
    },

    //根据仓库,用户获取单据类型
    getBusTypeByInv() {
      let query = {
        code: this.orderFormData.invCode,
      };
      findByInvUser(query)
        .then((response) => {
          this.busTypeOptions = response.data || [];
        })
        .catch(() => {
        });
    },
    //关闭预选单弹框
    closeDialogtwo() {
      this.selectorder = false;
    },

    initBusType() {
      let query = {
        code: this.orderFormData.invCode,
      };
      findByInvUser(query)
        .then((response) => {
          this.busTypeOptions = response.data || [];
          this.curAction = this.getActionItem(this.orderFormData.action);
          if (isBlank(this.curAction)) {
            this.curAction = {
              corpType: 0,
              genUnit: false,
              checkWebNew: null
            }
          }
          if (
            (this.curAction.checkEnable &&
              (this.curAction.checkWebNew ||
                this.curAction.checkUdims ||
                this.curAction.checkCopy ||
                this.curAction.checkPdaEd ||
                this.curAction.checkPdaUn ||
                this.curAction.checkPc ||
                this.curAction.checkWebNew ||
                this.curAction.checkSp ||
                this.curAction.checkChange ||
                this.curAction.checkBalance)) ||
            this.viewType == 1
          ) {
            this.bizShow = true;
          } else this.bizShow = false;
        })
        .catch(() => {
        });
    },

    //获取往来单位候选列表
    findMethod(query) {
      this.fromOptions = [];
      let cQuery = {
        key: query,
        corpType: null,
        outType: null,
        page: 1,
        limit: 20,
      };
      if (this.curAction.corpType == 3) {
        //内部科室
        cQuery.corpType = 3;
      } else if (this.curAction.corpType == 2) {
        //供应商信息
        cQuery.corpType = 2;
      } else if (this.curAction.corpType == 0) {
        cQuery.corpType = 2;
      } else if (this.curAction.corpType == 1) {
        cQuery.corpType = 1;
        cQuery.outType = 2;
      } else
        return;
      getBasicUnitMaintains(cQuery)
        .then((response) => {
          this.loading = false;
          this.fromOptions = response.data.list || [];
        })
        .catch(() => {
          this.loading = false;
        });
    },

    //单据类型改变
    actionChange(item) {
      this.orderFormData.checkPreInOption = null;
      this.orderFormData.checkPreInOrders = null;
      this.checkPreInArray = [];
      this.curAction = this.getActionItem(item);
      if ((this.curAction.checkEnable && this.curAction.checkWebNew != 0) || this.viewType == 1) {
        //单据类型需要一次校验 或 单据类型为1，显示业务单据页面
        this.bizShow = true;
      } else {
        this.bizShow = false;
      }

      if (
        this.curAction.corpType == 0 ||
        this.curAction.corpType == 2 ||
        (this.curAction.corpType == 1 && !this.curAction.genUnit)
      ) {
        //1.切换往来单位
        this.orderFormData.fromCorp = null;
        this.findMethod();
      } else if (this.curAction.corpType == 3) {
        //2. 切换往来仓库
        this.orderFormData.fromInvCode = null;
        this.findFromInvList();
      }
      if (this.curAction.scanPreIn && this.curAction.backPreinType == 1 && this.curAction.preInBack) {
        this.documentShow = true;
      } else if (this.curAction.scanPreIn && this.curAction.backPreinType == 2 && this.curAction.preInBack) {
        this.preInSpaceShow = true;
        this.findPreInInvList();
      } else {
        this.documentShow = false;
        this.preInSpaceShow = false;
      }

      if (this.curAction.scanPreIn && this.curAction.spaceOut == 1) {
        this.preInSpaceShow = true;
        this.curSpaceShow = false;
        this.preSpaceShow = false;
        this.orderFormData.curSpaceCode = null;
        this.orderFormData.checkPreInSpaceCode = null;
        this.orderFormData.preCurSpaceCode = null;
        this.findPreInSpaceList();
      } else if (this.curAction.advancePreIn && this.curAction.spaceOut == 1) {
        this.preInSpaceShow = false;
        this.curSpaceShow = false;
        this.preSpaceShow = true;
        this.orderFormData.curSpaceCode = null;
        this.orderFormData.checkPreInSpaceCode = null;
        this.orderFormData.preCurSpaceCode = null;
        this.findPreSpaceList()
      } else if (this.curAction.spaceOut == 1) {
        this.curSpaceShow = true;
        this.preInSpaceShow = false;
        this.preSpaceShow = false;
        this.orderFormData.curSpaceCode = null;
        this.orderFormData.checkPreInSpaceCode = null;
        this.orderFormData.preCurSpaceCode = null;
      } else {
        this.curSpaceShow = false;
        this.preInSpaceShow = false;
        this.preSpaceShow = false;
        this.orderFormData.curSpaceCode = null;
        this.orderFormData.checkPreInSpaceCode = null;
        this.orderFormData.preCurSpaceCode = null;
      }
    },

    //获取往来仓库列表
    findFromInvList(val) {
      let cQuery = {
        locInvCode: this.orderFormData.invCode,
      };
      findByFrom(cQuery)
        .then((response) => {
          this.fromInvOptions = response.data || [];
          if (val == 1) {
            this.orderFormData.fromInvCode = this.fromInvOptions[0].code;
          }
        })
        .catch(() => {
        });
    },

    //获取当前单据类型
    getActionItem(action) {
      for (let i = 0; i < this.busTypeOptions.length; i++) {
        if (this.busTypeOptions[i].action == action) {
          return this.busTypeOptions[i];
        }
      }
    },

    //切换预验收仓库
    changePreInInv(row) {
      this.orderFormData.checkPreInSpaceCode = null;
      this.findPreInSpaceList(row.code);
    },

    //获取预验收仓库
    findPreInInvList(val) {
      let query = {
        advanceType: 2,
        code: this.orderFormData.checkPreInInvCode,
      }
      filterSubAll(query)
        .then((response) => {
          this.curPreInInvOptions = response.data || [];
        })
        .catch(() => {
        });
    },

    //获取预验收货位
    findPreInSpaceList(val) {
      let query = {
        filterType: "preIn",
        status: 1,
        code: this.orderFormData.checkPreInSpaceCode,
      }
      getInvSpaceList(query).then((res) => {
        this.curPreInSpaceOptions = res.data.list || [];
      })
    },


    //获取寄售
    findPreSpaceList(val) {
      let query = {
        filterType: "pre",
        status: 1,
        code: this.orderFormData.preSpaceCode,
      }
      getInvSpaceList(query).then((res) => {
        this.curPreSpaceOptions = res.data.list || [];
      })
    },


    //获取当前货位
    findCurSpaceList(val) {
      let query = {
        invWarehouseCode: val,
        status: 1,
        code: this.orderFormData.curSpaceCode,
      }
      getInvSpaceList(query).then((res) => {
        this.curSpaceOptions = res.data.list || [];
      })
    },


    //添加条码
    addCode(event) {
      this.originCode = "";
      this.sictomText = "";
      this.orderFormData.corpOrderId = this.orderFormData.corpOrderId.trim();
      this.codeFormData.batchNo = "";
      this.codeFormData.produceDate = "";
      this.codeFormData.expireDate = "";
      this.codeFormData.serialNo = "";
      this.actionEnable = true;
      if (event == null) {
      } else event.target.select();
      this.$refs.inputRef.select();
      if (this.$isBlank(this.orderFormData.action)) {
        this.$message.warning("请选择单据类型！");
        return;
      }
      if (this.curAction.corpType === 0 || this.curAction.corpType === 2 || (this.curAction.corpType === 1 && !this.curAction.genUnit)) {
        if (this.$isBlank(this.orderFormData.fromCorp)) {
          this.$message.warning("请选择往来单位！");
          return;
        }
      }
      if (this.curAction.corpType == 3) {
        if (this.$isBlank(this.orderFormData.fromInvCode)) {
          this.$message.warning("请选择往来单位！");
          return;
        }
      }
      if (this.curAction.corpType === 1 && this.curAction.genUnit) {
        if (this.$isBlank(this.orderFormData.fromCorp)) {
          this.$message.warning("请选择往来单位！");
          return;
        }
      }

      if (this.curSpaceShow) {
        if (this.$isBlank(this.orderFormData.curSpaceCode)) {
          this.$message.warning("请选择当前货位！");
          return;
        }
      }
      if (this.preInSpaceShow) {
        if (this.$isBlank(this.orderFormData.checkPreInSpaceCode)) {
          this.$message.warning("请选择预验收出库货位！");
          return;
        }
      }
      if (this.preSpaceShow) {
        if (this.$isBlank(this.orderFormData.preCurSpaceCode)) {
          this.$message.warning("请选择寄售出库货位！");
          return;
        }
      }


      if (this.checkPreInArray != null) {
        this.orderFormData.checkPreInOrders = this.checkPreInArray.join(",");
      }

      if (this.$isBlank(this.orderFormData.corpOrderId)) {
        let date = new Date();
        this.orderFormData.corpOrderId =
          parseTime(date, "{y}{m}{d}{h}{i}{s}") +
          Math.ceil(Math.random() * 89 + 10);
      }
      this.codeFormData.code = this.codeFormData.code.trim();
      if (this.$isBlank(this.codeFormData.code)) return;
      this.loading = true;
      let tQuery = Object.assign(
        JSON.parse(JSON.stringify(this.orderFormData))
      );
      tQuery.code = this.codeFormData.code;
      tQuery.orderType = this.viewType;
      this.addCodeSubmit(tQuery);
      this.$refs.inputRef.select();
    },

    repeatAddCode(editData) {
      let tQuery = Object.assign(
        JSON.parse(JSON.stringify(this.orderFormData))
      );
      tQuery.code = this.codeFormData.code;
      tQuery.orderType = this.viewType;

      tQuery.batchNo = editData.batchNo;
      tQuery.produceDate = editData.produceDate;
      tQuery.expireDate = editData.expireDate;
      tQuery.serialNo = editData.serialNo;

      this.closeCodeDialog();
      this.addCodeSubmit(tQuery);
    },

    //添加条码后提交
    addCodeSubmit(tQuery) {
      addOrderWeb(tQuery).then((response) => {
        if (response.code === 20000) {
          this.orderFormData.billNo = response.data.orderId;
          if (response.data.errMsg != null) {
            this.$alert(response.data.errMsg, "提示", {
              confirmButtonText: "确定",
              type: "warning",
              closeOnClickModal: true,
              callback: (action) => {
              },
            });
          }
          this.corpOrderIdDisabled = true;
          this.refreshCodesPanel();
          this.$refs.inputRef.focus();
          this.$refs.inputRef.select();
        } else {
          if (response.code == 502) {
            //该产品绑定多个产品ID处理
            this.curRow = response.data;
            this.orderFormData.billNo = this.curRow.orderId;
            this.refreshCodesPanel();
            this.$message.error(response.message);
            this.selectRlTitle = response.message;
            this.selectRlVisible = true;
          } else if (response.code == 503) {
            //该产品绑定多个供应商处理
            this.curRow = response.data;
            this.orderFormData.billNo = this.curRow.orderId;
            this.codeFormData.relId = this.curRow.relId;
            this.refreshCodesPanel();
            this.selectUnitTitle = response.message;
            this.$message.error(response.message);
            this.dialogTableVisible = true;
          } else if (response.code == 504) {
            this.$confirm(response.message, "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "忽略",
              type: "error",
            })
              .then(() => {
                tQuery.ignoreExpire = true;
                this.addCodeSubmit(tQuery);
              })
              .catch(() => {
              });
          } else if (response.code == 505) {
            this.$confirm(response.message, "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "忽略",
              type: "warning",
            })
              .then(() => {
                tQuery.ignoreRecentExpire = true;
                this.addCodeSubmit(tQuery);
              })
              .catch(() => {
              });
          } else if (response.code == 605) {
            this.$confirm(response.message, "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(() => {
              })
              .catch(() => {
              });
            return;
          } else if (response.code == 604) {
            this.$confirm(response.message, "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(() => {
              })
              .catch(() => {
              });
            return;
          } else if (response.code == 507) {
            this.editOriginCodeVisible = true;
            this.editTitle = response.message;
            this.codeFormData.produceDate = response.data.produceDate;
            this.codeFormData.expireDate = response.data.expireDate;
            this.codeFormData.batchNo = response.data.batchNo;
            this.codeFormData.serialNo = response.data.serialNo;
            this.$message({
              type: "error",
              message: "提交失败！" + this.editTitle,
              customClass: "messageIndex",
            });
          } else if (response.code == 511) {
            //该UDI码绑定多个货位处理
            this.spaceCodeList = response.data;
            this.selectSpaceVisible = true;
          } else {
            this.$alert(response.message, "提示", {
              confirmButtonText: "确定",
              type: "warning",
              closeOnClickModal: true,
              callback: (action) => {
                this.$refs.inputRef.focus();
                this.$refs.inputRef.select();
              },
            });
          }
        }
        this.loading = false;
      });
    },

    refreshCodesPanel() {
      this.isCodeAlive = false;
      this.isBizDetailAlive = false;
      this.isCodeDetailAlive = false;
      this.$nextTick(() => {
        //重新加载组件
        this.isCodeAlive = true;
        this.isBizDetailAlive = true;
        this.isCodeDetailAlive = true;
      });
    },

    refreshOrder(orderQuery) {
      this.orderFormData = orderQuery;
    },

    submit(val) {
      if (this.$isBlank(this.orderFormData.action)) {
        this.$message.warning("单据类型不能为空！");
        return
      }
      if (this.curAction.corpType === 0 || this.curAction.corpType === 2 || (this.curAction.corpType === 1 && !this.curAction.genUnit)) {
        if (this.$isBlank(this.orderFormData.fromCorp)) {
          this.$message.warning("请选择往来单位！");
          return;
        }
      }
      if (this.curAction.corpType == 3) {
        if (this.$isBlank(this.orderFormData.fromInvCode)) {
          this.$message.warning("请选择往来单位！");
          return;
        }
      }
      if (this.curAction.corpType === 1 && this.curAction.genUnit) {
        if (this.$isBlank(this.orderFormData.fromCorp)) {
          this.$message.warning("请选择往来单位！");
          return;
        }
      }


      let tQuery = Object.assign(
        JSON.parse(JSON.stringify(this.orderFormData))
      );
      tQuery.orderId = this.orderFormData.billNo;
      console.log(this.curAction)
      if (this.viewType == 2) {   //扫码单据立即提交
        submitCodes(tQuery).then((response) => {
          if (response.code === 20000) {
            this.$message.success("提交成功,等待处理，请刷新后查看！");
            this.closeDialog();
          } else {
            this.$message.error(response.message);
          }
        });
      } else if (this.viewType == 1) {    //业务单据提交提交
        if (val == 1 || this.curAction.checkWebNew == 2)
          tQuery.fromVailPi = 2;
        else
          tQuery.fromVailPi = 1;
        submitBiz(tQuery).then((response) => {
          if (response.code === 20000) {
            this.$message.success("提交成功");
            this.closeDialog();
          } else {
            this.$message.error(response.message);
          }
        });
      } else if (this.viewType == 4) {
        submitAllocateBiz(tQuery).then((response) => {
          if (response.code === 20000) {
            this.$message.success("提交成功");
            this.closeDialog();
          } else {
            this.$message.error(response.message);
          }
        });
      } else {   //校验后提交
        submitOrderWeb(tQuery).then((response) => {
          if (response.code === 20000) {
            this.$message.success("提交成功");
            this.closeDialog();
          } else {
            this.$message.error(response.message);
          }
        });
      }

    },
    saveOrder() {
      let tQuery = Object.assign(
        JSON.parse(JSON.stringify(this.orderFormData))
      );
      tQuery.billNo = this.orderFormData.billNo;
      if (this.orderFormData.billNo == null) {
        this.$message.error("请先录入单据信息！");
        return;
      }
      saveOrderWeb(tQuery).then((response) => {
        if (response.code === 20000) {
          this.closeDialog();
        } else {
          this.$message.error(response.message);
        }
        this.loading = false;
      });
    },

    // 刷新表单
    resetForm() {
      if (this.$refs["dataForm"]) {
        // 清空验证信息表单
        this.$refs["dataForm"].clearValidate();
        // 刷新表单
        this.$refs["dataForm"].resetFields();
      }
    },
    enterKey(event) {
      this.checkSuccess = true;
      let tQuery = {
        originCode: this.originCode,
        code: this.codeFormData.code.trim(),
      };
      enterCodeWeb(tQuery).then((response) => {
        if (response.code === 20000) {
          this.$refs.inputRef.focus();
          this.$refs.inputRef.select();
          this.codeFormData.code = response.data;
          this.addCode();
        } else {
          if (response.code == 502) {
            this.checkSuccess = false;
            this.codeFormData.code = response.data;
            this.originCode = this.codeFormData.code;
          } else if (response.code == 501) {
            this.checkSuccess = false;
            this.$message.error(response.message);
          } else if (response.code == 503) {
            this.checkSuccess = false;
            this.codeFormData.code = response.data;
            this.$confirm(response.message, "提示", {
              type: "warning",
            })
              .then(() => {
                this.codeFormData.code = response.data;
                this.addCode();
              })
              .catch(() => {
                this.codeFormData.code = this.originCode;
              });
          } else if (response.code == 508) {
            this.originCode = "";
            this.codeFormData.code = "01" + response.data.nameCode;
            this.originCode = this.codeFormData.code;
          } else {
            this.$alert(response.message, "提示", {
              confirmButtonText: "确定",
              type: "warning",
              closeOnClickModal: true,
              callback: (action) => {
                this.$refs.inputRef.focus();
                this.$refs.inputRef.select();
              },
            });
          }
        }
        this.loading = false;
      });
    },

    getInputFocus(event) {
      event.currentTarget.select();
    },
    tableSelection() {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleAllSelection();
    },
    handleSizeChange(val) {
      this.query.limit = val;
      this.getCodeList();
    },
    handleCurrentChange(val) {
      this.query.page = val;
      this.getCodeList();
    },

    intentBack() {
      this.$router.go(-1);
    },
    selectSupUnit(row) {
      this.dialogTableVisible = false;
      this.orderFormData.selectSupId = row.erpId;
      this.addCode();
    },

    selectBindRl(row) {
      this.selectRlVisible = false;
      this.orderFormData.relId = row.id;
      this.addCode();
    },


    selectSpaceBind(val) {
      if (this.curAction.scanPreIn) {
        this.orderFormData.checkPreInSpaceCode = val;
      } else if (this.curAction.advancePreIn) {
        this.orderFormData.preCurSpaceCode = val;
      } else {
        this.orderFormData.curSpaceCode = val;
      }
      this.addCode();
      this.selectSpaceVisible = false;
    },


    closeCodeDialog() {
      this.editOriginCodeVisible = false;
      this.refreshCodesPanel();
    },
    //选入相关
    OptionalDocument() {
      this.selectorder = true;
    },
  },
  filters: {},
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
    var that = this;
    var inputer = document.getElementById("inputer");
    window.sc = new A.KeyScaner(inputer); //传入要监听的DOM节点
    sc.onInput = function (text) {
      if (text.includes("delete")) {
        that.codeFormData.code = "";
        that.sictomText = "";
        that.originCode = "";
        return;
      }
      if (that.sitcomScan) {
        let tempTxt = text;
        let str = tempTxt.replace(/[\r]/g, "");
        that.sictomText = that.sictomText + str;
        that.codeFormData.code = that.sictomText;
      } else {
        that.codeFormData.code = text;
      }
    };
    inputer.focus();
  },
  created() {
    //页面初始化
    this.codeFormData.code = "";
    //页面类型
    if (this.viewType == 2) {
      //2.新增扫码单据页面，1、新增业务单据页面，3、正常页面
      this.viewCodeVisible = true;
      this.viewCodeDetailVisible = true;
    } else if (this.viewType == 1) {
      this.viewBizVisible = true;
    } else {
      this.viewCodeVisible = true;
      this.viewCodeDetailVisible = true;
      this.viewBizVisible = true;
    }
    this.orderFormData.orderType = this.viewType;
    if (this.$isNotBlank(this.orderQuery.billNo)) {
      //编辑页面
      this.corpOrderIdDisabled = true;
      this.orderFormData = this.orderQuery;
      this.actionEnable = true;
      this.orderFormData.fromCorp=this.orderFormData.fromCorpName;
      if (this.$isNotBlank(this.orderFormData.checkPreInOrders)) {
        this.checkPreInArray = this.orderFormData.split(",");
      }
      this.curAction = {}
      this.refreshCodesPanel();
      //加载选择框候选数据
      this.findInvListByUser();
      this.initBusType();
      this.findMethod(this.orderFormData.fromCorp);
      this.findFromInvList();
    } else {
      //新增页面
      this.corpOrderIdDisabled = false;
      let date = new Date();
      this.orderFormData.createTime = parseTime(
        date,
        "{y}-{m}-{d} {h}:{i}:{s}"
      );
      this.orderFormData.corpOrderId =
        parseTime(date, "{y}{m}{d}{h}{i}{s}") +
        Math.ceil(Math.random() * 89 + 10);
      this.orderFormData.deptCode = this.$store.getters.deptCode;
      this.orderFormData.invCode = this.$store.getters.locInvCode;
      this.findInvListByUser();
    }
  },
};
</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;
}

.ime-disabled {
  ime-mode: disabled;
}
</style>



