import {
    getProOrgTreeByParentidForProOrg,
    getAreaTreeByParentidForArea,
    updateDropNodeForProMenu,
    getProDeptTreeByParentidForProDept,
    getProOrgTreeByLevelCodeForProOrg,
    updateDropNodeForProResourceAction,
    getProMenuTreeByLevelCode,
    getProMenuTreeByParentIdForProMenu,
    getProEbookArticleTypeTreeByParentid
} from '@/services/api_globaldata'

export default {
    namespace: 'globaldata',
    state: {
        selectOrganTree: {
            list:[],
        },
        selectAreaTree: {
            list:[],
        },
        dropTree: null,
        selectDeptTree: {
            data: []
        },
        selectOrgTree: {
            data: []
        },
        menuData: {
            list: []
        }
    },
    effects: {
        *get_area_tree_by_parentid_for_area({ payload, callback }, { call, put }) {
            const response = yield call(getAreaTreeByParentidForArea, payload)
            yield put({
                type: 'getAreaTreeByParentidForArea',
                payload: response,
            })

            callback && callback(response)
        },
        *get_proorgtree_by_parentid_for_proorg({ payload, callback }, { call, put }) {
            const response = yield call(getProOrgTreeByParentidForProOrg, payload)
            yield put({
                type: 'getProOrgTreeByParentidForProOrg',
                payload: response,
            })

            callback && callback(response)
        },
        *update_drop_node_for_pro_menu({ payload, callback }, { call, put }) {
            const response = yield call(updateDropNodeForProMenu, payload)
            yield put({
                type: 'updateDropNodeForProMenu',
                payload: response
            })

            callback && callback(response)
        },
        *get_prodepttree_by_parentid_for_prodept({ payload, callback }, { call, put }) {
            const response = yield call(getProDeptTreeByParentidForProDept, payload)
            yield put({
                type: 'getProDeptTreeByParentidForProDept',
                payload: response
            })

            callback && callback(response)
        },
        *get_proorg_tree_by_level_code_for_proorg({ payload, callback }, { call, put }) {
            const response = yield call(getProOrgTreeByLevelCodeForProOrg, payload)
            yield put({
                type: 'getProOrgTreeByLevelCodeForProOrg',
                payload: response
            })

            callback && callback(response)
        },
        *update_drop_node_for_pro_resource_action({ payload, callback }, { call, put }) {
            const response = yield call(updateDropNodeForProResourceAction, payload)
            yield put({
                type: 'updateDropNodeForProResourceAction',
                payload: response
            })

            callback && callback(response)
        },
        *get_promenutree_by_levelcode({ payload, callback }, { call, put }) {
            const response = yield call(getProMenuTreeByLevelCode, payload)
            yield put({
                type: 'getProMenuTreeByLevelCode',
                payload: response
            })

            callback && callback(response)
        },
        *get_promenutree_by_parentid_for_promenu({ payload, callback }, { call, put }) {
            const response = yield call(getProMenuTreeByParentIdForProMenu, payload)
            yield put({
                type: 'getProMenuTreeByParentIdForProMenu',
                payload: response
            })

            callback && callback(response)
        },
        *get_proebookarticletypetree_by_parentid({ payload, callback }, { call, put }) {
            const response = yield call(getProEbookArticleTypeTreeByParentid, payload)
            yield put({
                type: 'getProEbookArticleTypeTreeByParentid',
                payload: response
            })

            callback && callback(response)
        },
    },
    reducers: {
        getAreaTreeByParentidForArea(state, action) {
            return {
                ...state,
                selectAreaTree: action.payload,
            }
        },
        getProOrgTreeByParentidForProOrg(state, action) {
            return {
                ...state,
                selectOrganTree: action.payload,
            }
        },
        updateDropNodeForProMenu(state, action) {
            return {
                ...state,
                dropTree: action.payload
            }
        },
        getProDeptTreeByParentidForProDept(state, action) {
            action.payload.list = action.payload.data
            return {
                ...state,
                selectDeptTree: action.payload
            }
        },
        getProOrgTreeByLevelCodeForProOrg(state, action) {
            return {
                ...state,
                selectOrgTree: action.payload
            }
        },
        updateDropNodeForProResourceAction(state, action) {
            return {
                ...state,
                dropTree: action.payload
            }
        },
        getProMenuTreeByLevelCode(state, action) {
            return {
                ...state,
                menuData: action.payload
            }
        },
    }
}
