<template>
    <div>
        <el-card>
            <el-form :inline="true" :model="query" class="query-form" size="mini">
                <el-form-item class="query-form-item">
                    <el-input
                        v-model="query.name"
                        clearable
                        placeholder="仓库名称"
                        style="width: 400px"
                    ></el-input>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-select v-model="query.advanceType" placeholder="仓库类型" clearable>
                        <!--                        <el-option label="全部" value=""></el-option>
                                                <el-option label="寄售" value="true"></el-option>-->
                        <el-option label="仓库" value="false"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-select v-model="query.status" placeholder="仓库状态" clearable>
                        <el-option label="全部" value=""></el-option>
                        <el-option label="正常" value="1"></el-option>
                        <el-option label="禁用" value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button-group style="margin-left: 10px;display:flex;">
                        <el-button type="primary" icon="el-icon-refresh" @click="onReset"></el-button>
                        <el-button type="primary" icon="search" @click="search">查询</el-button>
                        <el-button type="primary" @click.native="handleForm(null, null,'addTop')"
                        >添加仓库
                        </el-button>
                    </el-button-group>
                </el-form-item>
            </el-form>
            <el-table
                :data="mergeList"
                style="width: 100%;margin-bottom: 20px;"
                row-key="id"
                border
                default-expand-all
                highlight-current-row
                @row-click="loadLinkData"
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">

                <el-table-column
                    prop="code"
                    label="仓库编码"
                >
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="仓库名称"
                >
                </el-table-column>
                <el-table-column
                    prop="advanceType"
                    label="仓库类型">
                    <template slot-scope="scope">
                        <span>{{ enableMap[scope.row.advanceType] }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="level"
                    label="仓库等级"
                >
                </el-table-column>
                <el-table-column
                    prop="pcode"
                    label="上级仓库"
                >
                </el-table-column>
                <el-table-column label="操作" fixed="right">
                    <template slot-scope="scope">

                        <!--<el-button
                            type="text"
                            size="small"
                            :disabled="scope.row.pid!=0"
                            @click.native.stop="handleSubForm(node, scope.row, 'add')"
                        >添加货位
                        </el-button>-->
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="handleForm(node, scope.row, 'edit')"
                        >编辑
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            :disabled="scope.row.code==1000 ||scope.row.code==1001"
                            @click.native.stop="handleDel(node,scope.row)"
                        >删除
                        </el-button
                        >
                    </template>

                </el-table-column>
            </el-table>
        </el-card>
        <div>
            <el-card style="width: 49%; float: left; margin-right: 0px;">
                <div slot="header" class="clearfix" style="margin-bottom: 20px;">
                    <el-button style="float: right;" type="primary" @click="addUser()"
                               :disabled="currentRow.status == 0">添加用户
                    </el-button>
                </div>
                <el-table
                    :data="userData"
                    stripe
                    style="width: 100%">
                    <el-table-column label="序号" type="index"></el-table-column>
                    <el-table-column
                        prop="username"
                        label="用户名"
                        min-width="30%">
                    </el-table-column>
                    <el-table-column
                        prop="isDirector"
                        label="主管"
                        min-width="30%">
                        <template slot-scope="scope">
                            <span>{{ isDirectorMap[scope.row.director] }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="操作"
                        min-width="30%"
                    >
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                size="small"
                                @click.native="updateDirector(scope.row)">
                                设为主管
                            </el-button>
                            <el-button
                                type="text"
                                size="small"
                                @click.native="delWarehouseUser(scope.row.id)">
                                移除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <el-pagination
                    :page-size="userQuery.limit"
                    @current-change="loadUserList"
                    layout="prev, pager, next"
                    :total="userTotal"
                    :current-page="userQuery.page"
                >
                </el-pagination>
            </el-card>

            <el-card style="width: 49%; float: right; margin-left: 0px;">
                <div slot="header" class="clearfix" style="margin-bottom: 20px;">
                    <el-button style="float: right;" @click="addBussinessType()" type="primary"
                               :disabled="currentRow.status == 0">添加单据类型
                    </el-button>
                </div>
                <el-table
                    :data="bussinessTypeData"
                    stripe
                    style="width: 100%">
                    <el-table-column label="序号" type="index"></el-table-column>
                    <el-table-column
                        prop="action"
                        label="单据类型编码"
                        min-width="30%">
                    </el-table-column>
                    <el-table-column
                        prop="name"
                        label="单据类型名称"
                        min-width="30%">
                    </el-table-column>
                    <el-table-column
                        label="操作"
                        min-width="20%"
                    >
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                size="small"
                                @click.native="delWarehouseBussinessType(scope.row.id)">
                                移除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <el-pagination
                    :page-size="busQuery.limit"
                    @current-change="loadBussinessTypeList"
                    layout="prev, pager, next"
                    :total="busTotal"
                    :current-page="busQuery.page"
                >
                </el-pagination>
            </el-card>
        </div>

        <!--添加用户弹窗-->
        <el-dialog
            title="用户列表"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :visible.sync="userFormVisible"
        >
            <el-table v-loading="loading" :data="userList" ref="checkUserList"
                      @selection-change="handleUserSelectionChange"
                      style="width: 100%;"
                      class="tableClass"
            >
                <el-table-column
                    type="selection"
                    :reserve-selection="true"
                    width="55">
                </el-table-column>
                <el-table-column label="账号" prop="userName" fixed></el-table-column>
                <el-table-column label="用户名" prop="employeeName" fixed></el-table-column>
            </el-table>

            <div slot="footer" class="dialog-footer">
                <el-button @click.native="userFormVisible = !userFormVisible"
                >取消
                </el-button
                >
                <el-button
                    type="primary"
                    @click.native="addUserData()"
                >提交
                </el-button>
            </div>

        </el-dialog>

        <!--添加单据类型弹窗-->
        <el-dialog
            title="单据类型"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :visible.sync="bussinessTypeFormVisible"
        >
            <el-table v-loading="loading" :data="bussinessTypeList" ref="typeList"
                      @selection-change="handleBussinessTypeSelectionChange"
                      style="width: 100%"
                      class="tableClass"
            >
                <el-table-column
                    type="selection"
                    :reserve-selection="false"
                    width="55">
                </el-table-column>
                <el-table-column label="单据类型编码" prop="action" fixed></el-table-column>
                <el-table-column label="单据类型名称" prop="name" fixed></el-table-column>
            </el-table>

            <div slot="footer" class="dialog-footer">
                <el-button @click.native="bussinessTypeFormVisible = !bussinessTypeFormVisible"
                >取消
                </el-button
                >
                <el-button
                    type="primary"
                    @click.native="addBussinessTypeData()"
                >提交
                </el-button>
            </div>

        </el-dialog>

        <!--表单界面-->
        <el-dialog
            :title="formMap[formName]"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :visible.sync="subFormVisible"
            :before-close="hideForm"
            width="60%"
            top="5vh"
        >
            <el-form :model="formData" :rules="formRules" ref="dataForm">
                <el-form-item label="本企业仓库" prop="pid">
                    <el-select v-model="formData.pid" placeholder="顶级" disabled>
                        <el-option
                            v-for="item in mergeList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                          <span style="float: left"
                          ><span v-html="item.html"></span>{{ item.name }}</span
                          >
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="货位编码" prop="title" class="query-form-item">
                    <el-input
                        disabled
                        v-model="formData.code" style="width: 80%"
                        auto-complete="off"
                    ></el-input>
                </el-form-item>
                <el-form-item label="货位名称" prop="name" class="query-form-item">
                    <el-input
                        v-model="formData.name" style="width: 80%"
                        auto-complete="off"
                    ></el-input>
                </el-form-item>


                <el-form-item class="query-form-item" label="仓库类型">
                    <el-select v-model="formData.advanceType" placeholder="仓库类型" clearable disabled>
                        <el-option label="寄售" :value=true></el-option>
                        <!--                        <el-option label="仓库" :value=false></el-option>-->
                    </el-select>
                </el-form-item>
                <el-form-item label="状态:" prop="status" class="query-form-item">
                    <el-radio-group v-model="formData.status">
                        <el-radio :label="0">禁用</el-radio>
                        <el-radio :label="1">正常</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="hideForm">取消</el-button>
                <el-button
                    type="primary"
                    @click.native="formSubmit()"
                    :loading="formLoading"
                >提交
                </el-button
                >
            </div>
        </el-dialog>
        <el-dialog
            :title="formMap[formName]"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :visible.sync="formVisible"
            :before-close="hideForm"
            width="60%"
            top="5vh"
        >

            <el-form :model="formData" :rules="formRules" ref="dataForm">


                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="12" class="el-col">
                        <el-form-item label="仓库编码" prop="title" class="query-form-item">
                            <el-input
                                v-model="formData.code" style="width: 80%"
                                auto-complete="off"
                                disabled
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" class="el-col">
                        <el-form-item label="仓库名称" prop="name" class="query-form-item">
                            <el-input
                                v-model="formData.name" style="width: 80%"
                                auto-complete="off"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="12" class="el-col">
                        <el-form-item class="query-form-item" label="仓库等级">
                            <el-select v-model="formData.level" placeholder="仓库等级" :disabled="true" clearable
                                       @change="changeLevel">
                                <el-option label="1 级库" :value="1"></el-option>
                                <el-option label="2 级库" :value="2"></el-option>
                                <el-option label="3 级库" :value="3"></el-option>
                                <el-option label="4 级库" :value="4"></el-option>
                                <el-option label="5 级库" :value="5"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" class="el-col">
                        <el-form-item class="query-form-item" label="上级仓库">
                            <el-select v-model="formData.pcode" placeholder="请选择当前仓库" :disabled="true"
                                       clearable="true"
                                       size="mini"
                                       @change="pcodeChange">
                                <el-option
                                    v-for="item in pCodes"
                                    :key="item.name"
                                    :label="item.name"
                                    :value="item.code">
                                    <span style="float: left">{{ item.name }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="12" class="el-col">
                        <el-form-item class="query-form-item" label="仓库类型">
                            <el-select v-model="formData.advanceType" placeholder="仓库类型"
                                       :disabled="formName =='edit'">
                                <!--                                <el-option label="寄售" :value="true"></el-option>-->
                                <el-option label="仓库" :value="false"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" class="el-col">
                        <el-form-item label="状态" prop="status" class="query-form-item">
                            <el-radio-group v-model="formData.status">
                                <el-radio :label="0">禁用</el-radio>
                                <el-radio :label="1">正常</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="hideForm">取消</el-button>
                <el-button
                    type="primary"
                    @click.native="formSubmit()"
                    :loading="formLoading"
                >提交
                </el-button
                >
            </div>
        </el-dialog>

        <el-dialog
            title="关联仓库"
            :visible.sync="thrWareHouseVisible"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
        >
            <el-table
                :data="thrWarehouseData"
                style="width: 100%;margin-top: 10px;"
                row-key="id"
                border
                highlight-current-row
                @current-change="changeThrWarehouse"
                default-expand-all
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                    type="index"
                    label="序号"
                >
                </el-table-column>
                <el-table-column
                    prop="code"
                    label="仓库编码"
                >
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="仓库名称"
                >

                </el-table-column>
                <el-table-column
                    prop="advanceType"
                    label="仓库类型">
                    <template slot-scope="scope">
                        <span>{{ enableMap[scope.row.advanceType] }}</span>
                    </template>
                </el-table-column>
            </el-table>

            <div slot="footer" class="dialog-footer">
                <el-button @click.native="hideThrWarehouseTable()">取消</el-button>
                <el-button
                    type="primary"
                    @click.native="bindThrWarehouse()"
                    :loading="formLoading"
                >提交
                </el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import {
    filterList, filterAll,
    saveWarehouse,
    deleteWarehouse, warehouseBussinessTypeList,
    warehouseUserList, saveWarehouseBussinessType,
    saveWarehouseUser, updateDirector,
    deleteWarehouseUser, deleteWarehouseBussinessType,
    bindThrWarehouse, unbindThrWarehouse
} from "../../api/basic/invWarehouse";
import {filterThrList} from "@/api/thrsys/thrInvWarehouse";
import {getUserList} from "../../api/auth/authUser";
import {getBussinessType} from "../../api/basic/bussinessType";
import axios from "axios";
import store from "@/store";

const formJson = {
    id: null,
    pid: null,
    name: null,
    code: null,
    status: 1,
    advanceType: false,
    isDefault: false,
    level: 1,
    customerId: null
};
export default {
    data() {
        return {
            query: {
                id: null,
                pid: null,
                name: null,
                code: null,
                status: null,
                advanceType: null,
                isDefault: null,
                customerId: this.$store.getters.customerId
            },
            headers: {},
            mergeList: [],
            node: null,
            defaultProps: {
                children: "children",
                label: "name"
            },
            treeList: [],
            fileList: [],
            pCodes: [],
            uploadData: {},
            loading: true,
            index: null,
            formName: null,
            formMap: {
                addTop: "添加仓库",
                add: "添加货位",
                edit: "编辑"
            },
            enableMap: {
                true: "寄售",
                false: "仓库"
            },
            isDirectorMap: {
                true: "是",
                false: "否"
            },
            formLoading: false,
            subFormVisible: false,
            formVisible: false,
            formData: formJson,
            formRules: {
                name: [
                    {required: true, message: "请输入仓库货位名", trigger: "blur"}
                ],
                code: [
                    {required: true, message: "请输入仓库货位代码", trigger: "blur"}
                ]
            },
            pidData: {},
            deleteLoading: false,
            uploadFileUrl: null,
            userData: null,
            bussinessTypeData: null,
            userList: null,
            bussinessTypeList: null,
            userFormVisible: false,
            currentCode: null,
            currentRow: {status: 1},
            selectUserList: [],
            selectBussinessTypeList: [],
            bussinessTypeFormVisible: false,
            thrWarehouseData: null,
            currentId: null,
            thrWareHouseVisible: false,
            checkThrWarehouseRow: null,
            currentSysId: null,
            thirdSysVisible: false,
            userQuery: {
                code: null,
                page: 1,
                limit: 10
            },
            userTotal: 0,
            busQuery: {
                code: null,
                page: 1,
                limit: 10
            },
            busTotal: 0,
            addUserQuery: {
                customerId: this.$store.getters.customerId
            },
            addUserTotal: 0,
            bussinessTypeQuery: {
                customerId: this.$store.getters.customerId,
                enabled: true
            },
            bussinessTypeTotal: 0
        };
    },
    methods: {
        /*eslint-disable */
        renderContent(h, {node, data, store}) {
            return (
                <span
                    style="flex: 1; display: flex; align-items: center; justify-content: space-between; font-size: 14px; padding-right: 8px;">
                    <span>
                    <span title={data.name} style="margin-left:8px">{node.label}</span>
                       <span style="margin-left:18px">(编码：{data.code})</span>
                    </span>
                    <span>
                    <el-button
                        style="font-size: 12px;"
                        type="text"
                        on-click={() => this.handleSubForm(node, data, "add")}
                    >
                添加货位
                </el-button>
                <el-button
                    style="font-size: 12px;"
                    type="text"
                    on-click={() => this.handleForm(node, data, "edit")}
                >
                编辑
                </el-button>
                <el-button
                    style="font-size: 12px;"
                    type="text"
                    on-click={() => this.handleDel(node, data)}
                >
                删除
                </el-button>
                </span>
                </span>
            );
        },
        onReset() {
            this.$router.push({
                path: ""
            });
            this.query = {
                name: "",
                status: "",
                customerId: this.$store.getters.customerId
            };
            this.getList();
        },
        onSubmit() {
            this.getList();
        },
        search() {
            this.getList();
        },
        getList() {
            this.loading = true;
            filterList(this.query)
                .then((response) => {
                    this.loading = false;
                    this.mergeList = response.data.list || [];
                    this.treeList = response.data.tree_list || [];
                })
                .catch(() => {
                    this.loading = false;
                    this.mergeList = [];
                    this.treeList = [];
                });
        },
        // 刷新表单
        resetForm() {
            if (this.$refs["dataForm"]) {
                // 清空验证信息表单
                this.$refs["dataForm"].clearValidate();
                // 刷新表单
                this.$refs["dataForm"].resetFields();
                this.getList();
            }
        },
        // 隐藏表单
        hideForm() {
            // 更改值
            this.formVisible = false;
            this.subFormVisible = false;
            return true;
        },
        // 显示表单
        handleForm(node, data, formName) {
            if (null != data) {
                this.currentId = data.id;
                this.thirdSysVisible = true;
            } else {
                this.thirdSysVisible = false;
            }
            this.pidData = data || null;
            formJson.pid = (data && parseInt(data.id)) || "";
            this.formData = JSON.parse(JSON.stringify(formJson));
            if (formName === "edit") {
                if (data.pid == 0) {
                    this.formVisible = true;
                } else {
                    this.subFormVisible = true;
                }

                this.formData = Object.assign({}, data);
                this.node = node;
            } else {
                this.formVisible = true;
            }
            this.formName = formName;
            this.getParentInv(this.formData.level);
        },
        handleSubForm(node, data, formName) {
            this.subFormVisible = true;
            this.pidData = data || null;
            formJson.pid = (data && parseInt(data.id)) || "";
            this.formData = JSON.parse(JSON.stringify(formJson));
            if (formName === "edit") {
                this.formData = Object.assign({}, data);
                this.node = node;
            }
            this.formData.pid = !this.formData.pid ? "" : this.formData.pid;
            this.formName = formName;
        },
        formSubmit() {
            this.$refs["dataForm"].validate((valid) => {
                if (valid) {
                    this.formLoading = true;
                    this.formData.customerId = this.$store.getters.customerId;
                    let data = Object.assign({}, this.formData);
                    saveWarehouse(data, this.formName)
                        .then((response) => {
                            this.formLoading = false;
                            this.subFormVisible = false;
                            if (response.code !== 20000) {
                                this.$message.error(response.message);
                                return false;
                            }
                            this.$message.success("操作成功");
                            this.formVisible = false;
                            if (this.formName !== "edit") {
                                if (response.data && response.data.id) {
                                    data.id = response.data.id;
                                    if (this.pidData) {
                                        if (!this.pidData.children) {
                                            this.$set(this.pidData, "children", []);
                                        }
                                        this.pidData.children.push(data);
                                    } else {
                                        this.mergeList.push(data);
                                    }
                                }
                            } else {
                                // const parent = this.node.parent;
                                // const children = parent.data.children || parent.data;
                                // const index = children.findIndex((d) => d.id === data.id);
                                // children.splice(index, 1, data);
                            }
                            // 刷新表单
                            this.resetForm();
                            this.getList();
                        })
                        .catch(() => {
                            this.formLoading = false;
                            this.subFormVisible = false;
                        });
                }
            });
        },
        // 删除
        handleDel(node, data) {
            if (data.children && data.children.length > 0) {
                this.$alert("请先删除子节点", "提示", {
                    confirmButtonText: "确定"
                });
                return false;
            }
            if (data.id) {
                this.$confirm("确认删除该记录吗?", "提示", {
                    type: "warning"
                })
                    .then(() => {
                        this.deleteLoading = true;
                        let para = {id: data.id};
                        deleteWarehouse(para)
                            .then((response) => {
                                this.deleteLoading = false;
                                if (response.code !== 20000) {
                                    this.$message.error(response.message);
                                    return false;
                                }
                                this.$message.success("删除成功");
                                this.getList();
                                const parent = node.parent;
                                const children = parent.data.children || parent.data;
                                const index = children.findIndex((d) => d.id === data.id);
                                children.splice(index, 1);
                            })
                            .catch(() => {
                                this.deleteLoading = false;
                            });
                    })
                    .catch(() => {
                    });
            }
        },
        getProjectNum() {
            const projectTime = new Date(); // 当前中国标准时间
            const Year = projectTime.getFullYear(); // 获取当前年份 支持IE和火狐浏览器.
            const Month = projectTime.getMonth() + 1; // 获取中国区月份
            const Day = projectTime.getDate(); // 获取几号
            var CurrentDate = Year;
            if (Month >= 10) { // 判断月份和几号是否大于10或者小于10
                CurrentDate += Month;
            } else {
                CurrentDate += "0" + Month;
            }
            if (Day >= 10) {
                CurrentDate += Day;
            } else {
                CurrentDate += "0" + Day;
            }
            return CurrentDate;
        },
        handleChange(response, files, fileList) {
            if (response.code != 20000) {
                this.$message.error(response.message);
            } else {
                this.$message.success(response.data);
                this.getList();
            }
        },
        init() {
            axios.get("./config.json").then(res => {
                // 基础地址
                let response = res.data.BASE_URL;
                this.uploadFileUrl = response + "/spms/inv/warehouse/uploadJson";
            });
            this.headers = {
                ADMIN_ID: store.getters.adminId,
                ADMIN_TOKEN: store.getters.token
            };
        },

        getParentInv(data) {
            this.pCodes = [];
            let query = {
                level: data - 1,
                customerId: this.$store.getters.customerId
            };
            filterAll(query)
                .then((response) => {
                    this.pCodes = response.data || [];
                })
                .catch(() => {
                });
        },
        changeLevel(row) {
            this.formData.pcode = "";
            this.getParentInv(this.formData.level);
        },
        pcodeChange() {
            this.$forceUpdate();
        },
        loadLinkData(row) {
            this.currentCode = row.code;
            this.currentRow = row;

            this.userQuery.code = this.currentCode;
            this.userQuery.page = 1;
            this.busQuery.code = this.currentCode;
            this.busQuery.page = 1;
            this.loadUserList();
            this.loadBussinessTypeList();
        },
        getAddUserList() {
            getUserList(this.addUserQuery).then((res) => {
                this.selectUserList = [];
                this.userList = res.data.list || [];
                this.addUserTotal = res.data.total || 0;
                var that = this;
                that.$nextTick(() => {
                    if (that.$refs.checkUserList) {
                        that.$refs.checkUserList.clearSelection();
                    }
                    that.userList.forEach(row => {
                        for (let j in that.userData) {
                            if (row.id == that.userData[j].userid) {
                                that.$refs.checkUserList.toggleRowSelection(row, true);
                            }
                        }
                    });
                });
            });
        },
        addUser(val) {
            if (this.currentCode == null || this.currentCode == "") {
                this.$message.error("请先选择仓库");
            } else {
                this.userFormVisible = true;
                this.getAddUserList();
            }
        },

        loadUserList(val) {
            if (val != null) {
                this.userQuery.page = val;
            }
            ;
            //加载用户数据
            warehouseUserList(this.userQuery).then((res) => {
                this.userData = res.data.list || [];
                this.userTotal = res.data.total || 0;
            }).catch((error) => {
                this.$message.error("用户数据加载失败")
            });
        },
        loadBussinessTypeList(val) {
            if (val != null) {
                this.busQuery.page = val;
            }
            //加载单据类型数据
            warehouseBussinessTypeList(this.busQuery).then((res) => {
                this.bussinessTypeData = res.data.list || [];
                this.busTotal = res.data.total || 0;
            }).catch((error) => {
                this.$message.error("单据类型数据加载失败");
            });
        },
        handleUserSelectionChange(row) {
            let _this = this;
            _this.selectUserList = [];
            row.forEach((item) => {
                _this.selectUserList.push(item);
            });
        },
        handleBussinessTypeSelectionChange(row) {
            let _this = this;
            _this.selectBussinessTypeList = [];
            row.forEach((item) => {
                _this.selectBussinessTypeList.push(item);
            });
        },
        addUserData() {
            if (this.selectUserList.length === 0) {
                this.$message.error("请选择需要添加的用户");
                return;
            }
            this.userFormVisible = false;
            let query = {
                code: this.currentCode,
                userList: JSON.stringify(this.selectUserList)
            };
            saveWarehouseUser(query).then((res) => {
                this.$message.success("添加成功");
                let loadParam = {
                    code: this.currentCode
                };
                this.loadUserList();
            }).catch((error) => {
                this.$message.error("添加失败");
            });
        },
        addBussinessType() {
            if (this.currentCode == null || this.currentCode == "") {
                this.$message.error("请先选择仓库");
            } else {
                this.bussinessTypeFormVisible = true;
                getBussinessType(this.bussinessTypeQuery).then((res) => {
                    this.selectBussinessTypeList = [];
                    this.bussinessTypeList = res.data.list || [];
                    this.bussinessTypeTotal = res.data.total || 0;

                    let busDataList = []; //仓库关联的单据类型列表
                    warehouseBussinessTypeList({code: this.currentCode}).then((res) => {
                        busDataList = res.data.list;

                        var that = this;
                        that.$nextTick(() => {
                            if (that.$refs.typeList) {
                                that.$refs.typeList.clearSelection();
                            }

                            that.bussinessTypeList.forEach(row => {
                                for (let j in busDataList) {
                                    if (row.action === busDataList[j].action) {
                                        that.$refs.typeList.toggleRowSelection(row, true);
                                    }
                                }
                            });
                        });
                    })

                }).catch((error) => {
                });
            }
        },
        addBussinessTypeData() {
            if (this.selectBussinessTypeList.length == 0) {
                this.$message.error("请选择单据类型");
            } else {
                this.bussinessTypeFormVisible = false;
                let query = {
                    code: this.currentCode,
                    bussinessTypes: JSON.stringify(this.selectBussinessTypeList)
                };
                saveWarehouseBussinessType(query).then((res) => {
                    if (res.code === 20000) {
                        this.$message.success("添加成功");
                        //重新加载数据表格
                        this.loadBussinessTypeList();
                    }
                }).catch((error) => {
                });
            }
        },
        updateDirector(row) {
            let params = {id: row.id};
            updateDirector(params).then((res) => {
                this.$message.success("设置成功");
                if (res.code === 20000) {
                    //重新加载数据
                    this.loadUserList();
                }
            }).catch((error) => {
                this.$message.error("设置失败");
            });
        },
        delWarehouseUser(id) {
            let params = {id: id};
            deleteWarehouseUser(params).then((res) => {
                this.$message.success("已移除");
                for (let i = 0; i < this.userData.length; i++) {
                    if (this.userData[i].id == id) {
                        this.userData.splice(i, 1);
                        return;
                    }
                }
            }).catch((error) => {
                this.$message.error("移除失败");
            });
        },
        delWarehouseBussinessType(id) {
            let params = {id: id};
            deleteWarehouseBussinessType(params).then((res) => {
                if (res.code == 20000) {
                    this.$message.success("已移除");
                    for (let i = 0; i < this.bussinessTypeData.length; i++) {
                        if (this.bussinessTypeData[i].id == id) {
                            this.bussinessTypeData.splice(i, 1);
                            return;
                        }
                    }
                } else {
                    this.$message.error("移除失败");
                }
            }).catch((error) => {
                this.$message.error("移除失败");
            });
        },
        //绑定第三方仓库
        bindThrWarehouse() {
            this.thrWareHouseVisible = false;
            let params = {
                id: this.currentId,
                thridWarehouseId: this.checkThrWarehouseRow.code,
                sysId: this.currentSysId
            };
            bindThrWarehouse(params).then((res) => {
                this.$message.success("绑定成功");
                this.getThrsysDetailData();
            }).catch((error) => {
                this.$message.error("绑定失败");
            });
        },
        //解绑第三方仓库
        unbindThrWarehouse(row) {
            let params = {
                id: this.currentId,
                sysId: row.sysId
            };
            unbindThrWarehouse(params).then((res) => {
                this.$message.success("解绑成功");
                this.getThrsysDetailData();
            }).catch((error) => {
                this.$message.error("解绑失败");
            });
        },
        intentSelect(row) {
            this.currentSysId = row.sysId;
            //加载第三方仓库数据
            filterThrList().then((res) => {
                this.thrWarehouseData = res.data.list;
                this.thrWareHouseVisible = true;
            }).catch((error) => {
                this.thrWarehouseData = [];
                this.thrWareHouseVisible = true;
                this.$message.error("第三方仓库数据加载失败");
            });
        },
        changeThrWarehouse(row) {
            this.checkThrWarehouseRow = row;
        },
        hideThrWarehouseTable() {
            this.thrWareHouseVisible = false;
        }
    },
    filters: {
        statusFilterType(status) {
            const statusMap = {
                0: "gray",
                1: "success"
            };
            return statusMap[status];
        },
        statusFilterName(status) {
            const statusMap = {
                0: "禁用",
                1: "正常"
            };
            return statusMap[status];
        }
    },
    mounted() {
    },
    created() {
        // 加载表格数据
        this.init();
        this.getList();
    }
};
</script>

<style lang="scss">

.tableClass {
    .el-table__fixed-right {
        height: 100% !important;
    }
}

</style>

