import { observable, action, toJS } from "mobx";
import * as API from "../apis/Imrtx";
import { Modal, message } from "antd";
import { WeaTableNew, WeaForm } from "comsMobx";
import { WeaLocaleProvider } from "ecCom";
import {getSearchFormHeight} from '../util/IntegrationUtil';

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

export default class IMrtx {
  @observable hasRight=false;
  @observable rightLoading= true;
  @observable tableBaseInfo = new TableStore(); //基本信息
  @observable searchForm = new WeaForm(); //高级查询的表单
  @observable newBaseForm = new WeaForm(); // 新建表单store
  @observable inputfaceOptions = [];
  @observable inputfaceVal = [];
  @observable interVal = null;

  @observable
  status = {
    selectedKey: 0,
    showSearchAd: false,
    showNewBaseFormDialog: false,
    rowSelectIds: [], //check选择行id
    baseFormFields: [], // 新建表单
    searchFormFields: [],
    initializeResult: {},
    initializeVisible: false,
    showOCSPassWord: false,
    advanceHeight: 0
  };

  constructor() {
    this.interVal = null;
  }

  @action getPassWord = (params={}) => {
    API.getPassWord(params).then(result => {
      if(result.status == "1") {
        this.setState({showOCSPassWord: result.getPwd});
      }
    })
  }

  doInit = () => {
    this.getForm();
    this.searchForm.reset();
    this.getTable();
    this.setState({selectedKey: 0, showSearchAd: false,})
  };

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

  @action getForm = params => {
    this.newBaseForm = new WeaForm();
    API.getForm(params).then(res => {
      if(res.status&&res.status=="false"){
        this.hasRight=false;
      }else{
        this.hasRight=true;
      }
      this.rightLoading = false;
      
      this.status = {
        ...this.status,
        baseFormFields: res.condition
      };
      this.inputfaceOptions=res.inputfaceOptions;
      this.newBaseForm.initFormFields(res.condition);
      if(this.newBaseForm.getFormParams().rtxOrElinkType == 1) {
        this.getPassWord();
      }
    });
  };

  @action getCondition = params => {
    API.getCondition(params).then(res => {
      this.status.searchFormFields = res.condition;
      if(res.condition) {
        this.searchForm = new WeaForm();
        this.searchForm.setCondition(res.condition);
        let advanceHeight = getSearchFormHeight(res.condition);
        this.setState({advanceHeight});
      }
    });
  };

  @action getTable = () => {
    const params = this.searchForm.getFormParams();
    API.getTable(params).then(res => {
      if (res.datas) {
        this.tableBaseInfo.getDatas(res.datas, 1);
      } else {
        res.message && message.warning(res.message);
      }
    });
  };

  @action save = params => {
    this.newBaseForm.validateForm().then(f=>{
      if(f.isValid || params.rtxOrElinkType != 1){
        API.save(params).then(res => {
          if(res&&res.isError&&res.msg) {
            message.error(res.msg);
          }else if (res.msg) {
            message.success(res.msg);
          }
        });
      }else{
        f.showErrors();
      }
    });
  };

  @action closeInitialize () {
    if (this.interVal) {
      clearInterval(this.interVal);
    }
    if (this.status.initializeResult && this.status.initializeResult.flag == "success") {
      this.status.initializeResult = {};
    }
    this.status.initializeVisible = false;
  }

  @action initialize = () => {
    const params = {};
    params.method = 'syn';
    if (this.interVal) {
      clearInterval(this.interVal);
      this.interVal = null;
    }
    let canCall = true;
    this.status.initializeVisible = true;
    this.interVal = setInterval(()=> {
      canCall && API.save(params, ()=> {
        canCall = false;
      }).then(res => {
        if (['success', 'error'].indexOf(res.flag) > -1) {
          if ('success' == res.flag) {
            res.company = 100;
            res.hrm = 100;
          }
          clearInterval(this.interVal);
          canCall = false;
        } else {
          canCall = true;
        }
        this.status.initializeResult = res;
      }, ()=> {
        canCall = true;
      })
    }, 100);
  };
}
