import * as Api from '../apis/recruitLibrary'; //todo
import {
    observable,
    action,
    computed,
    autorun,
} from 'mobx';
import {
    WeaForm,
    WeaTableNew,
    WeaSwitch,
} from 'comsMobx';
import {
    WeaLocaleProvider,
    WeaFormItem,
    WeaSearchGroup,
    WeaMoreButton,
    WeaButtonIcon,
} from 'ecCom';
import {
    message,
    Button,
    Row,
    Modal,
} from 'antd';
import {
    checkWeakPass
} from "../util/PASSUtil.js";
import {
    RSAEcrypt
} from "../util/RSAUtil";
const {
    TableStore
} = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
const confirm = Modal.confirm;

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

    @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;
    }

    @action
    refresh = () => {
        this.clickTab('1');
    }

    @action
    setProps = (bool, id, needTab) => {
        this.main.isShowInDialog = bool;
        this.main.inviteInfoId = id;
        this.mainTab.needTab = needTab;
    }

    //*************************Top*******************************
    @observable top = {
        title: getLabel(16251, '应聘库'), //todo
        menu: [],
    }

    @action
    getMenus = () => {
        Api.getButtonsMenu().then(cb => {
            const {
                api_status,
                btnMenu
            } = cb;

            if (api_status) {
                this.top.menu = btnMenu;
            } else {
                message.error();
            }
        }).catch(error => {
            message.error();
        });
    }

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

        const {
            tableStore
        } = this.mainTable;

        let buttons = [],
            dropMenuDatas = [];

        top.map((item, index) => {
            if (item.menuFun !== 'sendMail') { //todo
                buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@zo2ssa@${index}`} type='primary' disabled={tableStore.loading} onClick={() => this.handleTopBtnsClick(item)}>{item.menuName}</Button>);
            } else {
                buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@9bmyd7@${index}`} type='primary' disabled={tableStore.selectedRowKeys.length == 0} 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 (item.menuFun == 'collection' || item.menuFun == 'help' || item.menuFun == 'pageAddress') {
                obj.disabled = true;
            }
            if (item.menuFun == 'sendMail') { //todo
                obj.disabled = tableStore.selectedRowKeys.length == 0;
            }
            dropMenuDatas.push(obj);
        })

        return {
            buttons,
            dropMenuDatas
        };
    }

    @action
    handleTopBtnsClick = (item) => {
        const {
            isPanelShow
        } = this.mainTab;

        isPanelShow && this.changePanelStatus(false);
        this[item.menuFun] && this[item.menuFun](item.menuFun)
    }

    log = () => {
        const {
            curKey
        } = this.subTab, {
            id
        } = this.mainTable;

        if (curKey === '1') {
            //个人信息
            window.setLogViewProps({
                logSmallType: '3054',
                targetId: id,
            });
        } else if (curKey === '2') {
            //工作信息
            window.setLogViewProps({
                logSmallType: '3055',
                targetId: id,
            });
        } else {
            //基本信息
            window.setLogViewProps({
                logSmallType: '3053',
                targetId: id,
            });
        }
    }

    new = (funcName) => {
        this.openDialog(funcName);
    }

    sendMail = (funcName) => {
        this.openDialog(funcName);
    }

    custom = () => {
        const {
            tableStore
        } = this.mainTable;

        tableStore.setColSetVisible(true);
        tableStore.tableColSet(true);
    }

    //*************************MainTab*******************************
    @observable mainTab = {
        curKey: '1',
        isPanelShow: false,
        conditionCount: 10,
        form: new WeaForm(),
        conditions: [],
        needTab: true,
    }

    get mainTabDatas() {
        const datas = [{
            key: '0',
            title: getLabel(82857, '全部'),
        }, {
            key: '1',
            title: getLabel(16349, '待处理'),
        }, {
            key: '2',
            title: getLabel(15689, '备用'),
        }]

        return datas;
    }

    get buttonsAd() {
        const {
            form,
            replaceDatas,
        } = this.mainTab;

        const buttonsAd = [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@3sunyx`} type="primary" onClick={() => this.handleSearch()}>{getLabel(82529, '搜索')}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@pqx04n`} onClick={() => {form.reset();replaceDatas && form.updateFields({
                inviteInfoId:{
                    value:replaceDatas.id,
                    valueSpan:replaceDatas.name,
                    valueObj:[replaceDatas]
                }
            })}}>{getLabel(27088, '重置')}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@z2eudc`} onClick={() => this.changePanelStatus(false)}>{getLabel(32694, '取消')}</Button>),
        ];

        return buttonsAd;
    }

    get searchsAd() {
        const {
            form,
            conditions
        } = this.mainTab, {
            isFormInit
        } = form;
        let outterArray = [];

        isFormInit && conditions.map((c, i) => {
            let innerArray = [];
            c.items.map((field, index) => {
                const key = field.domkey[0];
                innerArray.push({
                    com: (
                        <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@ymh45h@${index}`}
                            label={`${field.label}`}
                            labelCol={{span: (index%2==0)?7:6}}
                            wrapperCol={{span: (index%2==0)?17:18}}>
                                {<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@on7jl1@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} />}
                        </WeaFormItem>
                    ),
                    colSpan: 1
                })
            })
            outterArray.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@vt36jy@${i}`} needTigger={true} title={c.title} showGroup={c.defaultshow} items={innerArray} col={2} />)
        })

        return <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@rssut1`}
            onKeyDown={(e) =>( e.keyCode == 13 && e.target.tagName === "INPUT")  && this.handleSearch()}
        >{outterArray}</Row>
    }

    get iconButtons_mainTab() {
        const {
            tableStore
        } = this.mainTable;

        return [
            <WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@wa2hdp`} buttonType='add' type="primary" onClick={() => this.openDialog('new')}/>,
            <div className='wea-button-icon'>
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@phoz4w`} type="primary" disabled={tableStore.selectedRowKeys.length == 0} onClick={() => this.sendMail('sendMail')}>
                     <span title={getLabel('2051',"发送邮件")} className='icon-coms-email' style={{fontSize:25}}></span>
                </Button>
            </div>
        ]
    }

    @action
    clickTab = (key) => {
        const {
            visible
        } = this.mainDialog;

        if (!visible) {
            this.mainTab.curKey = key;
            ['resetSearch', 'getTableInfo'].map(f => {
                this[f]();
            });
        } else {
            this.subTab.curKey = key;
            this.tableEdit.selectedRowsKey = '';

            const {
                index
            } = this.mainTable;

            if (index == '1' && key !== '0') {
                this.getFormInfoWithTable();
            }
            if (['3', '4', '5', '8'].includes(index) && key != '0') {
                this.getTableInfo();
            }
        }
    }

    @action
    changePanelStatus = (bool) => {
        this.mainTab.isPanelShow = bool;
    }

    @action
    handleSearch = () => {
        const {
            isPanelShow
        } = this.mainTab;

        isPanelShow && this.changePanelStatus(false);
        this.getTableInfo();
    }

    @action
    getSearchCondition = () => {
        const {
            inviteInfoId
        } = this.main;

        Api.getSearchCondition(inviteInfoId && {
            inviteInfoId
        }).then(rs => {
            const {
                api_status,
                conditions
            } = rs;

            if (api_status) {
                this.handleMainTabCondition(conditions);
                this.mainTab.conditions = conditions;
                this.mainTab.form.initFormFields(conditions);
            } else {
                message.error();
            }
        }).catch(error => {
            message.error();
        });
    }

    handleMainTabCondition = (conditions) => {
        conditions.map(c => {
            c.items.map(item => {
                const key = item.domkey[0];

                if (key == 'inviteInfoId') {
                    const {
                        browserConditionParam
                    } = item, {
                        replaceDatas
                    } = browserConditionParam;

                    this.mainTab.replaceDatas = replaceDatas[0] || '';
                }
            });
        });
    }

    @action
    resetSearch = () => {
        this.mainTab.isPanelShow = false;
        this.mainTab.form.reset();
    }

    @action
    unmountTab = () => {
        this.mainTab.isPanelShow = false;
        this.mainTab.form = new WeaForm();
        this.mainTab.conditions = [];
    }


    //*************************MainTable*******************************
    @observable mainTable = {
        tableStore: new TableStore(),
        index: '',
        isEdit: true,
    }

    @action
    getTableInfo = (arg) => {
        const {
            visible,
        } = this.mainDialog, {
            curKey
        } = this.mainTab, {
            isShowInDialog
        } = this.main, {
            index,
        } = this.mainTable;

        let params;
        let func;
        if (!visible) {
            params = {
                status: isShowInDialog ? '0' : curKey,
                inviteInfoId: this.main.inviteInfoId,
            };
            if (arg) {
                Object.assign(params, {
                    ...arg
                });
            } else {
                Object.assign(params, {
                    ...this.mainTab.form.getFormParams(),
                });
            }
            func = 'getSearchList';
        } else {
            params = {
                applyInfoId: this.mainTable.id
            };
            const sKey = this.subTab.curKey;
            func = (index == '8') ? 'getHrmShareList' : (sKey == '1') ? 'getInterviewAssessList' : 'getInterviewTestList';
        }
        Api[func](params).then(cb => {
            const {
                api_status,
                sessionkey,
                isEdit = true,
            } = cb;

            if (api_status) {
                if (!visible) {
                    this.mainTable.tableStore = new TableStore();
                    this.mainTable.tableStore.getDatas(sessionkey, 1);
                    this.mainTable.isEdit = isEdit;
                } else {
                    this.subTable.tableStore = new TableStore();
                    this.subTable.tableStore.getDatas(sessionkey, 1);
                }
            } else {
                message.error();
            }
        }).catch(error => {
            message.error();
        });
    }

    onOperatesClick = (record, rowIndex, operate) => {
        const {
            visible
        } = this.mainDialog;

        const {
            index
        } = operate, {
            randomFieldId,
        } = record;

        if (!visible) {
            const {
                lastname,
                jobtitlename,
                sex,
            } = record;

            if (index == '0') {
                window.open(`/spa/hrm/engine.html#/hrmengine/print?id=${randomFieldId}`);
            } else if (index == '10') {
                window.setLogViewProps({
                    logSmallType: '3053',
                    targetId: randomFieldId,
                });
            } else {
                this.openDialog(index, randomFieldId);
            }
        } else {
            if (index == '0') {
                this.subTable.id = randomFieldId;
                this.showConfirm('del');
            }
        }

    }

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

    get buttons() {
        const {
            index
        } = this.mainTable, {
            curKey
        } = this.subTab, {
            loading,
            hasSaveBtn,
            errorMsg,
        } = this.formInfo;

        let buttons = [];
        if (index == '9' || index == 'sendMail') {
            !errorMsg && buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@wzc0wl`} type="primary" onClick={this.sendEmail} disabled={loading}>{getLabel('1226',"邮件发送")}</Button>);
        } else {
            let b;
            if (index == '1') {
                b = true;
            } else {
                b = curKey == '0';
            }
            hasSaveBtn && b && buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@0x1vds`} type="primary" disabled={loading} onClick={(index=='1'&&curKey!=='0')?this.saveExtendInfo:this.save}>{getLabel('30986',"保存")}</Button>);
        }

        if (index == 'new') {
            buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@qly5iv`} type="primary" disabled={loading} onClick={() => this.save('saveAndSet')}>{getLabel(32159, '保存并进入详细设置')}</Button>)
        }

        if (index == '8') {
            buttons = [];
        }

        buttons.push(<WeaMoreButton ecId={`${this && this.props && this.props.ecId || ''}_WeaMoreButton@81cs9d`} datas={this.dropMenuDatas} />);

        return buttons;
    }

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

    get dropMenuDatas() {
        const {
            loading,
            hasSaveBtn,
            errorMsg,
        } = this.formInfo, {
            tableStore
        } = this.subTable, {
            index
        } = this.mainTable, {
            curKey
        } = this.subTab;

        let datas = [];
        if (index == '9' || index == 'sendMail') {
            !errorMsg && datas.push({
                key: '1',
                disabled: loading,
                icon: <i className='icon-coms-email' />,
                content: getLabel('1226', "邮件发送"),
                onClick: () => this.sendEmail(),
            })
        } else {
            let b;
            if (index == '1') {
                b = true;
            } else {
                b = curKey == '0';
            }
            hasSaveBtn && b && datas.push({
                key: '3',
                icon: <i className='icon-coms-Preservation' />,
                content: getLabel('30986', "保存"),
                disabled: loading,
                onClick: (index == '1' && curKey !== '0') ? this.saveExtendInfo : this.save,
            })
        }

        if (index == 'new') {
            datas.push({
                key: '4',
                icon: <i className='icon-coms-Preservation' />,
                content: getLabel(32159, '保存并进入详细设置'),
                disabled: loading,
                onClick: () => this.save('saveAndSet'),
            })
        }

        if (index == '8') {
            datas = [];
        }

        //添加【日志】菜单项
        if (index === '1') {
            datas.push({
                key: '5',
                icon: <i className='icon-coms-Print-log' />,
                content: getLabel(83, '日志'),
                onClick: () => this.log(),
            })
        }


        return datas;
    }

    @action
    openDialog = (index, id) => {
        const {
            visible
        } = this.mainDialog;

        this.mainTable.index = index;
        this.mainTable.id = id;

        let title;
        if (index == 'new') {
            title = getLabel('391209', "新建应聘信息");
        }
        if (index == '1') {
            title = getLabel('391175', "编辑应聘信息");
        }
        if (index == 'sendMail' || index == '9') {
            title = getLabel('15691', "邮件通知");
        }
        if (index == '7') {
            title = getLabel('1853', "录用");
        }
        if (index == '8') {
            title = !visible ? getLabel('119', "共享") : getLabel('18645', "添加共享");
        }
        if (index == '2') {
            title = getLabel('6103', "安排");
        }
        if (index == '3') {
            title = getLabel('15376', "通过");
        }
        if (index == '4') {
            title = getLabel('15689', "备用");
        }
        if (index == '5') {
            title = getLabel('15690', "淘汰");
        }
        if (index == '6') {
            title = getLabel('6102', "考评");
        }

        this[!visible ? 'mainDialog' : 'subDialog'].title = title;

        this[!visible ? 'mainDialog' : 'subDialog'].visible = true;
    }

    @action
    closeDialog = () => {
        const {
            visible
        } = this.subDialog;

        this[visible ? 'subDialog' : 'mainDialog'].visible = false;

        if (!visible) {
            this.formInfo.errorMsg = '';
        }
    }

    save = (arg) => {
        const {
            index,
            id
        } = this.mainTable;

        if (index == '7') {
            this.ignoreVerify();
        }

        this.formInfo.form.validateForm().then(f => {
            if (f.isValid) {
                const {
                    curKey
                } = this.subTab;

                let func;
                const params = this.formInfo.form.getFormParams();

                if (['new', '1'].includes(index)) {
                    const {
                        email
                    } = params;

                    if (!this.checkEmail(email)) {
                        message.warning(getLabel('388158', "邮件格式不正确"));
                        return
                    }
                }

                if (index == 'new') {
                    func = 'add';
                }
                if (index == '1' && curKey == '0') {
                    func = 'edit';
                    Object.assign(params, {
                        applyInfoId: id
                    })
                }
                if (index == '7') {
                    const {
                        isExist
                    } = this.formInfo, {
                        password,
                        password1,
                        email,
                    } = params;

                    if (isExist) {
                        this.hrmResourceCheck();
                        return
                    }

                    if (email && !this.checkEmail(email)) {
                        message.warning(getLabel('388158', "邮件格式不正确"));
                        return
                    }

                    if ((password || password1) && password !== password1) {
                        message.warning(getLabel('386414', "密码确认不正确"));
                        return
                    }

                    func = 'addEmploymentInfo';
                    Object.assign(params, {
                        type: 'simple',
                        rcId: this.mainTable.id
                    })
                    if (password) {
                        RSAEcrypt(this.openRSA,params.password).then(password => {
                            checkWeakPass(password,() => {
                                Object.assign(params,{
                                    password,
                                    password1:password
                                });
                                Api.addEmploymentInfo(params).then(res => {
                                    const {
                                        status,
                                    } = res;

                                    if (status == '1') {
                                        this.saveApplyInfoToResource();
                                    } else {
                                        message.warning(res.message);
                                        this.refreshVerificationCode();
                                    }
                                })
                            },() => {
                                this.refreshVerificationCode();
                            })
                        })
                        return;
                    }
                   
                }
                if (index == '8') {
                    func = 'addHrmShare';
                    Object.assign(params, {
                        applyInfoId: id
                    })
                }
                if (['2', '3', '4', '5', '6'].includes(index)) {
                    func = (index == '2') ? 'saveInterviewPlan' : (index == '6') ? 'saveInterviewAssess' : 'saveInterviewResult';

                    const {
                        stepId
                    } = this.formInfo;

                    Object.assign(params, {
                        applyInfoId: id,
                        stepId,
                    })

                    if (['3', '4', '5'].includes(index)) {
                        Object.assign(params, {
                            result: (index == '3') ? '1' : (index == '4') ? '2' : '0',
                        })
                    }
                }

                this.formInfo.loading = true;

                Api[func](params).then(rs => {
                    const {
                        api_status,
                        sign,
                        status,
                        applyInfoId,
                    } = rs;

                    if (index == '7') {
                        if (status == '1') {
                            this.saveApplyInfoToResource();
                        } else {
                            this.refreshVerificationCode();
                            message.warning();
                        }
                    } else {
                        if (api_status) {
                            this.formInfo.loading = false;
                            if (sign == '1') {
                                message.success(rs.message);
                                this.closeDialog();
                                this.getTableInfo();
                                if (arg == 'saveAndSet') this.openDialog('1', applyInfoId);

                            } else {
                                message.warning(rs.message)
                            }
                        }
                    }
                }).catch(error => {
                    message.error(error);
                });
            } else {
                f.showErrors();
                this.setDate(new Date());
            }
        });
    }

    checkEmail = (myemail) => {　　
        var myReg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;　　
        if (myReg.test(myemail)) {　　　　
            return true;　　
        } else {　　　　
            return false;
        }
    }

    @action
    hrmResourceCheck = () => {
        const {
            form
        } = this.formInfo, {
            lastname,
            managerid
        } = form.getFormParams();

        Api.hrmResourceCheck({
            lastname,
            managerid,
            workcode: ''
        }).then(cb => {
            const {
                status
            } = cb;

            this.formInfo.isExist = (status == '1') ? false : true;

            if (status == '-1') {
                const temp = this;
                confirm({
                    title: getLabel(131329, '信息确认'),
                    content: cb.message,
                    okText: getLabel(33703, '确定'),
                    cancelText: getLabel(32694, '取消'),
                    onOk() {
                        temp.formInfo.isExist = false;
                        temp.save()
                    },
                    onCancel() {
                        return false
                    },
                });
            } else {
                this.save();
            }
        }).catch(error => {
            message.error();
        });
    }

    saveApplyInfoToResource = () => {
        Api.saveApplyInfoToResource({
            applyInfoId: this.mainTable.id,
        }).then(cb => {
            const {
                api_status,
                sign,
            } = cb;

            if (api_status) {
                if (sign == '1') {
                    message.success(cb.message);
                    this.closeDialog();
                    this.getTableInfo();
                } else {
                    message.warning(cb.message);
                }
            } else {
                message.error();
            }
        }).catch(error => {
            message.error();
        });
    }

    ignoreVerify = () => {
        window.e9HideFormFieldKeys = [];
        const {
            accounttype
        } = this.formInfo;

        if (accounttype == '0') {
            window.e9HideFormFieldKeys.push('belongto');
        }
    }

    @action
    setDate = (date) => {
        this.formInfo.date = date;
    }

    sendEmail = () => {
        const {
            index,
            id,
            tableStore
        } = this.mainTable, {
            form
        } = this.formInfo;

        form.validateForm().then(f => {
            if (f.isValid) {
                const params = {
                    applyInfoId: (index == 'sendMail') ? tableStore.selectedRowKeys.toString() : id,
                    ...form.getFormParams(),
                };
                Api.sendEmail(params).then(cb => {
                    const {
                        api_status,
                        sign,
                    } = cb;

                    if (api_status) {
                        if (sign == '1') {
                            message.success(cb.message);
                            this.closeDialog();
                            this.getTableInfo();
                        } else {
                            message.warning(cb.message);
                        }
                    } else {
                        message.error();
                    }
                }).catch(error => {
                    message.error();
                });
            } else {　　　　　　
                f.showErrors();
                this.setDate(new Date());
            }
        })
    }

    showConfirm = (type) => {
        let _this = this;
        confirm({
            title: getLabel(131329, '信息确认'),
            content: (type == 'batch') ? getLabel(385625, '确定要删除选择的记录吗？') : getLabel(83877, '确定要删除吗？'),
            okText: getLabel(33703, '确定'),
            cancelText: getLabel(32694, '取消'),
            onOk() {
                _this.onOk(type)
            },
            onCancel() {
                return false
            },
        });
    }

    onOk = (type) => {
        this.del(type)
    }

    del = (type) => {
        const {
            tableStore,
            id,
        } = this.subTable;

        const params = {
            applyInfoId: this.mainTable.id,
            resourceIds: (type == 'batch') ? tableStore.selectedRowKeys.toString() : id,
        }

        Api.deleteHrmShare(params).then(rs => {
            const {
                api_status,
                sign
            } = rs;

            if (api_status) {
                if (sign == '1') {
                    message.success(rs.message);
                    this.getTableInfo();
                } else {
                    message.warning(rs.message)
                }
            }
        }).catch(error => {
            message.error();
        });
    }

    //*************************SubDialog*******************************
    @observable subDialog = {
        visible: false,
        title: '',
    }

    get sButtons() {
        const {
            loading
        } = this.formInfo;

        let buttons = [];
        buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ybhe88`} type="primary" disabled={loading} onClick={this.save}>{getLabel('30986',"保存")}</Button>);

        return buttons
    }

    get sStyle() {
        return {
            width: 700,
            height: 300,
        }
    }

    get sDropMenuDatas() {
        const {
            loading
        } = this.formInfo;

        let dropMenuDatas = [];
        dropMenuDatas.push({
            key: '1',
            disabled: loading,
            icon: <i className='icon-coms-Preservation' />,
            content: getLabel('30986', "保存"),
            disabled: loading,
            onClick: () => this.save(),
        })

        return dropMenuDatas;
    }



    //*************************SubTab*******************************
    @observable subTab = {
        curKey: '0',
    }

    get subTabDatas() {
        let datas = [];
        const {
            index
        } = this.mainTable;

        if (index == '1') {
            ['0', '1', '2'].map(v => {
                datas.push({
                    key: v,
                    title: (v == '0') ? getLabel('81711', "基本信息") : (v == '1') ? getLabel('391176', "个人信息") : getLabel('391177', "工作信息"),
                });
            });
        }
        if (['3', '4', '5'].includes(index)) {
            ['0', '1', '2'].map(v => {
                datas.push({
                    key: v,
                    title: (v == '0') ? getLabel('15729', "具体信息") : (v == '1') ? getLabel('15738', "考评情况") : getLabel('15920', "考核情况"),
                });
            });
        }
        if (index == '8') {
            ['0', '1'].map(v => {
                datas.push({
                    key: v,
                    title: (v == '0') ? getLabel('387366', "信息共享") : getLabel('387367', "共享管理"),
                });
            });
        }

        return datas;
    }

    get iconButtons() {
        const {
            tableStore
        } = this.subTable, {
            index
        } = this.mainTable, {
            selectedRowsKey
        } = this.tableEdit;

        return (
            [
                <div style={index != '8' ? {float:'left'}:{}}>
                    <WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@6cp6gz`} 
                        buttonType='add'  
                        type="primary" 
                        onClick={() => (index == '8') ? this.openDialog('8',this.mainTable.id) : this.tableEdit.ref.doAdd()}
                    />
                </div>,
                <div style={index != '8' ? {float:'left',margin: '0 10px'} : {}}>
                    <WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@ef62fl`} 
                        buttonType='del'  
                        type="primary" 
                        disabled={index != '8' ? !selectedRowsKey : tableStore.selectedRowKeys.length === 0}
                        onClick = {() => index != '8' ? this.tableEdit.ref.doDelete() :  this.showConfirm('batch')} 
                    /> 
                </div>
            ]
        )
    }

    getFormInfoWithTable = () => {
        this.resetFormInfo();

        const {
            curKey
        } = this.subTab;

        let func;
        const params = {
            applyInfoId: this.mainTable.id
        }
        if (curKey == '1') {
            func = 'getPersonalInfo';
        } else {
            func = 'getWorkInfo';
        }
        Api[func](params).then(rs => {
            const {
                api_status,
                conditions,
                table,
            } = rs;
            if (api_status) {
                this.formInfo.conditions = conditions;
                this.formInfo.form.initFormFields(conditions);
                this.formInfo.loading = false;
                this.tableEdit.table = table;
            } else {
                message.error();
            }
        }).catch(error => {
            message.error();
        });

    }

    saveExtendInfo = () => {
        this.formInfo.form.validateForm().then(f => {
            if (f.isValid) {
                const {
                    curKey
                } = this.subTab, {
                    table
                } = this.tableEdit;

                let func;
                const params = {
                    applyInfoId: this.mainTable.id,
                    ...this.formInfo.form.getFormParams(),
                }

                table.map(t => {
                    Object.assign(params, this.jsonTranslation(t.datas));
                    Object.assign(params, {
                        [t.rownumName]: t.datas.length
                    });
                })

                if (curKey == '1') {
                    func = 'addPersonalInfo';
                } else {
                    func = 'addWorkInfo';
                }

                this.formInfo.loading = true;

                Api[func](params).then(rs => {
                    const {
                        api_status,
                        sign
                    } = rs;

                    if (api_status) {
                        this.formInfo.loading = false;
                        if (sign == '1') {
                            message.success(rs.message);
                        } else {
                            message.warning(rs.message)
                        }
                    }
                }).catch(error => {
                    message.error(error);
                });
            } else {
                f.showErrors();
                this.setDate(new Date());
            }
        });
    }

    jsonTranslation = (datas) => {
        const obj = {};
        datas.map((data, index) => {
            Object.keys(data).map(key => {
                obj[`${key}_${index}`] = data[key]
            })
        });
        return obj
    }

    @action
    resetSubTab = () => {
        this.subTab.curKey = '0';
    }

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

    //*************************FormInfo*******************************
    @observable formInfo = {
        form: new WeaForm(),
        conditions: [],
        loading: true,
        hasSaveBtn: true,
        date: '',
        accounttype: '0',
        isExist: true,
        errorMsg: '',
    }

    @action
    getFormConditions = () => {
        this.formInfo.form.isFormInit && this.resetFormInfo();

        const {
            index,
            id
        } = this.mainTable;
        const params = {};
        let func;
        if (index == 'new' || index == '1') {
            func = 'getApplyInfoForm';
        }
        if (index == '7') {
            func = 'getHrmResourceAddForm';
            Object.assign(params, {
                type: 'simple',
            })
        }
        if (index == '8') {
            func = 'getHrmShareForm';
        }
        if (index == 'sendMail' || index == '9') {
            func = 'getEmailInfoForm';
        }
        if (index == '2') {
            func = 'getInterviewPlanForm';
        }
        if (['3', '4', '5'].includes(index)) {
            func = 'getInterviewResultForm';
            Object.assign(params, {
                result: (index == '3') ? '1' : (index == '4') ? '2' : '0',
            })
        }
        if (index == '6') {
            func = 'getInterviewAssessForm';
        }
        if (index !== '7') {
            id && Object.assign(params, {
                applyInfoId: id
            });
        }

        Api[func](params).then(rs => {
            const {
                conditions,
                condition,
                errorMsg,
                api_status = true,
                openRSA
            } = rs;

            this.openRSA = openRSA;

            if (api_status) {
                const temp = conditions ? conditions : condition;

                if (!temp) {
                    this.formInfo.errorMsg = errorMsg;
                    this.formInfo.loading = false;
                    return
                }

                if (['1', '7', 'new'].includes(index)) {
                    this.handleCondition(temp);
                }
                if (['2', '3', '4', '5', '6'].includes(index)) {
                    ['stepId', 'hasSaveBtn'].map(v => {
                        if (rs[v] !== undefined) this.formInfo[v] = rs[v];
                    });
                }

                this.formInfo.conditions = temp;
                this.formInfo.form.initFormFields(temp);
                this.formInfo.loading = false;
                if (index == '7') {
                    this.formInfo.form.isFormInit && this.getEmploymentInfo()
                }
            } else {
                message.error();
            }
        }).catch(error => {
            message.error();
        });
    }

    getEmploymentInfo = () => {
        const params = {
            applyInfoId: this.mainTable.id,
        }
        Api.getEmploymentInfo(params).then(rs => {
            const {
                api_status,
                lastname,
                jobtitle,
                jobtitlespan,
                sex,
            } = rs;
            if (api_status) {
                this.formInfo.form.updateFields({
                    lastname,
                    sex,
                    jobtitle: {
                        value: jobtitle,
                        valueSpan: jobtitlespan,
                        valueObj: [{
                            id: jobtitle,
                            name: jobtitlespan,
                        }]
                    }
                })
            } else {
                message.error();
            }
        }).catch(error => {
            message.error();
        });
    }

    handleCondition = (conditions) => {
        conditions.map(c => {
            c.items.map(item => {
                const key = item.domkey[0],
                    type = item.conditionType;
                if (key == 'resourceimageid' || key == 'picture') {
                    item.conditionType = 'UPLOAD';
                    item.uploadUrl = '/api/doc/upload/uploadFile';
                    item.category = 'string';
                    item.listType = 'img';
                    item.limitType = 'jpg,gif,png';
                    item.btnSize = 'small';
                    item.maxUploadSize = 5;
                    item.maxFilesNumber = 1;
                    item.showClearAll = false;
                    if (item.datas && !item.datas[0].filename) {
                        item.datas = [];
                    }
                }

                if (item.viewAttr == 3) {
                    if (type == 'INPUT' || type == 'BROWSER') {
                        item.rules = 'required|string'
                    }
                    if (type == 'SCOPE') {
                        item.rules = 'scopeRequired|scopeDirty'
                    }
                    if (type == 'INPUTNUMBER') {
                        item.rules = 'required|integer'
                    }
                }

                if (key == 'accounttype') {
                    const result = item.options.find(option => option.selected === true);
                    result && this.exportSelectedValue(key, result.key);
                }

            })
        })
    }

    @action
    exportSelectedValue = (name, value) => {
        this.formInfo[name] = value;
    }

    @action
    resetFormInfo = () => {
        ['conditions', 'form', 'loading', 'hasSaveBtn', 'isExist'].map((v, i) => {
            this.formInfo[v] = (i == 0) ? [] : (i == 1) ? new WeaForm() : true
        });
    }

    //*************************TableEdit*******************************
    @observable tableEdit = {
        table: [],
        selectedRowsKey: '',
    }

    @action
    exportDatas = (datas, index) => {
        this.tableEdit.table[index].datas = datas;
    }

    getTableEditDatas = () => {}

    @action
    setSelectedRowKeys = (keys) => {
        this.tableEdit.selectedRowsKey = keys.toString();
    }

    //*************************TabPane*******************************
    @observable tabPane = {
        key: '0',
    }

    @action
    clickTabPane = (key) => {
        this.tableEdit.selectedRowsKey = '';
        this.tabPane.key = key;
    }

    //*************************Print*******************************
    @observable print = {
        printInfo: [],
        loading: true,
    }

    @action
    printApplyInfo = (id) => {
        const params = {
            applyInfoId: id ? id : this.mainTable.id,
        };
        Api.printApplyInfo(params).then(rs => {
            const {
                api_status,
                printInfo,
            } = rs;

            if (api_status) {
                this.print.printInfo = printInfo;
                this.print.loading = false;
            } else {
                message.error();
            }
        }).catch(error => {
            message.error();
        });
    }

    @action
    resetPrintInfo = () => {
        this.print.printInfo = [];
        this.print.loading = true;
    }

    @action
    setRef = (com, tag) => {
        if (tag == 'tb') {
            this.tableEdit.ref = com;
        } else {
            this.print.ref = com;
        }
    }

    get printDropMenuDatas() {
        return [{
            key: '0',
            icon: <i className='icon-coms-Print' />,
            content: getLabel('257', "打印"),
            disabled: this.print.loading,
            onClick: () => this.printApplicantInfo(),
        }]
    }

    printApplicantInfo = () => {
        window.print();
    }

    @observable time = new Date();

    @action refreshVerificationCode  = () => {
        this.time = new Date();
    }


}