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

	$HEADER_LOC = 'main';
	$nav_title = "관리자 관리";


	$cd_dealerid = $_REQUEST["cd_dealerid"];

	$ds_type = "D1";

	$sql ="SELECT
				*
				FROM aap_master
				WHERE cd_dealerid='$cd_dealerid' AND ds_type = 'D1' AND ds_delind='N'";
// echo $sql;

	$mode = "Add";

	$ds_usertype = $_SESSION['s_ds_usertype'];

	$ds_company_bp1 =  $_SESSION['s_ds_company_bp1'];
	$ds_branch1 =  $_SESSION['s_ds_branch1'];
	$ds_company_bp2 =  $_SESSION['s_ds_company_bp2'];
	$ds_branch2 =  $_SESSION['s_ds_branch2'];
	$ds_company_bp3 =  $_SESSION['s_ds_company_bp3'];
	$ds_branch3 =  $_SESSION['s_ds_branch3'];

	$col=mysql_fetch_array(mysql_query($sql));
	if (!empty($col['cd_dealer'])){
		$cd_dealer 			= $col['cd_dealer'];
		$cd_dealerid 		= $col['cd_dealerid'];
		$cd_dealer_p 		= $col['cd_dealer_p'];
		$nm_pass 			= $col['nm_pass'];
		$ds_type 			= $col['ds_type'];
		$ds_usertype 		= $col['ds_usertype'];
		$ds_level 			= $col['ds_level'];
		$nm_company 		= $col['nm_company'];
		$nm_name 			= $col['nm_name'];
		$nm_birth 			= $col['nm_birth'];
		$nm_tel_01 			= $col['nm_tel_01'];
		$nm_tel_02 			= $col['nm_tel_02'];
		$nm_tel_03 			= $col['nm_tel_03'];
		$nm_hp1 			= $col['nm_hp_01'];
		$nm_hp2 			= $col['nm_hp_02'];
		$nm_hp3 			= $col['nm_hp_03'];
		$nm_fax_01 			= $col['nm_fax_01'];
		$nm_fax_02 			= $col['nm_fax_02'];
		$nm_fax_03 			= $col['nm_fax_03'];
		$nm_email 			= $col['nm_email'];
		$nm_zip 			= $col['nm_zip'];
		$nm_addr 			= $col['nm_addr'];
		$nm_addr_sub 		= $col['nm_addr_sub'];
		$nm_etc_01 			= $col['nm_etc_01'];
		$nm_etc_02 			= $col['nm_etc_02'];
		$nm_etc_03 			= $col['nm_etc_03'];
		$ds_status 			= $col['ds_status'];
		$nm_company_biz	 	= $col['nm_company_biz'];
		$nm_name_biz 		= $col['nm_name_biz'];
		$nm_no_biz 			= $col['nm_no_biz'];
		$nm_date_biz 		= $col['nm_date_biz'];
		$nm_type_biz 		= $col['nm_type_biz'];
		$nm_condition_biz 	= $col['nm_condition_biz'];
		$nm_tel_biz_01 		= $col['nm_tel_biz_01'];
		$nm_tel_biz_02 		= $col['nm_tel_biz_02'];
		$nm_tel_biz_03 		= $col['nm_tel_biz_03'];
		$nm_hp_biz_01 		= $col['nm_hp_biz_01'];
		$nm_hp_biz_02 		= $col['nm_hp_biz_02'];
		$nm_hp_biz_03 		= $col['nm_hp_biz_03'];
		$nm_fax_biz_01 		= $col['nm_fax_biz_01'];
		$nm_fax_biz_02 		= $col['nm_fax_biz_02'];
		$nm_fax_biz_03 		= $col['nm_fax_biz_03'];
		$nm_email_biz 		= $col['nm_email_biz'];
		$nm_zip_biz 		= $col['nm_zip_biz'];
		$nm_addr_biz 		= $col['nm_addr_biz'];
		$nm_addr_sub_biz 	= $col['nm_addr_sub_biz'];
		$ds_company_bp1 	= $col['ds_company_bp1'];
		$ds_branch1 		= $col['ds_branch1'];
		$ds_company_bp2 	= $col['ds_company_bp2'];
		$ds_branch2 		= $col['ds_branch2'];
		$ds_company_bp3 	= $col['ds_company_bp3'];
		$ds_branch3 		= $col['ds_branch3'];
		$nm_area 			= $col['nm_area'];
		$nm_title 			= $col['nm_title'];
		$nm_file 			= $col['nm_file'];
		$nm_etc 			= $col['nm_etc'];
		$nm_deposit 		= $col['nm_deposit'];
		$dt_insert 			= $col['dt_insert'];
		$nm_insert 			= $col['nm_insert'];
		$dt_update 			= $col['dt_update'];
		$nm_update 			= $col['nm_update'];
		$ds_delind 			= $col['ds_delind'];
	/*
		$arr_nm_info1 = explode("-",$nm_info1);
		if(count($arr_nm_info1) == 3){
			$nm_info11 = $arr_nm_info1['0'];
			$nm_info12 = $arr_nm_info1['1'];
			$nm_info13 = $arr_nm_info1['2'];
		}else{
			$nm_info11 = "";
			$nm_info12 = "";
			$nm_info13 = "";
		}

		$arr_nm_info3 = explode("@",$nm_info3);
		if(count($arr_nm_info3) == 2){
			$nm_info31 = $arr_nm_info3['0'];
			$nm_info32 = $arr_nm_info3['1'];
		}else{
			$nm_info31 = "";
			$nm_info32 = "";
		}
		$nm_info3 = $col['nm_info3'];
	*/
		$mode = "Update";
		$title_text = "수정";
	}else{
		$ds_level = "1";
		$title_text = "등록";
	}


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

