import {
    inject,
    observer,
} from 'mobx-react'

import {
    WeaTop,
    WeaTab,
    WeaDialog,
    WeaFormItem,
    WeaRightMenu,
    WeaSearchGroup,
} from 'ecCom'

import {
    Row,
    Col,
    Spin,
    Modal,
    Button,
    message,
} from 'antd'

import React from 'react'
import * as mobx from 'mobx'

import {
    WeaSwitch
} from 'comsMobx'
import {
    WeaTableNew
} from 'comsMobx'
import {
    i18n
} from '../../public/i18n';

import Editor from '../../coms/Editor'
import {
    HocFactory
} from './HocFactory'
const WrappedEditor = HocFactory(Editor);

const toJS = mobx.toJS;
const confirm = Modal.confirm;
const WeaTable = WeaTableNew.WeaTable;

import '../../style/area.css'

@inject('hrmEducationSet')
@observer
class Education extends React.Component {
    constructor(props) {
        super(props);
    }

    componentWillMount() {
        let bool = window.location.href.indexOf('/spa/hrm/engine.html') > -1;
        if (bool) {
            document.title = i18n.module.educationSet();
        }
    }

    componentDidMount() {
        this.init();
    }

    componentWillReceiveProps(nextProps) {
        if (this.props.location.key !== nextProps.location.key) {
            this.init();
        }
    }

    init() {
        const {
            hrmEducationSet
        } = this.props;

        hrmEducationSet.mobxDatasReset();
        hrmEducationSet.setOperation('getTableInfo');
        hrmEducationSet.getTableInfo();
    }

