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

	header("Content-Type: text/html; charset=utf-8");


	$cd_userid 	= $_POST['cd_userid'];
	$OWNER_NM 	= $_POST['nm_name'];
	$VHRNO 		= $_POST['nm_number'];
//	$OWNER_NM = "염수환";
//	$VHRNO = "37저4633";
	$arrCarInfo = array("OWNER_NM"=>$OWNER_NM, "VHRNO"=>$VHRNO);

	include_once($_SERVER['DOCUMENT_ROOT']."/common/lib/class.carhistory.php");

	$RES = array('CODE'=>'9999', 'MSG'=>'');
	try {

		$g_bszUser_key = "db,f8,e7,7b,4b,53,28,04,fc,f8,99,62,70,d5,ad,bb";
		$g_bszIV = "26,8d,66,a7,35,a8,1a,81,6f,ba,d9,fa,36,16,25,01";

		$carHistory = new CarHistory();

		if(empty($nm_number)) {
			throw new Exception('차량번호가 넘어오질 않았습니다.', 9999);
		}
		if(empty($cd_userid)) {
			throw new Exception('회원정보가 넘어오질 않았습니다.\n새로고침후에 이용해주세요', 9999);
		}
		// $nm_number = "28부5708";	//$nm_number
		// $nm_number = "53라9319";	//$nm_number

		$data = array();
		$data['sType'] 			= $carHistory->encrypt($g_bszIV, $g_bszUser_key, "1");			//1은 PC, 2는 모바일
		$data['carNum'] 		= $carHistory->encrypt($g_bszIV, $g_bszUser_key, $nm_number);
		$data['memberId'] 		= $carHistory->encrypt($g_bszIV, $g_bszUser_key, $cd_userid);
		$data['carNumType'] 	= "0";
		$data['stdDate'] 		= date("Ymd");

		$carData = $carHistory->getHistory($data);

		if($_SERVER['REMOTE_ADDR'] == '220.86.24.199') {
			// print_r($carData);
			// exit;
		}

		if($carData['r000'] != "000") {
			throw new Exception('차량이력정보를 가져오는데 실패하였습니다.', 9998);
		}

		$car_data = array();
		$cur_date = date("Y-m-d H:i:s");
		$car_data['dt_insert'] 		= $cur_date;
		$car_data['nm_insert'] 		= $cd_userid;
		$car_data['dt_update'] 		= $cur_date;
		$car_data['nm_update'] 		= $cd_userid;

		$db_result = $carHistory->db_update(0, $car_data, 'user');
		if (!$db_result) {
			throw new Exception('차량이력정보를 저장하는데 실패하였습니다.', 9997);
		}

		//제조국은 API에서 제공을 안해주니깐 1로 고정
		$cd_category = '1';
		//제조사
		$_whereis = " nm_brand = '".$carData['r111']."' ";

		$cd_brand = $dbCon->getOneDAO("cd_brand","car_brand", $_whereis, 1);

		if(!$cd_brand) {
			$sql="insert into car_brand(nm_brand,cd_category,nm_cont,ds_status,dt_insert,nm_insert,dt_update,nm_update,ds_delind)
					values('".$carData['r111']."','".$cd_category."','','A0',now(),'',now(),'','N')";
			// echo $sql;
			$result  = mysql_query($sql,$connect);
			$cd_brand  = mysql_insert_id();
		}

		//모델명
		$_whereis = " nm_model = '".$carData['r005']."' and cd_brand = '".$cd_brand."'";
		$cd_model = $dbCon->getOneDAO("cd_model","car_model", $_whereis, 1);

		if(!$cd_model) {
			$sql="insert into car_model(nm_model,cd_category,cd_brand,ds_status,ds_cate,dt_insert,nm_insert,dt_update,nm_update,ds_delind)
					values('".$carData['r005']."','".$cd_category."','".$cd_brand."','A0','A0',now(),'',now(),'','N')";
			$result  = mysql_query($sql,$connect);
			$cd_model  = mysql_insert_id();
		}

		$carData['CD_CATEGORY'] 	= $cd_category;
		$carData['CD_BRAND'] 		= $cd_brand;
		$carData['CD_MODEL'] 		= $cd_model;
		$carData['cd_car_history'] 	= $carHistory->cd_car_history;

		$DATAS = array();
		$DATAS['CARINFO'] = $carData;

		$RES['CODE'] = "0000";
		$RES['MSG'] = "정상처리.";
		$RES['DATAS'] = $DATAS;

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

	echo json_encode($RES);
	exit;

?>
