import * as Api from '../apis/staffPayroll';
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 HrmStaffPayrollSummary {
    //*************************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('391167', "人员工资汇总"),
        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@2szfsd@${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
        };
    }

    //*************************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 {
            department,
            deptId,
            payYearSelect,
            payYear,
        } = params;

        this.radioGroup.params = params;

        if (department == '1' && !deptId) {
            return
        }

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

        this.getDeptSalaryList();
    }

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

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

            const { status } = cb;

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

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

    onOperatesClick = (record, index, operate) => {
        const {
            departmentid,
        } = record;

        this.mainTable.departmentid = departmentid;

        this.openDialog();
    }

    reRenderColumns = (c) => {
        c.map(item => {
            if (item.dataIndex == 'departmentid') {
                item.render = (t, r) => {
                    const {
                        departmentid,
                        departmentidspan
                    } = r;

                    return <a onClick={() => {this.mainTable.departmentid = departmentid;this.openDialog()}}>{departmentidspan ? departmentidspan : departmentid}</a>
                }
            }
        });
    }

    @action
    resetTable = (params) => {
        const {
            type
        } = params;

        this[type == 'main' ? 'mainTable' : 'subTable'].tableStore = new TableStore();
    }

    //*************************MainDialog*******************************
    @observable mainDialog = {
        visible: false,
        title: '',
    }

    get buttons() {
        const buttons = [];
        return buttons;
    }

    get style() {
        return {
            width: 800,
            height: 700
        }
    }

    get dropMenuDatas() {
        const datas = [];
        return datas;
    }

    @action
    openDialog = () => {
        this.mainDialog.visible = true;
        this.mainDialog.title = getLabel('391160', "详细");
    }

    @action
    closeDialog = () => {
        this.mainDialog.visible = false;
    }

    //*************************Tab*******************************
    @observable tab = {
        searchBaseValue: ''
    }

    @action
    setSearchBaseValue = (val) => {
        this.tab.searchBaseValue = val;
    }

    handleSearch = () => {
        this.getResourceSalaryList();
    }

    @action
    resetTab = () => {
        this.setSearchBaseValue('');
    }

    //*************************SubTable*******************************
    @observable subTable = {
        tableStore: new TableStore(),
        colNum: 0,
    }

    @action
    getResourceSalaryList = () => {
        const {
            params
        } = this.radioGroup;

        this.fetch({
            name: 'getResourceSalaryList',
            fetchParams: {
                name: this.tab.searchBaseValue,
                ...(Object.assign({}, params, {
                    deptId: this.mainTable.departmentid
                }))
            },
            logic: (cb) => {
                const {
                    sessionkey,
                    colNum
                } = cb;

                this.subTable.tableStore = new TableStore();
                this.subTable.tableStore.getDatas(sessionkey, 1);
                this.subTable.colNum = colNum;
            }
        });
    }

    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();
        });
    }

}