import {
    inject,
    observer
} from 'mobx-react';
import {
    WeaDialog,
    WeaMoreButton,
    WeaLocaleProvider,
} from 'ecCom'
import {
    Button,
} from 'antd'
import {
    WeaTableNew
} from 'comsMobx';
import {
    jumpToHrmCard
} from '../../util/pure-util'
const WeaTable = WeaTableNew.WeaTable;
const getLabel = WeaLocaleProvider.getLabel;

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

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

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


        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 {
            importScheduleCode
        } = this.props, {
            recordDialog,
            queryLog,
        } = importScheduleCode, {
            loading
        } = recordDialog;

        return [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@bf2hyd`} type="primary" disabled={loading} onClick={() => queryLog()}>{getLabel(384041, '查看导入日志')}</Button>),
            (<WeaMoreButton ecId={`${this && this.props && this.props.ecId || ''}_WeaMoreButton@bnteg1`} />)
        ];
    }

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

        return (
            <div>
                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@nsn744`}
                    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@l08g3g`} 
                        comsWeaTableStore={tableStore}
                        hasOrder={true}
                        needScroll={true}
                        scroll={{y: height - 80}}
                        rowKey={(record,index) => index}
                        getColumns={c => this.reRenderColumns(c)}
                    />
                </WeaDialog>
                {logDialog.visible && <ImportLog ecId={`${this && this.props && this.props.ecId || ''}_ImportLog@1zdmz4`} />}
                {resultDialog.visible && <ImportResult ecId={`${this && this.props && this.props.ecId || ''}_ImportResult@ahhxyp`} />}
            </div>
        )
    }
}