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


	$cd_dealerid = $_SESSION['s_cd_dealerid'];

	$sql = "SELECT * FROM aap_master where cd_dealerid = '$cd_dealerid' LIMIT 1";
	$col=mysql_fetch_array(mysql_query($sql));

	if (empty($col['cd_dealer'])){
		AlertRedirect("로그인되어 있지 않습니다.", "login.php");
	}

	// extract($col);
	// foreach($col as $column => $val) {
	// 	${$column} = $val;
	// 	echo $column.'<br/>';
	// 	if($column == "nm_np") {
	// 		echo ${$column};
	// 		exit;
	// 	}
	// }

	$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_hp 			= $col['nm_hp'];
	$nm_hp_01 		= $col['nm_hp_01'];
	$nm_hp_02 		= $col['nm_hp_02'];
	$nm_hp_03 		= $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'];
	$dt_lastlogin 	= $col['dt_lastlogin'];
	$ds_delind 		= $col['ds_delind'];
	$ds_company_bp 	= $col['ds_company_bp'];
	$ds_branch 		= $col['ds_branch'];
	$enc_val 		= $col['enc_val'];
	$device_uuid 	= $col['device_uuid'];
	$device_os 		= $col['device_os'];

	list($nm_hp1, $nm_hp2, $nm_hp3) = explode("-", $nm_hp);
?>
<?php include("include/header.php") ?>
<style>
.txt_red{color:#be0606;}
</style>
<main class="normal--wrap pb123">
	<section class="join--form--wrap">
		<h2 class="">이차조아 <span>AAP 직원</span> 정보수정</h2>
		<p></p>
		<div class="join--form--contents">
			<form id="frmMember" name="frmMember" method="post" action="mypage_proc.php" enctype="multipart/form-data">
			<input type="hidden" name="mode" id="mode" value="Update"/>
			<input type="hidden" name="nm_hp" id="nm_hp" value=""/>
			<input type="hidden" name="nm_email" id="nm_email" value=""/>

			<table>
				<tbody>
					<tr>
						<th>아이디</th>
						<td>
							<?=$cd_dealerid?>
						</td>
					</tr>
					<tr>
						<th>변경 패스워드</th>
						<td>
							<input type="password" class="form-control" name="nm_pass" id="nm_pass" maxlength="20">
						</td>
					</tr>
					<tr>
						<th>이름</th>
						<td>
							<input type="text" class="form-control" name="nm_name" id="nm_name" maxlength="20" value="<?=$nm_name?>">
						</td>
					</tr>
					<tr>
						<th>연락처</th>
						<td>
							<div class="d-flex align-items-center">
								<select id="nm_hp1" name="nm_hp1" class="form-select" style="max-width:90px;"></select>
								<label for="nm_hp2" class="ml10 mr10">-</label>
								<input type="text" class="form-control" name="nm_hp2" id="nm_hp2" style="max-width:105px;" maxlength="4" value="<?=$nm_hp2?>">
								<label for="nm_hp3" class="ml10 mr10">-</label>
								<input type="text" class="form-control" name="nm_hp3" id="nm_hp3" style="max-width:105px;" maxlength="4" value="<?=$nm_hp3?>">
							</div>
						</td>
					</tr>

					<tr>
						<th>관계사</th>
						<td>
							<div class="d-flex align-items-center">
								<select name="ds_company_bp" id="ds_company_bp" class="form-select ml10" style="max-width:170px;"></select>
							</div>
						</td>
					</tr>
					<tr>
						<th>지점</th>
						<td>
							<div class="d-flex align-items-center">
								<select name="ds_branch" id="ds_branch" class="form-select ml10" style="max-width:170px;"></select>

							</div>
						</td>
					</tr>
				</tbody>
			</table>

			<div class="col-12 d-flex justify-content-center join--btn--wrap mt90">
				<button type="button" class="btn btn-secondary" style="width:180px; height:55px;" id="btn_back">취소</button>
				<button type="button" class="btn btn-primary ml15" style="width:180px; height:55px;" id="btn_modify_proc">정보수정<i class="join--ic"></i></button>
			</div>
			</form>

		</div>

	</section>
</main>
<script>
$(function() {
	setCodeSelect("listPhone","nm_hp1","","","<?=$nm_hp1?>","선택","");
	setCodeSelect("listEmail","nm_email3","","","","직접입력","");
	setCodeSelect("listcode","ds_company_bp", "aap_master","ds_company_bp","<?=$ds_company_bp?>","선택하세요","");
	setCodeSelect("listcode","ds_branch", "aap_master","ds_branch","<?=$ds_branch?>","선택하세요","<?=$ds_company_bp?>");

	$("#btn_back").on("click", function() {
		history.back(-1);
	});
	$("#ds_company_bp").change(function(){
		setCodeSelect("listcode","ds_branch", "aap_master","ds_branch","","선택하세요",$(this).val());
	});

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

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

		var fieldlist = [["nm_name","이름"],["nm_hp1","연락처"],["nm_hp2","연락처"],["nm_hp3","연락처"],["ds_company_bp","관계사"]]

		var nm_email3 = $("#nm_email3").val();
		if(nm_email3 == "") {
			fieldlist.push(["nm_email2"]);
		}

		//상사회원은 지점이 없음.
		var ds_company_bp = $("#ds_company_bp").val();
		if(ds_company_bp != "A0") {
			fieldlist.push(["ds_branch", "지점"]);
		}
		if (!fnCheckForm(fieldlist)){
			return false;
		}

		/*패스워드 처리*/


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

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

			custom_modal_alert(custom_opts);
			return false;
		}

		custom_opts.title = '정보를 수정 하시겠습니까?\n정보 수정시 재로그인하셔야 합니다.';
		custom_modal_confirm(custom_opts, function(confirm) {
			if(confirm) {
				$("#btn_modify_proc").hide();
				var data = $("#frmMember").serialize();
				$.ajax({
					url : 'mypage_proc.php',
					type: 'POST',
					dataType : 'json',
					data: data,
					success: function(res) {
						custom_modal_confirm_close();

						$("#btn_modify_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 = '정보수정이 완료되었습니다.\n재로그인을 위해 로그인 페이지로 이동합니다.';
						custom_opts.event_esc_key = false;

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

						custom_modal_confirm_close();

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

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

	});


})
</script>
<?php include("include/footer.php") ?>
