import React, {Component} from 'react';
import {inject, observer} from "mobx-react";
import {toJS} from 'mobx';
import {Table} from 'antd';
import {WeaLocaleProvider} from "ecCom";

const getLabel = WeaLocaleProvider.getLabel;

@inject("coremailPortal")
@observer
class Main extends Component {
    componentDidMount() {
        const {from, coremailPortal} = this.props;
        let params = {};
        if (from == 'portal') {
            params = this.props.params;
            coremailPortal.init('/api/integration/coremail/datas/top', params.esetting);
        } else {
            params = JSON.parse(this.props.location.query.jsonstr);
            // 获取location 的参数走接口;
            coremailPortal.init('/api/integration/coremail/datas/more', params.esetting);
        }
        // 根据配置信息设置columns
        let fieldcolumnList = ['subject', 'coreMailUser', 'createtime'];
        // let fieldcolumnList = ['coreMailTitle', 'coreMailUser', 'coreMailTime'];
        if (params && params.fieldcolumnList) {
            fieldcolumnList = params.fieldcolumnList
        }

        let fieldcolumnwidths = {
            subject: '30%',
            coreMailUser: '30%',
            createtime: '30%',
        }

        if (params && params.fieldcolumnwidths) {
            fieldcolumnwidths = params.fieldcolumnwidths;
        }

        let eContentHintText = '您有 $NoReadCnt$ 封未读邮件，$LoginLinkStart$请登录查看$LoginLinkEnd$！';
        let eContentUnreadMailQuantityColor = 'red';
        let eContentSingleSignOnLinkColor = 'red';
        if (params && params.esetting) {
            const {esetting} = params;
            if (esetting.eContentHintText) {
                eContentHintText = esetting.eContentHintText;
            }

            if (esetting.eContentUnreadMailQuantityColor) {
                eContentUnreadMailQuantityColor = esetting.eContentUnreadMailQuantityColor;
                eContentSingleSignOnLinkColor = esetting.eContentSingleSignOnLinkColor;
            }
        }


        const state = {
            columns: this.getColums(fieldcolumnList, fieldcolumnwidths),
            eContentHintText,
            eContentUnreadMailQuantityColor,
            eContentSingleSignOnLinkColor,
        }
        coremailPortal.setState(state);
    }

    getColums(fieldcolumnList, fieldcolumnwidths) {
        const names = {
            subject: getLabel(344, '主题'),
            coreMailUser: getLabel(129935, '发送人'),
            createtime: getLabel(129936, '发送时间'),
        }
        let columns = [];
        fieldcolumnList && fieldcolumnList.forEach((item) => {
            let col = {
                key: item,
                dataIndex: item,
                title: names[item],
                width: fieldcolumnwidths[item],
            }
            if ('subject' == item) {
                col.render = (text, record, index) => <a target="_blank" href={record.link}>{text}</a>
            }
            columns.push(col);
        })
        return columns;
    }

    renderText(state = {}, res = {}) {
        let text = state.eContentHintText.replace('$NoReadCnt$',
            `<span style="color: ${state.eContentUnreadMailQuantityColor}">${res.unReadCount}</span>`)
            .replace('$LoginLinkStart$',
                `<a href="${res.unReadLink}" target=${res.linkMode == 2 ? "_blank" : "_self"} style="color: ${state.eContentSingleSignOnLinkColor}">`)
            .replace('$LoginLinkEnd$', '</a>');
        return <div dangerouslySetInnerHTML={{__html: text}}></div>
    }

    render() {
        const {coremailPortal} = this.props;
        const {loaded} = coremailPortal;
        // const state = toJS(coremailPortal.state);
        const res = toJS(coremailPortal.res);

        return (<div>
            {
                loaded && !res.status && <div style={{color: 'red'}}
                                              onCilck={() => res.ext && window.open(res.ext)}
                >
                    {res.error}
                </div>
            }
            {
                loaded && res.status && <div
                >
                    {this.renderText(coremailPortal.state, res.data)}
                    <Table ecId={`${this && this.props && this.props.ecId || ''}_Table@548av7`} dataSource={res.data.mail} columns={toJS(coremailPortal.state.columns)} pagination={false}/>
                </div>
            }
        </div>);
    }
}

export default Main;

