<template>
    <div>
        <el-card class="el-card">
            <el-form
                :inline="true"
                :model="query"
                class="query-form"
                size="mini"

            >
                <el-row style="width: 100%">
                    <el-form-item class="query-form-item">
                        <el-input v-model="query.orderId" placeholder="单据号"></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-select v-model="query.action" placeholder="请选择业务类型" clearable="true" size="mini">
                            <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-form-item class="query-form-item" v-if="isSp ==110">
                        <el-input v-model="query.corpName" placeholder="往来单位" clearable></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-select v-model="query.locStorageCode" placeholder="请选择当前仓库" clearable="true" size="mini">
                            <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-form-item class="query-form-item">
                        <span style="color: #000; margin-left: 10px; margin-right: 6px">单据日期:</span>
                        <el-date-picker
                            :picker-options="pickerOptions"
                            v-model="actDateRange"
                            type="daterange"
                            format="yyyy 年 MM 月 dd 日"
                            value-format="yyyy-MM-dd"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item>
                        <el-button-group>
                            <el-button type="primary" icon="el-icon-refresh" @click="onReset"></el-button>
                            <el-button type="primary" icon="search" @click="onSubmit">查询</el-button>
                        </el-button-group>
                    </el-form-item>

                </el-row>
            </el-form>
            <el-table v-loading="loading"
                      :data="list"
                      style="width: 100%;" highlight-current-row
                      @row-click="getStockOrderDetailList" ref="multipleTable">
                <el-table-column
                    type="selection"
                    width="55">
                </el-table-column>
                <el-table-column type="index" label="序号" width="50"></el-table-column>
                <el-table-column label="单据号" prop="orderId" show-overflow-tooltip></el-table-column>
                <el-table-column label="往来单位" prop="fromCorp" show-overflow-tooltip></el-table-column>
                <el-table-column label="单据来源" prop="fromType" width="150" show-overflow-tooltip></el-table-column>
                <el-table-column label="单据日期" prop="actDate" show-overflow-tooltip></el-table-column>
                <el-table-column label="单据状态" show-overflow-tooltip>
                    <template slot-scope="scope">
                        {{ scope.row.status | statusFilterName }}
                    </template>
                </el-table-column>
                <el-table-column label="业务类型" prop="action" show-overflow-tooltip>
                    <template slot-scope="scope">
                        <span>{{ getActionName(scope.row.action) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="当前仓库" prop="locStorageCode" width="150">
                    <template slot-scope="scope">
                        <span>{{ getStorageName(scope.row.locStorageCode) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="200">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="intentDetail(scope.row)"
                        >详情
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            @click.native="printOrder(scope.row)"
                        >打印
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            @click.native="handleDetailForm(scope.$index, scope.row)"
                        >发票
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            @click.native="updateReceiveStatus(scope.$index, scope.row)"
                        >完成验收
                        </el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                :current-page="query.page"
                :page-size="query.limit"
                @current-change="handleCurrentChange"
                layout="prev, pager, next"
                :total="total"
            >
            </el-pagination>
        </el-card>

        <el-card class="el-card">
            <el-table v-loading="detailLoading"
                      :data="detailList"
                      @selection-change="changeFun"
                      style="width: 100%">

                <el-table-column
                    type="selection"
                    width="55">
                </el-table-column>

                <el-table-column type="index" label="序号" width="50"></el-table-column>
                <el-table-column label="产品通用名" width="200" prop="coName"></el-table-column>
                <el-table-column label="规格型号" width="200" prop="packSpec"></el-table-column>
                <el-table-column label="批次号" width="100" prop="lotno"></el-table-column>
                <el-table-column label="生产日期" width="120" prop="produceDate"></el-table-column>
                <el-table-column label="失效日期" width="120" prop="expireDate"></el-table-column>
                <el-table-column label="数量" width="100" prop="count"></el-table-column>
                <el-table-column label="单价" width="120" prop="price">
                    <template slot-scope="scope">
                        <el-input :disabled="scope.$index !== currentCheckIndex" v-model="scope.row.price"
                                  placeholder="请输入"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="注册/备案凭证号" width="150" prop="authCode"></el-table-column>
                <el-table-column label="生产厂家" width="150" prop="productCompany"></el-table-column>
                <el-table-column label="销售清单号（第二票）" width="180" prop="secSalesListNo">
                    <template slot-scope="scope">
                        <el-input :disabled="scope.$index !== currentCheckIndex" v-model="scope.row.secSalesListNo"
                                  placeholder="请输入"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="销售发票号（第一票）" width="180" prop="firstSalesInvNo">
                    <template slot-scope="scope">
                        <el-input :disabled="scope.$index !== currentCheckIndex" v-model="scope.row.firstSalesInvNo"
                                  placeholder="请输入"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="销售发票号（第二票）" width="180" prop="secSalesInvNo">
                    <template slot-scope="scope">
                        <el-input :disabled="scope.$index !== currentCheckIndex" v-model="scope.row.secSalesInvNo"
                                  placeholder="请输入"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="发票日期" width="150" prop="invoiceDate">
                    <template slot-scope="scope">
                        <el-date-picker :disabled="scope.$index !== currentCheckIndex" format="yyyy-MM-dd"
                                        value-format="yyyy-MM-dd" type="date" placeholder="选择日期"
                                        v-model="scope.row.invoiceDate" style="width: 100%;"></el-date-picker>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="150" prop="price" fixed="right">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑
                        </el-button>
                        <el-button
                            type="text"
                            size="small"
                            @click="handleSave(scope.$index, scope.row)"
                        >保存
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                :current-page="detailQuery.page"
                :page-size="detailQuery.limit"
                @current-change="detailHandleCurrentChange"
                layout="prev, pager, next"
                :total="detailTotal"
            >
            </el-pagination>
        </el-card>

        <el-dialog v-bind:width="'25%'" :visible.sync="salesListNoFormVisible">
            <el-form :model="batchForm">
                <label></label>
                <el-form-item label="销售清单：" :label-width="formLabelWidth">
                    <el-input style="width: 80%;;" v-model="batchForm.secSalesListNo" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="salesListNoFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="batchSetParams('1','salesListNo')">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog v-bind:width="'25%'" :visible.sync="firstSalesInvNoFormVisible">
            <el-form :model="batchForm">
                <label></label>
                <el-form-item label="销售发票第一票：" :label-width="formLabelWidth">
                    <el-input style="width: 80%;;" v-model="batchForm.firstSalesInvNo" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="firstSalesInvNoFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="batchSetParams('1','firstSalesInvNo')">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog v-bind:width="'25%'" :visible.sync="secSalesInvNoFormVisible">
            <el-form :model="batchForm">
                <label></label>
                <el-form-item label="销售发票第二票：" :label-width="formLabelWidth">
                    <el-input style="width: 80%;" v-model="batchForm.secSalesInvNo" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="发票日期：" :label-width="formLabelWidth">
                    <el-date-picker format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期"
                                    v-model="batchForm.invoiceDate" style="width: 80%;"></el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="secSalesInvNoFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="batchSetParams('1','secSalesInvNo')">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog
            title="单据设置"
            :visible.sync="stockOrderEditVisible"
            width="80%"
            v-if="stockOrderEditVisible"
            @close='closeDialog'
            :close-on-click-modal="false"
            :close-on-press-escape="false"
        >
            <StockOrderEdit
                :closeDialog="closeDialog"
                :detailList="detailList"

            ></StockOrderEdit>
        </el-dialog>


        <el-dialog
            title="单据设置"
            :visible.sync="distributionSetVisible"
            width="80%"
            v-if="distributionSetVisible"
            @close='closeDialog'
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            append-to-body
        >
            <stockOrderEditDistribution
                :closeDialog="closeDialog"
                :detailList="detailList"

            ></stockOrderEditDistribution>
        </el-dialog>
        <el-dialog
            title="配货"
            :visible.sync="distributionVisible"
            width="60%"
            v-if="distributionVisible"
            @close='closeDialog'
        >
            <stockOrderDistribution
                :closeDialog="closeDialog"
                :idQuery="idQuery"
            ></stockOrderDistribution>
        </el-dialog>

        <el-dialog
            title="编辑"
            :visible.sync="detailFormVisible"
            :before-close="hideDetailForm"
            width="85%"
            top="5vh"
        >
            <el-form :model="detailFormData" :rules="detailFormRules" ref="dataForm">

                <el-row :gutter="20">
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>产品通用名：</span>
                        </div>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item prop="productName">
                            <el-input v-model="detailFormData.productName" 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="spec">
                            <el-input v-model="detailFormData.spec" auto-complete="off" :disabled="true"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>单据数量：</span>
                        </div>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item prop="count">
                            <el-input v-model="detailFormData.count" 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="reCount">
                            <el-input v-model="detailFormData.reCount" auto-complete="off" :disabled="true"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>生产日期：</span>
                        </div>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item prop="productDate">
                            <el-input v-model="detailFormData.productDate" auto-complete="off"
                                      :disabled="false"></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="expireDate">
                            <el-input v-model="detailFormData.expireDate" auto-complete="off"
                                      :disabled="false"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>批次号：</span>
                        </div>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item prop="batchNo">
                            <el-input v-model="detailFormData.batchNo" auto-complete="off" :disabled="false"></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="sweepCount">
                            <el-input v-model="detailFormData.sweepCount" auto-complete="off" :disabled="false"
                                      oninput="value=value.replace(/[^\d]/g,'')"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="hideDetailForm">取消</el-button>
                <el-button
                    type="primary"
                    @click.native="submitUploadDetail()"
                    :loading="detailFormLoading"
                >提交
                </el-button
                >
            </div>
        </el-dialog>

        <el-dialog
            title="单号详情"
            :visible.sync="codeDetailVisible"
            width="80%"
            v-if="codeDetailVisible"
        >
            <codeDetail
                :idQuery="idQuery"
                v-on:closeDetailDialog="closeDetailDialog"
            ></codeDetail>
        </el-dialog>

    </div>
</template>

<script>
import {
    stockOrderList, stockOrderDetail,
    deleteStockOrderById, deleteStockOrderDetailById,
    copyStockOrderDetail, uploadStockOrderDetail,
    updateStockOrderDetail, updateReceiveStatus
} from "../../api/warehouse/stockOrder";
import stockOrderDistribution from "./stockOrderDistribution";
import stockOrderQRCodeDetail from "./stockOrderQRCodeDetail";
import stockOrderEditDistribution from "./stockOrderEditDistribution";
import stockQRCodeExport from "./stockQRCodeExport";
import draggable from "vuedraggable";
import {inspectionStockOrderPDFFromTemplateFile, stockOrderPDFFromTemplateFile} from "../../api/itextpdf/itextpdf";
import store from "../../store";
import {filterAllByUser} from "@/api/basic/invWarehouse";
import {getBusTypeByUser} from "../../api/warehouse/BusRole"
import StockOrderEdit from "@/views/warehouse/stockOrderEdit";
import codeDetail from "./code";

export default {
    name: "stockOrderSearch",
    data() {
        return {
            query: {
                action: null,
                corpName: null,
                locStorageCode: null,
                startTime: "",
                endTime: "",
                orderId: "",
                status: 3,
                page: 1,
                limit: 10
            },
            detailQuery: {
                orderIdFk: "",
                page: 1,
                limit: 20
            },
            busTypes: [],
            list: [],
            detailList: [],
            currentCheckIndex: -1,
            multipId: [],
            batchForm: {
                secSalesListNo: '',
                firstSalesInvNo: '',
                secSalesInvNo: '',
                invoiceDate: ''
            },
            salesListNoFormVisible: false,
            firstSalesInvNoFormVisible: false,
            secSalesInvNoFormVisible: false,
            codeDetailVisible: false,
            formLabelWidth: '150px',
            storageList: [],
            total: 0,
            detailTotal: 0,
            loading: false,
            detailLoading: false,
            idQuery: {
                id: "",
            },
            distributionSetVisible: false,
            stockOrderEditVisible: false,
            distributionVisible: false,
            detailFormRules: {
                batchNo: [
                    {required: true, message: "请输入批次号", trigger: "blur"}
                ],
                productDate: [
                    {required: true, message: "请输入生产日期", trigger: "blur"}
                ],
                expireDate: [
                    {required: true, message: "请输入失效日期", trigger: "blur"}
                ],
                sweepCount: [
                    {required: true, message: "请输入扫码数量", trigger: "blur"}
                ]
            },
            sourceMap: {
                "1": "web端新增单据",
                "2": "第三方系统",
                "3": "扫码单据转换",
            },
            isSp: store.getters.customerId,
            detailFormVisible: false,
            detailFormData: {},
            detailFormLoading: false,
            haveDistributionVisible: true,
            qrcodeDetailVisible: false,
            qrcodeExportVisible: false,
            actDateRange: [],
            pickerOptions: {
                shortcuts: [
                    {
                        text: "最近一周",
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit("pick", [start, end]);
                        },
                    },
                    {
                        text: "最近一个月",
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit("pick", [start, end]);
                        },
                    },
                    {
                        text: "最近三个月",
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                            picker.$emit("pick", [start, end]);
                        },
                    },
                ],
            },
        }
    },
    methods: {
        onReset() {
            this.$router.push({
                path: ""
            });
            this.query = {
                action: null,
                corpName: null,
                locStorageCode: null,
                startTime: "",
                endTime: "",
                orderId: "",
                status: 3,
                page: 1,
                limit: 10
            };
            this.getList();
            this.haveDistributionVisible = true;
            this.actDateRange = [];
        },
        onSubmit() {
            if (this.actDateRange !== null) {
                this.query.startTime = this.actDateRange[0];
                this.query.endTime = this.actDateRange[1];
            } else {
                this.query.startTime = null;
                this.query.endTime = null;
            }
            this.query.page = 1;
            this.getList();
            if (this.query.status === "202" || this.query.status === "502") {
                this.haveDistributionVisible = true;
            } else {
                this.haveDistributionVisible = false;
            }
        },
        handleCurrentChange(val) {
            this.query.page = val;
            this.getList();
        },
        handleEdit(index, row) {
            this.currentCheckIndex = index;
        },
        handleSave(index, row) {
            this.currentCheckIndex = -1;

            updateStockOrderDetail(row).then((res) => {
                if (res.code == 20000) {
                    this.$message.success(res.data);
                }
            })
        },
        changeFun(row) {
            let _this = this;
            _this.multipId = []; //每次改动重置ID
            row.forEach((item) => {
                _this.multipId.push(item.id);
            })
        },
        batchSetParams(flag, type) {
            if (flag == '1' && type == 'salesListNo') {
                this.salesListNoFormVisible = false;
                this.detailList.forEach(data => {
                    this.multipId.forEach(id => {
                        if (data.id == id) {
                            data.secSalesListNo = this.batchForm.secSalesListNo;
                            //更新到数据库
                            updateStockOrderDetail(data);
                        }
                    })
                });
                //置空批量设置表单
                Object.keys(this.batchForm).forEach(key => this.batchForm[key] = '');
            } else if (flag == '1' && type == 'firstSalesInvNo') {
                this.firstSalesInvNoFormVisible = false;
                this.detailList.forEach(data => {
                    this.multipId.forEach(id => {
                        if (data.id == id) {
                            data.firstSalesInvNo = this.batchForm.firstSalesInvNo;
                            //更新到数据库
                            updateStockOrderDetail(data);
                        }
                    })
                });
                Object.keys(this.batchForm).forEach(key => this.batchForm[key] = '');
            } else if (flag == '1' && type == 'secSalesInvNo') {
                this.secSalesInvNoFormVisible = false;
                this.detailList.forEach(data => {
                    this.multipId.forEach(id => {
                        if (data.id == id) {
                            data.secSalesInvNo = this.batchForm.secSalesInvNo;
                            data.invoiceDate = this.batchForm.invoiceDate;
                            //更新到数据库
                            updateStockOrderDetail(data);
                        }
                    })
                });
                Object.keys(this.batchForm).forEach(key => this.batchForm[key] = '');
            } else {
                if (this.multipId.length == 0) {
                    this.$message.warning("请选择数据");
                } else {
                    switch (type) {
                        case 'salesListNo':
                            this.salesListNoFormVisible = true;
                            return;
                        case 'firstSalesInvNo':
                            this.firstSalesInvNoFormVisible = true;
                            return;
                        case 'secSalesInvNo':
                            this.secSalesInvNoFormVisible = true;
                            return;
                        default:
                            return;
                    }
                }
            }
        },
        detailHandleCurrentChange(val) {
            this.query.page = val;
            this.getStockOrderDetailList();
        },
        // 刷新表单
        resetForm() {
            if (this.$refs["dataForm"]) {
                // 清空验证信息表单
                this.$refs["dataForm"].clearValidate();
                // 刷新表单
                this.$refs["dataForm"].resetFields();
                this.getList();
            }
        },
        getList() {
            this.loading = true;
            this.query.corpId = store.getters.customerId;
            this.query.receiveStatus = 0;
            stockOrderList(this.query)
                .then(response => {
                    this.loading = false;
                    this.list = response.data.list || [];
                    this.total = response.data.total || 0;
                    this.detailList = [];
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                    this.total = 0;
                    this.detailList = [];
                });
        },
        distributionForm(index, row) {
            this.idQuery.id = '';
            if (this.$isNotBlank(row.id)) {
                this.idQuery.id = row.id;
                this.idQuery.formData = row;
                this.distributionVisible = true;
            }
        },
        closeDialog(type) {
            this.distributionVisible = false;
            this.getList();
            if (this.$isNotBlank(type)) {
                this.detailList = [];
            }
        },
        printQRCodeText(row) {
            this.idQuery.id = '';
            if (this.$isNotBlank(row.id)) {
                this.idQuery.id = row.id;
                this.idQuery.formData = row;
                this.qrcodeExportVisible = true;
            }
        },
        printOrder(row) {
            let tQuery = {
                queryId: row.id,
                moduleId: 4
            };
            this.loading = true;
            inspectionStockOrderPDFFromTemplateFile(tQuery).then((response) => {
                if (response.code === 20000) {
                    tQuery = {
                        orderId: row.orderId,
                        moduleId: 4
                    }
                    stockOrderPDFFromTemplateFile(tQuery).then((response) => {
                        //将pdf文件转换为url。
                        const binaryData = [];
                        binaryData.push(response);
                        //获取blob链接。
                        let url = window.URL.createObjectURL(
                            new Blob(binaryData, {type: "application/pdf"})
                        );
                        this.loading = false;
                        window.open(url);//打开新标签页，预览pdf。
                    }).catch(() => {
                        this.loading = false;
                    });
                } else {
                    this.loading = false;
                    this.$message.error(response.message);
                }
            }).catch(() => {
                this.loading = false;
            });
        },
        closeQRCodeExportDialog() {
            this.qrcodeExportVisible = false;
            this.getList();
        },
        qrcodeDetail(row) {
            this.idQuery.id = '';
            if (this.$isNotBlank(row.id)) {
                this.idQuery.id = row.id;
                this.idQuery.formData = row;
                this.qrcodeDetailVisible = true;
            }
        },
        closeQRCodeDetailDialog() {
            this.qrcodeDetailVisible = false;
            this.getList();
        },
        getStockOrderDetailList(row) {
            if (this.$isNotBlank(row)) {
                this.detailQuery.orderId = row.orderId;
            }
            this.detailLoading = true;
            stockOrderDetail(this.detailQuery)
                .then((response) => {
                    this.detailLoading = false;
                    this.detailList = response.data || [];
                })
                .catch(() => {
                    this.detailLoading = false;
                    this.detailList = [];
                });
        },
        deleteStockOrder(row) {
            this.$confirm("此操作将永久删除该订单, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.loading = true;
                deleteStockOrderById({id: row.id})
                    .then(response => {
                        this.loading = false;
                        if (response.code === 20000) {
                            this.$message.success(response.data);
                            this.getList();
                            this.detailList = [];
                        } else {
                            this.$message.error(response.message);
                        }
                    })
                    .catch(() => {
                        this.loading = false;
                    })
            }).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);
                        }
                    });
            }).catch(() => {
            });
        },
        deleteStockOrderDetail(row) {
            this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.detailLoading = true;
                deleteStockOrderDetailById({id: row.id})
                    .then(response => {
                        this.detailLoading = false;
                        if (response.code === 20000) {
                            this.$message.success(response.data);
                            this.getStockOrderDetailList();
                        } else {
                            this.$message.error(response.message);
                        }
                    })
                    .catch(() => {
                        this.detailLoading = false;
                    })
            }).catch(() => {

            });
        },
        // 显示表单
        handleDetailForm(index, row) {
            this.stockOrderEditVisible = true;
        },
        hideDetailForm() {
            // 更改值
            this.detailFormVisible = !this.detailFormVisible;
            return true;
        },
        getActionName(action) {
            for (let i = 0; i < this.busTypes.length; i++) {
                if (this.busTypes[i].action === action) {
                    return this.busTypes[i].name;
                }
            }
        },
        getBusType() {
            let query = {};
            getBusTypeByUser(query).then((res) => {
                this.busTypes = res.data.list || [];
                this.filterQuery.billAction = this.busTypes[0].action;
            }).catch((error) => {
            });
        },
        getStorageName(code) {
            for (let i = 0; i < this.storageList.length; i++) {
                if (this.storageList[i].code === code) {
                    return this.storageList[i].name;
                }
            }
        },
        getStorage() {
            this.storageList = [];
            filterAllByUser()
                .then((response) => {
                    this.storageList = response.data || [];
                })
                .catch(() => {
                });
        },
        submitUploadDetail() {
            this.$refs["dataForm"].validate(valid => {
                if (valid) {
                    this.detailLoading = true;
                    let tQuery = this.detailFormData;
                    uploadStockOrderDetail(tQuery).then((response) => {
                        this.detailLoading = false;
                        if (response.code === 20000) {
                            this.$message.success("修改完成");
                            this.hideDetailForm();
                            this.getStockOrderDetailList();
                        } else {
                            this.$alert(response.message, '提示', {
                                confirmButtonText: '确定',
                                type: 'warning',
                                closeOnClickModal: true,
                                callback: action => {
                                    this.$refs.inputRef.focus();
                                }
                            });
                        }
                    });
                }
            });
        },
        //更新验收状态
        updateReceiveStatus(index, row) {
            let params = {
                orderId: row.orderId
            };
            updateReceiveStatus(params).then((res) => {
                this.$message.success("验收成功");
                this.getList();
            }).error((error) => {
                this.$message.error("验收失败");
            })
        },
        intentDetail(row) {
            this.codeDetailVisible = true;
            this.idQuery.id = row.orderId;
        }
    }
    ,
    components: {
        codeDetail,
        StockOrderEdit,
        draggable,
        stockOrderDistribution,
        stockOrderEditDistribution,
        stockOrderQRCodeDetail,
        stockQRCodeExport,
    }
    ,
    filters: {
        statusFilterType(status) {
            const statusMap = {
                0: "gray",
                1: "success"
            };
            return statusMap[status];
        }
        ,
        statusFilterName(status) {
            const statusMap = {
                "1": "草稿",
                "2": "未配货",
                "3": "未验收",
                "4": "已校验",
            };
            return statusMap[status];
        }
        ,
        typeFilterName(type) {
            const typeMap = {
                1: "预入库",
                2: "普通采购",
            };
            return typeMap[type];
        }
    }
    ,
    created() {
        this.getStorage();
        this.getBusType();
        let end = new Date();
        let start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
        this.actDateRange = [start, end];
        this.getList();
    }
}
</script>

<style scoped>

.el-dialog {
    display: flex;
    flex-direction: column;
    margin: 0 !important;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.el-dialog .el-dialog__body {
    flex: 1;
    overflow: auto;
}
</style>
