let iv = "2134123412ABCDEF";
    let secret_key = "BACDEF1234123412";
    function parse(string) {
      return CryptoJS.enc.Utf8.parse(string);
    }
    function decrypt(code) {
      return CryptoJS.AES.decrypt(code, parse(secret_key), {
        iv: parse(iv),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      }).toString(CryptoJS.enc.Utf8);
    }

    function encrypt(data) {
      // Encrypt   
      return CryptoJS.AES.encrypt(parse(data), parse(secret_key), {
        iv: parse(iv),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      }).toString();
    }


 	// 页面加载时，自动填充本地存储中的账号
    window.onload = function() {
        if (localStorage.getItem("username")) {
            document.getElementById("phone").value = localStorage.getItem("username");
        }
		if (localStorage.getItem("verify")) {
            document.getElementById("verify").value = localStorage.getItem("verify");
        }
    };


	// 公告关闭
	document.getElementById('confirmBtn').addEventListener('click', function() {
        document.getElementById('announcementModal').classList.add('hidden');
    });
	if(NoticeOpen=="1"){
		document.getElementById('announcementModal').classList.remove('hidden');
		document.getElementById('announcementContent').innerHTML = NoticeStr;
	}

	cocoMessage.config({duration: 2000});

	var httpRequest = new XMLHttpRequest();
	// 验证码
	var time=60;
	$('#vcode').click(function(){
		var phone = $('input[name=phone]').val();
		if(phone.length==0){
			swal("", '请先输入正确帐号', "warning");
			return
		}
		if($(this).hasClass('disabled')){
			return
		}
		$('#vcode').addClass('disabled')
		toast.style.display = "flex";
		httpRequest.open('GET', './zy_sendcode?phone='+encodeURIComponent(encrypt(phone)) , true);
        httpRequest.send();
        httpRequest.onreadystatechange = function () {
            if (httpRequest.readyState == 4) {// 请求已完成
                if (httpRequest.status === 200) { // 请求成功
                    var 请求结果 = httpRequest.responseText;
					if ((请求结果||"").indexOf('成功') > -1) {
						toast.style.display = "none";
						swal("", 请求结果, "success");
					}else{
						toast.style.display = "none";
						swal("", 请求结果, "warning");
					}
                    
                } else {
					toast.style.display = "none";
                    swal("", "网络繁忙,请重试!", "warning");
                }
            }
        };

		$('#vcode').text("重新发送 " + time)
		var timer = setInterval(function(){
			time--
			$('#vcode').text("重新发送 " + time)
			if(time==0){
				$('#vcode').text("获取验证码 ");
				clearInterval(timer)
				$('#vcode').removeClass("disabled ");
				time = 60
			}
		},1000)
		
	})

	$('#point').click(function(){
		//弹出菜单提示选择
		const menuContainerele = document.getElementById('menuContainer');
    	if (menuContainerele) {
    	  document.body.removeChild(menuContainerele);
    	}
    	const overlayele = document.getElementById('overlay');
    	if (overlayele) {
    	  document.body.removeChild(overlayele);
    	}

    	const overlay = document.createElement('div');
    	overlay.className = 'overlay';
    	overlay.id = 'overlay';
    	document.body.appendChild(overlay);

    	const menuContainer = document.createElement('nav');
    	menuContainer.className = 'bottom-menu';
    	menuContainer.id = 'menuContainer';


    	menuContainer.innerHTML = `
    	    <ul>
    	      <li data-id="line1"><a>线路一</a></li>
    	      <li data-id="line2"><a>线路二</a></li>
			  <li data-id="line3"><a>线路三</a></li>
			  <li data-id="line4"><a>线路四</a></li>
    	    </ul> 
    	`;
    	document.body.appendChild(menuContainer);

		
    	var overlayElement = document.getElementById('overlay');
    	var menuContainerElement = document.getElementById('menuContainer');
    	menuContainerElement.classList.add('active');
    	menuContainerElement.style.visibility = 'visible';
    	overlayElement.classList.add('show');

    	overlay.addEventListener('click', () => {
    	  menuContainer.style.visibility = 'hidden';
    	  overlay.classList.remove('show');
    	});


    	document.querySelector('.bottom-menu').addEventListener('click', function(event) {
    	  var liElement = event.target.closest('li');

		  switch (liElement.dataset.id) {
              case "line1":
			  	window.location=urls[0];
            	break;
            
              case "line2":
			  	window.location=urls[1];
                break;

			  case "line3":
			  	window.location=urls[2];
                break;

			  case "line4":
			  	window.location=urls[3];
                break;
          	}
    	  menuContainerElement.style.visibility = 'hidden';
    	  overlayElement.classList.remove('show');
    	  menuContainerElement.classList.remove('active');
    	});
  
	})

	// 提交验证
	$('.submit button').click(function(){
		var phone = $('input[name=phone]').val()
		var verify = $('input[name=verify]').val()
		if(phone.length==0){
			swal("", '请先输入正确帐号', "warning");
			return
		}
		
		if(!verify){ 
			swal("", '请输入验证码', "warning");
			return false; 
		}
		if(verify.length!=6){ 
			swal("", '请输入6位数验证码', "warning"); 
			return false; 
		}


		const username = document.getElementById("phone").value;
        localStorage.setItem("username", username);

		localStorage.setItem("verify", verify);
		
		toast.style.display = "flex";
		httpRequest.open('GET', './zy_login?phone='+encodeURIComponent(encrypt(phone))+"&code="+encodeURIComponent(encrypt(verify)), true);
        httpRequest.send();
        httpRequest.onreadystatechange = function () {
            if (httpRequest.readyState == 4) {// 请求已完成
                if (httpRequest.status === 200) { // 请求成功
                    var 请求结果 = httpRequest.responseText;
					if ((请求结果||"").indexOf('成功') > -1) {
						toast.style.display = "none";
						swal("", 请求结果, "success");
						window.location='./call?id='+ encodeURIComponent(encrypt(phone))
					}else{
						toast.style.display = "none";
						swal("", 请求结果, "warning");
					}
                } else {
					toast.style.display = "none";
                    swal("", "网络繁忙,请重试!", "warning");
                }
            }
        };
	})




	// 人脸识别
	$('#rlsb').click(function(){
		var phone = $('input[name=phone]').val()
		if(phone.length==0){
			swal("", '请先输入正确帐号', "warning");
			return
		}
		$('#files').click();
	})


	// 监听文件选择框的变化
	const fileInputElement = document.getElementById('files');
	fileInputElement.addEventListener('change', function (event) {
	  const file = event.target.files[0]; // 获取用户选择的文件

	  if (file) {
	    // 创建 FormData 对象，准备上传文件
	    const formData = new FormData();
	    formData.append('image', file);

		toast.style.display = "flex";
		var phone = $('input[name=phone]').val()
		const username = document.getElementById("phone").value;
		localStorage.setItem("username", username);

		$.ajax({
	        url: './zy_facelogin' ,
		 	type: 'POST',
		 	data: formData,
			headers: {'id': encodeURIComponent(encrypt(phone))},
		 	processData: false, // 防止 jQuery 对 FormData 做处理
	        contentType: false, // 让浏览器自定处理 Content-Type
		 	timeout : 20000, //超时时间设置，单位毫秒
		 	traditional:true, //防止深度序列化 ,向后台传输数组参数的一种方法,底层还是调用serialize()方法,如果没有没有加上traditional:true之前，jquery.param()解析出错了，后台获取到的userList为空
	     	success: function (returndata) {
				toast.style.display = "none";
				if ((returndata||"").indexOf('成功') > -1) {
					toast.style.display = "none";
					swal("", returndata, "success");
					window.location='./call?id='+ encodeURIComponent(encrypt(phone))
				}else{
					toast.style.display = "none";
					swal("", returndata, "warning");
				}
	     	},
			error: function(xhr, status, error) {
				toast.style.display = "none";
	        	if (status === 'timeout') {
	        	    swal("Error!", '请求超时，请重试', "warning");
	        	} else {
					swal("Error!", '上传失败，请重试', "warning");
	        	}
	    	}
		});
		// 处理完文件后，重置文件输入框
		fileInputElement.value = '';  // 清空输入框的值
	  }
	});