<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 placeholder="请输入领用记录号" v-model="filterQuery.orderId"
                                  clearable="true"></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-select v-model="filterQuery.deptCode" placeholder="请选择领用部门" clearable="true"
                                   size="mini">
                            <el-option
                                v-for="item in deptList"
                                :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%" @current-change="handleChange">
                <el-table-column label="序号" type="index" fixed></el-table-column>
                <el-table-column label="设备领用记录号" prop="orderId" width="180"></el-table-column>
                <el-table-column label="领用部门" prop="deptName" width="180"></el-table-column>
                <el-table-column label="领用日期" prop="createTime" width="180"></el-table-column>
                <el-table-column label="领用人" prop="createUser" width="120"></el-table-column>
                <el-table-column label="状态" prop="status" show-overflow-tooltip 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="orderAudit(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" kan1
            layout="prev, pager, next"
            :total="total"
        ></el-pagination>

        <el-card>
            <el-table v-loading="detailLoading" :data="detailList" style="width: 100%; margin-top: 10px;">
                <el-table-column label="序号" type="index" width="50"></el-table-column>
                <el-table-column label="条码" width="150" prop="code" show-overflow-tooltip></el-table-column>
                <el-table-column width="200"
                                 label="产品通用名"
                                 prop="productName"
                ></el-table-column>
                <el-table-column width="150" label="规格型号" prop="ggxh" show-overflow-tooltip></el-table-column>
                <el-table-column width="150" label="批次号" prop="batchNo"></el-table-column>
                <el-table-column width="150" label="序列号" prop="serialNo"></el-table-column>
                <el-table-column width="150" label="领用日期" prop="createTime"></el-table-column>
                <el-table-column width="150" label="领用仓库" prop="invWarehouseName"></el-table-column>
                <el-table-column width="150" label="领用货位" prop="invSpaceName"></el-table-column>
                <el-table-column width="150" label="生产日期(yyMMdd)" prop="produceDate"></el-table-column>
                <el-table-column width="160" label="失效日期(yyMMdd)" prop="expireDate"
                                 show-overflow-tooltip></el-table-column>
                <el-table-column width="250" label="注册/备案凭证号" prop="zczbhhzbapzbh"
                                 show-overflow-tooltip></el-table-column>
                <el-table-column width="200" label="生产厂家" prop="ylqxzcrbarmc"
                                 show-overflow-tooltip></el-table-column>
            </el-table>

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

    </div>
</template>

<script>
import {
    getCollectOrderList,
    deleteCollectOrder,
    updateCollectOrderStatus,
    getCollectOrderDetailList
} from "@/api/inventory/deviceCollectOrder";
import addDeviceCollectOrder from "@/views/inventory/addDeviceCollectOrder.vue";
import {filterDepts} from "@/api/auth/authDept";

export default {
    data() {
        return {
            filterQuery: {
                status: 1,
                orderId: null,
                deptCode: null,
                page: 1,
                limit: 20
            },
            list: [],
            total: 0,
            loading: false,
            statusMap: {
                0: "草稿",
                1: "未审核",
                2: "已完成"
            },
            deviceCollectOrder: null,
            deptList: [],
            detailLoading: false,
            detailList: [],
            detailQuery: {
                orderIdFk: null,
                page: 1,
                limit: 20
            },
            detailTotal: 0
        };
    },
    components: {
        addDeviceCollectOrder
    },
    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                status: 1,
                orderId: null,
                deptCode: null,
                page: 1,
                limit: 20,
            };
            this.getList();
        },
        onSubmit() {
            this.getList();
        },
        handleSizeChange(val) {
            this.filterQuery.limit = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getList();
        },
        getList() {
            this.loading = true
            this.detailList = [];
            this.detailTotal = 0;
            getCollectOrderList(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;
            });
        },
        deleteDialog(id) {
            this.$confirm('此操作将永久删除设备领用记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {id: id};
                deleteCollectOrder(params).then((res) => {
                    if (res.code === 20000) {
                        this.$message.success("删除成功！");
                        this.getList();
                    } else {
                        this.$message.error(res.message);
                    }
                })
            }).catch((error) => {
                this.$message.error(error.message);
            });
        },
        getDepts() {
            filterDepts().then((res) => {
                this.deptList = res.data.list || [];
            })
        },
        orderAudit(row) {
            this.$confirm('请选择审批意见', '提示', {
                confirmButtonText: '通过',
                cancelButtonText: '驳回',
                type: 'warning',
                center: true
            }).then(() => {
                let params = {
                    id: row.id,
                    status: 2
                };
                updateCollectOrderStatus(params).then((res) => {
                    if (res.code === 20000) {
                        this.$message("审核通过！");
                        this.getList();
                    }
                })
            }).catch((action) => {
                if (action === "cancel") {
                    let params = {
                        id: row.id,
                        status: 0
                    };
                    updateCollectOrderStatus(params).then((res) => {
                        if (res.code === 20000) {
                            this.$message.success("已驳回！");
                            this.getList();
                        } else {
                            this.$message.error(res.message);
                        }
                    }).catch((error) => {
                        this.$message.error(error.message);
                    })
                }
            });
        },
        handleCurrentDetailChange(val) {
            this.detailQuery.page = val;
            this.getOrderDetailList();
        },
        getOrderDetailList() {
            this.detailLoading = true;
            getCollectOrderDetailList(this.detailQuery).then((res) => {
                this.detailLoading = false;
                if (res.code === 20000) {
                    this.detailList = res.data.list || [];
                    this.detailTotal = res.data.total || 0;
                } else {
                    this.detailList = [];
                    this.detailTotal = 0;
                }
            }).catch(() => {
                this.detailLoading = false;
                this.detailList = [];
                this.detailTotal = 0;
            })
        },
        handleChange(row) {
            this.detailList = {
                orderIdFk: row.orderId,
                page: 1,
                limit: 20
            };
            this.getOrderDetailList();
        }
    },
    created() {
        this.getDepts();
        this.getList();
    },
};
</script>

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