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

const { WeaTable } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;

@inject("todoClientStore")
@observer
class TodoClient extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            selectedKey: 0
        };
    }

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

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

     /**
     * 获取高级查询
     * @returns {Array}
     */
    getAdvancedSearch = () => {
        const { todoClientStore } = this.props;
        const { searchForm,  status } = todoClientStore;
        const { selectedKey, condition} = toJS(status);
        let form1 = [];
        let condition1 = [];
        if (selectedKey == 0) {
          form1 = searchForm;
          condition1 = condition;
        }
        return IntegrationUtil.getAdvancedItem(form1, condition1, this.handleSearchFun, false, this && this.props && this.props.ecId || '', "integrationTodoCenterEditDetail");
    };

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

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

    /**
     * 获取右键菜单
     * @returns {Array}
     */
    getRightMenu() {
        const { todoClientStore } = this.props;
        const { selectedKey, rightMenus,rightMenus2,rowSelectIds} = toJS(todoClientStore.status);
        let btnArr = [];
        rightMenus&&rightMenus.map(m => {
            let btn= {
                key: m.type,
                icon: <i className={m.menuIcon} />,
                content: m.menuName,
                onClick: () => this.onRightMenuClick(m.type),
                disabled:(m.menuIcon==='icon-coms-delete' && rowSelectIds.length === 0)
            } ;
            btnArr.push(btn);

        });
        //显示列定制
        btnArr.push({
            key: "BTN_COLUMN",
            icon: <i className="icon-coms-Custom" />,
            content: `${getLabel(32535, "显示列定制")}`,
            onClick: () => this.onRightMenuClick("BTN_COLUMN")
        });



        return btnArr;
    }

  getColumnsInfo = (data) => {
    const that = this;
    // console.log(data);
    if (!isEmpty(data)) {
      for (let index = 0; index < data.length; index++) {
        if (data[index].dbField == "syscode") {
          data[index].render = (text, record, index) => {
            // console.log(record);
            record.sysid=record.randomFieldId;
            return (<a onClick={that.doEditDetailById.bind(that, record)}>{text}</a>);
          }
        }
      }
    }
  }


    /**
     * 右键菜单点击事件
     * @param type
     */
    onRightMenuClick = (type, recordId) => {
      const { todoClientStore } = this.props;
      const { newForm } = todoClientStore;
      const {selectedKey, rowSelectIds} = toJS(todoClientStore.status);
      // console.log('type:',type);
      if (type == "rb_1") {
        let params = {};
        todoClientStore.getOfsSendForm(params);
        todoClientStore.setState({showNewFormDialog:true});
      }
      if (type == "rb_2") {
        //删除
        if(rowSelectIds.length>0) {
          todoClientStore.confirmModal(`${getLabel(385625, '确定要删除选择的记录吗？')}`, todoClientStore.getOfsInfoOperation, {operation:'delete',id: rowSelectIds});
        }
      }

      if (type == "BTN_COLUMN") {
        //显示列定制
        todoClientStore.tableStore.setColSetVisible(true);
        todoClientStore.tableStore.tableColSet(true);
      }
    };

    /**
     * 获取顶部按钮
     * @returns {Array}
     */
    getBtns = () => {
        const { todoClientStore } = this.props;
        const { selectedKey,rightMenus,rightMenus2,  rowSelectIds} = toJS(todoClientStore.status);

        let btnArr = [];
        if(selectedKey==0) {
            if(!rightMenus){return btnArr}
            for(let j = 0,len=rightMenus.length; j < len; j++) {
                if(j==2){break }
                let m = rightMenus[j];
                let btn= <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@56qn65`} type="primary" disabled={ m.menuIcon==='icon-coms-delete' && rowSelectIds.length === 0}  onClick={() => {{this.onRightMenuClick(m.type);}}}>{`${m.menuName}`}</Button> ;
                btnArr.push(btn);
            }
        }

        return btnArr;
    };

    /**
     * 数据列表行数据的操作项
     * @param record
     * @param index
     * @param operate
     * @param flag
     */
    onOperatesClick = (record, index, operate, flag) => {
      const { todoClientStore } = this.props;
      const { status, setsysId } = todoClientStore;
      const { selectedKey } = toJS(todoClientStore.status);
      // console.log(record, index, operate);
      if (operate.index == "0") {
        //流程白名单
        let params = {sysid: record.randomFieldId};
        todoClientStore.getOfsSendWhiteWorkflowList(params);
        todoClientStore.setState({WhiteWorkflowDialogVisible:true});
      }
      else if (operate.index == "1") {
        //人员白名单
        let params = {sysid: record.randomFieldId};
        todoClientStore.getOfsSendWhiteHrmList(params);
        todoClientStore.setState({WhiteHrmDialogVisible:true});
      }
      else if (operate.index == "2") {
        this.doEditDetailById({sysid: record.randomFieldId});
      }
      else if (operate.index == "3") {
        todoClientStore.confirmModal(`${getLabel( 15097, '确定要删除吗？')}`, todoClientStore.getOfsSendOperation , {operate:'delete',sysid: record.randomFieldId});
    } else if (operate.index == '4') {
      status.WorkflowdataDialogVisible = !(status.WorkflowdataDialogVisible);
      setsysId(record.sysid);
      todoClientStore.getOfsSendWorkflowdataFrom();
      }
    };

  doEditDetailById(params){
    const { todoClientStore } = this.props;
    todoClientStore.getOfsSendForm(params);
    todoClientStore.setState({showNewFormDialog:true});
  }

  getRowSelection = () => {
    return {onChange: selectedRowKeys => this.props.todoClientStore.setState({rowSelectIds: selectedRowKeys})}
  }

    render() {
        const { todoClientStore } = this.props;
        const {searchForm, tableStore, status,hasRight, rightLoading} = todoClientStore;
        const {showSearchAd, advanceHeight, hasNonStandard, nonStandLoading} = toJS(status);

        const topTab = [
        ];
        return (nonStandLoading
          ? null
          : hasNonStandard
            ? rightLoading 
              ? null
              : hasRight 
                ? <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@0txa2e`}
                    datas={this.getRightMenu()}
                    collectParams={{
                        favname: `${getLabel(382188, "统一待办推送设置")}`,
                        favouritetype: 1,
                        objid: 0,
                        link: "wui/engine.html#/integration/todoclient",
                        importantlevel: 1
                    }}
                  >
                    <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@ubf0k7`}
                        title={getLabel(382188, "统一待办推送设置")}
                        icon={<i className="icon-coms-integration" />}
                        iconBgcolor="#1a57a0"
                        buttons={this.getBtns()}
                        showDropIcon={true}
                        dropMenuDatas={this.getRightMenu()}
                        dropMenuProps={{
                            collectParams: {
                                favname: `${getLabel(382188, "统一待办推送设置")}`,
                                favouritetype: 1,
                                objid: 0,
                                link: "wui/engine.html#/integration/todoclient",
                                importantlevel: 1
                            }
                        }}
                    >
                        <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@cf0wxh`}
                            datas={topTab}
                            keyParam="viewcondition" //主键
                            countParam="groupid" //数量
                            selectedKey={this.state.selectedKey}
                            searchType={ ['base','advanced']}
                            advanceHeight={advanceHeight}
                            searchsAd={this.getAdvancedSearch()}
                            showSearchAd={showSearchAd}
                            buttonsAd={this.getTabButtonsAd()}
                            setShowSearchAd={() => todoClientStore.setState({ showSearchAd: !showSearchAd })}
                            hideSearchAd={() => todoClientStore.setState({ showSearchAd: false })}
                            searchsBaseValue={
                                searchForm.getFormParams().sysdesc
                            }
                            onChange={v => {this.setState({ selectedKey: v });todoClientStore.setState({ selectedKey: v });searchForm.reset() ;}}
                            onSearch={this.handleSearchFun}
                            onSearchChange={value => {
                                searchForm.updateFields({sysdesc: value});
                            }}
                        />
                        <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@jwhnwd`}
                            comsWeaTableStore={tableStore}
                            hasOrder={true}
                            needScroll={true}
                            onOperatesClick={this.onOperatesClick}
                            // uniqueId={true}
                            rowSelection={this.getRowSelection()}
                            getColumns={this.getColumnsInfo}
                        />     
                    </WeaTop>
                    <EditFormDialog ecId={`${this && this.props && this.props.ecId || ''}_EditFormDialog@nn5s23`}/>
                    <WhiteWorkflowDialog ecId={`${this && this.props && this.props.ecId || ''}_WhiteWorkflowDialog@2f2oo1`} />
                    <WhiteHrmDialog ecId={`${this && this.props && this.props.ecId || ''}_WhiteHrmDialog@nynab3`} />
                    <WorkflowdataDialog ecId={`${this && this.props && this.props.ecId || ''}_WorkflowdataDialog@6tlyr9`} />
                  </WeaRightMenu>
                : <NoRight ecId={`${this && this.props && this.props.ecId || ''}_NoRight@ssajmc`}/>
            : <NonStand ecId={`${this && this.props && this.props.ecId || ''}_NonStand@g2zsw8`} />
        );
    }
}

export default TodoClient;
