import { observable, action } from 'mobx';
import {WeaForm} from 'comsMobx';
import * as API from '../apis/importorg';
import * as Util from '../util/index';
import {validate,getFormParamValue} from '../util'
import {message,Modal, Button} from 'antd';
import {WeaTableNew} from 'comsMobx'
import * as mobx from 'mobx';
import isEmpty from 'lodash/isEmpty';
const toJS = mobx.toJS;
const {TableStore} = WeaTableNew;
const confirm = Modal.confirm;

export class HrmImportOrg {
  title = "组织结构导入";
  titleResult = "导入结果";
  titleHistory = "历史导入记录";
  titleImportLog = "导入日志";
  templetName = "组织结构导入模板";
  importResultColumns = [];
  index = 1;
  @observable visible = false;
  @observable visibleResult = false;
  @observable visibleHistory = false;
  @observable visibleImportLog = false;
  @observable condition = [];
  @observable importtype = 'add';
  @observable filelist = [];
  @observable excelfile = '';
  @observable searchCondition = [];
  @observable form = new WeaForm();
  @observable showSearchAd=false;
  @observable searchParamsAd={};
  @observable importResultStore = new TableStore();
  @observable importHistoryStore = new TableStore();
  @observable importLogStore = new TableStore();
  @observable buttons = [];
  @observable importProcessLogDatas = [];
  @observable succnum = 0;
  @observable failnum = 0;
  @observable importResultTip= '';
  @observable importStatus = '';
  @observable pId = '';
  @observable ref;


  importResultColumns = [{
      "title": "行",
      "width": "10%",
      "dataIndex": "rownum"
      },{
      "title": "状态",
      "width": "30%",
      "dataIndex": "status"
      },
      {
      "title": "提示信息",
      "width": "60%",
      "dataIndex": "message"
      }];

  @action
  getImportForm(params={}){
    API.getImportForm(params).then(data=>{
      if (data.status == '1') {
         this.condition = data.condition;
      } else {
          message.warning(data.message);
      }
    }, error=> {
        // message.warning(error.message);
    })
  }

  saveImport(params={}){
    params.importtype = 'company';
    params.excelfile = this.excelfile || '';
    this.importStatus = '';
    if(params.excelfile == ''){
      message.warning("请选择导入模板");
      return;
    }
    API.saveImport(params).then(data=>{
      if (data.status == '1') {
        this.visibleResult=true;
        this.getImportProcess();
      } else {
        message.warning(data.message);
      }
    }, error=> {
        message.warning(error.message);
    })
  }

  getImportProcess(){
    window.setInterval(()=>this.getImportProcessLog(),2000);
  }

  getImportProcessLog(params = {}){
    if(this.importStatus == 'over') return;
    params.index=this.index;
    API.getImportProcessLog(params).then( (data) => {
      if(data.status=='1'){
        if(!isEmpty(this.importProcessLogDatas)){
          this.importProcessLogDatas = [...this.importProcessLogDatas,...data.datas]
        }else{
          this.importProcessLogDatas = data.datas;
        }
        //this.ref.scrollToLast();
        this.index = data.index;
        this.importStatus = data.importStatus;
        if(this.importStatus == 'over'){
          this.getImportResult({pId:data.pId});
        }
      }else{
          message.warning(data.message);
      }}, error => {
          message.warning(error.message);
      })
  }

  getImportResult(params = {}){
    this.importStatus = 'over';
    if(params.pId){
      this.pId = params.pId;
    }
    API.getImportResult(params).then( (data) => {
      if(data.status=='1'){
        this.failnum = data.datas.failnum;
        this.succnum = data.datas.succnum;
        if(this.failnum>0){
          this.importResultTip = `共${this.succnum+this.failnum}条数据，${this.succnum}导入成功！`;
          this.importResultStore.getDatas(data.datas.sessionkey, 1);
        }else{
          this.importResultTip = `共${this.succnum}条数据，全部导入成功！`;
        }
      }else{
          message.warning(data.message);
      }}, error => {
          message.warning(error.message);
      })
  }

  getImportHistory(params = {}){
    API.getImportHistory().then( (data) => {
      if(data.status){
        data.sessionkey && this.importHistoryStore.getDatas(data.sessionkey, 1);
      }else{
          message.warning(data.message);
      }}, error => {
          message.warning(error.message);
    })
  }

  getImportLogDetail(params = {}){
    if(this.pId)params.pId=this.pId;
    API.getImportLogDetail(params).then( (data) => {
      if(data.status){
        data.datas.sessionkey && this.importLogStore.getDatas(data.datas.sessionkey, 1);
      }else{
          message.warning(data.message);
      }}, error => {
          message.warning(error.message);
      })
  }

  getImportLogSearchCondition(params={}){
    if(this.pId)params.pId=this.pId;
    API.getImportLogSearchCondition(params).then( (data) => {
      if(data.status){
        this.searchCondition = data.searchCondition;
        this.form.initFormFields(data.searchCondition);
      }else{
          message.warning(data.message);
      }}, error => {
          message.warning(error.message);
      })
  }

  
  doSearch(params={}){
    const formParams = this.form.getFormParams();
    this.getImportLogDetail(formParams);
  }

}