<?php
	include $_SERVER['DOCUMENT_ROOT'].'/common/lib/comm.php';
	include $_SERVER['DOCUMENT_ROOT'].'/common/auth_chk.php';
	
	$nm_number = trim(avoid_crack($_POST["nm_number"]));

	if(empty($nm_number)) {
		AlertRedirect("차량정보가 없습니다. 올바른 방법으로 이용해주세요. ","/");
	}
	if(empty($_SESSION['s_cd_userid'])) {
		AlertRedirect("회원 로그인 후 이용할 수 있습니다.","/");
	}
	//테스트 서버 연결시 테스트 차량
	// 245머6418
	// 서울85바3080
	// 11고4424
	// 08오5060
	// 53라9319
	$test_user_id = (empty($_SESSION['s_cd_userid']) ? "noMember".rand(1000, 9999) : $_SESSION['s_cd_userid']);


	$ds_cate = 'A0';
?>

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


<main class="pb123">
	<section>
		<div class="sub--top--visual">
			<div class="contents--inner">
				<h2>차량 판매하기</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>차량 판매하기</span>
				</div>
			</div>
		</div>
	</section>

	<section class="mt60">
		<div class="car--accident--log">
			<h2>중고차 사고 이력 정보 보고서</h2>
			<div class="car--info">
				<div class="thumb"></div>
				<div class="car--num--sch--info">
					<h2 id="car_info"></h2>
					<p>정보 조회 일자 : <span id="r001_text"></span></p>
				</div>

			</div>
			<button type="button" class="btn btn-primary accident--show--btn" id="btn_accident_view">내차 사고이력 정보 자세히보기</button>
		</div>
	</section>


	<section class="mt50">
		<div class="car--accident--column">
			<h2>1. 중고차 사고 이력 정보 (요약)</h2>
			<div class="items--box">

				<div class="car--accident--box">
					<div class="thumb log--01"></div>
					<div class="desc">
						<h2>전손 보험 사고</h2>
						<div><strong id="r405"></strong>회</div>
					</div>
				</div>

				<div class="car--accident--box">
					<div class="thumb log--02"></div>
					<div class="desc">
						<h2>도난 보험 사고</h2>
						<div><strong id="r409"></strong>회</div>
					</div>
				</div>

				<div class="car--accident--box">
					<div class="thumb log--03"></div>
					<div class="desc">
						<h2>침수 보험 사고</h2>
						<div id="r407"></div>
					</div>
				</div>

				<div class="car--accident--box">
					<div class="thumb log--04"></div>
					<div class="desc">
						<h2>특수 용도 이력</h2>
						<div id="r302_text"></div>
					</div>
				</div>


				<div class="car--accident--box">
					<div class="thumb log--05"></div>
					<div class="desc">
						<h2>내차 피해</h2>
						<div><strong id="r401"></strong>회 (<span id="r402"></span>원)</div>
					</div>
				</div>

				<div class="car--accident--box">
					<div class="thumb log--06"></div>
					<div class="desc">
						<h2>상대차 피해</h2>
						<div><strong id="r403"></strong>회 (<span id="r404"></span>원)</div>
					</div>
				</div>

				<div class="car--accident--box">
					<div class="thumb log--07"></div>
					<div class="desc">
						<h2>소유자 변경</h2>
						<div><strong id="r204"></strong>회</div>
					</div>
				</div>

				<div class="car--accident--box">
					<div class="thumb log--08"></div>
					<div class="desc">
						<h2>차량번호 변경</h2>
						<div id="r201_text"></div>
					</div>
				</div>

				<div class="caution--text">
					<i class="caution--ic"></i>자동차 보험 사고 기록이 없었다고 해서 반드시 <strong>무사고</strong>라고 할 수는 없습니다.
				</div>
			</div>
		</div>
		<div class="car--accident--column">
			<h2>2. 자동차 일반 사양 정보</h2>
			<div class="description--text--wrap">
				<h2>자동차의 일반적인 사양 정보를 제공합니다.</h2>
				<div class="desc--contents">
					<dl>
						<dt>제조사</dt>
						<dd id="r111"></dd>
					</dl>
					<dl>
						<dt>자동차명</dt>
						<dd id="r005"></dd>
					</dl>
					<dl>
						<dt>배기량</dt>
						<dd id="r104"></dd>
					</dl>
					<dl>
						<dt>사용 연료</dt>
						<dd id="r106"></dd>
					</dl>
					<dl>
						<dt>연식</dt>
						<dd id="r004"></dd>
					</dl>
					<dl>
						<dt>차체 형상</dt>
						<dd id="r107"></dd>
					</dl>
					<dl>
						<dt>용도 및 차종</dt>
						<dd id="r101_text"></dd>
					</dl>
					<dl>
						<dt>최초 보험 가입 일자</dt>
						<dd id="r105_text"></dd>
					</dl>
				</div>
			</div>

		</div>
	</section>

	<div class="nm--btn--wrapper">
		<button type="button" class="btn btn-primary" id="btn_sell_car">차량판매하기</button>
	</div>


	<!-- loading -->
	<div class="loading_wrap hide">
		<?php include("../include/loading_dim.html") ?>
	</div>
	<!-- // loading -->

