import * as Api from '../apis/timeWage';
import {
    observable,
    action,
} from 'mobx';
import {
    WeaTableNew,
} from 'comsMobx';
import {
    WeaLocaleProvider,
} from 'ecCom';
import {
    message,
    Button,
} from 'antd';
const {
    TableStore
} = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;

export class HrmTimeWageSummary {
    //*************************index********************************
    @observable main = {
        authorized: false,
        loading: true,
    }

    @action
    getAuth = () => {
        Api.getAuth().then(cb => {
            const {
                status,
                hasRight
            } = cb;

            if (status == '1') {
                hasRight && this.grantAuth();
                this.stoploading();
            } else {
                message.error();
            }
        }).catch(error => {
            message.error();
        });
    }

    @action
    grantAuth = () => {
        this.main.authorized = true;
    }

    @action
    stoploading = () => {
        this.main.loading = false;
    }

    refresh = () => {
        this.getRadioGroupDatas();
    }


    //*************************Top*******************************
    @observable top = {
        title: getLabel('391166', "时间工资汇总"),
        menu: [],
    }

    get menus() {
        const top = this.top.menu.filter(item => item.isTop == '1'),
            right = this.top.menu;

        let buttons = [],
            dropMenuDatas = [];

        top.map((item, i) => {
            buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@z7m2zx@${i}`} type='primary' onClick={() => this.handleTopBtnsClick(item)}>{item.menuName}</Button>);
        });

        right.map((item, index) => {
            let obj = {
                key: item.menuFun,
                icon: <i className={`${item.menuIcon}`}/>,
                content: item.menuName,
                onClick: () => this[item.menuFun](item.menuFun)
            }

            if (['log', 'collection'].includes(item.menuFun)) {
                obj.disabled = true;
            }

            dropMenuDatas.push(obj);
        })

        return {
            buttons,
            dropMenuDatas
        };
    }

    @action
    handleTopBtnsClick = (item) => {
        this[item.menuFun] && this[item.menuFun](item.menuFun)
    }

    //*************************RadioGroup*******************************
    @observable radioGroup = {
        config: [],
        loading: false,
    }

    @action
    getRadioGroupDatas = () => {
        ['loading', 'config'].map((v, i) => {
            this.radioGroup[v] = (i == 0) ? true : [];
        });
        this.fetch({
            name: 'getRadioGroupDatas',
            logic: (cb) => {
                ['config', 'loading'].map((v, i) => {
                    this.radioGroup[v] = (i == 0) ? cb.conditions[0].items : false;
                });
            }
        });

    }

    @action
    resetRadioGroup = () => {
        ['config', 'loading'].map((v, i) => {
            this.radioGroup[v] = (i == 0) ? [] : false;
        });
    }

    @action
    radioGroupCallBack = (params) => {
        const {
            payYearSelect,
            payYear
        } = params;

        this.radioGroup.params = params;

        if (payYearSelect == '6' && !payYear) {
            return
        }

        this.getTableInfo();
    }

    //*************************table*******************************
    @observable table = {
        tableStore: new TableStore(),
        colNum: 0,
        hasDatas: true,
    }

    @action
    getTableInfo = () => {
        this.fetch({
            name: 'getTableInfo',
            fetchParams: this.radioGroup.params,
            logic: (cb) => {
                const {
                    sessionkey,
                    colNum = 0,
                    hasDatas = true,
                } = cb;

                

            const { status } = cb;

            if (status == "-1") {
                this.main.authorized = false;
            }

                this.table.hasDatas = hasDatas;
                if (hasDatas) {
                    this.table.tableStore = new TableStore();
                    this.table.tableStore.getDatas(sessionkey, 1);
                    this.table.colNum = colNum;
                }
            }
        });
    }

    @action
    resetTable = () => {
        this.table.tableStore = new TableStore();
    }

    fetch = (params) => {
        const {
            name,
            fetchParams = {},
            logic
        } = params;

        Api[name](fetchParams).then(cb => {
            const {
                api_status,
            } = cb;

            if (api_status) {
                logic(cb);
            } else {
                message.error();
            }
        }).catch(error => {
            message.error();
        });
    }

}