import {observable, action} from "mobx";
import * as API from '../apis/Alymail';
import * as Api from '../apis/login';
import {WeaForm, WeaTableNew, WeaLogView} from "comsMobx";
import {WeaLocaleProvider} from "ecCom";
import {message} from "antd";
import {
  checkNonstandard,
} from "../util/IntegrationUtil.js";

const {getLabel} = WeaLocaleProvider;
const {TableStore} = WeaTableNew;
const {LogStore} = WeaLogView;

export default class AlyMail {
  @observable form = new WeaForm();
  @observable searchForm = new WeaForm();
  @observable searchForm1 = new WeaForm();
  @observable mailForm = new WeaForm();
  @observable logSettingForm = new WeaForm();
  @observable tableStore = new TableStore();
  @observable logStore = new LogStore();
  @observable status = {
    condition: [],
    searchCondition: [],
    nonStandLoading: true,
    hasNonStandard: false,
  };

  @action setState = (params={}) => {
    let status = {...this.status};
    Object.keys(params).forEach(key => {
      status[key] = params[key];
    })
    this.status = status;
  }

  @action cleanStore = () => {
    this.form = new WeaForm();
    this.mailForm = new WeaForm();
    this.logSettingForm = new WeaForm();
    this.searchForm = new WeaForm();
    this.tableStore = new TableStore();
    this.setState({
      logSettingCondition: [],
      condition: [],
      searchCondition: [],
      searchCondition1: [],
      mailFormCondition: [],
      loading: false,
      buttonLoading: false,
      buttons: [],
      baseDialogVisible: false,
      operateIndex: "",
      formLoading: false,
      buttonDisabled: false,
      mailFormLoading: false,
      hasRight: true,
      hasRightLoading: true,
    })
  }

  @action closeDialog = () => {
    this.form = new WeaForm();
    this.mailForm = new WeaForm();
    this.setState({
      mailFormCondition: [],
      condition: [],
      buttonDisabled: false,
      formLoading: false,
      baseDialogVisible: false,
    });
  }

  @action doInit = () => {
    this.setState({
      nonStandLoading: true,
    })
    checkNonstandard({num: "134"}, (result) => {
      this.setState({
        hasNonStandard: result.isOpen,
        nonStandLoading: false,
      })
      if(result.isOpen) {
        this.cleanStore();
        this.getMailCondition(this.getMailList);
      }
    })
  }

  /**
   * getForm中需要进行权限的判断
   */
  @action getForm = () => {
    this.setState({
      loading: true, 
      buttonLoading: true
    });
    API.getForm().then(result => {
      if(result.status == "1") {
        this.form = new WeaForm();
        this.form.initFormFields(result.condition);
        this.setState({condition: result.condition || []});
      }
      this.setState({
        loading: false,
        buttonLoading: false
      })
    })
  }
  /**
   * 基础信息保存时提示信息后端做
   */
  @action getOperate = (params) => {
    this.setState({buttonDisabled: true});
    API.getOperate(params).then(result => {
      this.setState({buttonDisabled: false});
    })
  }
  /**
   * 获取日志页面的信息
   */
  @action getLogInfo = () => {
    this.setState({buttonLoading: true});
    this.tableStore = new TableStore();
    this.searchForm = new WeaForm();
    Promise.all([
      API.getLogMenus(),
      API.getLogCondition(),
      API.getLogDatas()
    ]).then(([result1, result2, result3]) => {
      if(result1.status == 1 && result2.status == 1 && result3.status == 1) {
        this.tableStore.getDatas(result3.sessionkey, 1);
        this.searchForm.initFormFields(result2.condition);
        this.setState({
          buttons: result1.rightMenus || [],
          searchCondition: result2.condition || []  
        });
      }
      this.setState({buttonLoading: false});
    })
  }

  @action getLogDatas = (params) => {
    API.getLogDatas(params).then(result => {
      if(result.status == "1") {
        this.tableStore.getDatas(result.sessionkey, 1);
      }
    })
  }

  // 基本信息页面重新构造
  @action getMailList = (params={}) => {
    this.tableStore = new TableStore();
    params = Object.assign({}, this.searchForm1.getFormParams(), params);
    API.getMailList(params).then(result => {
      if(result.status == '1') {
        this.tableStore.getDatas(result.sessionkey, 1);
      }
    })
  }

