<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>
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-select v-model="filterQuery.invSpaceCode" placeholder="请选择当前货位" clearable="true"
                                   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" icon="search" @click="addMAOrder"
                            >新增养护记录
                            </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" fixed></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="invSpaceName" width="180"></el-table-column>
                <el-table-column label="养护日期" prop="createTime" width="120"></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="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" kan1
            layout="prev, pager, next"
            :total="total"
        ></el-pagination>

        <el-dialog
            :title="formMap[formName]"
            :visible.sync="formVisible"
            width="70%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="formVisible"
        >
            <addInvMAOrder
                :orderId="currentOrderId"
                :closeDialog="closeDialog"
            ></addInvMAOrder>
        </el-dialog>

    </div>
</template>

<script>
import {isBlank} from "@/utils/strUtil";
import {userInfo} from "@/api/auth/login";
import {getMAOrderList, deleteOrder, submitAudit} from "@/api/inventory/invMAOrder";
import {filterSubAll} from "@/api/basic/invSubWarehouse";
import {getSpaceCodeList} from "@/api/inventory/invSpace";
import addInvMAOrder from "@/views/inventory/addInvMAOrder";

export default {
    data() {
        return {
            filterQuery: {
                invWarehouseCode: null,
                invSpaceCode: null,
                status: 0,
                orderId: null,
                page: 1,
                limit: 20
            },
            list: [],
            total: 0,
            loading: false,
            idQuery: null,
            subInvList: [],
            spaceCodeList: [],
            statusMap: {
                0: "草稿",
                1: "未审核",
                2: "已完成"
            },
            formVisible: false,
            formName: null,
            formMap: {
                add: "新增养护记录",
                edit: "编辑养护记录"
            },
            currentOrderId: null
        };
    },
    components: {
        addInvMAOrder
    },
    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                invWarehouseCode: null,
                invSpaceCode: null,
                status: 0,
                orderId: 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
            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;
                }
            }).catch((error) => {
                this.loading = false;
                this.list = [];
                this.total = 0;
            });
        },
        getAllWarehouse() {
            filterSubAll().then((res) => {
                this.subInvList = res.data || [];
            });
        },
        invWarehouseChange() {
            this.filterQuery.invSpaceCode = null;
            this.spaceCodeList = [];
            let params = {
                invStorageCode: this.filterQuery.invStorageCode,
                invWarehouseCode: this.filterQuery.invWarehouseCode,
                status: true
            };
            getSpaceCodeList(params).then((res) => {
                this.spaceCodeList = res.data || [];
            })
        },
        closeDialog() {
            this.formVisible = false;
        },
        addMAOrder() {
            this.currentOrderId = null;
            this.formVisible = true;
        },
        editOrder(row) {
            this.currentOrderId = row.orderId;
            this.formVisible = true;
        },
        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);
                }
            }).catch((error) => {
                this.$message.error(error.message);
            });
        },
        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();
                    } else {
                        this.$message.error(res.message);
                    }
                })
            }).catch((error) => {
                this.$message.error(error.message);
            });
        },
    },
    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>
