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

	$mode 		= $_REQUEST['mode'];
	$ds_cate 	= $_REQUEST['ds_cate'];

//	Auth -> Step 단계에 따른 정상적인 요청 체크
	if($mode != "Step3" || $mode == null)
		AlertBack("지정되지 않은 요청입니다.");

//	$s_cd_sale = $_SESSION['s_cd_sale'];
	$s_cd_car = $_SESSION['s_cd_car'];
	$s_car_update_ing = $_SESSION['s_car_update_ing'];

	$ds_cate = $_REQUEST['ds_cate'];
	if(!$ds_cate) $ds_cate='A0';

	$ds_model_cate= $ds_cate;

	if($s_cd_car !="" || $s_cd_car != null){

		$sql ="SELECT a.*, s.nm_recommend
			FROM car_master a left outer join sale_master s on ( a.cd_car = s.cd_car )
			WHERE  a.cd_car='$s_cd_car' AND a.ds_delind='N'";

		$col=mysql_fetch_array(mysql_query($sql));

		if (!empty($col['cd_car'])){

			$cd_car = $col['cd_car'];

			$upload_file_main_name 	= $col['nm_file_main'];
			$upload_file_list_name 	= $col['nm_file_list'];

			$nm_file_front 			= $col['nm_file_front'];
			$nm_file_back 			= $col['nm_file_back'];
			$nm_file_side1 			= $col['nm_file_side1'];
			$nm_file_side2 			= $col['nm_file_side2'];
			$nm_file_dashboard 		= $col['nm_file_dashboard'];
			$nm_recommend 			= $col['nm_recommend'];

		}else{
			AlertBack("게시물을 가져오지 못하였습니다.");
		}
		$PageNo = $PageNo;
//		$mode = "Update";
	}else{
//		$mode="Add";
	}

	if ($mode=="Add"){
//		$cd_userid = $_SESSION[admin_cd_userid];
	}

	if($ds_open==""){$ds_open="Y";}
	if($cd_category==""){$cd_category="1";}


//	$s_cd_sale = $_SESSION['s_cd_sale'];

	if(!$ds_cate) $ds_cate='A0';

	$ds_model_cate= $ds_cate;

?>
<?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">
		<form id="frmForm" name="frmForm" method="post" action="sell_proc.php" enctype="multipart/form-data">
		<input type="hidden" name="car_report" id="car_report" value="<?=$car_report?>"/>
		<input type="hidden" name="ds_cate" id="ds_cate" value="<?=$ds_cate?>"/>
		<input type="hidden" name="cd_car" id="cd_car" value="<?=$s_staff_cd_car?>"/>
		<input type="hidden" name="mode" id="mode" value="<?=$mode?>"/>
		<input type="hidden" name="cd_userid" id="cd_userid" value="<?=$cd_userid?>"/>
		<input type="hidden" name="PageNo" id="PageNo" value="<?=$PageNo?>"/>
		<input type="hidden" name="upload_file_main_name" id="upload_file_main_name" value="<?=$upload_file_main_name?>"/>
		<input type="hidden" name="upload_file_list_name" id="upload_file_list_name" value="<?=$upload_file_list_name?>"/>
		<input type="hidden" name="upload_file_front_name" id="upload_file_front_name" value="<?=$upload_file_front_name?>"/>
		<input type="hidden" name="upload_file_back_name" id="upload_file_back_name" value="<?=$upload_file_back_name?>"/>
		<input type="hidden" name="upload_file_side1_name" id="upload_file_side1_name" value="<?=$upload_file_side1_name?>"/>
		<input type="hidden" name="upload_file_side2_name" id="upload_file_side2_name" value="<?=$upload_file_side2_name?>"/>
		<input type="hidden" name="upload_file_dashboard_name" id="upload_file_dashboard_name" value="<?=$upload_file_dashboard_name?>"/>
		<input type="hidden" name="cd_category" id="cd_category" value="<?=$cd_category?>"/>
		<input type="hidden" name="recommend_confirm" id="recommend_confirm" value="N"/>

		<div class="options--wrapper">
			<h2>사진 입력<span class="astc--text">(필수 정보)</span></h2>
			<div class="pic--add--wrap">

				<div class="add--box">
					<div class="thumb car--01">
						<input type="file" name="nm_file_front" hidden id="nm_file_front" onchange="viewThumb(0,this)" accept="image/*">
						<label class="add--btn" for="nm_file_front"></label>
					</div>
					<div class="desc">차량 전면</div>
				</div>

				<div class="add--box">
					<div class="thumb car--02">
						<input type="file" name="nm_file_back" hidden id="nm_file_back" onchange="viewThumb(1,this)" accept="image/*">
						<label class="add--btn" for="nm_file_back"></label>
					</div>
					<div class="desc">차량 후면</div>
				</div>

				<div class="add--box">
					<div class="thumb car--03">
						<input type="file" name="nm_file_side1" hidden id="nm_file_side1" onchange="viewThumb(2,this)" accept="image/*">
						<label class="add--btn" for="nm_file_side1"></label>
					</div>
					<div class="desc">운전석 측면</div>
				</div>

				<div class="add--box">
					<div class="thumb car--04">
						<input type="file" name="nm_file_side2" hidden id="nm_file_side2" onchange="viewThumb(3,this)" accept="image/*">
						<label class="add--btn" for="nm_file_side2"></label>
					</div>
					<div class="desc">조수석 측면</div>
				</div>

				<div class="add--box">
					<div class="thumb car--05">
						<input type="file" name="nm_file_dashboard" hidden id="nm_file_dashboard" onchange="viewThumb(4,this)" accept="image/*">
						<label class="add--btn" for="nm_file_dashboard"></label>
					</div>
					<div class="desc">계기판</div>
				</div>

			</div>
		</div>
		</form>
	</section>

	<div class="btn--wrapper car--btn--wrapper">
		<button type='button' id="btn_write" class="next--btn">다음(추가 사진 입력)<i class="next--ic"></i></button>
	</div>

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



