<template>
    <div>
        <el-form :inline="true" :model="query" class="query-form" size="mini">
            <el-form-item class="query-form-item">
                <el-input v-model="query.code" placeholder="条码查询"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button-group>
                    <el-button type="primary" icon="search" @click="search"
                    >查询
                    </el-button
                    >
                    <el-button type="primary" icon="search" @click="editOrderTime"
                    >编辑
                    </el-button
                    >
                </el-button-group>
            </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="codeArry" style="width: 100%">
            <el-table-column label="序号" type="index"></el-table-column>
            <el-table-column
                label="条码"
                prop="code"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="扫码数量"
                prop="count"
                width="180"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column label="创建时间" prop="actDate" width="220" show-overflow-tooltip>
                <template slot-scope="scope">
                    <i class="el-icon-time"></i>
                    <span>{{ scope.row.actDate }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作员" prop="actor" width="180"></el-table-column>
            <el-table-column label="操作" fixed="right" width="180">
                <template slot-scope="scope">
                    <el-button
                        size="small"
                        type="text"
                        @click.native.stop="replaceCodes(scope.row)"
                    >
                        减一
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

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

        <el-dialog
            title="码替换"
            :visible.sync="replaceVisible"
            :append-to-body="true"
            width="70%"
        >
            <el-row :gutter="20" class="el-row" type="flex">
                <el-col :span="10" class="el-col">
                    <div class="text item">
                        <div class="itemTag">
                            <span>条码:&nbsp;</span>
                        </div>
                        <el-input
                            style="width: 400px"
                            size="small"
                            splaceholder="请输入内容"
                            disabled="true"
                            v-model="codeDetail.code"
                        ></el-input>
                    </div>
                </el-col>
                <el-col :span="10" class="el-col">
                    <div class="text item">
                        <div class="itemTag">
                            <span>扫码数量:&nbsp;</span>
                        </div>
                        <el-input
                            style="width: 400px"
                            disabled="true"
                            size="small"
                            splaceholder="请输入内容"
                            v-model="codeDetail.count"
                        ></el-input>
                    </div>
                </el-col>
            </el-row>

            <el-row :gutter="20" class="el-row" type="flex">
                <el-col :span="10" class="el-col">
                    <div class="text item">
                        <div class="itemTag">
                            <span>替换条码:&nbsp;</span>
                        </div>
                        <el-input
                            style="width: 400px"
                            size="small"
                            splaceholder="请输入内容"
                            v-model="codeDetail.newCode"
                        ></el-input>
                    </div>
                </el-col>
                <el-col :span="10" class="el-col">
                    <div class="text item">
                        <div class="itemTag">
                            <span>扫码数量:&nbsp;</span>
                        </div>
                        <el-input
                            style="width: 400px"
                            size="small"
                            splaceholder="请输入内容"
                            v-model="codeDetail.newCount"
                        ></el-input>
                    </div>
                </el-col>
            </el-row>

            <div style="text-align: center">
                <el-button type="primary" size="small" icon="search" @click="replaceCodes"
                >提交
                </el-button
                >
                <el-button type="primary" size="small" icon="search" @click="cancelDialog"
                >取消
                </el-button
                >
            </div>
        </el-dialog>

        <el-dialog
            title="修改时间"
            :visible.sync="editOrderVisible"
            width="35%"
            append-to-body
            v-if="editOrderVisible"
        >

            <el-row style="width: 100%">
                <el-lable>创建时间：</el-lable>
                <el-date-picker
                    v-model="actDate"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    :picker-options="pickerOptions"
                    placeholder="选择日期时间">
                </el-date-picker>
            </el-row>

            <el-row style="width: 100%; margin-top: 20px;">
                <el-lable>审核时间：</el-lable>
                <el-date-picker
                    v-model="auditTime"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    :picker-options="pickerOptions"
                    placeholder="选择日期">
                </el-date-picker>
            </el-row>

            <div style="text-align: right; margin-top: 10px;">
                <el-button type="primary" size="small" icon="search" @click="updateOrderInfo"
                >提交
                </el-button
                >
                <el-button type="primary" size="small" icon="search" @click="cancelEdit"
                >取消
                </el-button
                >
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {codeList, updateOrderInfo, getOrderById} from "../../api/warehouse/order";
    import draggable from "vuedraggable";
    import {replaceCodes} from "@/api/basic/invWarehouse";

    export default {
        name: "idQuery",
        props: {
            idQuery: {
                type: Object,
                required: true,
            },
        },
        data() {
            return {
                query: {
                    code: "",
                    corpOrderId: "",
                    page: 1,
                    limit: 20,
                },
                codeArry: [],
                total: 0,
                loading: true,
                index: null,
                formLoading: false,
                formVisible: false,
                replaceVisible: false,
                deleteLoading: false,
                orderNo: null,
                busTypes: [],
                codeDetail: {
                    code: null,
                    count: null,
                    newCode: null,
                    newCount: null,
                    orderId: null
                },
                actDate: null,
                auditTime: null,
                editOrderVisible: false
            };
        },
        components: {
            draggable,
        },
        methods: {
            onReset() {
                this.$router.push({
                    path: "",
                });
                this.query = {
                    code: "",
                    corpOrderId: "",
                    page: 1,
                    limit: 20,
                };
                this.getCodeList();
            },
            onSubmit() {
                this.$router.push({
                    path: "",
                    query: this.query,
                });
                this.getCodeList();
            },
            handleSizeChange(val) {
                this.query.limit = val;
                this.getCodeList();
            },
            handleCurrentChange(val) {
                this.query.page = val;
                this.getCodeList();
            },
            search() {
                this.query.page = 1;
                this.getCodeList();
            },
            getCodeList() {
                this.loading = true;
                this.query.corpOrderId = this.idQuery.id;
                codeList(this.query) //查找该单号下的所有条码
                    .then((response) => {
                        this.loading = false;
                        this.codeArry = response.data.list || [];
                        this.total = response.data.total || 0;
                    })
                    .catch(() => {
                        this.loading = false;
                        this.list = [];
                        this.total = 0;
                    });
            },
            intentBack() {
                this.$router.go(-1);
            },
            showReplaceDialog(row) {
                this.replaceVisible = true;
                this.codeDetail = {
                    code: row.code,
                    count: row.count,
                    orderId: row.orderId
                }
            },
            cancelDialog() {
                this.replaceVisible = false;
                this.codeDetail = {
                    code: null,
                    count: null,
                    newCode: null,
                    newCount: null
                };
            },
            replaceCodes(row) {
                this.$confirm("是否确认库存减一", "提示", {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.codeDetail = {
                        code: row.code,
                        count: row.count,
                        orderId: row.orderId
                    };
                    replaceCodes(this.codeDetail).then((res) => {
                        if (res.code === 20000) {
                            this.$message.success("操作成功");
                            this.getCodeList();
                        } else {
                            this.$message.error(res.message);
                        }
                    }).catch((error) => {
                        this.$message.error(error.message);
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消操作'
                    });
                });
            },
            editOrderTime() {
                let getOrderDataParam = {
                    id: this.query.corpOrderId,
                };
                getOrderById(getOrderDataParam).then((res) => {
                    this.actDate = res.data.actDate;
                    this.auditTime = res.data.auditTime;
                    this.editOrderVisible = true;
                }).catch((error) => {
                });
            },
            updateOrderInfo() {
                if (this.actDate === null || this.auditTime === null) {
                    this.$message.warning("创建时间和审核时间不能为空");
                    return;
                } else if (this.actDate > this.auditTime) {
                    this.$message.warning("请检查创建时间和审核时间是否正确");
                    return;
                }
                let data = {
                    actDate: this.actDate,
                    auditTime: this.auditTime,
                    orderIds: [this.query.corpOrderId]
                };
                updateOrderInfo(data).then((res) => {
                    if (res.code === 20000) {
                        this.$message.success("修改成功");
                    }
                }).catch((error) => {
                });
                this.actDate = null;
                this.auditTime = null;
                this.editOrderVisible = false;
            },
            cancelEdit() {
                this.editOrderVisible = false;
                this.actDate = null;
                this.auditTime = null;
            }
        },
        filters: {},
        mounted() {
            document.body.ondrop = function (event) {
                event.preventDefault();
                event.stopPropagation();
            };
        },
        created() {
            // 将参数拷贝进查询对象
            let query = this.$route.query;
            this.orderNo = query.id;

            this.query = Object.assign(this.query, query);
            this.query.limit = parseInt(this.query.limit);
            this.query.corpOrderId = query.id;

            // 加载表格数据
            this.getCodeList();
        },
    };
</script>

<style scoped>
    .itemTag {
        float: left;
        text-align: left;
        margin-top: 10px;
        width: 100px;
    }

    .text {
        font-size: 13px;
        font-family: "Microsoft YaHei";
    }

    .el-row {
        display: flex;
        flex-wrap: wrap;
        margin-bottom: 20px;
    }

    .el-col {
        border-radius: 4px;
        flex-wrap: wrap;
    }
</style>