    //WeaRightMenu 
    getDropMenuDatas() {
        const {
            hrmEducationSet
        } = this.props, {
            rightMenu
        } = hrmEducationSet;

        let menus = [];
        toJS(rightMenu).map((item, index) => {
            let obj = {
                key: item.menuFun,
                icon: <i className={`${item.menuIcon}`}/>,
                content: item.menuName,
            }
            if (item.menuFun == 'collection' || item.menuFun == 'help' || item.menuFun == 'pageAddress') {
                obj.disabled = true;
            }
            menus.push(obj);
        })

        return menus;
    }
    handleMenuClick(key) {
        const {
            hrmEducationSet
        } = this.props, {
            isMpPanelShow
        } = hrmEducationSet;

        isMpPanelShow && hrmEducationSet.setMpPanelStatus(false);
        this[key] && this[key]();
    }
    //WeaTop
    getTopMenuBtns() {
        const {
            hrmEducationSet
        } = this.props, {
            topMenu,
            mpTableStore,
            type
        } = hrmEducationSet;

        let btns = [];
        toJS(topMenu).map((item, index) => {
            if (item.menuFun !== 'batchDelete') {
                btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@wosvze@${index}`} type='primary' onClick={() => this.handleClick(item)}>{item.menuName}</Button>);
            } else {
                btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@c7nyfo@${index}`} type='primary' onClick={() => this.handleClick(item)} disabled={mpTableStore.selectedRowKeys.length>0?false:true} >{item.menuName}</Button>);
            }
        });
        return btns;
    }
    handleClick(item) {
        const {
            hrmEducationSet
        } = this.props, {
            isMpPanelShow
        } = hrmEducationSet;

        isMpPanelShow && hrmEducationSet.setMpPanelStatus(false);
        this[item.menuFun] && this[item.menuFun]()
    }
    new() {
        const {
            hrmEducationSet
        } = this.props;

        hrmEducationSet.setNeTitle(i18n.label.newEducation());
        hrmEducationSet.setNeVisible(true);
        hrmEducationSet.newSet('new', 'addForm');
        hrmEducationSet.getNeForm();
    }
    import () {
        const {
            hrmEducationSet
        } = this.props;

        hrmEducationSet.setItVisible(true);
        hrmEducationSet.setOperation('import');
        hrmEducationSet.getImportForm();
    }
    batchDelete() {
        const {
            hrmEducationSet
        } = this.props, {
            mpTableStore
        } = hrmEducationSet;

        let keys = toJS(mpTableStore.selectedRowKeys).toString();
        hrmEducationSet.setSelectedRowsKeys(keys);
        this.deleteRows();
    }

    custom = () => {
        const {
            hrmEducationSet
        } = this.props, {
            mpTableStore,
        } = hrmEducationSet;

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

    log = () => {
        window.setLogViewProps({
            logSmallType: '3017',
        });
    }

    deleteRows(id) {
        const {
            hrmEducationSet
        } = this.props;

        if (id) {
            hrmEducationSet.deleteSet(id, 'delete');
        } else {
            hrmEducationSet.setOperation('batchDelete');
        }

        let v = id ? 'del' : 'batchDel';
        this.showConfirm(v);
    }
    showConfirm(v) {
        let _this = this;
        confirm({
            title: i18n.confirm.defaultTitle(),
            content: (v == 'del') ? i18n.confirm.delete() : i18n.confirm.batchDeleteConfirm(),
            okText: i18n.button.ok(),
            cancelText: i18n.button.cancel(),
            onOk() {
                _this.onOk()
            },
            onCancel() {
                return false
            },
        });
    }
    onOk() {
        const {
            hrmEducationSet
        } = this.props;
        hrmEducationSet.delete();
    }
    save() {
        const {
            hrmEducationSet
        } = this.props, {
            formType
        } = hrmEducationSet;

        if (formType == 'new') {
            hrmEducationSet.setOperation('save_new');
            hrmEducationSet.add();
        } else if (formType == 'edit') {
            hrmEducationSet.setOperation('save_edit');
            hrmEducationSet.edit();
        }
    }
    //WeaTab
    getTabBtn() {
        const {
            hrmEducationSet
        } = this.props, {
            mpShForm
        } = hrmEducationSet;

        const btn = [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@23aj70`} type="primary" onClick={() => this.handleMpSearch()}>{i18n.button.search()}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@chmffd`} onClick={() => mpShForm.reset()}>{i18n.button.reset()}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@56kr0h`} onClick={() => hrmEducationSet.setMpPanelStatus(false)}>{i18n.button.cancel()}</Button>),
        ];
        return btn;
    }
    getMpShBaseValue() {
        const {
            hrmEducationSet
        } = this.props, {
            mpShForm,
            eduName
        } = hrmEducationSet;

        let val;
        if (mpShForm.isFormInit) {
            val = mpShForm.getFormParams().name;
        } else {
            val = eduName;
        }
        return val;
    }
    handleMpShChange(val) {
        const {
            hrmEducationSet
        } = this.props, {
            mpShForm
        } = hrmEducationSet;

        if (mpShForm.isFormInit) {
            hrmEducationSet.updateFields(val);
        }
        hrmEducationSet.setSearchName(val);
    }
    handleMpPanelStatus(bool) {
        const {
            hrmEducationSet
        } = this.props, {
            mpShCondition
        } = hrmEducationSet;

        if (bool) { //打开面板
            hrmEducationSet.setMpPanelStatus(true);
            if (mpShCondition.length == 0) { //第一次打开面板时加载高级搜索数据
                hrmEducationSet.setOperation('mpSearch');
                hrmEducationSet.getMpSearchCondition();
            }
        } else { //关闭面板
            hrmEducationSet.setMpPanelStatus(false);
            hrmEducationSet.mpSearchReset();
        }
    }
    handleMpSearch() {
        const {
            hrmEducationSet
        } = this.props, {
            isMpPanelShow
        } = hrmEducationSet;

        hrmEducationSet.setOperation('getTableInfo');
        hrmEducationSet.getTableInfo(); //获取表格信息
        isMpPanelShow && hrmEducationSet.setMpPanelStatus(false); //关闭面板
    }
    getPanelComponents() {
        const {
            hrmEducationSet
        } = this.props, {
            mpShCondition,
            mpShForm,
            mpShLoading
        } = hrmEducationSet, {
            isFormInit
        } = mpShForm;

        let arr = [];
        isFormInit && mpShCondition.map(c => {
            c.items.map((field, index) => {
                arr.push(<Col ecId={`${this && this.props && this.props.ecId || ''}_Col@9jivsf@${index}`} span={10} offset={1}>
                              <div style={{marginTop: 20}}>
                                  <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@mnpv43@${index}`}
                                      label={`${field.label}`}
                                      labelCol={{span: `${field.labelcol}`}}
                                      wrapperCol={{span: `${field.fieldcol}`}}>
                                          {<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@tukyl8@${index}`} fieldConfig={field} form={mpShForm} formParams={mpShForm.getFormParams()} />}
                                  </WeaFormItem>
                              </div>
                  	     </Col>)
            })
        })

        if (mpShLoading) {
            return (
                <div className='hrm-loading-center-small' style={{top:'25%'}}>
                    <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@njr5jt`} spinning={mpShLoading}></Spin>
                </div>
            )
        } else {
            return <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@9uc3dv`} onKeyDown={(e) =>( e.keyCode == 13 && e.target.tagName === "INPUT")  && this.handleMpSearch()}>{arr}</Row>
        }
    }
    //WeaTable
    reRenderColumns(columns) {
        let _this = this;
        columns.forEach((c, index) => {
            if (c.dataIndex == 'name') {
                c.render = function(text, record) {
                    return <a onClick={() => _this.doEdit(record.randomFieldId)} >{record.name}</a>
                }
            };
        })
    }
    onOperatesClick(record, index, operate) {
        let i = operate.index;

        i == '0' && this.doEdit(record.randomFieldId);
        i == '1' && this.doDel(record.randomFieldId);
        i == '2' && this.deLog(record.randomFieldId);
    }
    doEdit(id) {
        const {
            hrmEducationSet
        } = this.props;

        hrmEducationSet.setNeTitle(i18n.label.editEducation());
        hrmEducationSet.setNeVisible(true);
        hrmEducationSet.editSet(id, 'edit', 'editFrom');
        hrmEducationSet.getNeForm();
    }
    doDel(id) {
        const {
            hrmEducationSet
        } = this.props;
        this.deleteRows(id);
    }

    deLog(id) {
        window.setLogViewProps({
            logSmallType: '3017',
            targetId: id,
        });
    }

    //NewAndEditDialog
    handleNeCancel() {
        const {
            hrmEducationSet
        } = this.props;
        hrmEducationSet.setNeVisible(false);
        hrmEducationSet.neReset();
    }

    render() {
        const {
            hrmEducationSet
        } = this.props;
        const {
            wtpLoading,
            isEdit,
            date
        } = hrmEducationSet; //common
        const {
            mpTitle
        } = hrmEducationSet; //WeaTop
        const {
            isMpPanelShow,
            conditionNum
        } = hrmEducationSet; //WeaTab
        const {
            mpTableStore
        } = hrmEducationSet; //WeaTable
        const {
            neTitle,
            neVisible,
            neCondition,
            neForm,
            neLoading
        } = hrmEducationSet; //NewAndEditDialog

        return (
            <div ref='page'>
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@0uzl81`} 
                        datas={this.getDropMenuDatas()} 
                        onClick={ key=>this.handleMenuClick(key)}
                        collectParams={{favname:mpTitle,favouritetype:5}}
                    >
                        <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@3e7y4h`} 
                            title={mpTitle}
                            icon={<i className='icon-coms-hrm'/>}
                            iconBgcolor='#217346'
                            loading={true}
                            buttons={this.getTopMenuBtns()}
                            showDropIcon={true}
                            dropMenuDatas={this.getDropMenuDatas()}
                            onDropMenuClick={(e) => this.handleMenuClick(e)}
                        >
                            <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@xg4j3f`}
                                searchType={['base','advanced']}
                                showSearchAd={isMpPanelShow}
                                hasMask={false}
                                advanceHeight={Math.ceil(conditionNum/2)*52+20}
                                buttonsAd={this.getTabBtn()}
                                searchsBaseValue={this.getMpShBaseValue()}
                                setShowSearchAd={  b => this.handleMpPanelStatus(b)}
                                hideSearchAd={ ()=>hrmEducationSet.setMpPanelStatus(false)}
                                searchsAd= {isMpPanelShow?this.getPanelComponents():<div></div>}
                                onSearch={()=>this.handleMpSearch()}
                                onSearchChange={val=>this.handleMpShChange(val)}
                            />
                            <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@30fcny`} 
                                comsWeaTableStore={mpTableStore}
                                hasOrder={true}
                                needScroll={true}
                                getColumns={c => this.reRenderColumns(c)}
                                onOperatesClick={(record, index, operate) => this.onOperatesClick(record, index, operate)}
                            />
                        </WeaTop>
                    </WeaRightMenu>
                    <WrappedEditor ecId={`${this && this.props && this.props.ecId || ''}_WrappedEditor@aeigdh`} 
                        title={neTitle}
                        visible={neVisible}
                        condition={toJS(neCondition)}
                        form={neForm}
                        isFormInit={neForm.isFormInit}
                        loading={neLoading}
                        isEdit={isEdit}
                        save={() => this.save()}
                        onCancel={() => this.handleNeCancel()}
                    />
            </div>
        )
    }
}

export default Education