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

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

export class HrmPostSet {
    //ne: 新建和编辑
    //sh: search
    //mp: mainPage

    //common
    @observable type = 'all';
    @observable btnMenu = [];
    @observable wtpLoading = true;
    @observable date = '';
    //WeaRightMenu 
    @observable rightMenu = [];
    //WeaTop
    @observable topMenu = [];
    @observable mpLoading = true;
    @observable formType = '';
    @observable newType = '';
    //WeaLeftRightLayout-LeftTree
    @observable otReload = false;
    @observable treeRefresh = false;
    //TabInfo
    @observable jobGroupName = '';
    @observable jobName = '';
    @observable postName = ''
    @observable mpShCondition = [];
    @observable mpShForm = new WeaForm();
    @observable jobGroupId = '';
    @observable jobActivityId = '';
    @observable isMpPanelShow = false;
    @observable mpShLoading = true;
    @observable selectedKey = '0';
    //BriefInfo
    @observable briefInfo = [];
    @observable curJobType = '';
    @observable curJob = '';
    @observable curPost = '';
    //AreaTable
    @observable mpTableStore = new TableStore();
    @observable selectedRowsKeys = '';
    //NewAndEditDialog
    @observable jgTitle = '';
    @observable jgVisible = false;
    @observable jgCondition = [];
    @observable neJgForm = new WeaForm();
    @observable jgLoading = true;
    @observable jbTitle = '';
    @observable jbVisible = false;
    @observable jbCondition = [];
    @observable neJbForm = new WeaForm();
    @observable jbLoading = true;
    @observable ptTitle = '';
    @observable ptVisible = false;
    @observable ptCondition = [];
    @observable nePtForm = new WeaForm();
    @observable ptLoading = true;
    @observable curAddType = '';

