import React, { Component } from 'react';
import {toJS} from "mobx";
import {observer, inject} from "mobx-react";
import {WeaLocaleProvider} from "ecCom";
import {WeaForm, WeaLogView} from 'comsMobx';
import Header from './Header';
import {onCheck, doConfirm} from '../../util/IntegrationUtil';
import AddOrEdit from './AddOrEdit';
import NoRight from "../../public/NoRight";
import NonStand from "../../public/NonStand";

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

@inject("alyMail")
@observer
export default class alymail extends Component {
  constructor(props) {
    super(props);
    this.state = {
      targetId: "",
      visible: false,
    }
  }

  componentDidMount() {
    this.props.alyMail.doInit();
  }

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

  handleChangeTab = (selectedKey) => {
    const {alyMail} = this.props;
    const {getMailCondition, getLogInfo, getMailList} = alyMail;

    if(selectedKey == 3) {
      alyMail.getLogSettingFields();
    }else if(selectedKey == 2) {
      getLogInfo();
    }else if(selectedKey == 0) {
      getMailCondition(getMailList);
    }
  }

  onSearch = (selectedKey) => {
    const {alyMail} = this.props;
    const {searchForm, getLogDatas, getMailList, searchForm1} = alyMail;

    if(selectedKey == 2) {
      getLogDatas(searchForm.getFormParams());
    }else if(selectedKey == 0) {
      getMailList(searchForm1.getFormParams());
    }
  }

  onMenuClick = (selectedKey, type="", record=null) => {
    const {alyMail} = this.props;
    const {tableStore, getMailAddSetForm, getMailSaveOperation, getMailSelectSetForm, getMailDeleteOperation, logSettingForm, saveLogSetting} = alyMail;

    if(type.toLocaleUpperCase() === "BTN_COLUMN") {
      tableStore.setColSetVisible(true);
      tableStore.tableColSet(true);
    }else {
      if(selectedKey == "0") {
        let ids = record ? record.randomField0 : toJS(tableStore.selectedRowKeys);
        if(type === "BTN_NEW") {
          alyMail.setState({
            baseDialogVisible: true,
            operateIndex: "",
            formLoading: false, 
            buttonDisabled: false,
          });
          alyMail.mailForm = new WeaForm();
          getMailAddSetForm({mailtype: 1});
          getMailSelectSetForm({mailtype: 1});
        }else if(["BTN_DELETE", "1"].includes(type)) {
          let content = getLabel(385625, '确定要删除选择的记录吗？');
          if(record) {
            content = getLabel(15097, '确定要删除吗？');
          }
          doConfirm(content, getMailDeleteOperation, {operate: "delete", mailMasterIds: ids});
        }else if(["BTN_EDIT", "0"].includes(type)) {
          alyMail.setState({
            baseDialogVisible: true,
            operateIndex: ids,
            formLoading: false, 
            buttonDisabled: false,
          });
          getMailAddSetForm({mailMasterId: ids});
        }else if(type === "2") {
          getMailSaveOperation({operate: "test", mailMasterId: ids});
        }else if(type === "3") {
          let content = getLabel('519656','确定要初始化吗？');
          doConfirm(content, getMailSaveOperation, {operate: "init", mailMasterId: ids});
        }else if(type === "BTN_LOG" || type == "4") {
          this.setState({
            visible: true,
            targetId: record ? record.randomField0 : "",
          });
        }
      } else if(selectedKey == "3") {
        if(type === "BTN_INTERVAL_SAVE") {
          onCheck(logSettingForm, () => {
            const formParams = logSettingForm.getFormParams();
            try{
              if(formParams.cleanType == "4") {
                if(formParams.cleanCustomValue === undefined) {
                  logSettingForm.showError("cleanType", getLabel('516269','"日志记录" 未填写'));
                }else {
                  saveLogSetting({
                    ...formParams,
                  });
                }
              }else {
                saveLogSetting({
                  ...formParams,
                });
              }
            }catch(err) {}
          })
        }
      }
    }
  }

  closeLogDialog = () => this.setState({visible: false, targetId: ""});
  
  render() {
    const {alyMail} = this.props;
    const {logSettingForm, form, tableStore, searchForm, logStore, searchForm1, status: {
      hasNonStandard, 
      nonStandLoading, 
      logSettingCondition, 
      buttonDisabled,
      buttonLoading, 
      searchCondition, 
      buttons, 
      searchCondition1, 
      hasRightLoading, 
      hasRight,
      baseDialogVisible,
    }} = alyMail;
    const {visible, targetId} = this.state;
    return (nonStandLoading
      ? null
      : hasNonStandard
        ? hasRightLoading
          ? null
          : hasRight
            ? <div style={{height: "100%"}}>
                <Header ecId={`${this && this.props && this.props.ecId || ''}_Header@p2amlc`}
                  showDialog={baseDialogVisible}
                  initLoading={buttonDisabled}
                  title={getLabel('517408','邮箱集成')}
                  typename={12}
                  buttonLoading={buttonLoading}
                  form={logSettingForm}
                  condition={toJS(logSettingCondition)}
                  searchForm={searchForm}
                  searchForm1={searchForm1}
                  searchCondition1={toJS(searchCondition1)}
                  searchCondition={toJS(searchCondition)}
                  addOutter={form.getFormParams().isuse}
                  rightMenus={buttons}
                  searchKey={"syscode"}
                  collectParams={{
                      favname: getLabel('517408','邮箱集成'),
                      favouritetype: 1,
                      objid: 0,
                      link: "wui/engine.html#/integration/alymail",
                      importantlevel: 1
                  }}
                  tableStore={tableStore}
                  onSearch={this.onSearch}
                  onMenuClick={this.onMenuClick}
                  onChange={v => this.handleChangeTab(v)}
                />
                <AddOrEdit ecId={`${this && this.props && this.props.ecId || ''}_AddOrEdit@8ocs46`}/>
                <WeaLogViewComp ecId={`${this && this.props && this.props.ecId || ''}_WeaLogViewComp@f961k4`} 
                  visible={visible} 
                  onCancel={this.closeLogDialog} 
                  logStore={logStore} 
                  logType="15" 
                  logSmallType="99" 
                  targetId={targetId} />
              </div>
            : <NoRight ecId={`${this && this.props && this.props.ecId || ''}_NoRight@9u6bqq`} />
        : <NonStand ecId={`${this && this.props && this.props.ecId || ''}_NonStand@66pp7q`} />
    )
  }
}