<main class="pb123">
	<section>
		<div class="sub--top--visual">
			<div class="contents--inner">
				<h2>관리자 <?=$title_text?></h2>
				<p>이차조아에서는 사고 이력 조회 후 최고가 내차를 판매할 수 있습니다.</p>
			</div>
		</div>
	</section>


	<section>
		<div class="top--nav--wrap">
			<div class="contents--inner">
				<div class="breadcrumbs--wrap">
					<span></span>
					<span>관리자 관리</span>
					<span>관리자 <?=$title_text?></span>
				</div>
			</div>
		</div>
	</section>


	<section>
		<div class="car--info--wrap">
			<h2>관리자 <?=$title_text?></h2>
			<div class="form--wrap">
				<form id="frmMember" name="frmMember" method="post" action="manager_proc.php" enctype="multipart/form-data">
				<input type="hidden" name="validate" id="validate" value=""/>
				<input type="hidden" name="ds_type" id="ds_type" value="D1"/>
				<input type="hidden" name="ds_usertype" id="ds_usertype" value="<?=$ds_usertype?>"/>
				<input type="hidden" name="ds_company_bp1" id="ds_company_bp1" value="<?=$ds_company_bp1?>"/>
				<input type="hidden" name="ds_branch1" id="ds_branch1" value="<?=$ds_branch1?>"/>
				<input type="hidden" name="ds_company_bp2" id="ds_company_bp2" value="<?=$ds_company_bp2?>"/>
				<input type="hidden" name="ds_branch2" id="ds_branch2" value="<?=$ds_branch2?>"/>
				<input type="hidden" name="ds_company_bp3" id="ds_company_bp3" value="<?=$ds_company_bp3?>"/>
				<input type="hidden" name="ds_branch3" id="ds_branch3" value="<?=$ds_branch3?>"/>
				<input type="hidden" name="ds_level" id="ds_level" value="<?=$ds_level?>"/>
				<input type="hidden" name="cd_dealer" id="cd_dealer" value="<?=$cd_dealer?>"/>
				<input type="hidden" name="nm_hp" id="nm_hp" value="<?=$nm_hp?>"/>
				<input type="hidden" name="mode" id="mode" value="<?=$mode?>"/>
				<input type="hidden" name="PageNo" id="PageNo" value="<?=$PageNo?>"/>
				<?php if ($mode == "Add"){ ?>
					<input type="hidden" name="idchkresult" id="idchkresult" value="N"/>
				<?php }else{ ?>
					<input type="hidden" name="idchkresult" id="idchkresult" value="Y"/>
				<?php } ?>

				<div class="form--groups">
					<h2>아이디</h2>
					<div class="form--group d-flex align-items-center">
					<?php if ($mode == "Add"){ ?>
						<input type="text" class="" name="cd_dealerid" id="cd_dealerid" maxlength="20" value="<?=$cd_dealerid?>" >
						<label for="nm_file" class="ml10 btn btn-secondary d-flex align-items-center" style="height:43px;width: 146px;" id="btn_idchk">ID 중복확인</label>
						<!-- <p class="vaild--text mt-2">*사용가능한 아이디입니다.</p> -->
					<?php }else { ?>
						<input type="text" class="" name="cd_dealerid" id="cd_dealerid" maxlength="20" value="<?=$cd_dealerid?>" readonly>

					<?php } ?>

					</div>
				</div>

				<div class="form--groups mt-3">
					<h2>패스워드</h2>
					<div class="form--group">
						<input type="password" class="" style="" name="nm_pass" id="nm_pass" maxlength="20" />
					</div>
				</div>
			<?php if ($mode == "Add"){ ?>
				<div class="form--groups mt-3">
					<h2>패스워드 확인</h2>
					<div class="form--group">
						<input type="password" class="" style="" name="nm_pass2" id="nm_pass2" maxlength="20" />
					</div>
				</div>
			<?php } ?>
				<div class="form--groups">
					<h2>이름</h2>
					<div class="form--group">
						<input type="text" class="" style="" name="nm_name" id="nm_name" maxlength="20" value="<?=$nm_name?>" />
					</div>
				</div>

				<div class="form--groups">
					<h2>연락처</h2>
					<div class="form--group ty2">

						<select id="nm_hp1" name="nm_hp1">
							<option value="">선택</option>
						</select>

						<label for="" class="ml10">-</label>
						<input type="text" class="ml10" style="max-width:100px;" name="nm_hp2" id="nm_hp2" maxlength="4" value="<?=$nm_hp2?>" />

						<label for="" class="ml10">-</label>
						<input type="text" class="ml10" style="max-width:100px;" name="nm_hp3" id="nm_hp3" maxlength="4" value="<?=$nm_hp3?>" />

					</div>
				</div>

				<div class="form--groups">
					<h2>부서</h2>
					<div class="form--group">
						<input type="text" class="" style="" id="nm_title" name="nm_title" value="<?=$nm_title?>" />
					</div>
				</div>

				<div class="form--groups">
					<h2>사진(필수입력)</h2>
					<div class="form--group">
						<input type="file" id="nm_file" name="nm_file" class="" hidden style="" />
						<input type="text" id="upload-name"  readonly value="<?=$nm_file?>">
						<label for="nm_file" class="btn btn-secondary mt-3 d-flex justify-content-center align-items-center" style="width:100%; height:55px;">찾아보기</label>
					</div>
				</div>
				</form>
			</div>

		</div>
	</section>

