<template>
    <div>

        <el-form :model="formData" :rules="formRules" ref="dataForm">
            <el-row type="flex" justify="end">
                <el-button-group style="display: flex;margin-bottom: 15px; margin-right: 50px">
                    <el-button
                        type="primary"
                        @click.native="saveOrder('2')"
                        :loading="loading"
                    >保存
                    </el-button>
                    <!--&gt;-->
                    <!--<el-button-->
                    <!--type="primary"-->
                    <!--@click.native="saveOrder('2')"-->
                    <!--:loading="loading"-->
                    <!--&gt;未配货提交-->
                    <!--</el-button-->
                    <!--&gt;-->

                    <el-button
                        type="primary"
                        @click.native="saveOrder('3')"
                        :loading="loading"
                    >提交
                    </el-button
                    >
                </el-button-group>
            </el-row>


            <el-card style="margin-top: -5px;">
                <el-row :gutter="20" style="margin-top: 10px;">
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>单据号：</span>
                        </div>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item prop="billNo">
                            <el-input v-model="formData.billNo" auto-complete="off" :disabled="true"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>单据日期：</span>
                        </div>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item prop="billdate">
                            <!--                            <el-input v-model="formData.billdate" auto-complete="off"></el-input>-->
                            <el-date-picker
                                v-model="formData.billdate"
                                type="date"
                                placeholder="选择日期"
                                style="width: 100%"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" style="margin-top: -10px">
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>往来单位：</span>
                        </div>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item prop="corpName">
                            <el-input v-model="formData.corpName" auto-complete="off" :disabled="true"></el-input>
                            <!--                            <el-select-->
                            <!--                                style="width: 100%"-->
                            <!--                                v-model="sValue"-->
                            <!--                                :multiple="false"-->
                            <!--                                filterable-->
                            <!--                                remote-->
                            <!--                                reserve-keyword-->
                            <!--                                placeholder="请输入供应商名称"-->
                            <!--                                :remote-method="remoteMethod"-->
                            <!--                                :loading="sLoading"-->
                            <!--                                @change="selectOne">-->
                            <!--                                <el-option-->
                            <!--                                    v-for="item in sOptions"-->
                            <!--                                    :key="idx"-->
                            <!--                                    :label="item.name"-->
                            <!--                                    :value="item">-->
                            <!--                                </el-option>-->
                            <!--                        </el-select>-->
                        </el-form-item>
                    </el-col>
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>业务类型：</span>
                        </div>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item prop="billType">

                            <el-select v-model="formData.billType" placeholder="请选择业务类型" disabled>
                                <el-option
                                    v-for="item in busTypes"
                                    :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 :gutter="20" style="margin-top: -10px;">
                    <!--                    <el-col :span="3">-->
                    <!--                        <div class="ao-text">-->
                    <!--                            <span>采购类型：</span>-->
                    <!--                        </div>-->
                    <!--                    </el-col>-->
                    <!--                    <el-col :span="7">-->
                    <!--                        <el-form-item prop="type">-->
                    <!--                            <el-select v-model="formData.type" style="width: 100%" placeholder="采购类型">-->
                    <!--                                <el-option label="预入库" value="1"></el-option>-->
                    <!--                                <el-option label="普通采购" value="2"></el-option>-->
                    <!--                            </el-select>-->
                    <!--                        </el-form-item>-->
                    <!--                    </el-col>-->
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>当前仓库：</span>
                        </div>
                    </el-col>

                    <el-col :span="7">
                        <el-form-item prop="locStorageCode">
                            <!--<el-select v-model="formData.billType" style="width: 100%" placeholder="业务类型" :disabled="true">-->
                            <!--<el-option label="送货单" value="deliveryNote"></el-option>-->
                            <!--</el-select>-->
                            <el-select v-model="formData.locStorageCode" placeholder="当前仓库信息" disabled>
                                <el-option
                                    v-for="item in storageList"
                                    :key="item.name"
                                    :label="item.name"
                                    :value="item.code">
                                    <span style="float: left">{{ item.name }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-card>

            <el-card>
                <el-button-group style="display: flex">
                    <el-button
                        type="primary"
                        size="small"
                        @click.native.stop="selectProductFunction()"
                        style="margin: 0px 60px 10px auto; height: 35px"
                        :loading="loading"
                    >产品录入
                    </el-button
                    >
                </el-button-group>

                <!--                <el-row :gutter="20">-->
                <!--                    <el-col :span="3">-->
                <!--                        <div class="ao-text">-->
                <!--                            <span>扫码录入：</span>-->
                <!--                        </div>-->
                <!--                    </el-col>-->
                <!--                    <el-col :span="10">-->
                <!--                        <el-form-item prop="code">-->

                <!--                            &lt;!&ndash;                            @keyup.native.enter="focusNext('iCount')"   @focus="getInputFocus($event)"-->
                <!--                               @keypress.enter.native="selectProductFunction($event)"-->
                <!--                            &ndash;&gt;-->
                <!--                            <el-input-->

                <!--                                @keyup.enter.native="selectProductFunction($event)"-->
                <!--                                v-model="code"-->
                <!--                                auto-complete="off"-->
                <!--                                ref='inputRef'-->
                <!--                                @focus="getInputFocus($event)"-->
                <!--                            ></el-input>-->
                <!--                        </el-form-item>-->
                <!--                    </el-col>-->
                <!--                    &lt;!&ndash;                    <el-col :span="3">&ndash;&gt;-->
                <!--                    &lt;!&ndash;                        <div class="ao-text">&ndash;&gt;-->
                <!--                    &lt;!&ndash;                            <span>数量：</span>&ndash;&gt;-->
                <!--                    &lt;!&ndash;                        </div>&ndash;&gt;-->
                <!--                    &lt;!&ndash;                    </el-col>&ndash;&gt;-->
                <!--                    &lt;!&ndash;                    <el-col :span="4">&ndash;&gt;-->
                <!--                    &lt;!&ndash;                        <el-form-item prop="iCount">&ndash;&gt;-->
                <!--                    &lt;!&ndash;                            &lt;!&ndash;                            @keypress.enter.native="addCode()"&ndash;&gt;&ndash;&gt;-->
                <!--                    &lt;!&ndash;                            <el-input v-model="iCount" auto-complete="off" ref="iCount"&ndash;&gt;-->
                <!--                    &lt;!&ndash;                                      @keypress.enter.native="iCountEnterFunction()"&ndash;&gt;-->
                <!--                    &lt;!&ndash;                                      @focus="getInputFocus($event)"&ndash;&gt;-->
                <!--                    &lt;!&ndash;                                      @input="iCountChange"&ndash;&gt;-->
                <!--                    &lt;!&ndash;                                      oninput="value=value.replace(/[^\d]/g,'')"></el-input>&ndash;&gt;-->
                <!--                    &lt;!&ndash;                        </el-form-item>&ndash;&gt;-->
                <!--                    &lt;!&ndash;                    </el-col>&ndash;&gt;-->
                <!--                </el-row>-->

                <!--                highlight-current-row-->
                <!--                @current-change="rowChange"-->
                <el-table v-loading="loading" :data="codeArray" style="width: 100%;"
                          :row-class-name="tableRowClassName"
                          max-height="300" height="300" ref="multipleTable">
                    <!--                    <el-table-column-->
                    <!--                        type="selection"-->
                    <!--                        width="55">-->
                    <!--                    </el-table-column>-->
                    <el-table-column label="序号" type="index" width="50"></el-table-column>
                    <el-table-column width="150"
                                     label="产品通用名"
                                     prop="productName"
                                     show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column width="150"
                                     label="规格型号"
                                     prop="spec"
                                     show-overflow-tooltip
                    ></el-table-column>
                    <!--                    <el-table-column-->
                    <!--                        label="批次号"-->
                    <!--                        prop="batchNo"-->
                    <!--                        show-overflow-tooltip-->
                    <!--                    ></el-table-column>-->
                    <!--                    <el-table-column-->
                    <!--                        label="生产日期"-->
                    <!--                        prop="productDate"-->
                    <!--                        show-overflow-tooltip-->
                    <!--                    ></el-table-column>-->
                    <!--                    <el-table-column-->
                    <!--                        label="失效日期"-->
                    <!--                        prop="expireDate"-->
                    <!--                        show-overflow-tooltip-->
                    <!--                    ></el-table-column>-->
                    <!--                    <el-table-column-->
                    <!--                        label="数量"-->
                    <!--                        prop="count"-->
                    <!--                        show-overflow-tooltip-->
                    <!--                    ></el-table-column>-->
                    <el-table-column width="150" label="批次号">
                        <template slot-scope="scope">
                            <el-input size="small" v-model="scope.row.batchNo"
                                      :disabled="scope.row.index !== selectedIndex"
                                      ref='inputBatchNoRef'
                                      placeholder="请输入批次号" style="width: 80%"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column width="180" label="生产日期">
                        <template slot-scope="scope">
                            <!--                            <el-date-picker-->
                            <!--                                type="date"-->
                            <!--                                size="small" v-model="scope.row.productDate"-->
                            <!--                                :disabled="scope.row.index !== selectedIndex"-->
                            <!--                                placeholder="请选择生产日期" style="width: 80%"-->
                            <!--                                value-format="yyMMdd"-->
                            <!--                                format="yyyy-MM-dd"-->
                            <!--                            >-->
                            <!--                            </el-date-picker>-->
                            <el-input v-model="scope.row.productDate" auto-complete="off"
                                      oninput="if(value.length>6)value=value.slice(0,6)"
                                      :disabled="scope.row.index !== selectedIndex"
                                      type="number"
                            ></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column width="180" label="失效日期">
                        <template slot-scope="scope">
                            <!--                            <el-date-picker-->
                            <!--                                type="date"-->
                            <!--                                size="small" v-model="scope.row.expireDate"-->
                            <!--                                :disabled="scope.row.index !== selectedIndex"-->
                            <!--                                placeholder="请选择失效日期" style="width: 80%"-->
                            <!--                                value-format="yyMMdd"-->
                            <!--                                format="yyyy-MM-dd"-->
                            <!--                            >-->
                            <!--                            </el-date-picker>-->
                            <el-input v-model="scope.row.expireDate" auto-complete="off"
                                      oninput="if(value.length>6)value=value.slice(0,6)"
                                      :disabled="scope.row.index !== selectedIndex"
                                      type="number"
                            ></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column width="150" label="实际数量">
                        <template slot-scope="scope">
                            <el-input size="small" v-model="scope.row.reCount"
                                      placeholder="请输入数量" style="width: 80%"
                                      @change="tableCountChange(scope.row)"
                                      type='number'
                                      :disabled="scope.row.index !== selectedIndex"
                                      oninput="value=value.replace(/[^\d]/g,'')"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column width="150" label="价格">
                        <template slot-scope="scope">
                            <el-input size="small" v-model="scope.row.price"
                                      type='number'
                                      :disabled="scope.row.index !== selectedIndex"
                                      placeholder="请输入价格" style="width: 80%"
                            ></el-input>
                        </template>
                    </el-table-column>
                    <!--                    <el-table-column width="150"-->
                    <!--                                     label="单据数量"-->
                    <!--                                     prop="count"-->
                    <!--                                     show-overflow-tooltip-->
                    <!--                    ></el-table-column>-->
                    <el-table-column width="150" label="单据数量">
                        <template slot-scope="scope">
                            <el-input size="small" v-model="scope.row.count"
                                      placeholder="请输入数量" style="width: 80%"
                                      type='number'
                                      @change="tableCountChange(scope.row)"
                                      :disabled="scope.row.index !== selectedIndex"
                                      oninput="value=value.replace(/[^\d]/g,'')"></el-input>
                        </template>
                    </el-table-column>

                    <el-table-column width="150"
                                     label="注册/备案凭证号"
                                     prop="zczbhhzbapzbh"
                                     show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column width="150"
                                     label="生产厂家"
                                     prop="ylqxzcrbarmc"
                                     show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column label="操作" fixed="right" width="150">
                        <template slot-scope="scope">

                            <!--                            v-if="orderEditor && scope.row.index !== selectedIndex"-->
                            <el-button
                                type="text"
                                size="small"
                                :disabled="scope.row.index === selectedIndex"
                                @click.stop="true"
                                @click.native="rowChange(scope.row)"
                            >编辑
                            </el-button
                            >
                            <!--                            <el-button-->
                            <!--                                type="text"-->
                            <!--                                size="small"-->
                            <!--                                v-if="orderEditor && scope.row.index === selectedIndex"-->
                            <!--                                :disabled="scope.row.index !== selectedIndex"-->
                            <!--                                @click.stop="true"-->
                            <!--                                @click.native="saveCodeArray(scope.$index, scope.row)"-->
                            <!--                            >保存-->
                            <!--                            </el-button-->
                            <!--                            >-->
                            <el-button
                                type="text"
                                size="small"
                                @click.stop="true"
                                @click.native="copyDetail(scope.row)"
                            >复制
                            </el-button
                            >
                            <el-button
                                type="text"
                                size="small"
                                @click.stop="true"
                                @click.native="deleteCodeArray(scope.$index, scope.row)"
                            >删除
                            </el-button
                            >
                        </template>
                    </el-table-column>
                </el-table>

                <!--                <div class="block" v-if="orderEditor">-->
                <!--                    <el-pagination-->
                <!--                        @current-change="handleCurrentChange"-->
                <!--                        layout="prev, pager, next"-->
                <!--                        :page-size="query.limit"-->
                <!--                        :total="total">-->
                <!--                    </el-pagination>-->
                <!--                </div>-->
            </el-card>
        </el-form>

        <!--        <el-dialog-->
        <!--            title="产品录入"-->
        <!--            :visible.sync="selectProductVisible"-->
        <!--            width="85%"-->
        <!--            v-if="selectProductVisible"-->
        <!--            :append-to-body='true'-->
        <!--            :close-on-click-modal="false"-->
        <!--            :close-on-press-escape="false"-->
        <!--        >-->
        <!--            <stockOrderNewSelectProduct-->
        <!--                :closeDialog="closeDialogC2"-->
        <!--                :data="thisData"-->
        <!--            ></stockOrderNewSelectProduct>-->
        <!--        </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'
        >
            <stockOrderNewSelectProduct
                :closeDialog="closeDialogC2"
                :data="thisData"
            ></stockOrderNewSelectProduct>
        </el-dialog>

        <el-dialog
            title="库存产品录入"
            :visible.sync="selectInvProductVisible"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            width="85%"
            v-if="selectInvProductVisible"
            :append-to-body='true'
        >
            <dialogInvProduct
                :closeDialog="closeDialogC2"
                :data="thisData"
            ></dialogInvProduct>
        </el-dialog>
    </div>
</template>

<script>
import {
    stockOrderDetail,
    submitStockOrder,
    submitStockOrderError,
    uploadStockOrderDetailCount,
    stockOrderGenerateBillNo,
    getStockOrderDetailInstrument,
    deleteStockOrderById,
    insertStockOrderWeb,
    stockOrderDetailTemp,
    deleteStockOrderDetailById,
    addStockOrderDetailFromCode,
    uploadStockOrderDetail,
    copyStockOrderDetail,
    stockOrderDetailQueryProduct,
    getStockOrderDetailInstrumentById, stockOrderDetail2
} from "../../api/warehouse/stockOrder";
import draggable from "vuedraggable";
import {saveAs} from "file-saver";
import {parseTime} from "../../filtres/index";
import store from "../../store";
import stockOrderNewSelectProduct from "./stockOrderNewSelectProduct";
import {getBasicUnitMaintains, getBasicUnitMaintains2} from "../../api/basic/basicUnitMaintain";
import {getBussinessType} from "../../api/basic/bussinessType";
import {getLocalBusType, getLocalJoinBusType} from "../../api/basic/busLocalType";
import {filterAll, filterAllByUser} from "@/api/basic/invWarehouse";

export default {
    name: "idQuery",
    props: {
        closeDialog: {
            type: Function,
            required: true,
        },
        idQuery: {
            type: Object,
            required: true,
        },
        detailCountMax: {
            type: Object,
            required: true,
        }
        // closeConfirmFunction: {
        //     type: Function,
        //     required: true,
        // },
    },
    data() {
        return {
            code: "",
            query: {
                orderIdFk: "",
                page: 1,
                limit: 10,
            },
            formData: {},
            formRules: {
                corpName: [
                    {required: true, message: "请输入供应商", trigger: "blur"}
                ],
                billdate: [
                    {required: true, message: "请输入单据日期", trigger: "blur"}
                ],
                billType: [
                    {required: true, message: "请选择业务类型", trigger: "blur"}
                ],
                type: [
                    {required: true, message: "请选择采购类型", trigger: "blur"}
                ],
            },
            codeArray: [],
            fromStorageOptions: [],
            total: 0,
            loading: false,
            index: null,
            formLoading: false,
            formVisible: false,
            deleteLoading: false,
            orderNo: null,
            statusMap: {
                101: "未处理",
                102: "已处理",
                201: "未配货",
                202: "已配货",
                301: "已打印",
                401: "异常",
                501: "未提交",
                502: "已提交",
            },
            typeMap: {
                1: "预入库",
                2: "普通采购",
            },
            orderEditor: true,
            iCount: 0,
            sOptions: [],
            sValue: [],
            sList: [],
            sLoading: false,
            busTypes: [],
            currentRow: {},
            selectedIndex: "",
            selectProductVisible: false,
            selectInvProductVisible: false,
            thisData: {},
            storageList: [],
        };
    },
    components: {
        draggable,
        stockOrderNewSelectProduct,
    },
    methods: {
        saveOrder(status) {
            this.code = "";
            this.$refs.multipleTable.setCurrentRow();
            this.currentRow = {};
            this.selectedIndex = "";
            this.iCount = 0;

            this.$refs["dataForm"].validate(valid => {
                if (valid) {

                    if (this.codeArray.length < 1) {
                        this.$message.warning('未添加产品');
                        return;
                    }
                    for (let i = 0; i < this.codeArray.length; i++) {
                        if (!this.$isBlank(this.codeArray[i].productDate) && this.codeArray[i].productDate.length != 6) {
                            return this.$message.error('生产日期格式错误');
                        }
                        if (!this.$isBlank(this.codeArray[i].expireDate) && this.codeArray[i].expireDate.length != 6) {
                            return this.$message.error('失效日期格式错误');
                        }

                    }
                    if (status === '2') {
                        this.submitFunction(status);
                    } else {
                        for (let i = 0; i < this.codeArray.length; i++) {
                            if (this.$isBlank(this.codeArray[i].batchNo)) {
                                return this.$message.error('批次号不能为空');
                            }
                            if (this.$isBlank(this.codeArray[i].productDate) && this.$isBlank(this.codeArray[i].expireDate)) {
                                return this.$message.error('生产日期与失效日期不能全部为空');
                            }
                            if (this.$isBlank(this.codeArray[i].reCount) || this.codeArray[i].reCount === '0') {
                                return this.$message.error('配货数量不能小于0');
                            }
                            if (this.detailCountMax) {
                                if (this.codeArray[i].count < this.codeArray[i].reCount) {
                                    this.$message.error("实际数量不能高于订单数量");
                                    return;
                                }
                            }
                        }
                        this.$confirm("是否确定提交订单?", "提示", {
                            confirmButtonText: "确定",
                            cancelButtonText: "取消",
                            type: "warning",
                        }).then(() => {
                            this.submitFunction(status);
                        }).catch(() => {
                            this.loading = false;
                        });
                    }


                }
            });
        },
        submitFunction(status) {
            let tMessage = status === '501' ? '保存' : '提交';
            this.loading = true;
            let tQuery = this.formData;
            tQuery.status = status;
            tQuery.subErpOrders = this.codeArray;
            insertStockOrderWeb(tQuery)
                .then(response => {
                    this.loading = false;
                    if (response.code === 20000) {
                        this.$message.success(tMessage + "成功");
                        this.closeDialog(true);
                    } else {
                        this.$message.error(response.message);
                    }
                })
                .catch(() => {
                    this.loading = false;
                })
        },
        submit(formName) {
            if (this.total < 1) {
                this.$message.warning('未添加产品');
                return;
            }
            this.$confirm("是否提交？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                let tQuery = {
                    id: this.idQuery.id
                }
                this.loading = true;
                submitStockOrder(tQuery)
                    .then((response) => {
                        this.loading = false;
                        if (response.code === 20000) {
                            this.$message.success("提交成功");
                            this.closeDialog(true);
                        } else {
                            this.$message.error(response.message);
                        }
                    });
            }).catch(() => {
                this.loading = false;
            });
        },
        selectProductFunction(event) {

            if (event == null) {
                // this.$refs.inputRef.focus();
            } else {
                event.target.select();
            }
            if (this.$isBlank(event)) {
                this.code = "";
                this.$refs.multipleTable.setCurrentRow();
                this.currentRow = {};
                this.selectedIndex = "";
                this.iCount = 0;
                this.thisData.code = "";
                this.thisData.orderEditor = this.orderEditor;
                this.thisData.orderId = "";
                if (this.orderEditor) {
                    this.thisData.orderId = this.formData.id
                }
                this.thisData.stockOrderLists = this.codeArray;
                this.thisData.formData = this.formData;
                this.selectProductVisible = true;
                let item = this.getActionItem(this.formData.billType);
                if (item.mainAction == 'WareHouseOut' && this.formData.noInvOut != true)//内部单
                {
                    this.selectInvProductVisible = true;
                } else {
                    this.selectProductVisible = true;
                }
                return;
            }
            this.code = this.code.trim();

            if (this.code.length > 13) {
                let tStr = this.code.substring(0, 2);
                tStr = tStr.replace('m', 'M');
                tStr = tStr.replace('a', 'A');
                if (tStr.indexOf("MA") !== -1) {
                    this.code = tStr + this.code.substring(2, this.code.length);
                }
            }

            this.loading = true;
            let tQuery = {
                code: this.code,
                customerId: store.getters.customerId,
            };

            stockOrderDetailQueryProduct(tQuery).then((response) => {
                this.loading = false;
                if (response.code === 20000) {
                    if (response.data.getType === "1") {
                        this.loading = true;

                        let ids = [response.data.getId];
                        let tQuery2 = {
                            ids: ids,
                            udiEntity: response.data
                        };
                        getStockOrderDetailInstrumentById(tQuery2).then((response) => {
                            this.loading = false;
                            if (response.code === 20000) {
                                this.closeDialogC2(response.data);
                            } else {
                                this.$message.error(response.message);
                            }
                        }).catch(() => {
                            this.loading = false;
                        });
                    } else {
                        this.thisData = response.data;
                        this.thisData.code = this.code;
                        this.thisData.orderEditor = this.orderEditor;
                        this.thisData.orderId = "";
                        if (this.orderEditor) {
                            this.thisData.orderId = this.formData.id
                        }
                        this.selectProductVisible = true;
                    }
                } else {
                    this.$message.warning(response.message);
                    if (this.$isNotBlank(event)) {
                        event.target.select();
                    }
                    // event.target.select();
                    // event.currentTarget.select();
                }
            });


        },
        closeDialogC2(rData) {//012693548470801311200226172302271020022632100025
            this.selectProductVisible = false;
            this.thisData = {};

            if (this.$isNotBlank(rData)) {
                this.codeArray = [];
                rData.forEach((obj, index) => {
                    if (this.codeArray.length === 0) {
                        this.codeArray.push(obj);
                    } else {
                        this.codeArray.unshift(obj);
                    }
                });
                this.$refs.multipleTable.setCurrentRow(this.codeArray[0]);
                this.currentRow = this.codeArray[0];
                this.selectedIndex = 0;
                this.focusNext('iCount');
            }
        },
        rowChange(val) {
            this.currentRow = val;
            this.selectedIndex = val.index;
            this.iCount = val.reCount;

            this.focusNext('iCount');
        },
        iCountChange() {
        },
        iCountEnterFunction() {
            if (this.$isNotBlank(this.currentRow)) {
                this.currentRow.count = this.iCount;
                this.currentRow.reCount = this.iCount;
            }
            this.focusNext('inputRef');
        },
        tableCountChange(row) {
            if (this.$isNotBlank(row)) {
            }
        },
        tableRowClassName({row, rowIndex}) {
            row.index = rowIndex;
        },
        focusNext(nextRef) {
            this.$refs[nextRef].focus()
        },
        focusNextToTable(nextRef) {
            this.$refs[nextRef][0].focus()
        },
        remoteMethod(query) {
            if (query !== '') {
                this.sLoading = true;
                let tQuery = {
                    key: query,
                    page: 1,
                    limit: 20,
                };
                getBasicUnitMaintains2(tQuery)
                    .then((response) => {
                        this.sLoading = false;
                        this.sOptions = response.data.list;
                    })
                    .catch(() => {
                        this.sLoading = false;
                        this.sOptions = [];
                    });
            } else {
                this.sOptions = [];
            }
        },
        // 刷新表单
        resetForm() {
            if (this.$refs["dataForm"]) {
                // 清空验证信息表单
                this.$refs["dataForm"].clearValidate();
                // 刷新表单
                this.$refs["dataForm"].resetFields();
                this.getList();
            }
        },
        addCode() {
            this.code = this.code.trim();
            if (this.$isBlank(this.code)) {
                this.$message.warning('请输入条码');
                return;
            }
            if (this.iCount <= 0) {
                this.$message.warning('请输入数量');
                return;
            }
            this.loading = true;
            let tQuery = {
                orderId: this.formData.id,
                count: this.iCount,
                code: this.code,
            };

            //012693548470801311200226172302271020022632100025
            if (this.orderEditor) {
                addStockOrderDetailFromCode(tQuery).then((response) => {
                    this.loading = false;
                    if (response.code === 20000) {
                        this.getStockOrderDetailList();
                    } else {
                        this.$alert(response.message, '提示', {
                            confirmButtonText: '确定',
                            type: 'warning',
                            closeOnClickModal: true,
                            callback: action => {
                                this.$refs.inputRef.focus();
                            }
                        });
                    }
                });
            } else {
                getStockOrderDetailInstrument(tQuery).then((response) => {
                    this.loading = false;
                    if (response.code === 20000) {
                        for (let i = 0; i < this.codeArray.length; i++) {
                            if (this.codeArray[i].productId === response.data.productId) {
                                this.$alert("已添加", '提示', {
                                    confirmButtonText: '确定',
                                    type: 'warning',
                                    closeOnClickModal: true,
                                    callback: action => {
                                        this.$refs.inputRef.focus();
                                    }
                                });
                                break;
                            }
                            if (i + 1 === this.codeArray.length) {
                                this.codeArray.push(response.data);
                            }
                        }
                        if (this.codeArray.length === 0) {
                            this.codeArray.push(response.data);
                        }
                    } else {
                        this.$alert(response.message, '提示', {
                            confirmButtonText: '确定',
                            type: 'warning',
                            closeOnClickModal: true,
                            callback: action => {
                                this.$refs.inputRef.focus();
                            }
                        });
                    }
                });
            }
        },
        saveCodeArray(index, row) {
            if (this.$isBlank(row.batchNo)) {
                return this.$message.error('批次号不能为空');
            }
            if (this.$isBlank(row.productDate)) {
                return this.$message.error('生产日期不能为空');
            }
            if (this.$isBlank(row.expireDate)) {
                return this.$message.error('失效日期不能为空');
            }

            this.$refs.multipleTable.setCurrentRow();
            this.currentRow = {};
            this.selectedIndex = "";
            this.iCount = 0;

            this.detailLoading = true;
            // row.count = row.reCount;
            let tQuery = row;
            uploadStockOrderDetail(tQuery).then((response) => {
                this.detailLoading = false;
                if (response.code === 20000) {
                    this.$message.success("修改完成");
                    this.getStockOrderDetailList();
                    this.$refs.inputRef.focus();
                } else {
                    this.$message.error(response.message);
                }
            });
        },
        deleteCodeArray(index, row) {
            this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.$refs.multipleTable.setCurrentRow();
                this.currentRow = {};
                this.selectedIndex = "";
                this.iCount = 0;

                if (this.orderEditor) {
                    this.detailLoading = true;
                    if (this.$isNotBlank(row.id)) {
                        deleteStockOrderDetailById({id: row.id})
                            .then(response => {
                                this.detailLoading = false;
                                if (response.code === 20000) {
                                    this.$message.success(response.data);
                                    // this.getStockOrderDetailList();
                                    this.codeArray.splice(index, 1);
                                } else {
                                    this.$message.error(response.message);
                                }
                            })
                            .catch(() => {
                                this.detailLoading = false;
                            })
                    } else {
                        this.$message.success('删除成功');
                        this.codeArray.splice(index, 1);
                    }
                } else {
                    this.$message.success('删除成功');
                    this.codeArray.splice(index, 1);
                }
            }).catch(() => {

            });
        },
        copyDetail(row) {
            this.$confirm("是否复制该记录?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                // let tQuery = {
                //     id: row.id
                // }
                // this.detailLoading = true;
                // copyStockOrderDetail(tQuery)
                //     .then((response) => {
                //         this.detailLoading = false;
                //         if (response.code === 20000) {
                //             this.$message.success("复制成功");
                //             this.getStockOrderDetailList();
                //         } else {
                //             this.$message.error(response.message);
                //         }
                //     });

                let rData = JSON.parse(JSON.stringify(row));
                rData.id = null;
                if (this.codeArray.length === 0) {
                    this.codeArray.push(rData);
                    this.$refs.multipleTable.setCurrentRow(this.codeArray[0]);
                    this.currentRow = this.codeArray[0];
                    this.selectedIndex = 0;
                } else {
                    this.codeArray.unshift(rData);
                    this.$refs.multipleTable.setCurrentRow(this.codeArray[0]);
                    this.currentRow = this.codeArray[0];
                    this.selectedIndex = 0;
                }
                this.$message.success("复制成功");
                this.iCount = this.currentRow.reCount;
                this.focusNext('iCount');
            }).catch(() => {
            });
        },
        getInputFocus(event) {
            event.currentTarget.select();
        },
        tableSelection() {
            this.$refs.multipleTable.clearSelection();
            this.$refs.multipleTable.toggleAllSelection();
        },
        handleSizeChange(val) {
            this.query.limit = val;
            this.getStockOrderDetailList();
        },
        handleCurrentChange(val) {
            this.query.page = val;
            this.getStockOrderDetailList();
        },
        getStockOrderDetailList() {
            this.loading = true;
            stockOrderDetail(this.query) //查找该单号下的所有条码
                .then((response) => {
                    this.codeArray = response.data.list || [];
                    this.total = response.data.total || 0;
                    this.loading = false;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                    this.total = 0;
                });
        },
        intentBack() {
            this.$router.go(-1);
        },
        generateBillNo() {
            let tQuery = {
                action: this.formData.billType
            }
            this.loading = true;
            stockOrderGenerateBillNo(tQuery)
                .then((response) => {
                    this.loading = false;
                    if (response.code === 20000) {
                        this.formData.billNo = response.data;
                        // this.formData.billdate = parseTime(new Date(), '{y}{m}{d}');
                        this.formData.billdate = new Date();
                    } else {
                        this.closeDialog(true);
                    }
                });
        },
        getBusType() {
            let query = {
                enabled: true,
            };
            getLocalJoinBusType(query)
                .then((response) => {
                    this.busTypes = response.data.list || [];
                })
                .catch(() => {
                });
        },
        getStorage(event) {
            this.storageList = [];
            filterAllByUser()
                .then((response) => {
                    this.storageList = response.data || [];
                })
                .catch(() => {
                });
        },
        getActionName(action) {
            for (let i = 0; i < this.busTypes.length; i++) {
                if (this.busTypes[i].action === action) {
                    return this.busTypes[i].advanceType;
                }
            }
        },
        getActionItem(action) {
            for (let i = 0; i < this.busTypes.length; i++) {
                if (this.busTypes[i].localAction === action) {
                    return this.busTypes[i];
                }
            }
        },
        findStorageMethod(query) {
            this.fromStorageOptions = [];
            let cQuery = {
                key: query,
            };
            filterAllByUser(cQuery)
                .then((response) => {
                    this.fromStorageOptions = response.data || [];
                })
                .catch(() => {
                });
        },
    },
    filters: {},
    mounted() {
        document.body.ondrop = function (event) {
            event.preventDefault();
            event.stopPropagation();
        };
    },
    created() {
        this.getBusType();
        this.getStorage();
        this.formData = {};
        this.codeArray = [];
        // this.closeConfirmFunction(false);
        if (this.$isNotBlank(this.idQuery.id)) {
            this.query.limit = 100;
            this.query.orderIdFk = this.idQuery.id;
            this.formData = this.idQuery.formData;
            this.orderEditor = true;
            this.sValue = this.formData.corpName;
            this.getStockOrderDetailList();
        } else {
            this.formData = {
                billdate: "",
                corpName: store.getters.companyName,
                customerId: store.getters.customerId,
                // billType: "deliveryNote",
                billType: "",
                type: "1",
                // type: "",
            };
            this.iCount = 0;
            this.orderEditor = false;
            this.generateBillNo();
        }
    },
};
</script>
<style>

.ao-text {
    width: 100%;
    font-size: 13px;
    font-family: "Microsoft YaHei";
    float: right;
    text-align: right;
    margin-top: 10px;
}

</style>



