import axios from "axios"

function formatDet(data) {
    console.log
    let format = function (pid) {
        let arr = [];
        data.forEach(item => {
            if (item.pid == pid) {
                item.children = format(item.id);
                arr.push(item)
            }
        })
        return arr;
    }
    return format(0)
}
export default {
    namespaced: true,
    state: {
        formatDep: [],
        quota1: [],
        quota2: [],
        department: [],
        quota: [],
        principalList: [],
        currentPage:null,
    },
    mutations: {
        SET_CURRENT_PAGE(state,data){
            state.currentPage=data;
        },
        SET_DEPARTMENT(state, data) {
            state.department = data;
        },
        SET_QUOTAL1(state, data) {
            state.quota1 = data;
        },
        SET_QUOTAL2(state, data) {
            state.quota2 = data;
        },
        SET_FORMATDEP(state, data) {
            state.formatDep = data;
        },
        SET_QUOTA(state, data) {
            state.quota = data;
        },
        SET_PRINCIPAL_LIST(state, data) {
            state.principalList = data;
        }
    },
    actions: {
        getDepartment({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                if (state.department.length > 0) {
                    return;
                } else {
                    axios.get('dept/getDeptList/' + data).then(res => {
                        let data = res.data.data;
                        commit('SET_DEPARTMENT', data);
                        commit('SET_FORMATDEP', formatDet(data))
                    }).catch(() => {
                        commit('SET_DEPARTMENT', []);
                        commit('SET_FORMATDEP', []);
                    })
                }
            })
        },
        getPrincipalList({ commit, state }) {
            return new Promise((resolve, reject) => {
                if (state.principalList.length > 0) {
                    return;
                } else {
                    axios.get('/user/getUserList').then(res => {
                        console.log(res)
                        let data = res.data.data;
                        commit('SET_PRINCIPAL_LIST', data);
                    }).catch(() => {
                        commit('SET_PRINCIPAL_LIST', []);
                    })
                }
            })
        },
        getQuota1List({ commit }, data) {
            return new Promise((resolve) => {
                axios.get('/quota/getQuota1List/' + data).then(res => {
                    let data = res.data.data;
                    commit('SET_QUOTAL1', data);
                    resolve(data)
                }).catch(() => {
                    commit('SET_QUOTAL1', [])
                    resolve([]);
                })
            })
        },
        getQuota2List({ commit }, params) {
            return new Promise((resolve) => {
                axios.get('/quota/getQuota2List', {params}).then(res => {
                    let data = res.data.data;
                    commit('SET_QUOTAL2', data);
                    resolve(data)
                }).catch(() => {
                    commit('SET_QUOTAL2', [])
                    resolve([]);
                })
            })
        },
        getQuotaList({ commit }) {
            return new Promise((resolve) => {
                axios.get('/rigidityconstraint/getQuotaList').then(res => {
                    let data = res.data.data;
                    commit('SET_QUOTA', data);
                    resolve(data)
                }).catch(() => {
                    commit('SET_QUOTA', [])
                    resolve([]);
                })
            })
        },
    },
}