import {
    inject,
    observer
} from 'mobx-react'
import {
    WeaTableNew,
    WeaLogView
} from 'comsMobx'
import MainDialog from './MainDialog';
import SubDialog from './SubDialog';
import NoRight from "../../public/NoRight";

const WeaTable = WeaTableNew.WeaTable;
const WeaLogViewComp = WeaLogView.Component;

@inject('intelogin')
@observer
export default class MainTable extends React.Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        let typename = "";
        switch(this.props.intelogin.source) {
          case 'tencentmail':
            typename = '6';
            break;
          case 'coremail':
            typename = '8';
            break;
          case 'NC':
            typename = '1';
            break;
          case 'EAS':
            typename = '2';
            break;
          case 'U8':
            typename = '3';
            break;
          case 'K3':
            typename = '4';
            break;
        }
        if(this.props.typename) {
            typename = this.props.typename;
            this.props.intelogin.getMenus('0');
        }
        let params = {typename};
        if(this.props.type) {
          const {
            intelogin,
          } = this.props;
          // 从mail邮箱设置中过来的集成登录入口
          intelogin.getMenus('0');
          // 获取高级搜索
          intelogin.getSearchCondition('0');  
          params = {type: this.props.type};
        }
        this.props.intelogin.getList(params);
    }
    
    reRenderColumns = (columns) => {
        const {
            intelogin
        } = this.props, {
            openDialog,
            setIsSysAlgorithm,
        } = intelogin;

        columns.map(c => {
            if (c.dataIndex == 'sysid') {
                c.render = (t, r) => {
                    const {
                        sysid,
                        sysidspan,
                        id
                    } = r;

                    return <a onClick={() => openDialog({ type: '0', id: sysid, Id: id })}>{r.sysidspan}</a>
                }
            }
            if (c.dataIndex == 'encryptname') {
                c.render = (t, r) => {
                    const {
                        randomFieldId,
                        encryptnamespan
                    } = r;

                    return <a onClick={() => {setIsSysAlgorithm({isSysAlgorithm: parseInt(randomFieldId) < 0 ? true : false} );openDialog({type:'0',id:randomFieldId})}}>{encryptnamespan}</a>
                }
            }
            if(c.dataIndex == 'fullName') {
                c.render = (t, r) => {
                    const {
                        randomFieldId,
                        fullNamespan
                    } = r;
                    return <a onClick={() => openDialog({type:'0', id: randomFieldId})}>{fullNamespan}</a>
                }
            }
        })
    }

    onTableOperatesClick = (record, rowIndex, operate) => this.props.intelogin.onOperatesClick(record, rowIndex, operate, {type: this.props.type});

    render() {
        const {
            intelogin,
        } = this.props, {
            mainTable,
            mainTab,
            onOperatesClick,
            logView,
            closeLogView,
            mailTypeAuthorized,
            mailTypeAuthorizedLoading,
        } = intelogin, {
            tableStore
        } = mainTable, {
            visible,
            logStore,
            params
        } = logView, {
            id
        } = mainTable, {
            key
        } = mainTab;

        return ("mail" === this.props.type
            ?   mailTypeAuthorizedLoading
                  ? null
                  : mailTypeAuthorized
                    ?   <div>
                            <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@8b3pa5`}
                                comsWeaTableStore={tableStore}
                                hasOrder={true}
                                needScroll={true}
                                getColumns={this.reRenderColumns}
                                onOperatesClick={this.onTableOperatesClick}
                            />
                            <MainDialog ecId={`${this && this.props && this.props.ecId || ''}_MainDialog@3hxqh1`} type={this.props.type} typename={this.props.typename} />
                            <SubDialog ecId={`${this && this.props && this.props.ecId || ''}_SubDialog@84d1mi`} />
                            <WeaLogViewComp ecId={`${this && this.props && this.props.ecId || ''}_WeaLogViewComp@yw4o52`}
                                params={(this.props.typename && (id == "")) ? params : null}
                                visible={visible}
                                onCancel={closeLogView}
                                logStore={logStore}
                                logType={15}
                            logSmallType={this.props.type === "mail" ? 10 : key == 2 ? 11 : 10}
                                targetId={id}
                            />
                        </div>
                    :   <div style={{height: "calc(100% - 100px)"}}>
                            <NoRight ecId={`${this && this.props && this.props.ecId || ''}_NoRight@2iqqyh`} />
                        </div>
            :   <div>
                    <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@wzm7xr`}
                        comsWeaTableStore={tableStore}
                        hasOrder={true}
                        needScroll={true}
                        getColumns={columns => this.reRenderColumns(columns)}
                        onOperatesClick={this.onTableOperatesClick}
                    />
                    <MainDialog ecId={`${this && this.props && this.props.ecId || ''}_MainDialog@6b87ut`} type={this.props.type} typename={this.props.typename} />
                    <SubDialog ecId={`${this && this.props && this.props.ecId || ''}_SubDialog@2mxvl4`} />
                    <WeaLogViewComp ecId={`${this && this.props && this.props.ecId || ''}_WeaLogViewComp@ysewwf`}
                        params={(this.props.typename && (id == "")) ? params : null}
                        visible={visible}
                        onCancel={closeLogView}
                        logStore={logStore}
                        logType={15}
                    logSmallType={this.props.type === "mail" ? 10 : key == 2 ? 11 : 10}
                        targetId={id}
                    />
                </div>
        )
    }
}
