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

export default {
    namespace: 'dashboard',
    state: {
        currentDashboard: {},
        dashboardList: [{            //Dynamic Dashboard指看板（动态） Static Dashboard指报告（静态）
            dashboardID: 1,
            type: 'dynamic',
            title: 'Card 1',
            url: '',
            description: 'Description 1',
            coverImg:'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png',
            coverAlt: '',
            avatar: '',
            dashboardConfig: {},
            creator: {}
        },{
            dashboardID: 2,
            type: 'dynamic',
            title: 'Card 2',
            url: '',
            description: 'Description 1',
            coverImg:'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png',
            coverAlt: '',
            avatar:{},
            dashboardConfig: {},
            creator: {}
        },{
            dashboardID: 3,
            type: 'dynamic',
            title: 'Card 3',
            url: '',
            description: 'Description 1',
            coverImg:'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png',
            coverAlt: '',
            avatar:{},
            dashboardConfig: {},
            creator: {}
        },{
            dashboardID: 6,
            type: 'dynamic',
            title: 'Card 6',
            url: '',
            description: 'Description 1',
            coverImg:'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png',
            coverAlt: '',
            avatar:{},
            dashboardConfig: {},
            creator: {}
        },{
            dashboardID: 7,
            type: 'dynamic',
            title: 'Card 7',
            url: '',
            description: 'Description 1',
            coverImg:'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png',
            coverAlt: '',
            avatar:{},
            dashboardConfig: {},
            creator: {}
        },{
            dashboardID: 4,
            type: 'static',
            title: 'Card 4',
            url: '',
            description: 'Description 1',
            coverImg:'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png',
            coverAlt: '',
            avatar:{},
            dashboardConfig: {},
            creator: {}
        },{
            dashboardID: 5,
            type: 'static',
            title: 'Card 5',
            url: '',
            description: 'Description 1',
            coverImg:'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png',
            coverAlt: '',
            avatar:{},
            dashboardConfig: {},
            creator: {}
        }
        ],
    },
    reducers: {
        list(state, action) {
            let data = action.data;
            return Object.assign({}, state, {dashboardList: data});
        }
    },
    effets: {
        *fetchList(action, {select, call, put}) {
            try {
                const dashboard = yield select(state => state.present.dashboard)
                if(!action.mandatory && dashboard.list.length > 0) {
                    return;
                }
                const response = yield call(service.fetch, {
                    url: "127.0.0.1:5000/dashboard",
                    method: 'GET',
                    body: {}
                });
                if(!response.err && response.data.code > 0) {
                    // let data = response
                }
            }catch(e) {
                message.error('读取报告与看板错误')
            }
        }
    },
    subscriptions: {
        setup({ dispatch, history}) {
            return history.listen(({pathname}) => {
                if (pathname === '/dashboard') {
                    dispatch({ type: 'fetchList'});
                }
            }
        )
        }
    }
}