    @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();
                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() {
        let curAddType = this.curAddType;
        let form;
        let svFuncName;
        if (curAddType == 'jobGroup') {
            form = this.neJgForm;
            svFuncName = 'setJgVisible';
        } else if (curAddType == 'job') {
            form = this.neJbForm;
            svFuncName = 'setJbVisible';
        } else if (curAddType == 'post') {
            form = this.nePtForm;
            svFuncName = 'setPtVisible';
        }
        form.validateForm().then(f => {
            if (f.isValid) {
                Api[this.funcName](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[svFuncName](false);
                        this.refresh();
                    } else {
                        message.warning(data.message);
                    }
                }, error => {
                    message.warning(error.message);
                })
            } else {
                f.showErrors();
                this.setDate(new Date());
            }
        });
    }
    edit() {
        let funcName = this.funcName;
        let form;
        let svFuncName;
        if (funcName == 'editJobGroup') {
            form = this.neJgForm;
            svFuncName = 'setJgVisible';
        } else if (funcName == 'editJob') {
            form = this.neJbForm;
            svFuncName = 'setJbVisible';
        } else if (funcName == 'editPost') {
            form = this.nePtForm;
            svFuncName = 'setPtVisible';
        }
        form.validateForm().then(f => {
            if (f.isValid) {
                Api[this.funcName](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[svFuncName](false);
                        this.refresh();
                    } else {
                        message.warning(data.message);
                    }
                }, error => {
                    message.warning(error.message);
                })
            } else {
                f.showErrors();
                this.setDate(new Date());
            }
        });
    }
    delete() {
        Api[this.funcName](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);
        })
    }
    //BriefInfo
    getBriefInfo() {
        Api.getJobDetail(this.params).then((res) => {
            if (res.api_status) {
                if (res.datas) {
                    this.setBriefInfo(res.datas);
                    this.setBreadcrumb(res.datas);
                }
                if (res.pid) {
                    type == 'JobActivity' && this.setJobGroupId(res.pid);
                    type == 'JobTitle' && this.setJobActivityId(res.pid);
                }
                this.initSet('getRight'); //基本信息加载完成后，加载权限按钮
                this.getRight();
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })
    }
    setBreadcrumb(datas) {
        datas.map(data => {
            (data.label == i18n.label.categoryAbbreviation() || data.label == i18n.label.jobCategory()) && this.setCurJobType(data.value);
            (data.label == i18n.label.jobAbbreviation() || data.label == i18n.label.duties()) && this.setCurJob(data.value);
            data.label == i18n.label.postAbbreviation() && this.setCurPost(data.value);
        });
    }
    //TabInfo
    getMpSearchCondition() {
        Api.getSearchCondition(this.params).then((res) => {
            if (res.api_status) {
                if (res.condition) {
                    this.setMpShCondition(res.condition);
                    this.mpShForm.initFormFields(res.condition);
                }
                this.setMpShStatus(false);
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })
    }
    updateFields(val) {
        let type = this.type;
        type == 'all' && this.mpShForm.updateFields({
            jobgroupmark: {
                value: val
            }
        });
        type == 'JobGroup' && this.mpShForm.updateFields({
            jobactivitymark: {
                value: val
            }
        });
        type == 'JobActivity' && this.mpShForm.updateFields({
            jobtitlemark: {
                value: val
            }
        });
    }
    //AreaTable
    getTableInfo() {
        this.mpTableStore = new TableStore();
        Api[this.funcName](this.params).then((res) => {
            if (res.api_status) {
                res.sessionkey && this.mpTableStore.getDatas(res.sessionkey, 1);
                this.initSet('getRight'); //表格加载完成后，加载权限按钮
                this.getRight();
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })
    }
    //NewAndEdit
    getNeForm() {
        Api[this.funcName](this.params).then(res => {
            if (res.api_status) {
                if (res.condition) {
                    let newType = this.newType;
                    let formType = this.formType;
                    let funcName = this.funcName;
                    let form;
                    let scFuncName;
                    let stFuncName;
                    let stsFuncName;
                    if ((formType == 'new' && newType == 'jobGroup') || (formType == 'edit' && funcName == 'getJobGroupForm')) {
                        form = this.neJgForm;
                        scFuncName = 'setJgCondition';
                        stFuncName = 'setJgTitle';
                        stsFuncName = 'setJgStatus';
                    } else if ((formType == 'new' && newType == 'job') || (formType == 'edit' && funcName == 'getJobForm')) {
                        form = this.neJbForm;
                        scFuncName = 'setJbCondition';
                        stFuncName = 'setJbTitle';
                        stsFuncName = 'setJbStatus';
                    } else if ((formType == 'new' && newType == 'post') || (formType == 'edit' && funcName == 'getPostForm')) {
                        form = this.nePtForm;
                        scFuncName = 'setPtCondition';
                        stFuncName = 'setPtTitle';
                        stsFuncName = 'setPtStatus';
                    }
                    this[scFuncName](res.condition);
                    form.initFormFields(res.condition);
                    this[stsFuncName](false);
                }
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })
    }

    paramsDistribution() {
        let operation = this.operation;
        let type = this.type;
        let selectedKey = this.selectedKey;
        let ids = this.ids;
        let newType = this.newType;
        let formType = this.formType;
        let curAddType = this.curAddType;

        if (operation == 'getRight') {
            type == 'all' && this.setParams({
                type: type
            });
            type == 'JobGroup' && this.setParams({
                type: type,
                tab: selectedKey,
                id: this.jobGroupId
            });
            type == 'JobActivity' && this.setParams({
                type: type,
                tab: selectedKey,
                id: this.jobActivityId
            });
            type == 'JobTitle' && this.setParams({
                type: type,
                tab: selectedKey,
                id: this.jobTitleId
            });
        }
        if (operation == 'getNewForm') {
            newType == 'jobGroup' && this.setParams({});
            newType == 'job' && this.setParams({
                jobgroupid: this.jobGroupId
            });
            newType == 'post' && this.setParams({
                jobactivityid: this.jobActivityId
            });
        }
        if (operation == 'getEditForm') {
            if (type == 'all') {
                this.setParams({
                    jobgroupid: this.jobGroupId
                });
            }
            if (type == 'JobGroup') {
                if (selectedKey == '0') {
                    this.setParams({
                        jobgroupid: this.jobGroupId
                    });
                } else {
                    this.setParams({
                        jobactivityid: this.jobActivityId
                    });
                }
            }
            if (type == 'JobActivity') {
                if (selectedKey == '0') {
                    this.setParams({
                        jobactivityid: this.jobActivityId
                    });
                } else {
                    this.setParams({
                        jobtitleid: this.jobTitleId
                    });
                }
            }
            if (type == 'JobTitle') {
                this.setParams({
                    jobtitleid: this.jobTitleId
                });
            }
        }
        if (operation == 'getTableInfo') {
            if (type == 'all') {
                if (!this.mpShForm.isFormInit) {
                    this.setParams({
                        jobgroupmark: this.jobGroupName
                    });
                } else {
                    this.setParams({ ...this.mpShForm.getFormParams()
                    });
                }
            }
            if (type == 'JobGroup') {
                if (!this.mpShForm.isFormInit) {
                    this.setParams({
                        jobactivitymark: this.jobName,
                        jobgroupid: this.jobGroupId
                    });
                } else {
                    this.setParams({ ...this.mpShForm.getFormParams(),
                        jobgroupid: this.jobGroupId
                    });
                }
            }
            if (type == 'JobActivity') {
                if (!this.mpShForm.isFormInit) {
                    this.setParams({
                        jobtitlemark: this.postName,
                        jobactivityid: this.jobActivityId
                    });
                } else {
                    this.setParams({ ...this.mpShForm.getFormParams(),
                        jobactivityid: this.jobActivityId
                    });
                }
            }
        }
        if (operation == 'save_new') {
            curAddType == 'jobGroup' && this.setParams({ ...this.neJgForm.getFormParams()
            });
            curAddType == 'job' && this.setParams({ ...this.neJbForm.getFormParams()
            });
            curAddType == 'post' && this.setParams({ ...this.nePtForm.getFormParams()
            });
        }
        if (operation == 'save_edit') {
            if (type == 'all') {
                this.setParams({
                    id: this.jobGroupId,
                    ...this.neJgForm.getFormParams()
                })
            }
            if (type == 'JobGroup') {
                if (selectedKey == '0') {
                    this.setParams({
                        id: this.jobGroupId,
                        ...this.neJgForm.getFormParams()
                    })
                } else {
                    this.setParams({
                        id: this.jobActivityId,
                        ...this.neJbForm.getFormParams()
                    })
                }
            }
            if (type == 'JobActivity') {
                if (selectedKey == '0') {
                    this.setParams({
                        id: this.jobActivityId,
                        ...this.neJbForm.getFormParams()
                    })
                } else {
                    this.setParams({
                        id: this.jobTitleId,
                        ...this.nePtForm.getFormParams()
                    })
                }
            }
            if (type == 'JobTitle') {
                this.setParams({
                    id: this.jobTitleId,
                    ...this.nePtForm.getFormParams()
                })
            }
        }
        if (operation == 'delete') {
            if (type == 'all') {
                this.setParams({
                    ids: this.jobGroupId
                })
            }
            if (type == 'JobGroup') {
                if (selectedKey == '0') {
                    this.setParams({
                        ids: this.jobGroupId
                    })
                } else {
                    this.setParams({
                        ids: this.jobActivityId
                    })
                }
            }
            if (type == 'JobActivity') {
                if (selectedKey == '0') {
                    this.setParams({
                        ids: this.jobActivityId
                    })
                } else {
                    this.setParams({
                        ids: this.jobTitleId
                    })
                }
            }
            if (type == 'JobTitle') {
                this.setParams({
                    ids: this.jobTitleId
                })
            }
        }
        if (operation == 'batch_delete') {
            this.setParams({
                ids: this.selectedRowsKeys
            });
        }
        if (operation == 'getBriefInfo') {
            type == 'JobGroup' && this.setParams({
                jobGroupid: this.jobGroupId
            });
            type == 'JobActivity' && this.setParams({
                jobActivityid: this.jobActivityId
            });
            type == 'JobTitle' && this.setParams({
                jobTitleid: this.jobTitleId
            });
        }
        if (operation == 'mpSearch') {
            type == 'all' && this.setParams({
                type: 'JobGroup'
            });
            type == 'JobGroup' && this.setParams({
                type: 'JobActivity'
            });
            type == 'JobActivity' && this.setParams({
                type: 'JobTitle'
            });
        }
        if (operation == 'import' || operation == 'import_info_query' || operation == 'import_log_sh') {
            this.setParams({});
        }
        if (operation == 'import_submit') {
            this.setParams({
                importtype: 'jobtitle',
                excelfile: this.excelFileNo
            });
        }
        if (operation == 'import_result') {
            this.setParams({
                pid: this.pid
            });
        }
        if (operation == 'import_log') {
            this.setParams({ ...this.ilShForm.getFormParams()
            })
        }
    }
    isEmptyObject(obj) {
        for (let key in obj) {
            if (!obj[key]) return true;
            return false;
        }
        return true;
    }
    setParams(params) {
        this.params = params;
    }

    refresh() {
        let type = this.type;
        let selectedKey = this.selectedKey;
        let formType = this.formType;
        this.setTreeRefresh(); //刷新树
        this.setOperation('getTableInfo');
        if (type == 'all') {
            this.setFuncName('getJobGroupList');
            this.getTableInfo(); //刷新表格
        } else if (type == 'JobGroup' && selectedKey == '1') {
            this.setFuncName('getJobActivityList');
            this.getTableInfo();
        } else if (type == 'JobActivity' && selectedKey == '1') {
            this.setFuncName('getJobTitleList');
            this.getTableInfo();
        } else if (type !== 'all' && selectedKey == '0' && formType == 'edit') {
            this.setOperation('getBriefInfo');
            this.getBriefInfo();
        } else if (type !== 'all' && selectedKey == '0' && formType == 'new') {
            this.setOperation('getRight');
            this.getRight();
        }
    }

    //common
    setOperation(operation) {
        this.operation = operation;
        this.paramsDistribution();
    }
    setFuncName(funcName) {
        this.funcName = funcName;
    }
    setBtnMenu(btnMenu) {
        this.btnMenu = btnMenu;
    }
    setType(type) {
        this.type = type;
    }
    setJobGroupId(id) {
        this.jobGroupId = id;
    }
    setJobActivityId(id) {
        this.jobActivityId = id;
    }
    setJobTitleId(id) {
        this.jobTitleId = id;
    }
    setWtpStatus(bool) {
        this.wtpLoading = bool;
    }
    setDate(date) {
        this.date = date;
    }
    //init
    initSet(operation, funcName) {
        if (operation) this.setOperation(operation);
        if (funcName) this.setFuncName(funcName);
    }
    //WeaRightMenu
    setRightMenu(rightMenu) {
        this.rightMenu = rightMenu;
    }
    rightMenuReset() {
        this.rightMenu = [];
    }
    //WeaTop
    setTopMenu(topMenu) {
        this.topMenu = topMenu;
    }
    topMenuReset() {
        this.topMenu = [];
    }
    newSet(formType, newType, operation, funcName) {
        if (formType) this.setFormType(formType);
        if (newType) this.setNewType(newType);
        if (operation) this.setOperation(operation);
        if (funcName) this.setFuncName(funcName);
    }
    editSet(formType, operation, funcName) {
        if (formType) this.setFormType(formType);
        if (operation) this.setOperation(operation);
        if (funcName) this.setFuncName(funcName);
    }
    deleteSet(operation) {
        if (operation) this.setOperation(operation);
    }
    saveSet(curAddType, operation, funcName) {
        if (curAddType) this.setCurAddType(curAddType);
        if (operation) this.setOperation(operation);
        if (funcName) this.setFuncName(funcName);
    }
    setFormType(formType) {
        this.formType = formType;
    }
    setNewType(newType) {
        this.newType = newType;
    }
    //WeaLeftRightLayout-LeftTree
    orgTreeReload() {
        this.otReload = !this.otReload
    }
    setTreeRefresh() {
        this.treeRefresh = !this.treeRefresh;
    }
    //TabInfo
    setJobGroupName(name) {
        this.jobGroupName = name;
    }
    setJobName(name) {
        this.jobName = name;
    }
    setPostName(name) {
        this.postName = name;
    }
    setMpShCondition(condition) {
        this.mpShCondition = condition;
    }
    setMpPanelStatus(bool) {
        this.isMpPanelShow = bool;
    }
    mpSearchReset() {
        let type = this.type;
        this.mpShForm = new WeaForm();
        this.mpShCondition = [];
        this.setMpShStatus(true);
        if (type == 'all') {
            this.jobGroupName = '';
        } else if (type == 'JobGroup') {
            this.jobName = '';
        } else {
            this.postName = '';
        }
    }
    mpShBaseValueReset() {
        let type = this.type;
        if (type == 'all') {
            this.jobGroupName = '';
        } else if (type == 'JobGroup') {
            this.jobName = '';
        } else if (type == 'JobActivity') {
            this.postName = '';
        }
    }
    setSelectedKey(selectedKey) {
        this.selectedKey = selectedKey;
    }
    tabSet(operation, funcName) {
        if (operation) this.setOperation(operation);
        if (funcName) this.setFuncName(funcName);
    }
    setMpShStatus(bool) {
        this.mpShLoading = bool;
    }
    selectedKeyReset() {
        this.selectedKey = '0';
    }
    //BriefInfo
    setBriefInfo(datas) {
        this.briefInfo = datas;
    }
    setCurJobType(curJobType) {
        this.curJobType = curJobType
    }
    setCurJob(curJob) {
        this.curJob = curJob;
    }
    setCurPost(curPost) {
        this.curPost = curPost;
    }
    //AreaTable
    setSelectedRowsKeys(keys) {
        this.selectedRowsKeys = keys
    }
    selectedRowsKeysReset() {
        this.selectedRowsKeys = '';
    }
    //NewAndEditDialog
    setJgTitle(title) {
        this.jgTitle = title;
    }
    setJgVisible(bool) {
        this.jgVisible = bool;
        if (!bool) this.jgReset();
    }
    setJgCondition(condition) {
        this.jgCondition = condition;
    }
    setJgStatus(bool) {
        this.jgLoading = bool;
    }
    jgReset() {
        this.neJgForm = new WeaForm();
    }
    setJbTitle(title) {
        this.jbTitle = title;
    }
    setJbVisible(bool) {
        this.jbVisible = bool;
        if (!bool) this.jbReset();
    }
    setJbCondition(condition) {
        this.jbCondition = condition;
    }
    setJbStatus(bool) {
        this.jbLoading = bool;
    }
    jbReset() {
        this.neJbForm = new WeaForm();
    }
    setPtTitle(title) {
        this.ptTitle = title;
    }
    setPtVisible(bool) {
        this.ptVisible = bool;
        if (!bool) this.ptReset();
    }
    setPtCondition(condition) {
        this.ptCondition = condition;
    }
    setPtStatus(bool) {
        this.ptLoading = bool;
    }
    ptReset() {
        this.nePtForm = new WeaForm();
    }
    setCurAddType(type) {
        this.curAddType = type;
    }

}