  @action getMailCondition = (callBack=null, type) => {
    this.setState({buttonLoading: true});
    this.searchForm1 = new WeaForm();
    API.getMailListConditions().then(result => {
      if(result.status == '1') {
        if(result.ret === "noright") {
          this.setState({hasRight: false});
        } else {
          this.searchForm1.initFormFields(result.condition);
          this.setState({
            searchCondition1: result.condition || [],
            hasRight: true,
          });
          callBack && typeof(callBack) === "function" && callBack();
        }
      }
      this.setState({buttonLoading: false, hasRightLoading: false});
    })
  }

  @action getMailAddSetForm = (params={}, saveCallBack=null) => {
    let callBack = params.mailMasterId ? "getMailEditSetForm" : "getMailAddSetForm";
    this.form = new WeaForm();
    this.setState({formLoading: true});
    API[callBack](params).then(result => {
      if(result.status == "1") {
        this.form.initFormFields(result.condition);
        this.setState({condition: result.condition});
        if(saveCallBack && typeof(saveCallBack) === "function") {
          saveCallBack();
        }
      }
      this.setState({formLoading: false});
    })
  }

  @action getMailSaveOperation = (params={}, saveCallback) => {
    this.setState({buttonDisabled: true});
    params = Object.assign({}, this.form.getFormParams(), params);
    if(!params.mailMasterId) {
      params = Object.assign({}, params, this.mailForm.getFormParams());
    }
    let callBack = "getMailOperation";
    if(params.operate === "save") {
      callBack = "getMailSaveOperation";
      delete params.operate;
    }
    API[callBack](params).then(result => {
      this.setState({buttonDisabled: false});
      if(result.status == 1) {
        if(!['init', 'test'].includes(params.operate)) {
          if(saveCallback && typeof(saveCallback) === "function") {
            saveCallback(result);
          } else {
            this.setState({baseDialogVisible: false});
          }
        }
        if(!saveCallback) {
          if(result.msg) {
            if(result.isError === "true") {
              message.error(result.msg);
            } else {
              message.success(result.msg);
            }
          }
        }
        if(result.errmsgTip) {
          message.error(result.errmsgTip);
        }
        this.getMailList();
      }
    })
  }

  @action getMailDeleteOperation = (params={}) => {
    this.setState({buttonLoading: true});
    API.getMailDeleteOperation(params).then(result => {
      this.setState({buttonLoading: false});
      if(result.status == 1) {
        message.success(result.msg);
        this.getMailList();
      }
    })
  }

  // 切换下拉框，局部刷新
  @action getMailSelectSetForm = (params={}) => {
    this.setState({mailFormLoading: true});
    this.mailForm = new WeaForm();
    API.getMailSelectSetForm(params).then(result => {
      if(result.status == '1') {
        this.mailForm.initFormFields(result.condition);
        this.setState({mailFormCondition: result.condition});
      }
      this.setState({mailFormLoading: false});
    })
  }

  //生成秘钥
  @action createSecretKey = (params={}) => {
    Api.createSecretKey(params).then(result => {
      if (result.success) {
        if(this.status.operateIndex) {
          this.form.updateFields({
            email163_pubkey: result.pubKey || "",
            email163_key: result.priKey
          })
        }else {
          this.mailForm.updateFields({
            email163_pubkey: result.pubKey || "",
            email163_key: result.priKey
          })
        }
      }
    })
  }

  @action getLogSettingFields = () => {
    this.setState({buttonLoading: true});
    this.logSettingForm = new WeaForm();
    API.getLogSettingFields().then(result => {
      if(result.status == 1) {
        this.logSettingForm.initFormFields(result.condition);
        this.setState({
          logSettingCondition: result.condition || [], 
        });
      }
      this.setState({buttonLoading: false});
    });
  }

  @action saveLogSetting = (params) => {
    this.setState({buttonLoading: true});
    API.saveLogSetting(params).then(result => {
      if(result.status == 1) {
        message.success(getLabel('384521','设置成功'));
      }
      this.setState({buttonLoading: false});
      this.getLogSettingFields();
    })
  }
}