import {
    observable,
    action
} from 'mobx';
import * as mobx from 'mobx';
import * as Api from '../apis/group'; // 引入API接口文件
import {
    WeaForm
} from 'comsMobx';
import {
    Modal,
    message,
} from 'antd'
import {
    i18n
} from '../public/i18n';

const toJS = mobx.toJS;

export class GroupStore {
    @observable topMenu = []
    @observable rightMenu = [];
    @observable condition = [];
    @observable groupCondition = [];
    @observable isEdit = true;
    @observable form = new WeaForm();
    @observable form1 = new WeaForm();
    @observable nEdialogTitle = '';
    @observable visible = false;
    @observable dialogLoading = true;
    @observable selectedKey = 1;
    @observable topTab = [];
    @observable date = '';
    @observable hasRight = '';
    @observable loading = true;

    @action
    getGroupForm() {
        let params = {
            viewattr: 1,
            id: 1
        }
        this.setLoading(true);
        this.form1 = new WeaForm();
        Api.getGroupForm(params).then(res => {
            if (res.code === 200) {
                res.data.condition && this.setGroupCondition(res.data.condition);
                res.data.condition && this.form1.initFormFields(res.data.condition);
                this.setLoading(false);
            } else {
                message.warning(res.msg);
            }
        }, error => {
            message.warning(error.msg);
        })

        this.getTabInfo();
    }

    getTabInfo() {
        Api.getTabInfo().then(res => {
            if (res.code === 200) {
                res.data.topTabs && this.setTopTab(res.data.topTabs);
            } else {
                message.warning(res.msg);
            }
        }, error => {
            message.warning(error.msg);
        })
    }


    @action
    getHasRight() {
        Api.getHasRight().then(res => {
            if (res.code === 200) {
                this.setHasRight(res.data.hasRight);
                res.data.rightMenu && this.setRightMenu(res.data.rightMenu);
                res.data.topMenu && this.setTopMenu(res.data.topMenu);
            } else {
                message.warning(res.msg);
            }
        }, error => {
            message.warning(error.msg);
        })
    }


    edit() {
        let params = { ...this.form.getFormParams(), id: 1 };
        this.form.validateForm().then(f => {
            if (f.isValid) {
                Api.edit(params).then(response => {
                    return response.json()
                }).then(data => {
                    if (data.code === 200) {
                        this.getGroupForm();
                        message.success(data.msg);
                        this.setVisible(false);
                    } else {
                        message.warning(data.msg);
                    }
                }).catch(error => {
                    message.warning(error.msg);
                })
            } else {
                f.showErrors();
                this.setDate(new Date());
            }
        });
    }

    getForm() {
        let params = {
            viewattr: 2,
            id:1
        }
        Api.getGroupForm(params).then(res => {
            if (res.code === 200) {
                this.setDialogLoadingStatus(false);
                res.data.condition && this.setCondition(res.data.condition);
                res.data.condition && this.form.initFormFields(res.data.condition);
            } else {
                message.warning(res.msg);
            }
        }, error => {
            message.warning(error.msg);
        })
    }

    changeData(key) {
        this.setSelectedKey(key);
    }


    setVisible(bool) {
        this.visible = bool;
    }

    setGroupCondition(groupCondition) {
        this.groupCondition = groupCondition
    }

    setCondition(condition) {
        this.condition = condition;
    }

    setTopMenu(topMenu) {
        this.topMenu = topMenu;
    }

    setRightMenu(rightMenu) {
        this.rightMenu = rightMenu;
    }

    setTopTab(topTab) {
        this.topTab = topTab;
    }

    setNeDialogTitle(title) {
        this.nEdialogTitle = title;
    }

    setDialogLoadingStatus(bool) {
        this.dialogLoading = bool;
    }

    setHasRight(bool) {
        this.hasRight = bool;
    }

    setDate(date) {
        this.date = date;
    }

    setLoading(bool) {
        this.loading = bool;
    }

}