<script>
	function openLayer2(ele){
		var _this = $("."+ele + "_popup");
		var _thisW = _this.outerWidth() / 2 - 115;
		_this.css('margin-left',-_thisW);
		_this.show();
	}
	function viewThumb(seq, arg){
		if (arg.files && arg.files[0])
		{
			var reader = new FileReader();
			reader.onload = function (e) {
				$(".add--box .thumb:eq("+seq+")").css({"background-image":"url('"+e.target.result+"')","background-size":"cover","background-position":"center"});
				// $(".add--box .thumb:eq("+seq+") span").html("");
			}
			reader.readAsDataURL(arg.files[0]);
		}

	}

	// layer pop
	function lockLayer(ele){
		var _this = $("#"+ele);
		_this.show();
		$("body").append("<div class='dim'></div>");
	}
	function unlockLayer(ele){
		var _this = $("#"+ele);
		_this.hide();
		$(".dim").remove();
	}

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

			var fieldlist = [["nm_file_front","차량 전면 사진"],["nm_file_side1","운전석 측면 사진"],["nm_file_side2","조수석 측면 사진"],["nm_file_back","차량 후면 사진"],["nm_file_dashboard","계기판 사진"]] ;
			if (!fnCheckForm(fieldlist)){
				return false;
			}


			custom_opts.title = '정보를 등록하시겠습니까?';
			custom_modal_confirm(custom_opts, function(confirm) {
				if(confirm) {

					custom_modal_confirm_close();

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

					$("#btn_write").hide();
					var formData = new FormData();
					// lockLayer('loading');

					// confirm_l4(false, 'dconfirm');

					$("input[type=hidden]").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);
					});

					var url = "proc.php";
					$.ajax({
						url: url,
						type: 'post',
						data: formData,
						dataType : 'json',
						processData: false,
						contentType: false,
						success: function(res) {

							$("#btn_write").show();

							custom_modal_confirm_close();

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

							if(res.CODE != "0000"){
								custom_opts.title = res.MSG;
								custom_modal_alert(custom_opts);
								return false;
							}
							link("sell_step4.php?mode=Step4");
						},
						error:function(request,status,error){
							$("#btn_write").show();
							$(".loading_wrap").addClass("hide");

							custom_modal_confirm_close();

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

				}else {
					$("#btn_write").show();
					return false;
				}
			});
		});
	});
</script>

<?php include("../include/inc_footer.php") ?>
<?
mysql_close($connect);
?>
