import { observable, action } from 'mobx';
import { WeaForm } from 'comsMobx';
import * as API from '../apis/bindtokenkey';
import { message } from 'antd';
import { Modal } from 'antd'
const confirm = Modal.confirm;
import { WeaTools, WeaLocaleProvider } from 'ecCom'
const getLabel = WeaLocaleProvider.getLabel;

export class BindTokenKey {
  title = () => getLabel(386125, "令牌绑定");
  @observable form = new WeaForm();
  @observable formFields = [];
  @observable date;
  @observable dialogId;

  @action
  getFormField(params = {}) {
    this.form = new WeaForm();
    API.getFormField(params).then((data) => {
      data.formField.map(c => c.items.map(item => {
        if (item.otherParams && item.otherParams.tip) {
          item.self_tip = item.otherParams.tip;
          delete item.otherParams.tip
        }
      }))
      this.formFields = data.formField;
      this.form.initFormFields(this.formFields);
    });
  }

  save(params = {}) {
    const formParams = this.form.getFormParams();
    this.form.validateForm().then(f => {
      if (f.isValid) {

        if (params.syncTokenKey && params.syncTokenKey == '1') {1
          let tokenKey = formParams.tokenKey.trim() || '';
          let tokenKeyCode1 = formParams.tokenKeyCode1.trim() || '';
          let tokenKeyCode2 = formParams.tokenKeyCode2.trim() || '';

          if (tokenKey != "" && (!this.isdigit(tokenKey) || tokenKey.length != 10)) {
            message.warning(getLabel(83745, "令牌序列号必须为10位数字！"));
            tokenKey.focus();
            return false;
          }

          var tokenType = "";
          var startNumber = tokenKey.substr(0, 1);
          if (startNumber == "1") {      //动联1代
            tokenType = "5";
          } else {
            message.warning(getLabel(129133, "令牌序列号不正确，请确认后输入"));
            return false;
          }

          if (tokenKeyCode1 != "" && (!this.isdigit(tokenKeyCode1) || tokenKeyCode1.length != 6)) {
            message.warning(`${getLabel(129204, "动态口令")}1${getLabel(129205, "必须为6位数字")}`);
            return false;
          }

          if (tokenKeyCode2 != "" && (!this.isdigit(tokenKeyCode2) || tokenKeyCode2.length != 6)) {
            message.warning(`${getLabel(129204, "动态口令")}2${getLabel(129205, "必须为6位数字")}`);
            return false;
          }

          if (tokenKeyCode1 == tokenKeyCode2) {
            message.warning(`${getLabel(129204, "动态口令")}1${getLabel(129206, "与动态口令")}2${getLabel(26250, "不能相同")}`);
            return false;
          }
        } else {
          let tokenKey1 = formParams.tokenKey1.trim() || '';
          let tokenKey2 = formParams.tokenKey2.trim() || '';
          let tokenKeyCode = formParams.tokenKeyCode.trim() || '';
          if (tokenKey1 != "" && (!this.isdigit(tokenKey1) || tokenKey1.length != 10)) {
            message.warning(getLabel(83745, "令牌序列号必须为10位数字！"));
            return false;
          }

          if (tokenKey2 != "" && (!this.isdigit(tokenKey2) || tokenKey2.length != 10)) {
            message.warning(getLabel('386433',"确认序列号必须为10位数字！"));
            return false;
          }

          if (tokenKey1 != tokenKey2) {
            message.warning(getLabel('386434',"令牌序列号和确认令牌序列号不一致！"));
            return false;
          }

          if (tokenKeyCode != "" && (!this.isdigit(tokenKeyCode) || tokenKeyCode.length != 6)) {
            message.warning(getLabel('386435',"动态令牌口令必须为6位数字！"));
            return false;
          }
        }

        API.saveFormFields({ ...params, ...formParams }).then((data) => {
          if (data.status == '1') {
            message.success(data.message);
            setTimeout(()=>{
              let dialog = top.window.getParentDialog();
              if(!dialog){
                if (this.dialogId && window.top.window.dialogMapper && window.top.window.dialogMapper[this.dialogId]) {
                  dialog = window.top.window.dialogMapper[this.dialogId];
                }
              }
              dialog.close(); // 关闭。 会触发onCancel
            },3000);
          } else {
            message.warning(data.message);
          }
        }, error => {
          message.warning(error.message);
        });
      } else {
        f.showErrors();
        this.date = new Date();
      }
    });
  }


  isdigit(s) {
    var r, re;
    re = /\d*/i; //\d表示数字,*表示匹配多个数字
    r = s.match(re);
    return (r == s) ? true : false;
  }
}