import { clear, Console } from 'console';
import * as service from '../services';
import * as util from "@/utils/index.js";
import { title } from 'process';
export default {
    namespaced: true,
    state: {
        // 机构层级定义-列表查询数据
        departLevelDefData: {
            current: 1,
            pageSize: 0,
            total: 0,
            records: []
        },
        // 机构层级基本-列表查询数据
        departLevelInfoData: {
            current: 1,
            pageSize: 0,
            total: 0,
            records: []
        },
        //新增或修改的数据
        dialogData: {},


        ////////////////////////////////////////

        // 查询页
        departLevelData: {
            current: 1,
            pageSize: 0,
            total: 0,
            records: []
        },
        // 表格数据
        queryParam: {},
        // list页面查询条件
        insertOrModify: 1,
        //新增修改标志：1-新增，2-修改

        //新增修改
        formData: {},
        //表单数据
        title: '',
        // modal页面标题
        conversion: {
            current: 1,
            pageSize: 0,
            total: 0,
            records: []
        },
        //折算币种
        // 启用数据
        useNo: "",
        validateFlag: true,
        searchCurrency: [],
        // 新增修改页面折算后币种查询条件
        toCurNo: [],


        departLevelInfo: {
            current: 1,
            pageSize: 0,
            total: 0,
            records: []
        }
    },
    actions: {
        // 机构层级定义列表查询
        async queryDepartLevelDefData({ commit }, payload) {
            // 1.页面查询条件
            const params = {
                ...payload.values
            };
            // 2.调用接口 获取数据
            const resp = await service.queryDepartLevelDefData(params);
            // 3.更新state数据
            commit("update", {
                departLevelDefData: {
                    current: resp.pageNo,
                    pageSize: resp.pageSize,
                    total: resp.total,
                    records: resp.rows
                }
            });
        },

        // 新增或修改
        async addOrModify({ commit, state }, payload) {
            // 清理下数据
            console.log("wyq", payload);
            // commit("clearDepartLevelInfo");
            const data = await service.searchCurrency({
                use_yn: "Y"
            });
            if (payload == undefined) {
                // 新增
                const tableKey = await service.getTabKeyRule({
                    tabName: 'currency'
                });
                commit("update", {
                    insertOrModify: 1,
                    //title: '新增',
                    formData: {
                        key: tableKey.value
                    },
                    useNo: "",
                    searchCurrency: data.test,
                    validateFlag: true,
                    toCurNo: ''
                });
            } else {
                console.log("wyq:addOrModifyData 前", state.dialogData);
                // 修改页面
                // 1.查询层级信息
                const resp = await service.queryDepartLevelInfoData({
                    no: payload.no
                });
                //resp.rows.forEach(item => item.from_cur_no = payload.name);
                commit("update",
                    {
                        //title: '修改',
                        dialogData: payload,
                        departLevelInfoData: {
                            total: resp.total,
                            records: resp.rows
                        },


                        insertOrModify: 2,
                        formData: payload,
                        useNo: payload.no,
                        searchCurrency: data.test,
                        validateFlag: true,
                        toCurNo: payload.no
                    });
                console.log("wyq:addOrModifyData 后", state.dialogData);

            }
        },

        cleanData({
            commit,
            state
        }) {
            commit("clearDepartLevelInfo");
        },


        // 新增一行
        async addRows({
            commit,
            state
        }) {
            const resp = state.departLevelInfoData;
            resp.records.push({
                rowsId: Date.now(),
                key: '123',
                name: '321',
                use_yn: "Y"
            });
            // 总行数加1
            resp.total++;
            commit("update", {
                departLevelInfoData: {
                    total: resp.total,
                    records: resp.records
                }
            });
        },

        // 删除一行
        async deleteRows({
            commit,
            state
        }, payload) {
            const resp = state.departLevelInfoData;
            if (payload.index) {
                resp.records.splice(payload.index, 1);
            }
            // 总行数加1
            resp.total--;
            commit("update", {
                departLevelInfoData: {
                    total: resp.total,
                    records: resp.records
                }
            });
        },
        // 保存
        async save({
            commit,
            state
        }, payload) {
            // const data = state.formData;
            // if (state.insertOrModify == 1) {
            //     // 新增
            //     const tableKey = await service.getTabKeyRule({
            //         tabName: 'currency'
            //     });
            //     data.key = tableKey.value;
            //     const resp = await service.addDepartLevelDefData(data);
            //     if (resp.code == 0) {
            //         commit("update", {
            //             title: '新增',
            //             dialogData: {},
            //             insertOrModify: 1,
            //             formData: {},
            //             useNo: "",
            //             searchCurrency: [],
            //             validateFlag: true,
            //             toCurNo: ''
            //         });
            //         util.successMessage("新增成功");
            //     } else {
            //         util.errorMessage("新增失败");
            //     }
            // } else {
            //     // 修改
            //     const resp = await service.modifyDepartLevelDefData(data);
            //     if (resp.code == 0) {
            //         commit("update", {
            //             title: '修改',
            //             dialogData: {},
            //             insertOrModify: 1,
            //             formData: {},
            //             useNo: "",
            //             searchCurrency: [],
            //             validateFlag: true,
            //             toCurNo: ''
            //         });
            //         util.successMessage("修改成功");
            //     } else {
            //         util.errorMessage("修改失败");
            //     }
            // }
        },
        ////////////////////////////////////////


        // 列表查询
        async queryDepartLevel({
            commit,
            state
        }, payload) {
            // 页面查询条件
            const queryParam = state.queryParam;
            payload.values = util.initQueryParam(payload, queryParam);
            const params = {
                use_yn: "Y",
                ...payload.values
            };
            const data = await service.queryDepartLevelDefData(params);
            commit("update", {
                departLevelData: {
                    current: data.pageNo,
                    pageSize: data.pageSize,
                    total: data.total,
                    records: data.rows
                },
                queryParam: payload.values
            });
        },




        // 新增一行
        async addRows2({
            commit,
            dispatch,
            state
        }, payload) {
            let conversion = state.conversion;
            conversion.records.push({
                rowsId: Date.now(),
                use_yn: "Y",
                key: '123',
                name: '321',
            });
            console.log(conversion.records);
            const name = payload.name;
            if (name) {
                conversion.records.forEach(item => {
                    item.from_cur_no = name;
                });
            }
            commit("update", {
                conversion
            });
        }
    },
    mutations: {
        update(state, payload) {
            for (let key in payload) {
                state[key] = payload[key];
            }
        },
        // 清理数据
        clearDepartLevelInfo(state) {
            state.departLevelInfo = {
                current: 1,
                pageSize: 0,
                total: 0,
                records: []
            },
                state.title = '',
                state.dialogData = {}
        }
    }
}
