import {
    observer
} from 'mobx-react';
import {
    WeaDialog,
    WeaNewScroll,
    WeaMoreButton,
} from 'ecCom'
import {
    Row,
    Col,
    Icon,
    Spin,
    Modal,
    Table,
    Button,
} from 'antd'
import {
    WeaTableNew
} from 'comsMobx';
import {
    toJS
} from 'mobx';
import {
    WeaLocaleProvider
} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const WeaTable = WeaTableNew.WeaTable;

import ImportLog from './ImportLog'

@observer
export default class ImportResult extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            width: 740,
            height: 450,
        }
    }

    getButtons = () => {
        const {
            store
        } = this.props, {
            resultDialog,
            setLogDialogVisible,
        } = store, {
            failnum,
            succnum,
            importStatus
        } = resultDialog;

        const buttons = [];

        if (importStatus == 'over' || importStatus == 'error') {
            // if (failnum > 0 && succnum > 0) {
            //     buttons.push(<Button type="primary" disabled onClick={() => {}}>{i18n.button.downloadErrorDatas()}</Button>);
            // }
            // buttons.push(<Button type="primary" disabled onClick={() => {}}>{i18n.button.downloadAllDatas()}</Button>);
            buttons.push(<Button type="primary" onClick={() => setLogDialogVisible(true)}>{getLabel(24835, '导入日志')}</Button>);
        }
        buttons.push(<WeaMoreButton />);

        return buttons;
    }

    reRenderColumns = (c) => {
        c.forEach(item => {
            if (item.dataIndex == 'operatedetail') {
                item.render = function(text, record) {
                    return <span style={{color: '#FF0000'}}>{text}</span>
                }
            }
        });
    }

    getErrorInfo = () => {
        const {
            store
        } = this.props, {
            resultDialog
        } = store, {
            errorTip,
            errorInfo
        } = resultDialog

        const columns = [{
            "title": getLabel(24960, '提示信息'),
            "width": "100%",
            "dataIndex": "message",
            render: (text, record, index) => {
                return <span style={{ color: '#FF0000' }}>{text}</span>
            }
        }];

        const elements = <div style={{color: '#FF0000'}} className='hrm-import-table-th'>
                            <div style={{margin: '20px 0'}}>
                                <div className='hrm-import-error-tip'>
                                    <Icon type={'cross'} style={{ color: '#fff',fontSize: 18 }} />
                                </div>
                                {errorTip}
                            </div>
                            <div style={{padding: '0 30px'}}><Table dataSource={errorInfo} columns={columns} pagination={false} bordered/></div>
                         </div>;
        return elements;
    }

    getResultInfo = () => {
        const {
            store
        } = this.props, {
            resultDialog
        } = store, {
            succnum,
            failnum,
            tableStore,
        } = resultDialog, {
            height
        } = this.state;

        const icon = <div style={{backgroundColor: succnum>0 ? '#54D3A2' : '#FF0000',marginLeft: 270 }} className='hrm-import-error-tip'>
                            <Icon type={succnum>0 ? 'check' : 'cross'} style={{ color: '#fff',fontSize: 18 }} />
                      </div>;
        const tip = <span style={{color: succnum > 0 ? '#54D3A2' : '#FF0000'}}>{(succnum > 0 && failnum == 0) ? getLabel(384061, '共{rownum}条数据，全部导入成功！').replace('{rownum}',`${succnum+failnum}`) : getLabel(384060, '共{rownum}条数据，{succnum}条导入成功！').replace('{rownum}',`${succnum+failnum}`).replace('{succnum}',`${succnum}`)}</span>

        const resultTable = ((succnum + failnum > 0) && failnum > 0) && <WeaTable
                                                                            comsWeaTableStore={tableStore}
                                                                            bordered
                                                                            pagination={true}
                                                                            rowKey={(record, index) => index}
                                                                            getColumns={c => this.reRenderColumns(c)}
                                                                        />
        return <div>
                    <div style={( (succnum + failnum > 0) && failnum > 0) ? {marginTop: 20} : {marginTop: height/2 -30}}>{icon}{tip}</div>
                    <div style={{padding: '10px 20px'}}>{resultTable}</div>
                </div>
    }

    getImportProcessInfo = () => {
        const {
            store
        } = this.props, {
            resultDialog
        } = store, {
            datas,
            importStatus
        } = resultDialog;

        const columns = [{
            "title": getLabel(27592, '行'),
            "width": "15%",
            "dataIndex": "rownum"
        }, {
            "title": getLabel(602, '状态'),
            "width": "15%",
            "dataIndex": "status",
            render: (text, record, index) => {
                return <span style={{ color: text=='1'?'#54D3A2':'#FF0000'}}>{text=='1'?getLabel(15242, '成功'):getLabel(498, '失败')}</span>
            }
        }, {
            "title": getLabel(24960, '提示信息'),
            "width": "70%",
            "dataIndex": "message",
            render: (text, record, index) => {
                return <span style={{ color: '#FF0000' }}>{text}</span>
            }
        }];

        const loadingTip = (importStatus !== 'over' || importStatus !== 'error') && <Spin tip={getLabel(384042, '服务器正在处理数据导入，请稍后……')}></Spin>;

        const processTable = <Table dataSource={toJS(datas)} columns={columns} pagination={false} bordered/>

        return <div className='hrm-import-ant-table'>
                    <div style={{margin: '20px 0',paddingLeft: 260}} className='hrm-import-loading'>{loadingTip}</div>
                    <div style={{padding: '0 20px 10px'}}>{processTable}</div> 
                </div>
    }

    render() {
        const {
            store
        } = this.props, {
            resultDialog,
            logDialog,
            setResultInfoDialogVisible,
            hasErrorInfo,
            setScoll
        } = store, {
            title,
            visible,
            importStatus,
            errorInfo,
            loading
        } = resultDialog, {
            width,
            height
        } = this.state;

        return (
            <div>
                <WeaDialog
                    title={title}
                    icon="icon-coms-hrm"
                    iconBgcolor="#217346"
                    visible={visible}
                    closable={true}
                    onCancel={() => setResultInfoDialogVisible(false)}
                    buttons={this.getButtons()}
                    style={{width: width, height: height}}
                    onChangeHeight={(h) => this.setState({height: h})}
                >
                    <Spin spinning={loading}> 
                        <WeaNewScroll height={height} ref={com => setScoll(com)}>
                            {
                                 !loading && (hasErrorInfo(errorInfo) ? this.getErrorInfo(errorInfo) : 
                                    (importStatus == 'over' || importStatus == 'error' ) ? this.getResultInfo() : this.getImportProcessInfo() )
                            }
                        </WeaNewScroll>
                    </Spin>
                </WeaDialog>
                {logDialog.visible && <ImportLog store={store}/>}
            </div>
        )
    }
}