import {
    observable,
    action
} from 'mobx';
import * as mobx from 'mobx';
import * as Api from '../apis/titleset';
import {
    WeaForm
} from 'comsMobx';
import {
    WeaTableNew
} from 'comsMobx';
import {
    Modal,
    message,
} from 'antd'
import {
    i18n
} from '../public/i18n';

const toJS = mobx.toJS;
const {
    TableStore
} = WeaTableNew;

export class HrmTitleSet {
    //ne: 新建和编辑
    //ih:importHistory
    //ir:importResult
    //il:importLog
    //sh: search
    //it:import
    //mp: mainPage

    //common
    @observable btnMenu = [];
    @observable wtpLoading = true;
    @observable date = '';
    @observable operation = '';
    @observable isEdit = true;
    //WeaRightMenu 
    @observable rightMenu = [];
    //WeaTop
    @observable mpTitle = i18n.module.titleSetting();
    @observable topMenu = [];
    @observable mpLoading = true;
    @observable formType = '';
    @observable editId = '';
    //WeaTab
    @observable titleName = '';
    @observable isMpPanelShow = false;
    @observable conditionNum = 2;
    @observable mpShForm = new WeaForm();
    @observable mpShCondition = [];
    @observable mpShLoading = true;
    //WeaTable
    @observable mpTableStore = new TableStore();
    @observable selectedRowsKeys = '';
    @observable selectedRowKey = '';
    //NewAndEditDialog
    @observable neTitle = '';
    @observable neVisible = false;
    @observable neCondition = [];
    @observable neForm = new WeaForm();
    @observable neLoading = true;