</main>

<div class="btn--wrapper col-12 d-flex justify-content-center floating--btn mt-5">
	<button type="button" class="btn btn-secondary" style="width:190px; height:65px;" id="btn_back">목록</button>
<?php if($mode == "Update") { ?>
	<button type="button" class="btn btn-primary" style="width:190px; height:65px;" id="btn_write">정보수정</button>
	<button type="button" class="btn btn-secondary" style="width:180px; height:65px;" id="btn_delete">삭제</button>

<?php } else {?>
	<button type="button" class="btn btn-primary" style="width:190px; height:65px;" id="btn_write">등록하기</button>
<? } ?>
</div>




<script type="text/javascript">

	$(document).ready(function() {
		setCodeSelect("listPhone","nm_hp1","","","<?=$nm_hp1?>","선택","");

		$("#btn_back").on("click", function() {
			history.back(-1);
		})
		$("#cd_dealerid").bind("keyup",function(){
			 $("#idchkresult").val("N");
		});

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

	 		var fieldlist = [["cd_dealerid","아이디"]]

	 		if (!fnCheckForm(fieldlist)){
	 			return false;
	 		}

			var idReg = /^[a-z]+[a-z0-9]{4,19}$/g;
			if( !idReg.test( $("#cd_dealerid").val() ) ) {
				custom_opts.title = '아이디는 영문자로 시작하는 5~20자 영문자 또는 숫자이어야 합니다.';

				custom_modal_alert(custom_opts);
				return false;
			}

			if(!fnCheckAlphaNumber($("#cd_dealerid").val())){
				custom_opts.title = '아이디는 영문/숫자 조합으로 이루어져야 합니다.';

				custom_modal_alert(custom_opts);
				return false;
			}

			if($("#cd_dealerid").val().length <5){
				custom_opts.title = '5글자 이상 아이디를 입력하세요.';

				custom_modal_alert(custom_opts);
				return false;
			}

			$.post("manager_proc.php", { mode: "IdChk", cd_dealerid: $("#cd_dealerid").val()},function(data) {
				if(data=="Y"){
					custom_opts.title = '이용가능합니다. 아이디를 사용하시겠습니까?';
		  			custom_modal_confirm(custom_opts, function(confirm) {
		  				if(confirm) {
							$("#cd_dealerid").attr("readonly",true).css("background","#d2d3d5");
  							$("#idchkresult").val("Y");

		  					custom_modal_confirm_close();

		  				}
		  			});
				}else{
					$("#idchkresult").val("N");
					$("#cd_dealerid").attr("readonly",false).css("background","#fff");
					custom_opts.title = '사용불가능한 아이디 입니다.';

	  				custom_modal_alert(custom_opts);
	  				return false;
				}
			 });
			 return false;

		 });

		 $("#btn_delete").click(function() {
			 var custom_opts = {
	 			title : '',
	 			btn_close_show : false,
	 		}
			 custom_opts.title = '관리자를 삭제하시겠습니까?';
			 custom_modal_confirm(custom_opts, function(confirm) {
				 if(confirm) {
					$("#btn_delete").hide();
					var data = $("#frmMember").serialize();
					data+= "&mode=Delete";

					$.ajax({
						url : 'manager_proc.php',
						type: 'POST',
						dataType : 'json',
						data: data,
						success: function(res) {
							custom_modal_confirm_close();

							$("#btn_delete").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("manager_list.php");
							});
						},
						error:function(request,status,error){
							$("#btn_delete").show();

							custom_modal_confirm_close();

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

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

		$("#btn_write").bind("click",function(){
			var custom_opts = {
			   title : '',
			   btn_close_show : false,
			}
		<? if ($mode == "Add"){ ?>
			 var fieldlist = [["cd_dealerid","아이디"],["nm_pass","비밀번호"],["nm_pass2","비밀번호 확인"],["nm_name","이름"],["nm_hp1","핸드폰"],["nm_hp2","핸드폰"],["nm_hp3","핸드폰"]]
			 //,["nm_file","사진"]
		<? }else{ ?>
			 var fieldlist = [["nm_name","이름"],["nm_hp1","핸드폰"],["nm_hp2","핸드폰"],["nm_hp3","핸드폰"]]
		<? } ?>

			 if (!fnCheckForm(fieldlist)){
				 return false;
			 }else{

				 /*아이디 관련 처리*/

		<? if ($mode == "Add"){ ?>

				var idReg1 = /[a-z]/g;
				var idReg2 = /[0-9]/g;

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

					custom_modal_alert(custom_opts);
					return false;
				}

				if(!fnCheckAlphaNumber($("#cd_dealerid").val())){
					custom_opts.title = '아이디는 영문/숫자 조합으로 이루어져야 합니다.';

					custom_modal_alert(custom_opts);
					return false;
				}
				if($("#cd_dealerid").val().length <5){
					custom_opts.title = '5글자 이상 아이디를 입력하세요.';

 					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 <8){
					custom_opts.title = '8글자 이상 패스워드를 입력하세요.';

					custom_modal_alert(custom_opts);
					return false;
				}

				var check = /^(?=.*[a-zA-Z])(?=.*[!@#$%^*+=-])(?=.*[0-9]).{6,16}$/;
				var nm_pass = $("#nm_pass").val();
				if (!check.test(nm_pass))     {
					custom_opts.title = "비밀번호는 문자, 숫자, 특수문자의 조합으로 입력해주세요.";
					custom_modal_alert(custom_opts);
					return false;
				}

				if (nm_pass.length < 6 || nm_pass.length > 16) {
					custom_opts.title = "비밀번호는 6 ~ 16 자리로 입력해주세요.";
					custom_modal_alert(custom_opts);
					return false;
				}
				//
				// if(!isStrongValidFormPassword($("#nm_pass").val())){
				// 	return false;
				// }

		<? } ?>

				if($("#nm_pass").val().length > 0){
					if($("#cd_dealerid").val()==$("#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;
					}

					if(!isStrongValidFormPassword($("#nm_pass").val())){
						 return false;
					}
				}

				var nm_hp = $("#nm_hp1").val()+"-"+$("#nm_hp2").val()+"-"+$("#nm_hp3").val();
				$("#nm_hp").val(nm_hp);

				custom_opts.title = '정보를 등록하시겠습니까?';
				custom_modal_confirm(custom_opts, function(confirm) {
					if(confirm) {
						$("#btn_write").hide();
						var data = $("#frmMember").serialize();

						var formData = new FormData();

						$("#frmMember input, #frmMember select").each(function() {
							var input_name = $(this).attr("name");
							var input_value = $(this).val();
							formData.append(input_name, input_value);
						});

						$("input[type=file]").each(function() {
							var input_name = $(this).attr("name");
							var input_value = $(this)[0].files[0];
							formData.append(input_name, input_value);
						});

						$.ajax({
							url : 'manager_proc.php',
							type: 'POST',
							dataType : 'json',
							data: formData,
							processData: false,
							contentType: false,
							success: function(res) {
								custom_modal_confirm_close();

								$("#btn_write").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 = '관리자 <?=$title_text?>이 완료되었습니다.';
								custom_opts.event_esc_key = false;

								custom_modal_alert(custom_opts, function() {
									reload();
									// link("manager_form.php");
								});
							},
							error:function(request,status,error){
								$("#btn_write").show();

								custom_modal_confirm_close();

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

					}else {
						$("#btn_write").show();
						return false;
					}
				});
		 // fnCheckForm_ty10("정보를 등록하시겠습니까?");
						// var c=confirm("정보를 등록하시겠습니까?");
				// if(c){
				//     $("#btn_write").hide();
				//     $("#frmMember").submit();
				// }else{
				//     $("#btn_write").show();
				//     return false;
				// }
			}
		});

		$('#nm_file').on('change', function(){
			// 값이 변경되면
			if(window.FileReader){ // modern browser
				var filename = $(this)[0].files[0].name;
			} else {
				// old IE
				var filename = $(this).val().split('/').pop().split('\\').pop(); // 파일명만 추출
			} // 추출한 파일명 삽입
			$('#upload-name').val(filename);
		});

	});
</script>

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