import { clear } from 'console';
import * as service from '../services';
import * as util from "@/utils/index.js";
export default {
    namespaced: true,
    state: {
        // 查询页
        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 queryDepartLevel({
            commit,
            state
        }, payload) {
            console.log("wyq--", payload);
            console.log("wy1q--", state.queryParam.values);

            // 页面查询条件
            const queryParam = state.queryParam;
            console.log("wy2q--", queryParam);

            payload.values = util.initQueryParam(payload, queryParam);
            const params = {
                use_yn: "Y",
                ...payload.values
            };
            const data = await service.queryDepartLevel(params);
            commit("update", {
                departLevelData: {
                    current: data.pageNo,
                    pageSize: data.pageSize,
                    total: data.total,
                    records: data.rows
                },
                queryParam: payload.values
            });
        },


        // 新增/修改页面打开
        async clickInsertOrModifyBtn({ commit }, payload) {
            // 清理下数据
            commit("clearDepartLevelInfo");

            const conversion = {
                current: 1,
                pageSize: 0,
                total: 0,
                records: []
            };
            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
                    },
                    conversion,
                    useNo: "",
                    searchCurrency: data.test,
                    validateFlag: true,
                    toCurNo: ''
                });
            } else {
                // 修改页面
                console.log(payload.no);
                const currencyConversion = await service.queryDepartLevelInfo({
                    no: payload.no
                });
                currencyConversion.rows.forEach(item => item.from_cur_no = payload.name);
                conversion.records = currencyConversion.rows;
                commit("update", {
                    insertOrModify: 2,
                    title: '币种修改',
                    formData: payload,
                    conversion,
                    useNo: payload.no,
                    searchCurrency: data.test,
                    validateFlag: true,
                    toCurNo: payload.no
                });
            }
        },
        // 新增一行
        async addRows({
            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: []
            }
        }
    }
}
