import { message } from 'antd'
import * as service from '../services/index'
import URLS from '../constants/url'

export default {
    namespace: 'authority',
    state: {
        originData: {
            panelLoading: false,
            tabActiveKey: 'userGroup',
            menuSelectedKeys: [],
            groupFilterLabel: '',
            userFilterLabel: '',
            dashboardFilterLabel: '',
            groupLimit: 30,
            userLimit: 30,
            dashboardList: [],
        },
    },
    reducers: {
        setField(state, action) {
            const { name, value } = action;
            let obj = {};
            obj[name] = value;
            return Object.assign({}, state, obj);
        },
        setFields(state, action) {
            const { fields } = action;
            let obj = {};
            fields.map(f => (obj[f.name] = f.value));
            return Object.assign({}, state, obj);
        },
        reset(state, action) {
            return Object.assign({}, state, state.originData);
        },
    },
    effects: {
        *fetchDashboardTree(action, { put, call, select }) {
            try{
                const { dtype, code, silent } = action;
                if(!silent) {
                    yield put({ type: 'setField', name: 'panelLoading', value: true });
                }
                const res = yield call(service.fetch, {
                    url: URLS.ACCESS_DASHBOARD_TREE + '/' + dtype + '/' + code,
                    method: 'GET'
                })
                
                if(!res.err && res.data.code > 0) {
                    const list = res.data.data || [];
                    const dashboardList = list.map(l => l.id + '');
                    yield put({ type: 'setFields', fields: [
                        { name: 'dashboardList', value: dashboardList },
                    ] });
                }else {
                    message.error('获取报表目录失败: ' + (res.err || res.data.msg));
                    return false;
                }
            }catch(e) {
                message.error('获取报表目录失败: ' + e.message);
                return false;
            }finally {
                yield put({ type: 'setField', name: 'panelLoading', value: false });
            }
        },
        *add(action, { select, put, call }) {
            try {
                const { dtype, code, dashboardCode, silent } = action;
                if(!silent) {
                    yield put({ type: 'setField', name: 'panelLoading', value: true });
                }
                const res = yield call(service.fetch, {
                    url: URLS.ACCESS_ADD,
                    method: 'POST',
                    body: {
                        bizId: code,
                        dashboardId: dashboardCode,
                        type: dtype
                    }
                });
                if(!res.err && res.data.code > 0) {
                    yield put({ type: 'fetchDashboardTree', dtype, code });
                }else {
                    message.error('设置权限失败: ' + (res.err || res.data.msg));
                }
            }catch(e) {
                message.error('设置权限失败: ' + e.message);
            }finally {
                yield put({ type: 'setField', name: 'panelLoading', value: false });
            }
        },
        *addAll(action, { select, put, call }) {
            try {
                const { dtype, code, dashboardCodes, silent } = action;
                if(!silent) {
                    yield put({ type: 'setField', name: 'panelLoading', value: true });
                }
                const res = yield call(service.fetch, {
                    url: URLS.ACCESS_ADD_ALL,
                    method: 'POST',
                    body: {
                        bizId: code,
                        dashboardIds: dashboardCodes.join(','),
                        type: dtype
                    }
                });
                if(!res.err && res.data.code > 0) {
                    yield put({ type: 'fetchDashboardTree', dtype, code });
                }else {
                    message.error('设置权限失败: ' + (res.err || res.data.msg));
                }
            }catch(e) {
                message.error('设置权限失败: ' + e.message);
            }finally {
                yield put({ type: 'setField', name: 'panelLoading', value: false });
            }
        },
        *remove(action, { select, put, call }) {
            try {
                const { dtype, code, dashboardCode, silent } = action;
                if(!silent) {
                    yield put({ type: 'setField', name: 'panelLoading', value: true });
                }
                const res = yield call(service.fetch, {
                    url: URLS.ACCESS_REMOVE,
                    method: 'POST',
                    body: {
                        bizId: code,
                        dashboardId: dashboardCode,
                        type: dtype
                    }
                });
                if(!res.err && res.data.code > 0) {
                    yield put({ type: 'fetchDashboardTree', dtype, code });
                }else {
                    message.error('设置权限失败: ' + (res.err || res.data.msg));
                }
            }catch(e) {
                message.error('设置权限失败: ' + e.message);
            }finally {
                yield put({ type: 'setField', name: 'panelLoading', value: false });
            }
        },
        *removeAll(action, { select, put, call }) {
            try {
                const { dtype, code, dashboardCodes, silent } = action;
                if(!silent) {
                    yield put({ type: 'setField', name: 'panelLoading', value: true });
                }
                const res = yield call(service.fetch, {
                    url: URLS.ACCESS_REMOVE_ALL,
                    method: 'POST',
                    body: {
                        bizId: code,
                        dashboardIds: dashboardCodes.join(','),
                        type: dtype
                    }
                });
                if(!res.err && res.data.code > 0) {
                    yield put({ type: 'fetchDashboardTree', dtype, code });
                }else {
                    message.error('设置权限失败: ' + (res.err || res.data.msg));
                }
            }catch(e) {
                message.error('设置权限失败: ' + e.message);
            }finally {
                yield put({ type: 'setField', name: 'panelLoading', value: false });
            }
        },
        *batchActions(action, { select, put, call}) {
            try {
                const { actions } = action;
                for(let i = 0; i < actions.length; i++) {
                    let act = actions[i];
                    yield put({ ...act });
                }
            }catch(e) {
                message.error(e.message);
            }
        }
    },
    subscriptions: {
        setup({ dispatch, history }) {
            dispatch({ type: 'reset' });
        }
    }
}