</main>

<form id="frmSearch" name="frmSearch" method="post" action="proc.php" enctype="multipart/form-data">
	<input type="hidden" name="mode" id="mode" value="PreStep"/>
	<input type="hidden" id="cd_model" name="cd_model" value="">
	<input type="hidden" id="cd_category" name="cd_category" value="">
	<input type="hidden" id="cd_brand" name="cd_brand" value="">
	<input type="hidden" id="ds_type" name="ds_type" value="A0">

	<input type="hidden" id="ds_year" name="ds_year" value="">
	<input type="hidden" id="nm_mileage" name="nm_mileage" value="">
	<input type="hidden" id="ds_transmission" name="ds_transmission" value="">
	<input type="hidden" id="ds_oil" name="ds_oil" value="">
	<input type="hidden" id="nm_color" name="nm_color" value="">
	<input type="hidden" id="ds_accident" name="ds_accident" value="">
	<input type="hidden" id="nm_cont_accident" name="nm_cont_accident" value="">
	<input type="hidden" id="nm_cont_pac" name="nm_cont_pac" value="">
	<input type="hidden" id="ds_business_num" name="ds_business_num" value="">
	<input type="hidden" id="nm_name" name="nm_name" value="">
	<input type="hidden" id="nm_number" name="nm_number" value="<?=$nm_number?>">
	<input type="hidden" id="nm_cc" name="nm_cc" value="">
	<input type="hidden" id="nm_regdate_yyyy" name="nm_regdate_yyyy" value="">
	<input type="hidden" id="nm_regdate_mm" name="nm_regdate_mm" value="">
	<input type="hidden" id="nm_regdate_dd" name="nm_regdate_dd" value="">
	<input type="hidden" id="ds_status" name="ds_status" value="A0">


	<input type="hidden" id="cd_car_history" name="cd_car_history" value="">
</form>


<!-- 레이어 팝업 -->
<?php include("../common/inc/inc_layer_popup.php") ?>
<script>
var nm_number = "<?=$nm_number?>";

