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

	$enc_passwd = "upro_contract";

	$RES = array("CODE" => "9999", "MSG" => "");

	$mode = $_POST['mode'];

	try {
		$login_data = $_POST['login_data'];
		$ds_agree1 = $_POST['ds_agree1'];
		$ds_agree2 = $_POST['ds_agree2'];
		$ds_agree3 = $_POST['ds_agree3'];

		switch($mode) {
			case 'dealer':
				if(empty($login_data)) {
					throw new Exception("로그인 정보가 없습니다.", "9998");
				}

				$dec_data = upro_decrypt(base64_decode($login_data), $enc_passwd);
				$dealer = json_decode($dec_data);

				if(empty($dealer->cd_dealer)) {
					throw new Exception("로그인 정보가 없습니다.", "9997");
				}

				if($ds_agree1 != "Y") {
					throw new Exception("[필수] 딜러 회원 계약서 항목에 동의해야 합니다.", "9995");
				}
				if($ds_agree2 != "Y") {
					throw new Exception("[필수] 개인정보 수집이용 항목에 동의해야 합니다.", "9995");
				}
				if($ds_agree3 != "Y") {
					throw new Exception("[필수] 회원 이용약관 항목에 동의해야 합니다.", "9995");
				}

				$table_name = "dealer_master a";
				$_whereis = "  cd_dealer = '". $dealer->cd_dealer . "' and cd_dealerid = '". $dealer->cd_dealerid ."' ";

				$row = $dbCon->selectDAO("*, ifnull((select ds_status from dealer_master where cd_dealer = a.cd_dealer_p), '') ds_status_p ", $table_name, $_whereis);
				if (!$row['cd_dealer']) {
					throw new Exception("로그인 정보가 없습니다.", "9991");
				}

				$sql = "UPDATE dealer_master set dt_lastlogin = now(), ds_info_contract = 'Y', ds_info_privacy = 'Y', ds_info_service = 'Y' where cd_dealer = '". $row['cd_dealer'] . "'";
				$result  = $dbCon->query($sql);

				$_SESSION['s_cd_dealerid']=$row[cd_dealerid];
				$_SESSION['s_nm_email']=$row[nm_email];
				$_SESSION['s_nm_name']=$row[nm_name];
				$_SESSION['s_nm_hp']=$row[nm_hp_01].$row[nm_hp_02].$row[nm_hp_03];
				$_SESSION['s_nm_hp_form']=$row[nm_hp_01]."-".$row[nm_hp_02]."-".$row[nm_hp_03];
				$_SESSION['s_ds_type']=$row[ds_type];	//$ds_type -> A0:개인,B0:업체,C0:비회원,D0:딜러,D1:딜러-관리자
				$_SESSION['s_ds_level']=$row[ds_level];
				$_SESSION['s_cd_dealer']=$row[cd_dealer];
				$_SESSION['s_cd_dealer_p']=$row[cd_dealer_p];
				$_SESSION['s_nm_area']=$row[nm_area];
		//				$_SESSION['s_ds_area_sub']=$row[ds_area_sub];
				$_SESSION['s_ds_usertype']=$row[ds_usertype];	//$ds_usertype	ds_type 상세 사용 가능 -> A0:개인,B0:업체,B1:업체1,B2:업체2,C0:비회원

				$_SESSION['s_ds_company_bp1']=$row[ds_company_bp1];	//관계사1
				$_SESSION['s_ds_branch1']=$row[ds_branch1];			//지점1
				$_SESSION['s_ds_company_bp2']=$row[ds_company_bp2];	//관계사2
				$_SESSION['s_ds_branch2']=$row[ds_branch2];			//지점2
				$_SESSION['s_ds_company_bp3']=$row[ds_company_bp3];	//관계사3
				$_SESSION['s_ds_branch3']=$row[ds_branch3];			//지점3

				$_SESSION['s_ds_kind']= substr($row[ds_type],0,1);	//$s_ds_kind -> A:개인, D:딜러,딜러-관리자		예)(($row[ds_type] == "A0")?"D0":"D1");
				$_SESSION['s_ds_status_p']=$row[ds_status_p];	//총괄관리자(딜러) 승인여부 : Z0-승인, 이외-미승인
				break;

			//회원은 아직 사용안함 
			case 'member':
				break;
		}


		$RES['CODE'] = "0000";
		$RES['MSG'] = "정상";

	}catch(Exception $e) {
		$RES['CODE'] = $e->getCode();
		$RES['MSG'] = $e->getMessage();
	}

	echo json_encode($RES);


	mysql_close($connect);
?>