    @action
    //common
    getRight() {
        this.setWtpStatus(true); //再次调用该接口时，loading状态值置为true
        Api.getHasRight(this.params).then((res) => {
            if (res.api_status) {
                res.btnMenu && this.setBtnMenu(res.btnMenu);
                this.btnDistribution();
                res.hasOwnProperty('isEdit') && this.setEditStatus(res.isEdit);
                this.setWtpStatus(false);
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })
    }
    btnDistribution() {
        let topMenu = [];
        let rightMenu = [];
        this.btnMenu.map(item => {
            (item.isTop == '1' || item.isBatch == '1') && topMenu.push(item);
            !item.isBatch && rightMenu.push(item);
        });
        this.setTopMenu(topMenu);
        this.setRightMenu(rightMenu);
    }
    //WeaTop
    add() {
        this.neForm.validateForm().then(f => {
            if (f.isValid) {
                Api.add(this.params).then((res) => {
                    if (res.api_status) {
                        if (res.message) {
                            if (res.sign == '1') {
                                message.success(res.message);
                            } else {
                                message.warning(res.message);
                            }
                        } else {
                            if (res.sign == '1') {
                                message.success('新建成功');
                            } else {
                                message.warning('新建失败');
                            }
                        }
                        this.setNeVisible(false);
                        this.refresh();
                    } else {
                        message.warning(data.message);
                    }
                }, error => {
                    message.warning(error.message);
                })
            } else {
                f.showErrors();
                this.setDate(new Date());
            }
        });
    }
    edit() {
        this.neForm.validateForm().then(f => {
            if (f.isValid) {
                Api.edit(this.params).then(res => {
                    if (res.api_status) {
                        if (res.message) {
                            if (res.sign == '1') {
                                message.success(res.message);
                            } else {
                                message.warning(res.message);
                            }
                        } else {
                            if (res.sign == '1') {
                                message.success('编辑成功');
                            } else {
                                message.warning('编辑失败');
                            }
                        }
                        this.setNeVisible(false);
                        this.refresh();
                    } else {
                        message.warning(data.message);
                    }
                }, error => {
                    message.warning(error.message);
                })
            } else {
                f.showErrors();
                this.setDate(new Date());
            }
        });
    }
    delete() {
        Api.deleteRows(this.params).then((res) => {
            if (res.api_status) {
                if (res.message) {
                    if (res.sign == '1') {
                        message.success(res.message);
                    } else {
                        message.warning(res.message);
                    }
                } else {
                    if (res.sign == '1') {
                        message.success('删除成功');
                    } else {
                        message.warning('删除失败');
                    }
                }
                this.refresh();
                this.selectedRowsKeys && this.selectedRowsKeysReset();
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })
    }
    //WeaTab
    getMpSearchCondition() {
        Api.getSearchCondition(this.params).then((res) => {
            if (res.api_status) {
                if (res.conditions) {
                    this.setMpShCondition(res.conditions);
                    this.mpShForm.initFormFields(res.conditions);
                    this.setConditionNum(res.conditions[0].items.length);
                }
                this.setMpShStatus(false);
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })
    }
    updateFields(val) {
        this.mpShForm.updateFields({
            name: {
                value: val
            }
        });
    }
    //WeaTable
    getTableInfo() {
        this.mpTableStore = new TableStore();
        Api.getSearchList(this.params).then(res => {
            if (res.api_status) {
                res.sessionkey && this.mpTableStore.getDatas(res.sessionkey, 1);
                this.setOperation('getRight'); //表格加载完成后，加载权限按钮
                this.getRight();
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })
    }
    //NewAndEdit
    getNeForm() {
        this.setNeStatus(true);
        Api.getNeForm(this.params).then(res => {
            if (res.api_status) {
                if (res.condition) {
                    this.setNeCondition(res.condition);
                    this.neForm.initFormFields(res.condition);
                }
                this.setNeStatus(false);
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })
    }

    paramsDistribution() {
        let operation = this.operation;
        if (operation == 'getTableInfo') {
            if (!this.mpShForm.isFormInit) {
                this.setParams({
                    name: this.titleName
                });
            } else {
                this.setParams({ ...this.mpShForm.getFormParams()
                });
            }
        } else if (operation == 'editFrom') {
            this.setParams({
                jobcallid: this.editId
            });
        } else if (operation == 'save_new') {
            this.setParams({ ...this.neForm.getFormParams()
            });
        } else if (operation == 'save_edit') {
            this.setParams({ ...this.neForm.getFormParams(),
                id: this.editId
            });
        } else if (operation == 'delete') {
            this.setParams({
                ids: this.selectedRowKey
            });
        } else if (operation == 'batchDelete') {
            this.setParams({
                ids: this.selectedRowsKeys
            });
        } else {
            this.setParams({});
        }
    }
    setParams(params) {
        this.params = params;
    }
    refresh() {
        this.setOperation('getTableInfo');
        this.getTableInfo();
    }
    mobxDatasReset() {
        this.titleNameReset();
        this.mpShForm = new WeaForm();
        this.mpShCondition = [];
        this.isMpPanelShow && this.setMpPanelStatus(false);
    }
    //common
    setOperation(operation) {
        this.operation = operation;
        this.paramsDistribution();
    }
    setBtnMenu(btnMenu) {
        this.btnMenu = btnMenu;
    }
    setWtpStatus(bool) {
        this.wtpLoading = bool;
    }
    setDate(date) {
        this.date = date;
    }
    setEditStatus(bool) {
        this.isEdit = bool;
    }
    //WeaRightMenu
    setRightMenu(rightMenu) {
        this.rightMenu = rightMenu;
    }
    rightMenuReset() {
        this.rightMenu = [];
    }
    //WeaTop
    setTopMenu(topMenu) {
        this.topMenu = topMenu;
    }
    topMenuReset() {
        this.topMenu = [];
    }
    newSet(formType, operation) {
        if (formType) this.setFormType(formType);
        if (operation) this.setOperation(operation);
    }
    editSet(id, formType, operation) {
        if (id) this.setEditId(id);
        if (formType) this.setFormType(formType);
        if (operation) this.setOperation(operation);
    }
    deleteSet(id, operation) {
        if (id) this.setSelectedRowKey(id);
        if (operation) this.setOperation(operation);
    }
    setFormType(formType) {
        this.formType = formType;
    }
    setEditId(id) {
        this.editId = id;
    }
    setSelectedRowKey(key) {
        this.selectedRowKey = key;
    }
    //WeaTab
    setSearchName(name) {
        this.titleName = name;
    }
    setConditionNum(num) {
        this.conditionNum = num;
    }
    titleNameReset() {
        this.titleName = '';
    }
    setMpShCondition(condition) {
        this.mpShCondition = condition;
    }
    setMpPanelStatus(bool) {
        this.isMpPanelShow = bool;
    }
    mpSearchReset() {
        // this.mpShForm.reset();
    }
    setMpShStatus(bool) {
        this.mpShLoading = bool;
    }
    //WeaTable
    setSelectedRowsKeys(keys) {
        this.selectedRowsKeys = keys
    }
    selectedRowsKeysReset() {
        this.selectedRowsKeys = '';
    }
    //NewAndEditDialog
    setNeTitle(title) {
        this.neTitle = title;
    }
    setNeVisible(bool) {
        this.neVisible = bool;
        if (!bool) this.neReset();
    }
    setNeCondition(condition) {
        this.neCondition = condition;
    }
    setNeStatus(bool) {
        this.neLoading = bool;
    }
    neReset() {
        this.neForm = new WeaForm();
    }

}