
token = "";
pubKey = "";

// 页面初始化方法
function init(){
	let params = this.location.search;
	if(null != params){
		let token = getQueryValue('token',params);
		let sysKey = getQueryValue('sysKey',params);
		//单点登陆
		ssoLogin(token,sysKey);
	}	

	$.ajax({
    type : 'POST',
    url : '../../../ses-manage-v1/frame/optr/queryPubKey.htm',
    data : {
		
    },
    dataType : 'json',
    success : function(data) {
			
			pubKey = data.entity;
      
    },
    
	});
	


}

/**
 2  * [通过参数名获取url中的参数值]
 3  * 示例URL:http://htmlJsTest/getrequest.html?uid=admin&rid=1&fid=2&name=小明
 4  * @param  {[string]} queryName [参数名]
 5  * @return {[string]}           [参数值]
 6  */
  function getQueryValue(queryName,params) {
	     var query = decodeURI(params.substring(1));
	     var vars = query.split("&");
      for (var i = 0; i < vars.length; i++) {
          var pair = vars[i].split("=");
          if (pair[0] == queryName) { 
						return pair[1]; 
					}
      }
      return null;
  }

function ssoLogin(token,sysKey) {
	this.token = token;
	if (null == token) {
		console.log('token为空');	
		return;
	}

	if (null == sysKey) {
		console.log('sysKey为空');	
		return;
	}

	showErrorMsg('正在登录中，请稍后...');

	// 所有客户端验证成功,提交服务器验证
	$(":button").attr("disabled", "disabled");
	let _this = this;

  $.ajax({
    type : 'POST',
    url : '../../../ses-manage-v1/frame/optr/ssoLogin.htm',
    data : {
			token : token,
			sysKey : sysKey
    },
    dataType : 'json',
    success : function(data) {
      if (data.success == false) {
        // 刷新验证码
				showErrorMsg(data.errorMsg);
			 $(":button").removeAttr("disabled");
			  freshValidCode(document.getElementById('code'));
        return;
      } else {
				
				if(data.array){
					showErrorMsg("");
					accountDetailDiv(data.array);
				}else{
					window.location = '../../index.html';
				}	
        
      }
      $(":button").removeAttr("disabled");
    },
    error : function() {
      $(":button").removeAttr("disabled");
    }
  });
	return false;
}

function ssoLoginStepTwo(login_name) {
	showErrorMsg('正在登录中，请稍后...');

	// 所有客户端验证成功,提交服务器验证
	$(":button").attr("disabled", "disabled");
	let _this = this;

  $.ajax({
    type : 'POST',
    url : '../../../ses-manage-v1/frame/optr/ssoLoginStepTwo.htm',
    data : {
			token : this.token,
			loginName : login_name
    },
    dataType : 'json',
    success : function(data) {
      if (data.success == false) {
        // 刷新验证码
				showErrorMsg(data.errorMsg);
				$("#login_code_div").show();
				$("#accountDetailDiv").hide();

			 $(":button").removeAttr("disabled");
			  freshValidCode(document.getElementById('code'));
        return;
      } else {
				window.location = '../../index.html';  
      }
      $(":button").removeAttr("disabled");
    },
    error : function() {
			$(":button").removeAttr("disabled");
			$("#login_code_div").show();
			$("#accountDetailDiv").hide();
    }
  });
	return false;
}

