<template>
    <div>
        <el-card class="el-card">
            <el-form
                :inline="true"
                :model="filterQuery"
                class="query-form"
                size="mini"
            >
                <el-row>
                    <el-form-item class="query-form-item">
                        <el-input v-model="filterQuery.id" placeholder="单号"></el-input>
                    </el-form-item>

                    <el-form-item class="query-form-item">
                        <el-select v-model="filterQuery.status" placeholder="单据状态">
                            <el-option label="全部" value=""></el-option>
                            <el-option label="盘点中" value="盘点中"></el-option>
                            <el-option label="已盘点" value="已盘点"></el-option>
                        </el-select>
                    </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 type="primary" icon="search" @click="createOrder"
                            >新建
                            </el-button
                            >
                        </el-button-group>
                    </el-form-item>
                </el-row>
            </el-form>
            <el-table
                v-loading="loading"
                :data="list"
                style="width: 100%"
                @row-click="getOrderDetail"
                highlight-current-row
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column label="序号" type="index"></el-table-column>

                <el-table-column label="订单ID" prop="id"></el-table-column>
                <el-table-column label="业务类型" prop="action">
                    <template slot-scope="scope">
                        <span>{{ geActionName(scope.row.action) }}</span>
                    </template>
                </el-table-column>

                <el-table-column
                    label="创建时间"
                    prop="actDate"
                    show-overflow-tooltip="true"
                >
                    <template slot-scope="scope">
                        <i class="el-icon-time"></i>
                        <span>{{ scope.row.actDate }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="结束时间"
                    prop="endTime"
                    show-overflow-tooltip="true"
                >
                    <template slot-scope="scope">
                        <i class="el-icon-time"></i>
                        <span>{{ scope.row.actDate }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="单据状态" prop="status"></el-table-column>
                <el-table-column label="操作" fixed="right" width="230">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="checkStockErp(scope.row)"
                        >校验
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="uploadOrderDialog(scope.row)"
                        >已完成
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="deleteDialog(scope.row.id)"
                        >删除
                        </el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                :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="loading"
                :row-class-name="tableRowClassName"
                :data="detailList"
                style="width: 100%"
            >
                <el-table-column label="序号" type="index"></el-table-column>

                <el-table-column
                    label="产品通用名"
                    prop="coName"
                    :show-overflow-tooltip="true"
                ></el-table-column>
                <el-table-column
                    label="包装规格"
                    prop="packSpec"
                    :show-overflow-tooltip="true"
                ></el-table-column>
                <el-table-column
                    label="批次号"
                    prop="lotno"
                    :show-overflow-tooltip="true"
                ></el-table-column>
                <el-table-column label="库存现有量" prop="reCount"></el-table-column>
                <el-table-column label="扫码数量" prop="count"></el-table-column>
                <el-table-column label="盈亏数量" prop="count">
                    <template slot-scope="scope">
                        <span>{{ scope.row.reCount - scope.row.count }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="生产企业"
                    prop="productCompany"
                    :show-overflow-tooltip="true"
                ></el-table-column>
                <el-table-column
                    label="批准文号"
                    prop="authCode"
                    :show-overflow-tooltip="true"
                ></el-table-column>
            </el-table>
        </el-card>

        <el-dialog
            title="单号详情"
            :visible.sync="codeDetailVisible"
            width="80%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="codeDetailVisible"
        >
            <codeDetail
                :idQuery="idQuery"
                v-on:closeDetailDialog="closeDetailDialog"
            ></codeDetail>
        </el-dialog>
    </div>
</template>

<script>
    import {
        updateUnit,
        mergeOrder,
    } from "../../api/inout/order";
    import {
        createStcokCheck,
        filterStcokCheck,
        finishStockCheck,
        deleteStockCheck,
        getDetail,
        checkErp,
    } from "../../api/inventory/stockCheck";
    import {getBussinessType} from "../../api/basic/bussinessType";
    import {getBasicUnitMaintains} from "../../api/basic/basicUnitMaintain"

    import draggable from "vuedraggable";

    import codeDetail from "../inout/IOFinishCode";
    import selectErpOrder from "../inout/DialogSelectErpOrder";

    const formJson = {
        site_id: "",
        site_name: "",
        describe: "",
        ads: [],
    };
    export default {
        props: {
            isSelect: {
                type: Boolean,
                default: false,
            },
        },

        data() {
            return {
                query: {
                    page: 1,
                    limit: 20,
                },
                checkStatus: {
                    error: "校验异常",
                    null: "未校验",
                    success: "正常",
                },
                //往来单位
                unitquery: {key: "", page: 1, limit: 20},
                unitlist: [],
                list: [],
                busTypes:[],
                detailList: [],
                unitUpdateQuery: {
                    id: "",
                    fromCorpId: "",
                    fromCorp: "",
                },
                filterQuery: {
                    status: null,
                    page: 1,
                    limit: 20,
                },
                detailQuery: {
                    id: "",
                },
                deleteData: {
                    id: "",
                },
                idQuery: {
                    id: "",
                },
                curIndex: "",
                adListNoDataText: "无数据",
                queryAdIdAsyncLoading: false,
                codeDetailVisible: false,
                total: 0,
                loading: true,
                index: null,
                formName: null,
                orderId: "",
                formMap: {
                    add: "新增",
                    edit: "编辑",
                },
                exportQuery: {
                    orderId: "",
                    exportStatus: "",
                },

                uploadQuery: {
                    orderId: "",
                },

                multipleSelection: [],
                dialogTableVisible: false,
                formLoading: false,
                formVisible: false,
                formData: formJson,
                deleteLoading: false,
                addDialogVisible: false,
            };
        },
        components: {
            draggable,
            codeDetail,
            selectErpOrder,
        },
        methods: {
            onReset() {
                this.$router.push({
                    path: "",
                });
                this.filterQuery = {
                    id: "",

                    action: "StockCheck",
                    page: 1,
                    limit: 20,
                    code: "",
                    exportStatus: null,
                };
                this.getList();
            },
            onSubmit() {
                this.getList();
            },

            createOrder() {
                createStcokCheck()
                    .then((response) => {
                        if (response.code == 20000) {
                            this.getList();
                        }
                        this.loading = false;
                    })
                    .catch(() => {
                        this.loading = false;
                    });
            },

            handleSizeChange(val) {
                this.filterQuery.limit = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.filterQuery.page = val;
                this.getList();
            },
            handleSelectionChange(val) {
                console.log(val);
                this.multipleSelection = val;
            },

            mergesOrders() {
                var repeatData = this.multipleSelection;
                if (repeatData.length <= 1) {
                    this.$message.warning("选择的订单需大于1");
                    return;
                }
                var orderid = repeatData[0].id;
                var unit = repeatData[0].fromCorp;
                var orderList = [];
                repeatData.forEach((obj, index) => {
                    if (unit != obj.fromCorp) {
                        this.$message.warning("往来单位不一致！");
                        return;
                    }
                    orderList.push(obj.id);
                });
                var tquery = {
                    orderList: orderList,
                };

                this.$confirm(
                    "所选择的订单将会合并到订单号为" + orderid + "的订单下",
                    "提示",
                    {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }
                )
                    .then(() => {
                        mergeOrder(tquery).then((response) => {
                            if (response.code == 20000) {
                                this.getList();
                                this.$message({
                                    type: "success",
                                    message: "合并成功",
                                });
                            } else {
                                this.$message.error(response.message);
                            }
                        });
                    })
                    .catch(() => {
                    });
            },
            //获取订单列表
            getList() {
                this.loading = true;
                filterStcokCheck(this.filterQuery)
                    .then((response) => {
                        this.loading = false;
                        this.list = response.data.list || [];
                        this.total = response.data.total || 0;
                    })
                    .catch(() => {
                        this.loading = false;
                        this.list = [];
                        this.total = 0;
                    });
            },
            //业务明细表
            getOrderDetail(row) {
                this.loading = true;
                this.detailQuery = {
                    id: row.id,
                };
                this.refresgOrderDetail();
            },

            refresgOrderDetail() {
                getDetail(this.detailQuery)
                    .then((response) => {
                        this.loading = false;
                        this.detailList = response.data.list || [];
                    })
                    .catch(() => {
                        this.loading = false;
                        this.detailList = [];
                    });
            },

            handleUnitClick(row) {
                this.curIndex = row.id;
                this.dialogTableVisible = true;
            },
            getUnitList() {
                this.loading = true;
                getBasicUnitMaintains(this.unitquery)
                    .then((response) => {
                        this.loading = false;
                        this.unitlist = response.data.list || [];
                    })
                    .catch(() => {
                        this.loading = false;
                    });
            },

            intentDetail(row) {
                this.codeDetailVisible = true;
                this.idQuery.id = row.id;
            },

            selectUnit(row) {
                console.log(row.id);
                this.unitUpdateQuery.id = this.curIndex;
                this.unitUpdateQuery.fromCorp = row.name;
                this.unitUpdateQuery.fromCorpId = row.unitid;
                updateUnit(this.unitUpdateQuery)
                    .then((response) => {
                        this.dialogTableVisible = false;
                        this.getList();
                    })
                    .catch(() => {
                        this.dialogTableVisible = false;
                    });
            },
            deleteOrders(data) {
                this.loading = true;
                this.deleteData.id = data;
                deleteStockCheck(this.deleteData)
                    .then((response) => {
                        this.getList();
                        this.refresgOrderDetail();
                        this.$message({
                            type: "success",
                            message: "删除成功！",
                        });
                    })
                    .catch(() => {
                    });
            },
            tableRowClassName({row, rowIndex}) {
                if (row.reCount == row.count) {
                    return "success-row";
                } else {
                    return "warning-row";
                }
            },

            uploadOrderDialog(value) {
                console.log(value.id + "---" + value.orderid);

                finishStockCheck(value)
                    .then((response) => {
                        if (response.code == 20000) {
                            this.getList();
                        } else {
                            this.$message.error(response.message);
                        }
                    })
                    .catch(() => {
                    });
            },
            checkStockErp(row) {
                let query = {
                    id: row.id,
                };
                checkErp(query)
                    .then((response) => {
                        if (response.code == 20000) {
                            this.getList();
                        } else {
                            this.$message.error(response.message);
                        }
                    })
                    .catch(() => {
                    });
            },
            deleteDialog(rowId) {
                this.$confirm("此操作将永久删除该订单, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                        this.deleteOrders(rowId);
                    })
                    .catch(() => {
                    });
            },
            closeDetailDialog(val) {
                this.codeDetailVisible = false;
            },
            closeManuDialog(val) {
                this.addDialogVisible = false;
                if (val) {
                    this.$message.success("选入成功");
                    this.refresgOrderDetail();
                    this.getList();
                } else {
                    this.$message.error("选入失败");
                }
            },
            cancelDialog() {
                this.addDialogVisible = false;
            },
            importOrder(row) {
                this.orderId = row.id;
                this.addDialogVisible = true;
            },
            getBusType() {
                let query = {
                    enabled: true,
                };
                getBussinessType(query)
                    .then((response) => {
                        this.busTypes = response.data.list || [];
                    })
                    .catch(() => {
                    });
            },
            geActionName(action) {
                for (let i = 0; i < this.busTypes.length; i++) {
                    if (this.busTypes[i].action === action) {
                        return this.busTypes[i].name;
                    }
                }
            },
        },
        filters: {
            statusFilterType(status) {
                const statusMap = {
                    false: "success",
                    true: "danger",
                };
                return statusMap[status];
            },
        },
        mounted() {
            document.body.ondrop = function (event) {
                event.preventDefault();
                event.stopPropagation();
            };
        },

        created() {
            // 将参数拷贝进查询对象
            let query = this.$route.query;
            this.query = Object.assign(this.query, query);
            this.query.limit = parseInt(this.query.limit);
            // 加载表格数据
            this.getList();
            this.getBusType();
        },
    };
</script>


<style>
    .el-table .warning-row {
        background: #ffb5b5;
    }

    .el-table .success-row {
        background: #ffffff;
    }
</style>
