import {createAction, NavigationActions, Storage} from '../utils';
import SyncStorage from 'sync-storage';
import * as authService from '../services/user';
import ResponseError from '../components/ResponseError';

export default {
    namespace: 'auth',
    state: {
        loading: false,
        tipsCount: {},
    },
    reducers: {
        updateState(state, {payload}) {
            return {...state, ...payload};
        },
        queryList(state, {payload}) {
            return {...state, ...payload};
        },
    },
    effects: {
        * authLogin({payload, callback, unLogin}, {call, put}) {
            const result = yield call(authService.authLogin, payload);
            // token错误，error=true
            if (!result.code && !result.data.error) {
                const data = {
                    token: result.data.token,
                    username: result.data.username,
                    user_id: result.data.user_id,
                    manager_stores: result.data.manager_stores,
                };
                SyncStorage.set('credential', data);
                SyncStorage.set('premissions', result.data.permissions);
                if (callback) {
                    callback();
                }
            } else {
                SyncStorage.remove('credential');
                unLogin();
            }
        },
        * login({payload, callback, unLogin}, {call, put}) {
            yield put(createAction('updateState')({loading: true}));
            const result = yield call(authService.login, payload);
            if (!result.code) {
                const data = {
                    token: result.data.token,
                    username: result.data.username,
                    user_id: result.data.user_id,
                    manager_stores: result.data.manager_stores,
                };
                SyncStorage.set('credential', data);
                SyncStorage.set('premissions', result.data.permissions);
                if (callback) {
                    callback();
                }
            } else {
                if (result.msg) {
                    unLogin(result.msg);
                }
            }
            yield put(createAction('updateState')({loading: false}));
        },
        * logout({payload}, {call, put}) {
            yield call(authService.loginOut);
            SyncStorage.remove('credential');
            yield put(NavigationActions.navigate({
                routeName: 'Login',
                actions: [NavigationActions.navigate({routeName: 'Login'})],
            }));
        },
        //修改密码
        * changePassword({payload, callback}, {call, put}) {
            yield put(createAction('queryList')({loading: true}));
            const response = yield call(authService.changePassword, payload);
            if (!response.code) {
                if (callback) {
                    callback();
                }
            } else {
                ResponseError(response);
            }
            yield put(createAction('queryList')({loading: false}));
        },
        * fetchTipsCount({payload}, {call, put}) {
            yield put(createAction('updateState')({loading: true}));
            const response = yield call(authService.queryTipsCount);
            if (!response.code) {
                yield put(createAction('updateState')({tipsCount: response.data, loading: false}));
            } else {
                ResponseError(response);
                yield put(createAction('updateState')({tipsCount: {}, loading: false}));
            }
        },
    },
    subscriptions: {
        setup({dispatch}) {
            //dispatch({ type: 'loadStorage' })
        },
    },
};
