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

export class HrmRoleSet {
  @observable verified = false;
  @observable hasRight = false;
  @observable dialogShow = false;
  @observable isEditor = false;
  @observable table = new TableStore();
  @observable form = new WeaForm();
  @observable formFields = [];
  @observable dataKey = '';
  targetId = null;
  hrmId = null;

  @action
  getData(params = {}) {
    this.hrmId = params.id;
    this.getFormField(params);
    this.getSearchList(params);
  }

  getFormField(params = {}) {
    API.getFormField(params).then((data) => {
      this.formFields = data.condition;
      this.hasRight = data.hasRight;
      this.verified = true
      this.form.initFormFields(this.formFields);
    });
  }

  getSearchList(params = {}) {
    API.getSearchList(params).then((data) => {
      this.dataKey = data.sessionkey;
      this.table.getDatas(this.dataKey,1);
    });
  }

  save(hrmId) {
    const formParams = this.form.getFormParams();
    const rolesid = formParams.rolesid || '';
    if(rolesid==''){
      message.warning(i18n.message.requireFieldNone());
      return false;
    }

    const params = formParams;
    Object.assign(params, {id: hrmId});
    if(this.targetId)
      Object.assign(params, {recordId: this.targetId});

    API.saveFormFields(params).then((data) => {
      if (data.status == '1') {
        message.success(i18n.message.saveSuccess());
        this.init();
        this.update();
      } else {
          message.warning(data.message);
      }
    }, error=> {
      message.warning(error.message);
    });
  }

  onCancel(){
    this.dialogShow = false;
    this.form = new WeaForm();
  }

  update(){
    this.table.getDatas(this.dataKey,1);//刷新table
  }

  edit(id, hrmId){
    this.targetId = id;
    this.dialogShow = true;
    this.isEditor = true;
    this.getFormField({id: hrmId, recordId: id});
  }

  add(params = {}){
    this.targetId = null;
    this.dialogShow = true;
    this.isEditor = false;
    this.getFormField(params);
  }

  delBatch(ids){
    if(ids==''){
      message.warning(i18n.message.pleaseSelectDelRecord());
      return false;
    }
    let _this = this;
    confirm({
      title: i18n.confirm.systemhints(),
      content: i18n.confirm.batchDeleteConfirm(),
      onOk(){ 
        let idArr = ids.split(",");
        let ajaxNum = 0;
        for(var i=0;i<idArr.length;i++){
          ajaxNum++;
          WeaTools.callApi('/api/hrm/roleset/delete','POST',{id:idArr[i]}).
          then(data=>{
              if (data.status == '1') {
                ajaxNum--;
                if(ajaxNum==0){
                  message.success(i18n.message.deleteSuccess());
                  _this.update();
                }
              } else {
                  message.warning(data.message);
              }
          }, error=> {
              message.warning(error.message);
          })
        }
      },
      onCancel(){},
    });
  }

  del(id){
    let _this = this;
    confirm({
      title: i18n.confirm.systemhints(),
      content: i18n.confirm.delete(),
      onOk(){ 
        WeaTools.callApi('/api/hrm/roleset/delete','POST',{id}).
        then(data=>{
            if (data.status == '1') {
                message.success(i18n.message.deleteSuccess());
                this.update();
            } else {
                message.warning(data.message);
            }
        }, error=> {
            message.warning(error.message);
        })
      },
      onCancel(){},
    });
  }

  init() {
    this.isEditor = false;
    this.dialogShow = false;
    this.form = new WeaForm();
  }

  getFormParams() {
    return this.form.getFormParams();
  }
}