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

export default {
    namespace: 'user',
    state: {
        originData: {
            list: [],
            newOne: {},
            filterLabel: ''
        },
        newOne: {}
    },
    reducers: {
        list(state, action) {
            const { list } = action;
            return { ...state, list }
        },
        setFilterLabel(state, action) {
            const { label } = action;
            return { ...state, filterLabel: label}
        },
        setNewModelFields(state, action) {
            const { fields } = action;
            let newOne = state.newOne;
            for(let i = 0; i < fields.length; i++) {
                newOne[fields[i]['name']] = fields[i]['value'];
            }
            delete newOne.invalid;
            return Object.assign({}, state, {newOne});
        },
        reset(state, action) {
            let newState = Object.assign({}, state, state.originData);
            return Object.assign({}, newState);
        },
    },
    effects: {
        *fetchList(action, { put, call, select }) {
            const user = yield select(state => state.user);
            try {
                if(!action.mandatory && user.list.length > 0) {
                    return;
                }
                const res = yield call(service.fetch, {
                    url: URLS.USER_LIST
                });
                if(res.code > 0) {
                    const list = res.data.map(d => ({
                        code: d.id+'',
                        userName: d.userName,
                        fullName: d.name,
                        role: d.role
                    }));
                    yield put({ type: 'list', list });
                }else {
                    message.error('请求用户列表失败: ' + res.msg);
                }
            }catch(e) {
                message.error('请求用户列表失败: ' + e.message);
            }
        },
        *remoteModify(action, { put, call, select }) {
            const user = yield select(state => state.user);
            const { newOne } = user;
            const { fields } = action;
            const body = {
                id: newOne.code
            };
            for(let k in fields) {
                if(k !== 'id') {
                    body[k] = fields[k]
                }
            }
            try {
                const res = yield call(service.fetch, {
                    url: URLS.USER_UPDATE,
                    body
                });
                if(res.code > 0) {
                    message.success('修改成功');
                    yield put({ type: 'fetchList', mandatory: true });
                }else {
                    message.error('修改失败: ' + res.msg);
                }
            }catch(e) {
                message.error('修改失败: ' + e.message);
            }
        }
    },
    subscriptions: {
        setup({ dispatch, history }) {
            dispatch({ type: 'reset' });
        }
    }
};