function get_car_history() {
	var custom_opts = {
		title : '',
		btn_close_show : false,
	}

	$(".loading_wrap").removeClass("hide");

	$.post("/sell/car_info_car_history_proc.php", { nm_name: "<?=$_SESSION['s_nm_name']?>", nm_number: nm_number, cd_userid : "<?=$test_user_id?>"},function(res) {

		$(".loading_wrap").addClass("hide");

		if(res.CODE != "0000") {


			custom_opts.title = '차량 정보 조회에 실패하였습니다.';//res.MSG;
			custom_modal_alert(custom_opts);
			// alert(res.MSG);
			return false;
		}

		var CARINFO = res.DATAS.CARINFO;



		var car_info = CARINFO.r005 + " / "+nm_number;
		$("#car_info").text(car_info);

		var r001_text = CARINFO.r001.substring(0,4) + '-' + CARINFO.r001.substring(4,6) + '-'+CARINFO.r001.substring(6,8);
		$("#r001_text").text(r001_text);

		$("#r005").text(CARINFO.r005);
		$("#r111").text(CARINFO.r111);


		var r101_text = CARINFO.r103_text + " " + CARINFO.r102_text;
		$("#r101_text").text(r101_text);

		var r105_text = CARINFO.r105.substring(0,4) + '년 ' +CARINFO.r105.substring(4,6) + '월 '+CARINFO.r105.substring(6,8)+'일';
		$("#r105_text").text(r105_text);

		var r107 = "";
		if(typeof r107 != "undefined") {
			r107 = CARINFO.r107;
		}
		$("#r107").text(r107);

		$("#r004").text(CARINFO.r004);
		$("#r106").text(CARINFO.r106);
		$("#r104").text(CARINFO.r104+'cc');


		//중고차 사고 이력 정보 세팅
		var r201_text = "없음";
		if(parseInt(CARINFO.r201) > 0) {
			r201_text = "있음";
		}
		$("#r201_text").text(r201_text);
		$("#r204").text(CARINFO.r204);

		$("#r401").text(CARINFO.r401);
		$("#r402").text(number_format(""+CARINFO.r402));
		$("#r403").text(CARINFO.r403);
		$("#r404").text(number_format(""+CARINFO.r404));

		$("#r405").text(CARINFO.r405);
		$("#r407").text(CARINFO.r407);
		$("#r409").text(CARINFO.r409);
		var r407_text = "없음";
		if(parseInt(CARINFO.r407) > 0) {
			r407_text = "있음";
		}
		$("#r407_text").text(r407_text);


		var r302_text = "없음";
		if(CARINFO.r302 == "Y" || CARINFO.r303 == "Y" || CARINFO.r301 == "Y") {
			r302_text = "있음";
		}
		$("#r302_text").text(r302_text);

		//판매 차량 정보 세팅
		$("#cd_brand").val(CARINFO.CD_BRAND);
		$("#cd_model").val(CARINFO.CD_MODEL);
		$("#cd_category").val(CARINFO.CD_CATEGORY);
		$("#ds_year").val(CARINFO.r004);
		$("#ds_year").val(CARINFO.r004);


		var nm_regdate = CARINFO.r203[0].r203;
		$("#nm_regdate_yyyy").val(nm_regdate.substring(0,4));
		$("#nm_regdate_mm").val(nm_regdate.substring(4,6));
		$("#nm_cc").val(CARINFO.r104);
		$("#ds_oil").val(CARINFO.r106);

		//사고건수가 1건이상이면 사고유무를 자동 세팅
		if(parseInt(CARINFO.r501) > 0) {
			$("#ds_accident").val("A0");
		}else {
			$("#ds_accident").val("B0");
		}
		//주행거리 제공이면
		if(CARINFO.r601 == "1") {
			// console.log(CARINFO.r602[0]);
			for(var m in CARINFO.r602[0]) {
				if(m == "r602-03") {
					$("#nm_mileage").val(CARINFO.r602[0][m]);
				}
			}
			// var nm_mileage = ;.r602-03;
		}


		//사고이력정보 세팅
		cd_car_history = CARINFO.cd_car_history;
		$("#cd_car_history").val(cd_car_history);


	}, "json");

	return false;
}
$(function() {

	get_car_history();


	//차량이력정보 보기 팝업
	$("#btn_accident_view").on("click", function() {

		var custom_opts = {
			title : '',
			btn_close_show : false,
		}

		$.get("/sell/car_info_car_history_view.php", { mode: 'view', cd_car_history: cd_car_history},function(data) {
			$("#scr_cont").html(data);
			viewLayerPopup('car_history');
		});
	});

	$(".car_history_popup .close").on("click", function() {
		closeLayerPopup('car_history');
	});

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

		// var fieldlist = [["nm_number","차량번호"]];
		//
		// if (!fnCheckForm(fieldlist)){
		// 	return false;
		// }

		custom_opts.title = '차량을 판매 하시겠습니까?';
		custom_modal_confirm(custom_opts, function(confirm) {
			if(confirm) {
				$("#btn_sell_car").hide();
				var data = $("#frmSearch").serialize();
				$.ajax({
					url : 'proc.php',
					type: 'POST',
					dataType : 'json',
					data: data,
					success: function(res) {
						custom_modal_confirm_close();

						$("#btn_sell_car").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;
						}
						link("sell_step1.php?Step=1");
						//
						// custom_opts.title = '차량판매 페이지로 이동합니다.';
						// custom_opts.event_esc_key = false;
						//
						// custom_modal_alert(custom_opts, function() {
						// 	link("login.php");
						// });
					},
					error:function(request,status,error){
						$("#btn_sell_car").show();

						custom_modal_confirm_close();

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

			}else {
				$("#btn_sell_car").show();
				return false;
			}
		});
	});
});
</script>
<?php include("../include/inc_footer.php") ?>
<?
mysql_close($connect);
?>
