import * as service from '../services';
import * as util from "@/utils/index.js";
import { message } from "ant-design-vue";
export default {
    namespaced: true,
    state: {
        // 机构层级定义-列表查询条件
        tableName: 'com_depart_level_info',
        // 保存按钮状态
        saveFlag: '0',
        // 机构层级定义-列表查询数据
        departLevelDefData: {
            current: 1,
            pageSize: 0,
            total: 0,
            records: []
        },
        //新增或修改的数据
        dialogData: {
            no: '',
            key: '',
            name: '',
            use: '',
            use_yn: 'Y',
            modalType: '',// 1-新增，2-修改
            title: '',
            departLevelInfoData: {
                current: 1,
                pageSize: 0,
                total: 0,
                records: []
            }
        },
    },
    actions: {
        // 机构层级定义列表查询
        async queryDepartLevelDefData({ state, commit }, payload) {
            // 1.获取vuex中的departLevelDefData数据
            const departLevelDefData = state.departLevelDefData;
            // 2.页面查询条件
            const params = {
                ...payload.values
            };
            // 2.调用接口 获取数据
            const resp = await service.queryDepartLevelDefData(params);
            departLevelDefData.total = resp.total;
            departLevelDefData.pageSize = resp.pageSize;
            departLevelDefData.records = resp.rows;
            // 3.提交下vuex中的departLevelDefData数据
            commit("update", { departLevelDefData: departLevelDefData });
        },

        async loadDialogDataData({ state, commit }, payload) {
            // 1.获取vuex中的dialogData数据
            const dialogData = state.dialogData;
            if (payload == undefined) {
                // 新增对话框数据初始化
                dialogData.modalType = '1';
                dialogData.title = '新增';
                // 2.新增页面获取下机构层级外部编码
                const resp = await service.getTabKeyRule({
                    tabName: state.tableName
                });
                dialogData.key = resp.num;
                dialogData.name = '';
                dialogData.use = '';
                dialogData.departLevelInfoData.current = 1;
                dialogData.departLevelInfoData.pageSize = 0;
                dialogData.departLevelInfoData.total = 0;
                dialogData.departLevelInfoData.records = [];
            } else {
                // 修改对话框数据初始化
                dialogData.modalType = '2';
                dialogData.title = '修改';
                // 2.修改页面查询层级列表信息
                const resp = await service.queryDepartLevelInfoData({
                    no: payload.no,
                    page: 1,
                    rows: 10
                });
                dialogData.no = payload.no;
                dialogData.key = payload.key;
                dialogData.name = payload.name;
                dialogData.use = payload.use;
                dialogData.use_yn = payload.use_yn;
                dialogData.departLevelInfoData.current = 1;
                dialogData.departLevelInfoData.pageSize = resp.pageSize;
                dialogData.departLevelInfoData.total = resp.total;
                dialogData.departLevelInfoData.records = resp.rows;
            }
            // 3.提交下vuex中的dialogData数据
            commit("update", { dialogData: dialogData });
        },

        async queryDepartLevelInfoData({ state, commit }, payload) {
            // 1.获取vuex中的dialogData数据
            const dialogData = state.dialogData;
            // 2.页面查询条件
            const params = {
                ...payload.values
            };
            // 2.调用接口 获取数据
            const resp = await service.getDepartLevelInfoList(params);
            dialogData.departLevelInfoData.total = resp.total;
            dialogData.departLevelInfoData.records = resp.rows;
            // 3.提交下vuex中的dialogData数据
            commit("update", { dialogData: dialogData });
        },

        async addDepartLevelInfoData({ state, commit }) {
            // 1.获取vuex中的dialogData数据
            const dialogData = state.dialogData;
            dialogData.departLevelInfoData.records.push({
                no: util.createUniqueString(),
                key: '',
                name: '',
                use_yn: ''
            });
            // 总行数加1
            dialogData.departLevelInfoData.total++;
            // 4.提交下vuex中的dialogData数据
            commit("update", { dialogData: dialogData });

        },

        async deleteDepartLevelInfoData({ state, commit }, payload) {
            // 1.获取vuex中的dialogData数据
            const dialogData = state.dialogData;
            if (payload.index) {
                dialogData.departLevelInfoData.records.splice(payload.index, 1);
            }
            // 总行数加1
            dialogData.departLevelInfoData.total--;
            // 3.提交下vuex中的dialogData数据
            commit("update", { dialogData: dialogData });
        },

        async saveDepartLevelInfoData({ state, commit, dispatch }, payload) {
            // 1.获取vuex中的dialogData数据
            const modalType = state.dialogData.modalType;
            const dialogData = state.dialogData;
            console.log("wyq:saveDepartLevelInfoData 前 dialogData", dialogData);
            if (modalType == 1) {
                console.log("wyq:saveDepartLevelInfoData 新增 dialogData", dialogData);
                // 新增
                // 1.新增页面获取下机构层级外部编码
                if (dialogData.departLevelInfoData.records.length == 0) {
                    message.error("请至少添加一个层级！");
                    return;
                }

                // 2.调用接口 保存数据
                const params = {
                    key: dialogData.key,
                    name: dialogData.name,
                    use: dialogData.use,
                    use_yn: dialogData.use_yn,
                    _backFromClent: 'Y',
                    data: JSON.stringify(dialogData.departLevelInfoData.records)
                };
                try {
                    await service.insertDepartLevelInfoData(params);
                    // 查询下数据
                    // await dispatch('queryDepartLevelDefData', { values: { key: dialogData.key } });
                    payload.closeMethod();
                    // 处理成功响应
                    message.success("新增成功！");
                } catch (error) {
                    // 处理异常响应，比如弹窗提示等;
                    message.error(`请求失败: ${error.message}`);
                    return
                }
            } else {
                console.log("wyq:saveDepartLevelInfoData 修改 dialogData", dialogData);
                // 修改
                // 2.调用接口 修改数据
                const params = {
                    i_key: dialogData.key,
                    i_name: dialogData.name,
                    i_use: dialogData.use,
                    no: dialogData.no,
                    use_yn: dialogData.use_yn,
                    data: JSON.stringify(dialogData.departLevelInfoData.records)
                }
                try {
                    await service.updateDepartLevelInfoData(params);
                    // service.queryDepartLevelDefData({ values: {} });
                    payload.closeMethod();
                    // 处理成功响应
                    message.success("修改成功！");
                } catch (error) {
                    // 处理异常响应，比如弹窗提示等;
                    message.error(`请求失败: ${error.message}`);
                    return
                }
            }
            commit("update", { saveFlag: state.saveFlag === '0' ? '1' : '0' });
        },
        async changeUseYn({ state, commit, dispatch }, payload) {
            console.log("wyq:changeUseYn 前 payload", payload.values.use_yn, payload.values.no);
            const useYn = payload.values.use_yn;
            const params = {
                no: payload.values.no
            }
            try {
                await service.changeUseYn(params);
                // 处理成功响应
                if (useYn == 'Y') {
                    message.success("停用成功");
                } else {
                    message.success("启用成功");
                }
            } catch (error) {
                // 处理异常响应，比如弹窗提示等;
                message.error(`请求失败: ${error.message}`);
                return
            }
        }
    },
    mutations: {
        update(state, payload) {
            for (let key in payload) {
                state[key] = payload[key];
            }
        },
    }
}