function accountDetailDiv(accounts) {
	if(accounts){
		var html = "";
		
		html +='<div class="col-lg-12 ml-0">                                                       ';
		html +='  <table class="table table-sm table-striped table-hover table-bordered bg-white" style="margin-top:15px;"> ';
		html +='    <thead><tr> <th colspan="4" align="center" style="text-align:center;">请选择登陆账号</th></tr> </thead>     ';
		html +='    <thead>                                                                        ';
		html +='      <tr>                                                                         ';
		html +='        <th align = "center" style="text-align:center;"></th>                                                                  ';
		html +='        <th align = "center" style="text-align:center;">登录名称</th>                                                          ';
		html +='        <th align = "center" style="text-align:center;">机构类型</th>                                                          ';
		html +='        <th align = "center" style="text-align:center;">机构</th>                                                              ';
		html +='      </tr>                                                                        ';
		html +='    </thead>                                                                       ';
		html +='    <tbody>                                                                        ';
		
		for(var i=0;i<accounts.length;i++){
			html +='      <tr>                                                                         ';
			html +='        <td align = "center">                                                      ';
			html +='            <label class="radio-inline c-radio" style="margin: 0px !important">    ';
			html +='              <input type="radio" name="radio" style="cursor: pointer;" onclick="ssoLoginStepTwo(\''+accounts[i].login_name+'\')"/>   ';
			html +='              <span class="fa fa-circle" style="margin: 0px !important"></span>    ';
			html +='            </label>                                                               ';
			html +='        </td>                                                                      ';
			html +='        <td>'+accounts[i].login_name+'</td>                                               ';
			html +='        <td>'+accounts[i].orgtype+'</td>                                                  ';
			html +='        <td>'+accounts[i].org_name+'</td>                                                 ';
			html +='      </tr>                                                                        ';
		}
		
		html +='    </tbody>                                                                       ';
		html +='  </table>                                                                         ';
		html +='</div>                                                                             ';
		
		
		$("#login_code_div").hide();
		$("#accountDetailDiv").html(html).show();
	}

}

function showErrorMsg(msg, el) {
	if (msg == undefined || msg.length < 1)
		$("#error_msg").html('').hide();
	else
		$("#error_msg").html(msg).show();
	if (el != undefined) {
		$('#' + el).select();
		$('#' + el).focus();
	}
}
function keyDownElement(event, object) {
	$("#error_msg").html('').hide();
	if (event.keyCode == 13) {
		if (object.id == 'login_name') {
			document.getElementById('login_pass').focus();
			return;
		}
		if (object.id == 'login_pass') {
			document.getElementById('login_code').focus();
			return;
		}

		if (object.id == 'login_code') {
			document.getElementById('btnSubmit').focus();
			return;
		}
		object.click();
	}
}
function login() {
	var login_name = $.trim($('#login_name').val());
	var login_pass = $.trim($('#login_pass').val());
	var login_code = $.trim($('#login_code').val());
	$("#error_msg").html('').hide();
	if (login_name.length < 1) {
		showErrorMsg('请输入正确的登录名称', 'login_name');
		return false;
	}
	if (login_pass < 3) {
		showErrorMsg('登录密码不能少于3位', 'login_pass');
		return false;
	}
	if (login_code == "") {
		showErrorMsg('请填写验证码', 'login_code');
		return false;
	}
	// 所有客户端验证成功,提交服务器验证
	$(":button").attr("disabled", "disabled");

  $.ajax({
    type : 'POST',
    url : '../../../ses-manage-v1/frame/optr/loginGx.htm',
    data : {
      loginName : login_name.replace(/</g,'').replace(/>/g,''),
      loginPass : getSignString(hex_md5(login_pass).toLocaleUpperCase()),
      loginCode : login_code
    },
    dataType : 'json',
    success : function(data) {
      if (data.success == false) {
        // 刷新验证码
        showErrorMsg(data.errorMsg);
		$(":button").removeAttr("disabled");
		freshValidCode(document.getElementById('code'));
        return;
      } else {
        window.location = '../../index.html';
      }
      $(":button").removeAttr("disabled");
    },
    error : function() {
      $(":button").removeAttr("disabled");
    }
  });
	return false;
}


function freshValidCode(obj) {
	obj.src = "../../../ses-manage-v1/frame/optr/validateCode.htm?" + new Date().getTime();
}

function checkLoginCode() {
	
}

// 加密
function getSignString(data) {
	var encrypt = new JSEncrypt();
	// 公匙，由后端返回到前台
	encrypt.setPublicKey(pubKey);
	var encodePassword = encrypt.encrypt(data);
	return encodePassword;
}
