<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.invWarehouseCode" placeholder="请选择当前仓库" clearable="true"
                                   @change="invWarehouseChange" 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>
                                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.parentName }}</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="disableSpace"
                                   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>
                                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.invSubStorageName }}</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" fixed></el-table-column>
                <el-table-column label="养护记录号" prop="orderId" width="180"></el-table-column>
                <el-table-column label="仓库" prop="invWarehouseName" width="180"></el-table-column>
                <el-table-column label="货位" prop="invSpaceName" width="180"></el-table-column>
                <el-table-column label="养护日期" prop="createTime" width="150"></el-table-column>
                <el-table-column label="养护人" prop="createUser" 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="auditOrder(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="filterQuery.limit"
                :current-page="filterQuery.page"
                @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%; 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 label="养护日期" prop="createTime" width="150"></el-table-column>
                <el-table-column width="150" label="养护数量" prop="count"></el-table-column>
                <el-table-column width="150" label="养护状态" prop="maintenanceStatus">
                    <template slot-scope="scope">
                        <el-radio disabled="true" v-model="scope.row.maintenanceStatus"
                                  :label="0">未养护
                        </el-radio>
                        <el-radio disabled="true" v-model="scope.row.maintenanceStatus"
                                  :label="1">已养护
                        </el-radio>
                    </template>
                </el-table-column>
                <el-table-column width="150" label="设备状态" prop="deviceStatus">
                    <template slot-scope="scope">
                        <el-radio v-model="scope.row.deviceStatus" :label="1" disabled="true">
                            正常
                        </el-radio>
                        <el-radio v-model="scope.row.deviceStatus" :label="0" disabled="true">
                            异常
                        </el-radio>
                    </template>
                </el-table-column>
                <el-table-column width="200" label="养护备注" prop="remark">
                </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 {filterSubAll} from "@/api/basic/invSubWarehouse";
import {getSpaceCodeList} from "@/api/inventory/invSpace";
import {isBlank} from "@/utils/strUtil";
import {userInfo} from "@/api/auth/login";
import {getMAOrderList, getMAOrderDetailList, updateStatus, deleteOrder} from "@/api/inventory/invMAOrder";


export default {
    data() {
        return {
            filterQuery: {
                invWarehouseCode: null,
                invSpaceCode: null,
                status: 1,
                orderId: null,
                page: 1,
                limit: 20
            },
            detailQuery: {
                orderIdFk: null,
                page: 1,
                limit: 20,
            },
            list: [],
            total: 0,
            detailTotal: 0,
            detailList: [],
            loading: false,
            index: null,
            subInvList: [],
            spaceCodeList: [],
            statusMap: {
                0: "草稿",
                1: "未审核",
                2: "已完成"
            },
            disableSpace: false,
        };
    },
    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                invWarehouseCode: null,
                invSpaceCode: null,
                status: 1,
                orderId: null,
                page: 1,
                limit: 20
            };
            this.spaceCodeList = [];
            this.disableSpace = true;
            this.getList();
        },
        onSubmit() {
            this.filterQuery.page = 1;
            this.getList();
        },
        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getList();
        },
        getList() {
            this.loading = true
            this.detailList = [];
            this.detailTotal = 0;
            getMAOrderList(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;
                }
            });
        },
        handleChange(row) {
            this.detailQuery.orderIdFk = row.orderId;
            this.detailQuery.page = 1;
            this.getDetailList();
        },
        getDetailList() {
            getMAOrderDetailList(this.detailQuery).then((res) => {
                if (res.code === 20000) {
                    this.detailList = res.data.list || [];
                    this.detailTotal = res.data.total || 0;
                } else {
                    this.detailList = [];
                    this.detailTotal = 0;
                }
            }).catch(() => {
                this.detailList = [];
                this.detailTotal = 0;
            });
        },
        handleCurrentDetailChange(val) {
            this.detailQuery.page = val;
            this.getDetailList();
        },
        getAllWarehouse() {
            filterSubAll().then((res) => {
                this.subInvList = res.data || [];
            });
        },
        invWarehouseChange() {
            this.filterQuery.invSpaceCode = null;
            this.spaceCodeList = [];
            if (isBlank(this.filterQuery.invWarehouseCode)) {
                this.disableSpace = true;
            } else {
                this.disableSpace = false;
                let params = {
                    invStorageCode: this.filterQuery.invStorageCode,
                    invWarehouseCode: this.filterQuery.invWarehouseCode,
                    status: true
                };
                getSpaceCodeList(params).then((res) => {
                    this.spaceCodeList = res.data || [];
                })
            }
        },
        auditOrder(row) {
            this.$confirm('请选择审核意见', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '驳回',
                type: 'warning',
                center: true,
                closeOnPressEscape:false,//按下 ESC 键关闭弹窗
                closeOnClickModal:false,//点击遮罩关闭弹窗
                distinguishCancelAndClose:true,//区分取消与关闭
            }).then(() => {
                let params = {
                    id: row.id,
                    status: 2
                };
                updateStatus(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);
                })
            }).catch((action) => {
                if (action === "cancel") {
                    let params = {
                        id: row.id,
                        status: 0
                    };
                    updateStatus(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);
                    });
                } else {
                    return;
                }
            });
        },
        deleteDialog(id) {
            this.$confirm('是否确认删除此条养护记录?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {id: id};
                deleteOrder(params).then((res) => {
                    if (res.code === 20000) {
                        this.$message.success("删除成功");
                        this.getList();
                        this.detailList = [];
                        this.detailTotal = 0;
                    }
                });
            }).catch(() => {
            });
        }
    },
    created() {
        this.getAllWarehouse();
        //获取用户默认仓库信息
        userInfo().then((res) => {
            if (res.code === 20000) {
                if (!isBlank(res.data.locInvCode)) {
                    this.filterQuery.invWarehouseCode = res.data.locSubInvCode;
                    this.invWarehouseChange();
                }
                this.getList();
            }
        }).catch((error) => {
        });
    },
};
</script>

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