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

export default {
    namespace: 'logs',
    state: {
        originData: {
            listLoading: false,
            list: [],
            total: 0,
            filterLabel: '',
            filterItems: [
                { name: 'module', label: '模块', type: 'enum', options: [
                    { name: '数据链接', label: '数据链接' },
                    { name: '数据源', label: '数据源' },
                    { name: '图表', label: '图表' },
                    { name: '报表', label: '报表' },
                    // { name: '数据源分组', label: '数据源分组' },
                    // { name: '图表分组', label: '图表分组' },
                    { name: '权限管理', label: '权限管理' },
                    { name: '用户管理', label: '用户管理' },
                ] },
                { name: 'name', label: '名称', type: 'string' },
                { name: 'operator', label: '操作人', type: 'string' },
                { name: 'date', label: '操作时间', type: 'date' },
            ], // 可选过滤字段
            filterItem: { name: 'module', label: '模块', type: 'enum', options: [
                { name: '数据链接', label: '数据链接' },
                { name: '数据源', label: '数据源' },
                { name: '图表', label: '图表' },
                { name: '报表', label: '报表' },
                // { name: '数据源分组', label: '数据源分组' },
                // { name: '图表分组', label: '图表分组' },
                { name: '权限管理', label: '权限管理' },
                { name: '用户管理', label: '用户管理' },
            ] }, // 已选过滤字段
            listScrollTop: 0, // 记录列表界面滚动条位置
            currentPage: 1, // 记录分页器当前所在页数
        },
    },
    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);
        },
        setFilterItem(state, action) {
            const { item } = action;
            return Object.assign({}, state, {filterItem: item, filterLabel: ''});
        },
        setFilterLabel(state, action) {
            const { label } = action;
            return Object.assign({}, state, {filterLabel: label, currentPage: 1});
        },
        reset(state, action) {
            return Object.assign({}, state, state.originData);
        },
    },
    effects: {
       *fetchList(action, { select, call, put }) {
            const { pageNum, pageSize, silent } = action;
            const body = {
                pageNum: pageNum || 1,
                pageSize: pageSize || 99999999
            }
            try{
                // const logs = yield select(state => state.present.logs);
                // if(!action.mandatory && logs.list.length > 0) {
                //     return;
                // }
                if(!silent) {
                    yield put({ type: 'setField', name: 'listLoading', value: true });
                }
                const res = yield call(service.fetch, {
                    url: URLS.MESSAGELOG_LIST,
                    method: 'GET',
                    body
                });
                if(!res.err && res.data.code > 0) {
                    let total = res.data.data.total;
                    let list = res.data.data.list.map(d => {
                        return {
                            code: d.id + '',
                            date: d.date,
                            operation: d.content,
                            module: d.module,
                            name: d.order,
                            result: d.result,
                            search: d.search,
                            operator: d.userName
                        }
                    })
                    yield put({ type: 'setFields', fields: [
                        { name: 'list', value: list },
                        { name: 'total', value: total },
                    ] });
                }else {
                    message.error('请求操作日志列表失败: ' + (res.err || res.data.msg));
                }
            }catch(e) {
                message.error('请求操作日志列表失败: ' + e.message);
            }finally {
                yield put({ type: 'setField', name: 'listLoading', value: false });
            }
        } 
    },
    subscriptions: {
        setup({ dispatch, history }) {
            dispatch({ type: 'reset' });
        }
    }
};