import {inject, observer} from "mobx-react";
import {toJS} from "mobx";
import {Button} from "antd";
import {WeaLocaleProvider, WeaRightMenu, WeaTab, WeaTop} from "ecCom";
import {WeaLogView, WeaTableNew} from "comsMobx";
import NoRight from "../../public/NoRight";
import NonStand from "../../public/NonStand";
import * as IntegrationUtil from "../../util/IntegrationUtil";
import EditFormDialog from "./EditFormDialog";

const {WeaTable} = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
const WeaLogViewComp = WeaLogView.Component;

@inject("authenticationSourceStore")
@observer
class TodoClient extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            showSearchAd: false,
            visible: false,
            targetId: "",
            collectParams: {
                favname: `${getLabel(521396, "认证源")}`,
                favouritetype: 1,
                objid: 0,
                link: "wui/engine.html#/integration/authentication_source",
                importantlevel: 1
            }
        };
    }

    componentDidMount() {
        const { authenticationSourceStore } = this.props;
        authenticationSourceStore.doInit();
    }

    componentWillReceiveProps(nextProps) {
        const { authenticationSourceStore } = this.props;
        if (this.props.location.key !== nextProps.location.key) {
            authenticationSourceStore.doInit();
        }
    }

    getAdvancedSearch = () => {
        const {authenticationSourceStore} = this.props;
        const {searchForm, status} = authenticationSourceStore;
        const {condition} = toJS(status);
        return IntegrationUtil.getAdvancedItem(searchForm, condition, this.handleSearchFun, false, this && this.props && this.props.ecId || '', "integrationAuthSource");
    };

    handleSearchFun = () => {
        const { authenticationSourceStore } = this.props;
        authenticationSourceStore.getAuthenticationSourceList();
        this.setState({showSearchAd: false});
    }

    getTabButtonsAd = () => {
        const { authenticationSourceStore } = this.props;
        return IntegrationUtil.getSearchBtns(
            this.handleSearchFun,
            () => authenticationSourceStore.searchForm.reset(),
            () => this.setState({showSearchAd: false}),
            this && this.props && this.props.ecId || '', 
            "integrationAuthSource"
        );
    }

    getRightMenu() {
        const {
            authenticationSourceStore,
        } = this.props, {
            tableStore,
            status,
        } = authenticationSourceStore, {
            rightMenus,
        } = toJS(status);
        let btnArr = rightMenus && rightMenus.map(m => {
            return {
                key: m.type,
                icon: <i className={m.menuIcon} />,
                content: m.menuName,
                onClick: () => this.onRightMenuClick(m.type),
                disabled: m.type==='rb_2' && toJS(tableStore.selectedRowKeys).length === 0
            };
        });
        btnArr = btnArr.concat([{
            key: "BTN_Log",
            icon: <i className="icon-coms-Custom" />,
            content: getLabel('83','日志'),
            onClick: () => this.onRightMenuClick("4")
        }, {
            key: "BTN_COLUMN",
            icon: <i className="icon-coms-Custom" />,
            content: `${getLabel(32535, "显示列定制")}`,
            onClick: () => this.onRightMenuClick("BTN_COLUMN")
        }]);
        return btnArr;
    }

    getColumnsInfo = (data) => {
        return data && data.map(item => {
            if("name_" === item.dbField) {
                item.render = (text, record) => <a onClick={() => this.onRightMenuClick("2", record)}>{text}</a>
            }
            return item;
        })
    }

    onRightMenuClick = (type, record={}) => {
      const { 
        authenticationSourceStore,
     } = this.props, {
        tableStore,
     } = authenticationSourceStore;
      if (type == "rb_1") {
        authenticationSourceStore.getAuthenticationSourceForm();
        authenticationSourceStore.setState({showNewFormDialog: true});
      }else if (type == "rb_2" || type == "3") {
        let title = "3" == type ? getLabel( 15097, '确定要删除吗？') : getLabel(385625, '确定要删除选择的记录吗？');
        let id = "3" == type ? record.randomFieldId : toJS(tableStore.selectedRowKeys);
        IntegrationUtil.doConfirm(title, authenticationSourceStore.getAuthenticationSourceOperation, {operation:'delete', id});
      }else if (type == "BTN_COLUMN") {
        tableStore.setColSetVisible(true);
        tableStore.tableColSet(true);
      }else if (type == "2") {
        authenticationSourceStore.getAuthenticationSourceForm({id: record.randomFieldId});
        authenticationSourceStore.setState({showNewFormDialog: true});
      }else if (type == "4") {
        this.setState({
          visible: true,
          targetId: record.randomFieldId || "",
        })
      }
    };

    getBtns = () => {
        const {
            authenticationSourceStore,
        } = this.props, {
            tableStore,
            status,
        } = authenticationSourceStore, {
            rightMenus,
        } = toJS(status);
        let datas = rightMenus.slice(0, 2);
        return datas && datas.map(m => <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@xm0l1e@integrationAuthSourceButton${m.type}`} type="primary" disabled={m.type === 'rb_2' && toJS(tableStore.selectedRowKeys).length === 0} onClick={() => this.onRightMenuClick(m.type)}>{m.menuName}</Button>);
    };

    onOperatesClick = (record, index, operate) => {
      this.onRightMenuClick(operate.index, record);
    };

    onCloseLogDialog = () => this.setState({visible: false, targetId: ""});

    render() {
        const { 
            authenticationSourceStore, 
        } = this.props, {
            searchForm, 
            tableStore, 
            status,
            hasRight, 
            rightLoading,
            logTable,
        } = authenticationSourceStore, {
            advanceHeight, 
            hasNonStandard, 
            nonStandLoading, 
            selectedKey=0,
            tabDatas=[],
        } = toJS(status), {
            showSearchAd,
            visible,
            targetId,
            collectParams,
        } = this.state;

        return (nonStandLoading
          ? null
          : hasNonStandard
            ? rightLoading 
              ? null
              : hasRight 
                ? <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@0wc24v`}
                    datas={this.getRightMenu()}
                    collectParams={collectParams}
                  >
                    <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@vdy8fo`}
                        title={getLabel(521396, "认证源")}
                        icon={<i className="icon-coms-integration" />}
                        iconBgcolor="#1a57a0"
                        buttons={this.getBtns()}
                        showDropIcon={true}
                        dropMenuDatas={this.getRightMenu()}
                        dropMenuProps={{collectParams}}
                    />
                    <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@yecy9p`}
                        datas={tabDatas}
                        keyParam="viewcondition"
                        selectedKey={selectedKey}
                        searchType={['base','advanced']}
                        advanceHeight={advanceHeight}
                        searchsAd={this.getAdvancedSearch()}
                        showSearchAd={showSearchAd}
                        buttonsAd={this.getTabButtonsAd()}
                        setShowSearchAd={() => this.setState({showSearchAd: !showSearchAd})}
                        hideSearchAd={() => this.setState({showSearchAd: false})}
                        searchsBaseValue={searchForm.getFormParams().name_}
                        onChange={v => authenticationSourceStore.setState({selectedKey: v})}
                        onSearch={this.handleSearchFun}
                        onSearchChange={value => searchForm.updateFields({name_: value})}
                    />
                    <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@jocqij`}
                        comsWeaTableStore={tableStore}
                        hasOrder={true}
                        needScroll={true}
                        onOperatesClick={this.onOperatesClick}
                        getColumns={this.getColumnsInfo}
                    />  
                    <WeaLogViewComp ecId={`${this && this.props && this.props.ecId || ''}_WeaLogViewComp@6ccx3o`} 
                      visible={visible}
                      onCancel={this.onCloseLogDialog}
                      targetId={targetId}
                      logStore={logTable}
                      logSmallType={97}
                    />
                    <EditFormDialog ecId={`${this && this.props && this.props.ecId || ''}_EditFormDialog@2bdtyw`}/>
                  </WeaRightMenu>
                : <NoRight ecId={`${this && this.props && this.props.ecId || ''}_NoRight@srbzf5`}/>
            : <NonStand ecId={`${this && this.props && this.props.ecId || ''}_NonStand@arpy95`} />
        );
    }
}

export default TodoClient;
