/**
 * 历史记录
 */
// import { message } from 'antd'
import * as service from '../services/index'
import URLS from '../constants/url'
import CHART_TYPE from './chartType.json'
import moment from 'moment'

export default {
    namespace: 'recent',
    state: {
        originData: {
            recentChart: [],
            recentDashboard: []
        }
    },
    reducers: {
        listRecentChart(state, action) {
            const { recentChart } = action;
            return { ...state, recentChart };
        },
        listRecentDashboard(state, action) {
            const { recentDashboard } = action;
            return { ...state, recentDashboard };
        },
        reset(state, action) {
            let newState = Object.assign({}, state, state.originData);
            return Object.assign({}, newState);
        },
    },
    effects: {
        *fetchRecentChart(action, { select, call, put }) {
            try {
                const res = yield call(service.fetch, {
                    url: URLS.HOMEPAGE_RECENT_CHART_GET,
                });
                
                if(res.code > 0) {
                    let recentChart = res.data.map((r, i) => {
                        return {
                            key: r.chartId + '',
                            code: r.chartId + '',
                            name: r.chartName,
                            type: CHART_TYPE[r.chartType],
                            createBy: r.createBy,
                            chartOption: r.chartOption ? JSON.parse(r.chartOption) : {},
                            description: r.describes || "",
                            createTime: r.createDate,
                            recentTime: moment(r.reDate)
                        }
                    });
                    yield put({ type: 'listRecentChart', recentChart });
                }else {
                    // message.error('读取最近访问图表列表错误: ' + res.msg);
                }
            }catch(e) {
                console.error(e);
                // message.error('读取最近访问图表列表错误: ' + e.message);
            }
        },
        *fetchRecentDashboard(action, { select, call, put }) {
            try {
                // const recent = yield select(state => state.recent);
                // if(!action.mandatory && recent.recentDashboard.length > 0) {
                //     return;
                // }
                const res = yield call(service.fetch, {
                    url: URLS.HOMEPAGE_RECENT_DASHBOARD_GET,
                });
                if(res.code > 0) {
                    let recentDashboard = res.data.map((r, i) => {
                        return {
                            key: r.id + '',
                            code: r.id + '',
                            name: r.bdName,
                            thumbnail: r.thumbnail,
                            createBy: r.createBy,
                            description: r.bdNote || "",
                            recentTime: r.reDate
                        }
                    });
                    yield put({ type: 'listRecentDashboard', recentDashboard });
                }else {
                    // message.error('读取最近访问报表列表错误: ' + res.msg);
                }
            }catch(e) {
                console.error(e);
                // message.error('读取最近访问报表列表错误: ' + e.message);
            }
            
        },
        *addRecentRecord(action, { call }) {
            const { tarId, recordType } = action
            try {
                yield call(service.fetch, {
                    url: URLS.HOMEPAGE_ADD_RECENT_RECORD,
                    body: {
                        tarId: tarId,
                        type: recordType
                    }
                });
            }catch(e) {
                console.error(e);
                // message.error('添加访问记录错误: ' + e.message);
            }
        }
    },
    subscriptions: {
        setup({ dispatch, history }) {
            dispatch({ type: 'reset' });
        }
    }
};