import {
    observable,
    action,
    computed
  } from 'mobx';
  import {
    WeaForm
  } from 'comsMobx';
  
  import * as API from '../apis/importDialog';
  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';
  import {
    has
  } from 'lodash';
  const toJS = mobx.toJS;
  const {
    TableStore
  } = WeaTableNew;
  const confirm = Modal.confirm;
  import {
    i18n
  } from '../public/i18n';
  import {
    findIndex
  } from 'lodash';
  
  export class ImportDialogStore {
  
    @observable importVisible = false;
    @observable importModule = ''; //导入模块

  

    /********************* importDialog  *********************/
  
    @observable steps = [
      { key: 547227,value:'上传文件'},
      { key: 547221,value:'导入结果'},
    ];

    @observable importType = '';
    @observable current = 0;
    @observable buttonTitle = '';
    @observable pvisable = false;
  
  
    /********************* importContent  *********************/
    @observable data = [];

  
    @observable condition = [];
    @observable filelist = []; //文件信息
    @observable excelfile = ''; //文件id
    @observable templetName = "导入模板";
    @observable percent = 0;
    @observable failnum = 0; //失败数量
    @observable succnum = 0; //成功数量
    @observable importResultTip = ''; //导入结果提示
    @observable importResultStore = new TableStore();
    @observable importStatus = 'importing'; //导入状态
    @observable loading = true;
    @observable date = '';
    @observable pId = '';//导入记录
  
    interval
    
    @action("获取导入表单")  getImportForm() {
      
      const params = {
        importModule: this.importModule,
      }
      API.getImportForm(params).then(res => {
        if (res.code == 200) {
          this.condition = res.data.condition;        
        } else {
           message.warning(res.msg);
        }
      }, error => {
        message.warning(error.msg);
      })
    }
  
    @action("开始导入") startImport() {
      if(this.filelist.length == 0) {
        message.error(`${i18n.message.checkFile()}`)
        this.current = this.current - 1;
        return;
      }
      this.pvisable = true;
      //导入文件提交
      let params = {
        importModule: this.importModule,
        excelfile:this.excelfile
      }
      if (this.otherParams != null) {
        Object.assign(params, {
          otherParams: JSON.stringify(this.otherParams)
        })
      }
      this.doImport(params)
      this.interval = setInterval(() => this.getImportProcess(), 200);
  
    }
  
    @action("文件提交") doImport(params) {
      API.saveImport(params).then(res => {
        if(res.code == 200) {
          this.pId = res.data.pId;
        }else {
          clearInterval(this.interval);
          this.pvisable = false;
          message.error(`${i18n.message.checkFileContent()}`)
        }
      })
    }
  
   
  
    @action("获取导入进度") getImportProcess() {
      if(this.pId != '') {
        clearInterval(this.interval);
        this.percent = 100;
        const _this = this;
        setTimeout(function(){
          _this.pvisable = false;
          _this.getImportResult();
        },1000)
      }else {
        //调用导入进度api
        let max = 95;
        let min = this.percent;
        if(this.percent < max) {
          this.percent = Math.floor(Math.random() * (max - min)) + min;
        }
       
      }
    }
  
    @action("获取导入结果") getImportResult() {
      let params = {
        pId:this.pId
      }
      API.getImportResult(params).then((res) => {
        if (res.code == 200) {
          this.failnum = res.data.failnum;
          this.succnum = res.data.succnum;
          this.importStatus = res.data.importStatus;
          if (this.failnum > 0) {
            let info = i18n.label.importResourceResultInfo();
            info = info.replace('{rownum}', this.succnum + this.failnum).replace('{succnum}', this.succnum);
            this.importResultTip = info;
            this.importResultStore.getDatas(res.data.datas, 1);
          } else {
            let info = i18n.label.importResourceResultInfo1();
            info = info.replace('{rownum}', this.succnum);
            this.importResultTip = info;
          }
        } else {
          message.warning(res.msg);
        }
      }, error => {
        message.warning(error.msg);
      })
    }

    @action init(){
      this.filelist = [];
      this.excelfile = '';
      this.importVisible = false;
      this.percent = 0;
      this.failnum = 0;
      this.succnum = 0;
      this.importResultTip = '';
      this.importResultStore = new TableStore();
      this.importStatus = 'importing';
      this.data = [];
      this.condition = [];
      this.pId = '';
    }
  
   
    setExcelfile(field) {
        this.excelfile = field;
    }
 
  
  
  }