<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="subInvChange"
                                   :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="invWarehouseDisabled"
                                   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
                                type="primary"
                                @click.native.stop="addCountOrder(0)"
                            >新增整库盘点单
                            </el-button
                            >
                            <el-button
                                type="primary"
                                @click.native.stop="addCountOrder(1)"
                            >新增货位盘点单
                            </el-button
                            >
                            <el-button
                                type="primary"
                                @click.native.stop="addCountOrder(2)"
                            >新增部分盘点单
                            </el-button
                            >
                        </el-button-group>
                    </el-form-item>
                </el-row>
            </el-form>
            <el-table v-loading="loading" :data="list" style="width: 100%">
                <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="220"></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-column label="操作" width="180" fixed="right">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="printOrder(scope.row)"
                        >打印
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="editOrder(scope.row)"
                        >编辑
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="submitAudit(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-card>

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

        <el-dialog
            :title="formName"
            :visible.sync="addCountOrderVisible"
            width="70%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="addCountOrderVisible"
        >
            <addCountOrder
                :closeDialog="closeAddDialog"
                :countOrder="countOrder"
                :getCountOrderList="getList"
                :countType="countType"
            ></addCountOrder>
        </el-dialog>

    </div>
</template>

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

export default {
    data() {
        return {
            filterQuery: {
                orderId: null,
                invWarehouseCode: null,
                invStorageCode: null,
                invSpaceCode: null,
                status: 0,
                page: 1,
                limit: 20,
                customerId: null
            },
            list: [],
            storageList: [],
            subInvList: [],
            codeDetailVisible: false,
            total: 0,
            loading: true,
            invWarehouseDisabled: true,
            spaceCodeList: [],
            statusMap: {
                0: "未提交",
                1: "未审核",
                2: "已审核"
            },
            addCountOrderVisible: false,
            countOrder: null,
            formName: null,
            formType: null,
            countType: null,
            countTypeMap: {
                0: "整库盘点",
                1: "货位盘点",
                2: "部分盘点"
            }
        };
    },
    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                orderId: null,
                customerId: null,
                invWarehouseCode: null,
                invStorageCode: null,
                status: 0,
                page: 1,
                limit: 20,
            };
            this.invChange();
            this.getList();
        },
        onSubmit() {
            this.loading = true;
            this.filterQuery.page = 1;
            this.getList();
        },
        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getList();
        },
        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;
            })
        },

        deleteOrders(data) {
            this.loading = true;
            let params = {
                id: data,
            }
            deleteCountOrder(params).then((res) => {
                if (res.code === 20000) {
                    this.$message.success("删除成功！");
                    this.getList();
                } else {
                    this.$message.error(res.data.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 = "";
            this.subInvList = [];
            if (this.filterQuery.invStorageCode != null && 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;
            }
        },
        subInvChange() {
            this.filterQuery.invSpaceCode = "";
            this.spaceCodeList = [];
            let params = {
                invStorageCode: this.filterQuery.invStorageCode,
                invWarehouseCode: this.filterQuery.invWarehouseCode,
                status: true
            }
            getSpaceCodeList(params).then((res) => {
                this.spaceCodeList = res.data || [];
            });
        },
        submitAudit(row) {
            let params = {id: row.id};
            submitAudit(params).then((res) => {
                if (res.code === 20000) {
                    this.$message.success("提交成功");
                    this.getList();
                } else {
                    this.$message.error(res.message);
                }
            })
        },
        addCountOrder(type) {
            this.addCountOrderVisible = true;
            this.countOrder = {};
            this.countType = type;
            this.formType = 'add';
            if (this.countType === 0) {
                this.formName = "新增整库盘点单";
            } else if (this.countType === 1) {
                this.formName = "新增货位盘点单";
            } else if (this.countType === 2) {
                this.formName = "新增部分盘点单";
            }
        },
        closeAddDialog() {
            this.addCountOrderVisible = false;
            this.getList();
        },
        editOrder(row) {
            this.addCountOrderVisible = true;
            this.countOrder = row;
            this.formType = 'edit';
            if (this.countOrder.countType === 0) {
                this.formName = "编辑整库盘点单";
            } else if (this.countOrder.countType === 1) {
                this.formName = "编辑货位盘点单";
            } else if (this.countOrder.countType === 2) {
                this.formName = "编辑部分盘点单";
            }
        },
        printOrder(row) {
            let query = {moduleId: 6};
            this.loading = true;
            verifyTemplateFile(query).then((res) => {
                if (res.code === 20000) {
                    let printParams = {
                        templateId: res.data,
                        orderId: row.orderId
                    };
                    printOrder(printParams).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。
                    })
                } else {
                    this.loading = false;
                    this.$message.error(res.message);
                }
            }).catch((error) => {
                this.loading = false;
                this.$message.error(error.message);
            })
        }
    },
    components: {
        addCountOrder
    },
    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.subInvChange();
                }
                this.getList();
            }
        }).catch((error) => {
        });
    },
};
</script>

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