import { observable, action } from 'mobx';
import * as API from '../apis/constrpsubsearch';
import * as Util from '../util/index';
import cloneDeep from 'lodash/cloneDeep'
import {Modal,message} from 'antd'
import {WeaTableNew,WeaForm} from 'comsMobx'
import { toJS } from 'mobx';
const {TableStore} = WeaTableNew;
const confirm = Modal.confirm;
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import {i18n} from '../public/i18n';

export class HrmConstRpSubSearch {
  title = getLabel('15687',"个人信息");
  @observable verified = false;
  @observable hasRight = false;
  @observable showResult = false;
  @observable topTabkey = '15687';
  @observable scopeId = '1';
  @observable scopeCmd = '0';
  @observable tabkey = '15687';
  @observable templateid = "0";
  @observable form = new WeaForm();
  @observable table = new TableStore();
  @observable tabDatas = [];
  @observable config = []
  @observable visible = false
  @observable templeteValue = ''
  @observable defaulttabkey = ''
  @observable isReSave = false;
  @observable isCustom = '';
  @observable prePath = '';
  @observable prePathName = '';
  configbak = []

  @action
  getTabInfo(params={},cb){
    this.title = "";
    this.tabkey = '';
    this.tabDatas = [];
    this.verified = false;
    params.scopeId = this.scopeId;
    params.isCustom = this.isCustom;
    API.getTabInfo(params).then(data=>{
      this.verified = true;
      if (data.status == '1') {
        this.title = data.tabInfo[0].title;
        this.topTabkey = data.tabInfo[0].key;
        this.tabkey = data.tabInfo[0].key;
        this.scopeCmd = data.tabInfo[0].key;
        this.defaulttabkey = data.tabInfo[0].key;
        this.tabDatas = data.tabInfo;
        this.hasRight = true;
        cb && cb();
      } else {
        this.hasRight = false;
        //message.warning(data.message);
      }
    }, error=> {
      message.warning(error.message);
    })
  }

  changeTab(key) {
    this.tabkey = key;
    this.config=[];
    this.configbak=[];
    this.templateid = "0";
    if(key&& key.split("_").length==3){
      let tmpValue = key.split("_");
      this.scopeCmd = tmpValue[1];
      this.templateid = tmpValue[2];
    }else{
      this.scopeCmd = key;
    }
    this.getCondition();
  }

  changeTopTab(key) {
    this.topTabkey = key;
    this.changeTab(key)
  }

  updateTempleteValue(v) {
    this.templeteValue = v;
  }

  updateVisible(bool) {
    this.visible = bool;
  }

  getCondition(params = {}) {
    params.scopeId = this.scopeId;
    params.cmd = this.scopeCmd;
    params.templateid = this.templateid;
    params.isCustom = this.isCustom;
    this.showResult = false;
    this.config = [];
    this.configbak = [];
    API.getCondition(params).then(data=>{
      if (data.status == '1') {
        this.config = data.condition;
        this.configbak = cloneDeep(data.condition);
      } else {
        message.warning(data.message);
      }
    }, error=> {
        message.warning(error.message);
    })
  }

  doSearch(params){
    this.getSearchList(params);
  }

  reSearch(){
    this.showResult = false;
  }

  reset(){
    this.config=[];
    let __this = this;
    setTimeout(function () {
        __this.config=cloneDeep(__this.configbak);
    }, 0);
  }

  saveTemplete(params={}){
    let doSave = params.doSave || false;
    if(doSave){
      params.templateid=this.templateid;
    }else{
      let templeteValue = this.templeteValue ||'';
      if(templeteValue==''){
        message.warning(i18n.message.requireFieldNone());
        return;
      }
    }

    if(this.isReSave){
      params.cmd="ctrlc";
    }
    params.templateName = this.templeteValue;
    params.isCustom = this.isCustom;
    API.saveTemplete(params).then(data=>{
      if (data.status == '1') {
        if(doSave){

        }else{
          this.templateid = data.templateid;
          let tmpTabKey = params.scopeId+"_"+params.scopeCmd+"_"+data.templateid;
          this.updateVisible(false)
          this.templeteValue='';
          this.isReSave = false;
          this.getTabInfo({},()=> {
            this.topTabkey = params.scopeCmd;
            this.changeTab(tmpTabKey);
          });
        }
        message.success(i18n.message.saveSuccess());
      } else {
        message.warning(data.message);
      }
    }, error=> {
        message.warning(error.message);
    })
  }

  deleteTabs(){
    let __this = this;
    let params={};
    params.cmd = "del";
    params.scopeId=this.scopeId;
    params.scopeCmd=this.scopeCmd;
    params.templateid=this.templateid;
    confirm({
      title: '系统提示',
      content: '确定要删除该页签吗？',
      onOk(){
        API.saveTemplete(params).then(data=>{
          if (data.status == '1') {
            scopeId:1
            __this.getTabInfo({},()=>{
              __this.changeTopTab(params.scopeCmd);
            });
            message.success("删除成功");
          } else {
            message.warning(data.message);
          }
        }, error=> {
            message.warning(error.message);
        })
      },
      onCancel(){},
    });
  }
  
  importExcel(params = {}){
    this.table.exportAll();
  }
  
  getSearchList(params = {}) {
    params.scopeId = this.scopeId;
    params.isCustom = this.isCustom;
    if(this.isCustom=='1'){
      params.scopeCmd = '0';
    }else{
      params.scopeCmd = this.scopeCmd;
    }
    API.getSearchList(params).then(data=>{
      if (data.status == '1') {
        this.table.getDatas(data.sessionkey,1);
        this.showResult = true;
      } else {
        message.warning(data.message);
      }
    }, error=> {
        message.warning(error.message);
    })
  }
}