<?php
	include $_SERVER['DOCUMENT_ROOT'].'/common/lib/comm.php';

	$nav_title = "회원가입";
	// $show_btn_back = true;

?>
<?php include("include/header.php") ?>
<style>
.txt_red{color:#be0606;}
</style>

<main class="normal--wrap" style="padding-bottom:90px;">
	<section class="join--form--wrap">
		<div class="join--form--contents">
			<form id="frmMember" name="frmMember" method="post" action="join_proc.php" enctype="multipart/form-data">
			<input type="hidden" name="mode" id="mode" value="Add"/>
			<input type="hidden" name="idchkresult" id="idchkresult" value="N"/>
			<input type="hidden" name="ds_agree1" id="ds_agree1" value="Y"/>
			<input type="hidden" name="ds_agree2" id="ds_agree2" value="Y"/>
			<input type="hidden" name="ds_agree3" id="ds_agree3" value="Y"/>
			<input type="hidden" name="ds_agree4" id="ds_agree4" value="Y"/>
			<!-- <input type="hidden" name="nm_hp" id="nm_hp" value=""/> -->
			<!-- <input type="hidden" name="nm_email" id="nm_email" value=""/> -->

			<div class="join--form--inner">
				<h2>개인 정보 입력</h2>
				<div>
					<input type="text" class="form-control" name="cd_userid" id="cd_userid" placeholder="아이디">
					<p id="id_chk_info" class="hide"></p>
				</div>
				<div>
					<input type="password" class="form-control" name="nm_pass" id="nm_pass" maxlength="20" placeholder="패스워드">
				</div>
				<div>
					<input type="password" class="form-control" name="nm_pass2" id="nm_pass2" maxlength="20" placeholder="패스워드 확인">
				</div>

				<div>
					<input type="text" class="form-control" name="nm_name" id="nm_name" maxlength="20" placeholder="이름">
				</div>
				<div>
					<input type="text" class="form-control" id="nm_hp" name="nm_hp" placeholder="핸드폰 번호">
				</div>
				<div>
					<input type="text" class="form-control" id="nm_email" name="nm_email" placeholder="이메일">
				</div>

				<h2 class="mt45">사용자 선택</h2>
				<div>
					<select name="ds_company_bp" id="ds_company_bp" class="form-select"></select>
					<!-- <select id="inputState" class="form-select">
						<option selected>지점 직접 입력</option>
					</select> -->
				</div>
				<div>
					<select name="ds_branch" id="ds_branch" class="form-select"></select>
					<!-- <input type="text" class="form-control" id="" placeholder="지점명을 입력해주세요"> -->
				</div>
			</div>



      <div class="agree--wrapper mt35 d-flex justify-content-end">
        <input type="checkbox" id="ds_agree10" class="ty2" hidden>
        <label for="ds_agree10">회원 이용약관 (필수)</label>
        <button type="button" class="info--btn btn" data-bs-toggle="modal" data-bs-target="#agreeModal1">약관보기</button>
      </div>
      <div class="agree--wrapper mt35 d-flex justify-content-end">
        <input type="checkbox" id="ds_agree20" class="ty2" hidden>
        <label for="ds_agree20">개인정보 수집 및 이용 동의 (필수)</label>
        <button type="button" class="info--btn btn" data-bs-toggle="modal" data-bs-target="#agreeModal2">약관보기</button>
      </div>


			<div class="col-12 d-flex justify-content-center join--btn--wrap floating--btn">
				<button type="button" class="btn btn-primary" id="btn_join_proc">회원가입</button>
			</div>
			</form>

		</div>

	</section>
</main>


<!-- 회원이용약관 -->
<div class="modal fade type2" id="agreeModal1" tabindex="-1" aria-labelledby="agree_lable1" aria-hidden="true">
  <div class="modal-dialog  modal-dialog-centered" style="max-width:calc(100vw - 40px);">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title" id="agree_lable1">회원 이용약관 동의</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body modal--body pt0">
        <div class="date--text" style="max-height:60vh; overflow-y: auto;">
					<?php include("../../include/clause_txt1.txt"); ?>
        </div>
      </div>
      <div class="modal-footer d-flex justify-content-center ty2 ty--2">
        <button type="button" class="btn btn-primary" data-bs-dismiss="modal" style="max-width:100px;">확인</button>
      </div>
    </div>
  </div>
</div>


<!-- 개인정보 수집이용 -->
<div class="modal fade type2" id="agreeModal2" tabindex="-1" aria-labelledby="agree_lable2" aria-hidden="true">
  <div class="modal-dialog  modal-dialog-centered" style="max-width:calc(100vw - 40px);">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title" id="agree_lable2">개인정보 수집 및 이용 동의</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body modal--body pt0">
        <div class="date--text" style="max-height:60vh; overflow-y: auto;">
					<? include("../../include/clause_txt2.txt"); ?>
        </div>
      </div>
      <div class="modal-footer d-flex justify-content-center ty2 ty--2">
        <button type="button" class="btn btn-primary" data-bs-dismiss="modal" style="max-width:100px;">확인</button>
      </div>
    </div>
  </div>
</div>

<script>
function id_chk() {
	var cd_userid = $("#cd_userid").val();
	var custom_opts = {
		title : '',
		btn_close_show : false,
	}

	if($.trim(cd_userid) == ""){
		return false;
	}

	$("#id_chk_info").removeClass("txt_red").removeClass("hide");

	$.post("join_proc.php", { mode: "IdChk", cd_userid: cd_userid},function(res) {

		if(res.CODE != "0000"){
			$("#idchkresult").val("N");

			id_chk_info = "사용불가능한 아이디 입니다.";

			$("#id_chk_info").addClass("txt_red").text(id_chk_info);

		}else {
			$("#idchkresult").val("Y");

			id_chk_info = "사용할 수 있는 아이디입니다.";
			$("#id_chk_info").removeClass("txt_red").text(id_chk_info);
		}

	}, "json");
	return false;
}
$(function() {
	setCodeSelect("listPhone","nm_hp1","","","","선택","");
	setCodeSelect("listEmail","nm_email3","","","","직접입력","");
	setCodeSelect("listcode","ds_company_bp", "sa_master","ds_company_bp",""," 유형을 선택하세요","");
	setCodeSelect("listcode","ds_branch", "sa_master","ds_branch","","유형을 선택하세요","-1");

	//아이디 체크
	$("#cd_userid").bind("focusout", id_chk);

	$("#btn_login_page").on("click", function() {
		link("login.php");
	});
	$("#ds_company_bp").change(function(){
		setCodeSelect("listcode","ds_branch", "sa_master","ds_branch","","선택하세요",$(this).val());
	});

	$("#cd_userid").bind("keyup",function(){
		$("#idchkresult").val("N");

		$("#id_chk_info").removeClass("txt_red").addClass("hide").text("");
	});

	$("#nm_email3").on("change", function(){
		if($(this).val() == ""){
			$("#nm_email2").show();
		}else{
			$("#nm_email2").hide();
		}
	});

	$("#btn_join_proc").on('click', function() {
		var custom_opts = {
			title : '',
			btn_close_show : false,
		}

		var fieldlist = [["cd_userid","아이디"],["nm_pass","패스워드"],["nm_pass2","패스워드 확인"],["nm_name","이름"],["nm_hp","휴대번호"]
		,["ds_company_bp","관계사"], ["ds_branch", "지점"]]


		var nm_email3 = $("#nm_email3").val();
		if(nm_email3 == "") {
			fieldlist.push(["nm_email2", "이메일"]);
		}
		if (!fnCheckForm(fieldlist)){
			return false;
		}

		// var idReg1 = /[a-z]/g;
		// var idReg2 = /[0-9]/g;
		//
		// if( !(idReg1.test( $("#cd_userid").val() )) || !(idReg2.test( $("#cd_userid").val() )) ) {
		// 	custom_opts.title = '아이디는 영문/숫자 조합으로 이루어져야 합니다.';
		//
		// 	custom_modal_alert(custom_opts);
		// 	return false;
		// }
		// var idReg = /^[a-z]+[a-z0-9]{5,19}$/g;
		// if( !idReg.test( $("#cd_userid").val() ) ) {
		// 	custom_opts.title = '아이디는 영문자로 시작하는 6~20자 영문자 또는 숫자이어야 합니다.';
		//
		// 	custom_modal_alert(custom_opts);
		// 	return false;
		// }
		var idReg = /^[a-z]+[a-z0-9]{3,19}$/g;
		var idReg = /^[a-zA-Z]+[a-zA-Z0-9]{3,19}$/g;
		// var idReg = /^[a-zA-Z](?=.*[a-zA-Z])(?=.*[0-9]).{4,12}$/g;
		var idReg = /^[a-zA-Z](?=.*[a-zA-Z])(?=.*[0-9]).{3,19}$/g;
		if( !idReg.test( $("#cd_userid").val() ) ) {
			// custom_opts.title = '아이디는 영문자로 시작하는 4~20자 영문자 또는 숫자이어야 합니다.';
			custom_opts.title = '아이디는 영문자로 시작하는 4~20자 영문자/숫자 조합이어야 합니다';

			custom_modal_alert(custom_opts);
			return false;
		}

		// if(!fnCheckAlphaNumber($("#cd_userid").val())){
		// 	custom_opts.title = '아이디는 영문/숫자 조합으로 이루어져야 합니다.';
		//
		// 	custom_modal_alert(custom_opts);
		// 	return false;
		//
		// }
		// if($("#cd_userid").val().length <6){
		// 	custom_opts.title = '아이디는6글자 이상 아이디를 입력하세요.';
		//
		// 	custom_modal_alert(custom_opts);
		// 	return false;
		// }
		if($("#cd_userid").val().length <4){
			custom_opts.title = '아이디는4글자 이상 아이디를 입력하세요.';

			custom_modal_alert(custom_opts);
			return false;
		}

		if($("#idchkresult").val() != "Y"){
			custom_opts.title = '아이디 중복확인을 해 주세요.';

			custom_modal_alert(custom_opts);
			return false;
		}

		/*패스워드 처리*/
		if($("#nm_pass").val().length == 0 || $("#nm_pass2").val().length == 0){
			custom_opts.title = '패스워드를 입력하세요.';

			custom_modal_alert(custom_opts);
			return false;
		}

	   if($("#nm_pass").val().length > 0){
			if($("#nm_email1").val()==$("#nm_pass").val()){
				custom_opts.title = '아이디와 패스워드는 다르게입력하세요.';

				custom_modal_alert(custom_opts);
				return false;
			}
			if($("#nm_pass").val()!=$("#nm_pass2").val()){
				custom_opts.title = '패스워드가 일치하지 않습니다.';

				custom_modal_alert(custom_opts);
				return false;
			}
			if(!fnCheckAlphaNumber($("#nm_pass").val())){
				custom_opts.title = '패스워드는 영문/숫자 조합으로 이루어져야 합니다.';

				custom_modal_alert(custom_opts);
				return false;
			}
			if($("#nm_pass").val().length <8){
				custom_opts.title = '8글자 이상 패스워드를 입력하세요.';

				custom_modal_alert(custom_opts);
				return false;
			}
		}

		/*연락처*/
		// $("#nm_hp").val($("#nm_hp1").val()+"-"+$("#nm_hp2").val()+"-"+$("#nm_hp3").val());
		//
		// /*이메일 체크*/
		// var nm_email3 = $("#nm_email3").val();
		// if($.trim(nm_email3) == "") {
		// 	$("#nm_email").val($("#nm_email1").val()+"@"+$("#nm_email2").val());
		// }else {
		// 	$("#nm_email").val($("#nm_email1").val()+"@"+$("#nm_email3").val());
		// }

		var nm_email = $("#nm_email").val();
		if($.trim(nm_email) != "") {
			if(!isValid(nm_email,"regEmail")){
				custom_opts.title = '정확한 이메일을 입력하세요.';

				custom_modal_alert(custom_opts);
				return false;
			}
		}


    if(!document.getElementById("ds_agree10").checked){
      custom_opts.title = "[필수] 회원 이용약관에 동의해 주세요.";
      custom_modal_alert(custom_opts);
      return false;
    }

    if(!document.getElementById("ds_agree20").checked){
      custom_opts.title = "[필수] 개인정보 수집이용 에 동의해 주세요.";
      custom_modal_alert(custom_opts);
      return false;
    }


		custom_opts.title = '회원가입 하시겠습니까?';
		custom_modal_confirm(custom_opts, function(confirm) {
			if(confirm) {
				$("#btn_join_proc").hide();
				var data = $("#frmMember").serialize();
				$.ajax({
					url : 'join_proc.php',
					type: 'POST',
					dataType : 'json',
					data: data,
					success: function(res) {
						custom_modal_confirm_close();

						$("#btn_join_proc").show();

						//세션 종료시 로그인 페이지로 이동
						if(res.CODE != "0000") {
							custom_opts.title = res.MSG;

							if(res.CODE == "4444") {
								window.location.href = res.DATAS.URL;
							}
							custom_modal_alert(custom_opts);
							return false;
						}

						custom_opts.title = '회원 가입이 완료 되었습니다. 관리자 승인 후 로그인이 가능합니다.';
						custom_opts.event_esc_key = false;

						custom_modal_alert(custom_opts, function() {
							link("login.php");
						});
					},
					error:function(request,status,error){
						$("#btn_join_proc").show();

						custom_modal_confirm_close();

						alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
					}
				});

			}else {
				$("#btn_join_proc").show();
				return false;
			}
		});

	});


})
</script>

<?php include("include/bottom.php") ?>
