import {
    inject,
    observer
} from 'mobx-react';
import {
    WeaDialog,
    WeaMoreButton,
} from 'ecCom'
import {
    Button,
} from 'antd'
import {
    WeaTableNew
} from 'comsMobx';
import {
    jumpToHrmCard
} from '../../public/pulic-func'
import {
    i18n
} from '../../public/i18nbk';
const WeaTable = WeaTableNew.WeaTable;

import ImportLog from './ImportLog'
import ImportResult from './ImportResult'

@inject('hrmImportCommon')
@observer
export default class ImportHistory extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            width: 870,
            height: 510,
        }
    }

    reRenderColumns = (c)=> {
        const {
            hrmImportCommon
        } = this.props, {
            jumpToImportResult
        } = hrmImportCommon;


        c.forEach(item => {
            if (item.dataIndex == 'operator') {
                item.render = function(text, record) {
                    return <span><a style={{cursor:'pointer', color: '#4D7AD8'}} onClick={ () => jumpToHrmCard(record.operator)}>{record.operatorspan}</a></span>
                }
            }
            if (item.dataIndex == 'allnum') {
                item.render = function(text, record) {
                    return <span><a style={{cursor:'pointer', color: '#4D7AD8'}} onClick={ () => jumpToImportResult(record.randomFieldId)}>{record.allnumspan}</a></span>
                }
            }
        });
    }

    getButtons = () => {
        const {
            hrmImportCommon
        } = this.props, {
            recordDialog,
            queryLog,
        } = hrmImportCommon, {
            loading
        } = recordDialog;

        return [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@p1bp2b@1`} type="primary" disabled={loading} onClick={() => queryLog()}>{i18n.button.queryImportLog()}</Button>),
            (<WeaMoreButton ecId={`${this && this.props && this.props.ecId || ''}_WeaMoreButton@uqammc@2`} />)
        ];
    }

    getRowKey = (record,index) => index

    render() {
        const {
            hrmImportCommon
        } = this.props, {
            recordDialog,
            logDialog,
            resultDialog,
            setRecordDialogVisible
        } = hrmImportCommon, {
            title,
            visible,
            tableStore
        } = recordDialog, {
            width,
            height
        } = this.state;

        return (
            <div>
                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@p8dp6d`}
                    title={title}
                    icon="icon-coms-hrm"
                    iconBgcolor="#217346"
                    visible={visible}
                    closable={true}
                    onCancel={() => setRecordDialogVisible(false)}
                    buttons={this.getButtons()}
                    style={{width: width, height: height}}
                    onChangeHeight={(h) => this.setState({height: h})}
                >
                    <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@6lukxd`} 
                        comsWeaTableStore={tableStore}
                        hasOrder={true}
                        needScroll={true}
                        scroll={{y: height - 80}}
                        rowKey={this.getRowKey}
                        getColumns={this.reRenderColumns}
                    />
                </WeaDialog>
                {logDialog.visible && <ImportLog ecId={`${this && this.props && this.props.ecId || ''}_ImportLog@hvvkcl`} />}
                {resultDialog.visible && <ImportResult ecId={`${this && this.props && this.props.ecId || ''}_ImportResult@twyqc1`} />}
            </div>
        )
    }
}