import {queryPage,deptAdd,deptEdit,deptDelete,selectDeptDetails} from "@/api/jurisdiction/dept";
import {message} from "@/utils/common";

// 字段
const getDefaultState = () =>{
    return {
        tableData:[], // 部门列表
        loading:false,
        addStatus:false, // 新增弹框
        page:{
            pageSize:10,
            pageNum:1,
            total:0
        },
    }
}

const state = getDefaultState();
const mutations = {
    GET_DEPTS(state,data){
        state.tableData = data.list;
        state.page.total = data.total;
        state.loading = false;
    },
    DEPT_ADD(state,data){
        state.addStatus = false;
    },
}

const actions = {
    // 查询列表
    async GetDepts({commit,dispatch},data){
        await queryPage(data).then(res=>{
            try {
                commit('GET_DEPTS',res.body)
            } catch (error) {
                throw new Error(err)
            }
        })
    },
    // 新增
    async DeptAdd({commit,dispatch},data){
        await deptAdd(data).then(res=>{
            try {
                message('success','新增成功',3000);
                commit('DEPT_ADD',res.body)
            } catch (error) {
                throw new Error(err)
            }
        })
    },
    // 修改
    async DeptEdit({commit},data){
        await deptEdit(data).then(res=>{
            try {
                message('success','修改成功',3000);
                commit('DEPT_ADD',res.body)
            } catch (error) {
                throw new Error(err)
            }
        })
    },
    // 删除
    async DeptDelete({commit},data){
        await deptDelete(data).then(res=>{
            try {
                message('success','删除成功',3000);
            } catch (error) {
                throw new Error(err)
            }
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}

