import { observable, action, toJS } from "mobx";
import * as API from "../apis/TencentMailService";

import { Modal, message } from "antd";
import { WeaTableNew, WeaForm } from "comsMobx";

const { TableStore } = WeaTableNew;

import { WeaLocaleProvider } from "ecCom";
import isFunction from 'lodash/isFunction';
import {
  getSearchFormHeight,
  checkNonstandard
} from '../util/IntegrationUtil';

const getLabel = WeaLocaleProvider.getLabel;

export class TencentMailStore {
    @observable hasRight=false;
    @observable tableStore = new TableStore();
    @observable tableStoreLog = new TableStore();
    @observable searchForm = new WeaForm();
    @observable searchFormLog = new WeaForm();
    @observable newForm = new WeaForm();
    @observable baseSetForm = new WeaForm();
    @observable rightLoading = true;
    @observable reloadFlowInfo = false;
    @observable timer = '';
    @observable showSyncParam='';


    @observable
    status = {
        selectedKey: 0,
        BaseSetFormFields:[],
        loading: false,
        isAlertPage: false,
        showSearchAd: false,
        rightMenus: [],
        rightMenusLog: [],
        rowSelectIds: [],
        rowSelectIdsLog: [],
        showNewFormDialog: false,
        tencentMailList: ''

    };
    settingRegisterID = '';

    @action
    setReloadFlowInfo = (val) => {
        this.reloadFlowInfo = val;
    }

    @action cleanStore = () => {
      this.searchForm = new WeaForm();
      this.newForm = new WeaForm();
      this.baseSetForm = new WeaForm();
      this.setState({
        selectedKey: 0,
        TriggerConditionInfo: [],
        TencentMailConditionInfoLog: [],
        searchAdName: '',
        searchAdTarget: '',

        BaseSetFormFields:[],
        isAlertPage: false,
        showSearchAd: false,
        rightMenus: [],
        rightMenusLog: [],
        rowSelectIds: [],
        rowSelectIdsLog: [],
        showNewFormDialog: false,
        loading: false,
        tencentMailList: '',
        advanceHeight: 0
      })
    }

    @action
    doInit = () => {
      this.setState({
        nonStandLoading: true
      })
      checkNonstandard({num: "121"}, (result) => {
        this.setState({
          hasNonStandard: result.isOpen,
          nonStandLoading: false
        })
        if(result.isOpen) {
          this.cleanStore();
          this.getTencentMailCondition();
          this.getTencentMailRightMenu();
          // this.getTencentMailList();
          this.getTencentMailAddForm();
          this.getBaseSetForm();
          this.getTencentMailLogList();
          this.getTencentMailLogCondition();
          this.getTencentMailLogRightMenu();
        }
      })
    };

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

  getTencentMailLogList = () => {
    let params = (this.searchFormLog && this.searchFormLog.getFormParams()) || {};
    API.getTencentMailLogList(params).then(result => {
      if (result.sessionkey) {
        this.tableStoreLog.getDatas(result.sessionkey,1);
      } else {
        this.status = { ...this.status, isAlertPage: true };
      }
    });
  };

  getTencentMailLogRightMenu = (params) => {
    API.getTencentMailLogRightMenu(params).then(result => {
      this.status = { ...this.status, rightMenusLog: result.rightMenus };
    });
  };

  getTencentMailLogCondition = (params) => {
    API.getTencentMailLogCondition(params).then(result => {
      if(result.condition) {
        this.searchFormLog.initFormFields(result.condition);
        let advanceHeight = getSearchFormHeight(result.condition);
        this.setState({advanceHeight});
     }
      this.status = {
        ...this.status,
        TencentMailConditionInfoLog: result.condition
      };
    });
  };

    // getTencentMailList = () => {
    //     let params = (this.searchForm && this.searchForm.getFormParams()) || {};
    //     API.getTencentMailList(params).then(result => {
    //         if(result.ret&&result.ret=="noright"){
    //             this.hasRight=false;
    //         }else{
    //             this.hasRight=true;
    //         }
    //         this.rightLoading = false;
    //         if (result.sessionkey) {
    //             this.tableStore.getDatas(result.sessionkey,1);
    //         } else {
    //             this.status = { ...this.status, isAlertPage: true };
    //         }
    //     });
    // };

    getTencentMailRightMenu = (params) => {
        API.getTencentMailRightMenu(params).then(result => {
            this.status = { ...this.status, rightMenus: result.rightMenus };
        });
    };

    getTencentMailCondition = (params) => {
        /*API.getTencentMailCondition(params).then(result => {
            this.searchForm.initFormFields(result.condition);
            this.status = {
                ...this.status,
                TriggerConditionInfo: result.condition
            };
        });*/
    };

    setSetRettingRegisterID = (val) => {
        this.settingRegisterID = val;
    }

    getTencentMailAddForm = (params) => {
    };


  getTencentMailOperation = (params, callback) => {
    if(params.operate === 'init') {
      this.setState({loading: true, tip: getLabel('501324', "正在初始化请稍后")});
    }
    if(params.operate === 'removeAll') {
      this.setState({loading: true, tip: getLabel('501406','正在清除数据请稍后')});
    }
    API.getTencentMailOperation(params).then(result => {
      if (params.operate == "deleteLog") {
        this.getTencentMailLogList();
        this.setState({showNewFormDialog: false, rowSelectIdsLog: []});
      } else if (params.operate != 'test' && params.operate != 'checkShowName') {
        (params.operate === 'init' || params.operate === 'removeAll') && this.setState({loading: false});
        // this.getTencentMailList();
        this.setState({showNewFormDialog: false, rowSelectIds: []});
      }
      if (!callback) {
        if (result.errmsgTip) {
          if(result.isError) {
            message.error(result.errmsgTip);
          } else {
            message.success(result.errmsgTip);
          }
        }
      }

      isFunction(callback) && callback(result);
    });
  };

    getBaseSetForm = (params) => {
        API.getBaseSetForm(params).then(result => {
          this.rightLoading = false;
          if(result.status) {
            if(result.errmsg === "noright") {
              this.hasRight=false;
            }else {
              this.hasRight=true;
              this.status = { ...this.status, BaseSetFormFields: result.condition };
              this.baseSetForm = new WeaForm();
              this.baseSetForm.initFormFields(result.condition);
              result.condition[0].items.map(item=>{
                if(item.domkey[0]=='issync'){
                    if(item.value=='0'){
                        this.showSyncParam='none';
                    }
                }
              })
            }
          }
        });
    };

//控制字段的显阴
    changeShowOrHiden=(switchValue)=>{
        if(switchValue=='0'){
            this.showSyncParam='none';
        }else{
            this.showSyncParam='';
        }
    }

    //确认
    confirmModal = (content, func, val) => {
        //确认信息， 方法， 方法参数
        Modal.confirm({
            title: `${getLabel(131329,'信息确认')}`,
            content: content,
            okText: `${getLabel(16631,'确定')}`,
            cancelText:`${getLabel(201,'取消')}`,
            onOk: () => func(val),
        })
    };
}
