<?php

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

	$cd_model = $_REQUEST['cd_model'];

	$mp = "11";	//set top nav

	//$cd_brand = $_REQUEST[cd_category];
	//if(!$cd_category) $cd_category='1';
	//$ds_cate = $_REQUEST[ds_cate];
	//if(!$ds_cate) $ds_cate='A0';

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

		$sql ="SELECT
				*
			FROM car_model mst
			WHERE  cd_model='$cd_model' AND ds_delind='N' ";

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

		if (!empty($col['cd_model'])){
			$cd_model =$col["cd_model"];
			$cd_category =$col["cd_category"];
			$nm_model  =$col["nm_model"];
			$cd_brand  =$col["cd_brand"];
			$nm_file  =$col["nm_file"];
			$mode ="Update";
		}else{
			AlertBack("게시물을 가져오지 못하였습니다.");
		}
		$PageNo = $PageNo;
	}else{
		$mode="Add";
	}

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

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

<section id="contents">

	<!-- 모델 -->
	<h2>모델</h2>
	<form id="frmForm" name="frmForm" method="post" action="model_proc.php" enctype="multipart/form-data">
	<input type="hidden" name="cd_model" id="cd_model" value="<?=$cd_model?>"/>
	<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="ds_status" id="ds_status" value="<?=$ds_status?>"/>
	<input type="hidden" name="PageNo" id="PageNo" value="<?=$PageNo?>"/>
	<div class="tbl_wrap">
		<table class="tbl_list2 ty2">
			<colgroup>
				<col width="20%">
				<col width="*">
			</colgroup>

			<tr>
				<th>구분</th>
				<td>
					<span class="select" style="width:200px;" id="sel_cd_category">
						<select name="cd_category" id="cd_category" onchange="cd_category_chg(this.value)"></select>
					</span>
				</td>
			</tr>
			<tr>
				<th>제조사</th>
				<td>
					<span class="select" style="width:200px;" id="sel_cd_brand">
						<select name="cd_brand" id="cd_brand" onchange="cd_brand_chg(this.value)"></select>
					</span>
				</td>
			</tr>
			<tr>
				<th>모델</th>
				<td><input type="text" class="inp_txt" style="width:60%;" name="nm_model" id="nm_model" value="<?=$nm_model?>"></td>
			</tr>

		</table>
	</div>
	</form>

	<div class="btn_right">
		<? if($mode=="Add"){?>
		<a href="#" class="rnd_btn ty2 black" id="btn_write">등록</a>
		<?}elseif($mode=="Update"){?>
		<a href="#" class="rnd_btn ty2 black" id="btn_write">수정</a>
		<a href="#" class="rnd_btn ty2 black" onclick="fnDelete('<?=$cd_model?>');">삭제</a>
		<?}?>
		<a href="#" class="rnd_btn ty2 black" onclick="fnList();return false;">목록</a>
	</div>
</section>
<form id="delFrm" name="delFrm" method="post" action="./model_proc.php">
	<input type="hidden" name="mode" id="mode" value="Delete"/>
	<input type="hidden" name="cd_model" id="cd_model_del"/>
</form>
<script language="javascript" type="text/javascript">
	var ds_mode ="<?=$mode?>";
	function fnList(){
		location.replace("./model.php");
	}

	function fnDelete(seq){
		$("#cd_model_del").val(seq);

		var custom_opts = {
			title : '정말로 삭제 하시겠습니까?',
			btn_close_show : false,
		}
		custom_modal_confirm(custom_opts, function(confirm) {
			if(confirm) {
				$("#btn_delete").hide();
				var data = $("#delFrm").serialize();
				$.ajax({
					url : 'model_proc.php',
					type: 'POST',
					dataType : 'json',
					data: data,
					success: function(res) {
						custom_modal_confirm_close();

						$("#btn_delete").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 = '삭제 되었습니다.';
						custom_opts.event_esc_key = false;

						custom_modal_alert(custom_opts, function() {
							link("<?=ADMIN_PATH?>/car/model.php");
						});
					},
					error:function(request,status,error){
						$("#btn_delete").show();

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

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

	}
	function cd_category_chg(val) {
		// console.log("AAAAA")
		setCodeSelectJqTransForm("listBrandBycate","cd_brand","car_brand","cd_brand","",'선택하세요',val);
	}
	function cd_brand_chg(val) {
	}

	$(document).ready(function() {
		setCodeSelectJqTransForm("listcategory","cd_category", "car_category","cd_category","<?=$cd_category?>","선택하세요","");
		setCodeSelectJqTransForm("listBrandBycate","cd_brand","car_brand","cd_brand",'<?=$cd_brand?>','선택하세요','<?=$cd_category?>');


		$("#btn_write").bind("click",function(){
			var fieldlist = [["cd_category","구분"],["cd_brand","제조사"],["nm_model","모델"]]
			if (!fnCheckForm(fieldlist)){
				return false;
			}

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

			var mode = "<?=$mode?>";

			if(mode == "Update") {
				custom_opts.title = '정보를 수정하시겠습니까?';
			}else {
				custom_opts.title = '정보를 등록하시겠습니까?';
			}

			custom_modal_confirm(custom_opts, function(confirm) {
				if(confirm) {
					$("#btn_write").hide();
					var data = $("#frmForm").serialize();
					$.ajax({
						url : 'model_proc.php',
						type: 'POST',
						dataType : 'json',
						data: data,
						success: function(res) {
							custom_modal_confirm_close();

							$("#btn_write").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;
							}

							if(mode == "Update") {
								custom_opts.title = '수정이 완료되었습니다.';
							}else {
								custom_opts.title = '등록이 완료되었습니다.';
							}
							custom_opts.event_esc_key = false;

							custom_modal_alert(custom_opts, function() {
								reload();
							});
						},
						error:function(request,status,error){
							$("#btn_write").show();
							custom_modal_confirm_close();

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

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

		});
	});
</script>
<? include("../inc/inc_footer.php") ?>
<? include("../inc/inc_bottom.php") ?>
<?
mysql_close($connect);
?>
