<template>
    <div>
        <el-card>
            <el-form :inline="true" :model="filterQuery" size="mini">
                <el-row style="width: 100%">
                    <el-form-item class="query-form-item">
                        <el-input v-model="filterQuery.orderId" placeholder="盘点单号" clearable="true"></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-select v-model="filterQuery.invStorageCode" placeholder="请选择当前仓库" clearable="true"
                                   @change="invChange"
                                   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">
                        <el-select v-model="filterQuery.invWarehouseCode" placeholder="请选择当前分库" clearable="true"
                                   @change="invWarehouseChange"
                                   :disabled="invWarehouseDisabled"
                                   size="mini">
                            <el-option
                                v-for="item in subInvList"
                                :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">
                        <el-select v-model="filterQuery.invSpaceCode" placeholder="请选择当前货位" clearable="true"
                                   :disabled="invSpaceDisabled"
                                   size="mini">
                            <el-option
                                v-for="item in spaceCodeList"
                                :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>
                        <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="true"
                      @current-change="handleChange"
            >
                <el-table-column label="序号" type="index"></el-table-column>
                <el-table-column label="盘点单号" prop="orderId" width="180"></el-table-column>
                <el-table-column label="仓库" prop="invStorageName" width="180"></el-table-column>
                <el-table-column label="分库" prop="invSubStorageName" width="180"
                                 show-overflow-tooltip></el-table-column>
                <el-table-column label="货位" prop="invSpaceName" width="200"></el-table-column>
                <el-table-column label="盘点类型" prop="countType" width="100">
                    <template slot-scope="scope">
                        <span>{{ countTypeMap[scope.row.countType]}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="盘点人" prop="createUser" width="120"></el-table-column>
                <el-table-column label="盘点时间" prop="createTime" width="150"></el-table-column>
                <el-table-column label="状态" prop="status" width="120">
                    <template slot-scope="scope">
                        <el-tag>
                            {{ statusMap[scope.row.status] }}
                        </el-tag>
                    </template>
                </el-table-column>
            </el-table>

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

        <el-card>
            <el-table v-loading="loading" :data="detailList" style="width: 100%">
                <el-table-column label="序号" type="index"></el-table-column>
                <el-table-column label="产品名称" prop="productName" width="220"></el-table-column>
                <el-table-column label="批次号" prop="batchNo" width="180"
                                 show-overflow-tooltip></el-table-column>
                <el-table-column label="生产日期" prop="produceDate" width="120"></el-table-column>
                <el-table-column label="失效日期" prop="expireDate" width="120"></el-table-column>
                <el-table-column label="盘点数量" prop="countNum" width="150"></el-table-column>
                <el-table-column label="账面数量" prop="invNum" width="150"></el-table-column>
                <el-table-column label="盘盈数量" prop="profitNum" width="150"></el-table-column>
                <el-table-column label="盘亏数量" prop="lossNum" width="150"></el-table-column>
                <el-table-column label="盈亏状态" prop="status" width="100">
                    <template slot-scope="scope">
                        <el-tag>
                            {{ countStatusMap[scope.row.status] }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="120" fixed="right">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="intentDetail(scope.row)"
                        >详情
                        </el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

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

        <el-dialog
            title="码详情"
            :visible.sync="codesVisible"
            width="70%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="codesVisible"
            append-to-body
        >
            <countOrderCodes
                :codeQuery="codeQuery"
            ></countOrderCodes>
        </el-dialog>

    </div>
</template>

<script>
import store from "../../store";
import {filterUplLocInv} from "@/api/basic/invWarehouse";
import {filterSubByInv} from "@/api/basic/invSubWarehouse";
import {userInfo} from "@/api/auth/login";
import {isBlank} from "@/utils/strUtil";
import {
    getCountOrderList,
    getCountOrderDetailList,
    deleteCountOrder,
    updateCountOrderStatus
} from "@/api/inventory/invCountOrder";
import {getSpaceCodeList} from "@/api/inventory/invSpace";
import countOrderCodes from "@/views/inventory/countOrderCodes";

export default {
    data() {
        return {
            filterQuery: {
                orderId: null,
                invWarehouseCode: null,
                invStorageCode: null,
                invSpaceCode: null,
                status: 2,
                page: 1,
                limit: 20,
                customerId: null,
            },
            detailQuery: {
                orderIdFk: null,
                page: 1,
                limit: 20
            },
            list: [],
            detailList: [],
            storageList: [],
            subInvList: [],
            spaceCodeList: [],
            codeDetailVisible: false,
            total: 0,
            detailTotal: 0,
            loading: true,
            invWarehouseDisabled: true,
            invSpaceDisabled: true,
            statusMap: {
                0: "未提交",
                1: "未审核",
                2: "已审核"
            },
            countStatusMap: {
                0: '亏损',
                1: '盈利',
                2: "平衡"
            },
            codeQuery: {
                orderIdFk: null,
                productId: null,
                edit: false
            },
            codesVisible: false,
            countTypeMap: {
                0: "整库盘点",
                1: "货位盘点",
                2: "部分盘点"
            }
        };
    },
    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                orderId: null,
                customerId: null,
                invWarehouseCode: null,
                invStorageCode: null,
                invSpaceCode: null,
                status: 2,
                page: 1,
                limit: 20,
            };
            this.onRestDetail();
            this.invChange();
            this.getList();
        },
        onRestDetail() {
            this.detailQuery = {
                orderIdFk: null,
                page: 1,
                limit: 20
            };
            this.detailList = [];
            this.detailTotal = 0;
        },
        onSubmit() {
            this.loading = true;
            this.filterQuery.page = 1;
            this.getList();
            this.onRestDetail();
        },
        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getList();
        },
        handleCurrentDetailChange(val) {
            this.detailQuery.page = val;
            this.getCountOrderDetailList();
        },
        getList() {
            this.loading = true;
            this.filterQuery.customerId = store.getters.customerId;
            getCountOrderList(this.filterQuery).then((res) => {
                this.loading = false;
                if (res.code === 20000) {
                    this.list = res.data.list || [];
                    this.total = res.data.total || 0;
                } else {
                    this.list = [];
                    this.total = 0;
                }
            }).catch((error) => {
                this.loading = false;
                this.list = [];
                this.total = 0;
            });
        },
        getCountOrderDetailList() {
            getCountOrderDetailList(this.detailQuery).then((res) => {
                if (res.code === 20000) {
                    this.detailList = res.data.list || [];
                    this.detailTotal = res.data.total || 0;
                }
            }).catch(() => {
            });
        },
        deleteOrders(data) {
            this.loading = true;
            let query = {
                id: data,
            }
            deleteCountOrder(query).then((res) => {
                if (res.code === 20000) {
                    this.$message.success("删除成功");
                    this.getList();
                } else {
                    this.$message.error(res.message());
                }
            })
        },

        deleteDialog(rowId) {
            this.$confirm("此操作将永久删除该订单, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.deleteOrders(rowId);
                })
                .catch(() => {
                });
        },
        getStorage() {
            this.storageList = [];
            filterUplLocInv()
                .then((response) => {
                    this.storageList = response.data || [];
                })
                .catch(() => {
                });
        },
        invChange() {
            this.filterQuery.invWarehouseCode = null;
            this.filterQuery.invSpaceCode = null;
            this.subInvList = [];
            this.spaceCodeList = [];
            if (!isBlank(this.filterQuery.invStorageCode)) {
                this.invWarehouseDisabled = false;
                let query = {
                    pcode: this.filterQuery.invStorageCode
                };
                filterSubByInv(query)
                    .then((response) => {
                        this.subInvList = response.data || [];
                    })
                    .catch(() => {
                    });
            } else {
                this.invWarehouseDisabled = true;
            }
        },
        invWarehouseChange() {
            this.invSpaceDisabled = false;
            this.filterQuery.invSpaceCode = null;
            if (!isBlank(this.filterQuery.invWarehouseCode) && !isBlank(this.filterQuery.invStorageCode)) {
                //查询货位列表
                let params = {
                    invStorageCode: this.filterQuery.invStorageCode,
                    invWarehouseCode: this.filterQuery.invWarehouseCode,
                    status: true
                };
                getSpaceCodeList(params).then((res) => {
                    if (res.code === 20000) {
                        this.spaceCodeList = res.data || [];
                    }
                })
            }
        },
        handleChange(row) {
            this.detailQuery.orderIdFk = row.orderId;
            this.detailQuery.page = 1;
            this.getCountOrderDetailList();
        },
        intentDetail(row) {
            this.codeQuery = {
                orderIdFk: row.orderIdFk,
                productId: row.productId,
                batchNo: row.batchNo,
                edit: false
            };
            this.codesVisible = true;
        },
        auditCountOrder(row) {
            let params = {
                id: row.id,
                status: null
            };
            this.$confirm('请选择审核意见', '提示', {
                confirmButtonText: '通过',
                cancelButtonText: '驳回',
                type: 'warning',
                center: true
            }).then(() => {
                params.status = 2; //审核通过
                updateCountOrderStatus(params).then((res) => {
                    if (res.code === 20000) {
                        this.$message.success("审核通过");
                        this.getList();
                        this.onRestDetail();
                    } else {
                        this.$message.error(res.data.message);
                    }
                }).catch(() => {
                });
            }).catch(() => {
                params.status = 0; //驳回到上一个状态
                updateCountOrderStatus(params).then((res) => {
                    if (res.code === 20000) {
                        this.$message.success("审核通过");
                        this.$message.success("已驳回");
                        this.getList();
                        this.onRestDetail();
                    } else {
                        this.$message.error(res.data.message);
                    }
                }).catch(() => {
                });
            });

        },
    },
    components: {
        countOrderCodes
    },
    created() {
        this.getStorage();
        //获取用户默认仓库信息
        userInfo().then((res) => {
            if (res.code === 20000) {
                if (!isBlank(res.data.locInvCode)) {
                    this.filterQuery.invStorageCode = res.data.locInvCode;
                    this.invChange();
                    this.filterQuery.invWarehouseCode = res.data.locSubInvCode;
                    this.invWarehouseChange();
                }
                this.getList();
            }
        }).catch((error) => {
        });
    },
};
</script>

<style type="text/scss" lang="scss">
</style>
