<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.key" placeholder="请输入查询内容" clearable></el-input>
                    </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="addSpace">新增货位</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="code" width="150"></el-table-column>
                <el-table-column label="货位名称" prop="name" width="200"></el-table-column>
                <el-table-column label="货位分类" prop="type" width="200"></el-table-column>
                <el-table-column label="所属仓库" prop="invStorageName" width="200"
                                 show-overflow-tooltip></el-table-column>
                <el-table-column label="所属分库" prop="invSubStorageName"
                                 show-overflow-tooltip width="150"></el-table-column>
                <el-table-column label="状态" prop="status"
                                 show-overflow-tooltip width="150">
                    <template slot-scope="scope">
                        <el-tag>
                            {{ statusMap[scope.row.status] }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="remark" width="150"></el-table-column>
                <el-table-column label="操作" width="150" fixed="right">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="editSpace(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
            :current-page="filterQuery.page"
            :page-size="filterQuery.limit"
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            :total="total"
        ></el-pagination>

        <el-dialog
            :title="formMap[formName]"
            :visible.sync="modifySpaceVisible"
            width="65%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            append-to-body
            v-if="modifySpaceVisible"
        >
            <el-form :model="formData" :rules="rules" ref="formData">
                <el-row :gutter="20">
                    <el-col :span="3">
                        <span>货位编码：</span>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item prop="code">
                            <el-input v-model="formData.code" auto-complete="off"
                                      placeholder="请输入货位编码"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="3">
                        <span>货位名称：</span>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item prop="name">
                            <el-input v-model="formData.name" auto-complete="off"
                                      placeholder="请输入货位名称"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20">
                    <el-col :span="3">
                        <span>货位分类：</span>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item prop="remark">
                            <el-input v-model="formData.type" auto-complete="off"
                                      placeholder="请输入货位分类"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="3">
                        <span>启用状态：</span>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item prop="status">
                            <el-radio v-model="formData.status" :label="0">禁用</el-radio>
                            <el-radio v-model="formData.status" :label="1">启用</el-radio>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20">
                    <el-col :span="3">
                        <span>备注：</span>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item prop="remark">
                            <el-input v-model="formData.remark" auto-complete="off"
                                      placeholder="请输入备注内容"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <div style="text-align: center">
                    <el-button type="primary" size="small" icon="search" @click="saveSpace"
                    >提交
                    </el-button
                    >
                    <el-button type="primary" size="small" icon="search" @click="cancelDialog"
                    >取消
                    </el-button
                    >
                </div>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
import store from "../../store";
import {filterUplLocInv} from "@/api/basic/invWarehouse";
import {filterSubByInv} from "@/api/basic/invSubWarehouse";
import {getInvSpaceList, saveSpace, deleteSpace} from "@/api/inventory/invSpace";

export default {
    name: "invInfo",
    props: {
        invInfo: {
            type: Object,
            required: true
        }
    },
    data() {
        return {
            filterQuery: {
                invWarehouseCode: null,
                invStorageCode: null,
                key: null,
                page: 1,
                limit: 20,
                customerId: null,
            },
            list: [],
            storageList: [],
            subInvList: [],
            codeDetailVisible: false,
            total: 0,
            loading: true,
            index: null,
            dialogTableVisible: false,
            statusMap: {
                0: "禁用",
                1: "启用"
            },
            formMap: {
                add: "添加货位",
                edit: "编辑货位"
            },
            formName: null,
            modifyWarehouseDisable: true,
            modifySpaceVisible: false,
            formData: {
                id: null,
                code: null,
                name: null,
                type: null,
                invStorageCode: this.invInfo.invStorageCode,
                invWarehouseCode: this.invInfo.invWarehouseCode,
                status: 1,
                remark: null
            },
            rules: {
                code: [
                    {required: true, message: "请输入货位编码", trigger: "blur"}
                ],
                name: [
                    {required: true, message: "请输入货位名称", trigger: "blur"}
                ],
            },
            modifySubInvList: []
        };
    },
    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                customerId: null,
                invWarehouseCode: this.invInfo.invWarehouseCode,
                invStorageCode: this.invInfo.invStorageCode,
                page: 1,
                limit: 20,
            };
            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;
            getInvSpaceList(this.filterQuery).then((res) => {
                this.loading = false;
                if (res.code === 20000) {
                    this.list = res.data.list || [];
                    this.total = res.data.total || 0;
                } else {
                    this.$message.error(res.data);
                }
            })
        },

        deleteDialog(rowId) {
            this.$confirm("此操作将永久删除该货位, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    let params = {id: rowId};
                    deleteSpace(params).then((res) => {
                        if (res.code === 20000) {
                            this.$message.success("删除成功！");
                            this.getList();
                        } else {
                            this.$message.error(res.message);
                        }
                    })
                })
                .catch(() => {
                });
        },
        cancelDialog() {
            this.modifySpaceVisible = false
            this.getList();
        },
        getStorage() {
            this.storageList = [];
            filterUplLocInv()
                .then((response) => {
                    this.storageList = response.data || [];
                })
                .catch(() => {
                });
        },
        resetForm() {
            if (this.$refs["formData"]) {
                // 清空验证信息表单
                this.$refs["formData"].clearValidate();
                this.getList();
            }
        },
        editSpace(row) {
            this.resetForm();
            this.modifySpaceVisible = true;
            this.formName = "edit";
            this.formData = row;
        },
        addSpace() {
            this.modifySpaceVisible = true;
            this.formName = "add";
            this.resetForm();
            this.formData = {
                id: null,
                code: null,
                name: null,
                type: null,
                invStorageCode: this.invInfo.invStorageCode,
                invWarehouseCode: this.invInfo.invWarehouseCode,
                status: 1,
                remark: null
            }
        },
        saveSpace() {
            this.$refs['formData'].validate((valid) => {
                if (valid) {
                    saveSpace(this.formData, this.formName).then((res) => {
                        if (res.code === 20000) {
                            if ("add" === this.formName) {
                                this.$message.success("新增成功");
                            } else {
                                this.$message.success("保存成功");
                            }
                            this.getList();
                            this.modifySpaceVisible = false;
                        } else {
                            this.$message.error(res.message);
                        }
                    })
                }
            })
        },
    },
    created() {
        this.getStorage();
        //加载表格数据
        this.filterQuery.invStorageCode = this.invInfo.invStorageCode;
        this.filterQuery.invWarehouseCode = this.invInfo.invWarehouseCode;
        this.getList();
    },
};
</script>

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