import {
  observable,
  action
} from 'mobx';
import {
  WeaTools
} from 'ecCom';
import {
  Button,
  message
} from 'antd';
import * as API from '../apis/card';
import isEmpty from 'lodash/isEmpty';
import {
  i18n
} from '../public/i18n';

export class HrmCard {
  userId;
  @observable spinning = false;
  @observable tabInfo = [];
  @observable tabKey = 'cardInfo'
  @observable verified = false;
  @observable isAdmin = false;
  @observable dialogParams = {
    icon: "icon-coms-hrm",
    iconBgcolor: "#217346",
    visible: false,
    title: i18n.label.uploadPortrait,
    style: {
      width: 460,
      height: 300
    },
    onCancel: () => {
      this.dialogParams.visible = false;
    }
  }
  @observable portraitSettingVisible = false;

  @observable info = {}
  @observable uploadPortrailAuth = false;

  @observable topButtons = null;
  @observable topRightMenus = null;
  @observable topShowDropIcon = false;
  @observable hasRight = false;
  @observable updateSuccess = false;

  setTopButtons() {

  }

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

  @action
  setTopButtons=(getBtns)=>{
    this.topButtons = getBtns;
  }

  @action
  setTopRightMenus=(getMenus)=>{
    this.topRightMenus = getMenus;
  }

  @action
  setTopShowDropIcon=(bool)=>{
    this.topShowDropIcon = bool;
  }

  @action
  changeTab(key) {
    this.tabKey = key;
  }

  @action
  init(key) {
    this.tabKey = key || 'cardInfo';
  }

  @action
  verifyIsAdmin(params = {}) {
    API.isAdmin(params).then((data) => {
      this.isAdmin = data.isAdmin;
    })
  }

  getTabBaseInfo(params = {}) {
    params.isMobx = "1";
    API.getResourceBaseTitle(params).then((data) => {
      this.info = data.result;
      this.userId = data.id;
      this.hasRight = data.hasRight;
      this.verified = true;
      API.uploadPortrailAuth({userId: this.userId}).then(data => {
        this.uploadPortrailAuth = data.hasRight;
      })
    })
    if (window.WeaHrm4Formal) {
      API.getHrmResourceTab4Formal(params).then((data) => {
        this.tabInfo = data;
      })
    } else {
      API.getHrmResourceTab(params).then((data) => {
        this.tabInfo = data;
      })
    }
  }

  getData(params = {}) {
    this.userId = params.id;
    this.getTabBaseInfo(params);
  }

  showPortraitSetting = () => {
    this.dialogParams.time = new Date().getTime();
    this.dialogParams.visible = true;
  }

  getDialogOpButtons = (event) => {
    let buttons = [];
    buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ebcw3l@1`} type="primary" onClick={event.ok}>{i18n.button.ok()}</Button>);
    buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@gb1dxp@2`} type="default" onClick={() => {event.cancel(); this.onCancel()}}>{i18n.button.cancel()}</Button>);
    return buttons;
  }

  onOk = (data) => {
    if(this.spinning)
      return;
    this.spinning = true;
    API.saveUserIcon({userId: this.userId, imageDataUrl: data}).then(data => {
      this.spinning = false;
      if(data.status === '1'){
        // this.info.messagerurl = data.messagerurl;
        API.getResourceBaseTitle({id: this.userId}).then((data) => {
          this.info = data.result;
        });
        this.onCancel();
        this.updateSuccess = true;
      }else{
        message.error(data.message);
      }
    }, error => {
      this.spinning = false;
    })
  }

  onCancel = () => {
    this.dialogParams.onCancel();
  }

  // 人员卡片编辑时，保存页面需要重新请求头像
  setupdateStatus = () => {
    this.updateSuccess = true;
  }
}