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

	$HEADER_LOC = 'main';
	$nav_title = "거래연기";

	$list_url 		= "delay_list.php";

	$s_cd_dealer 		= $_SESSION['s_cd_dealer'];
	$s_cd_dealer_p 		= $_SESSION['s_cd_dealer_p'];
	$s_aap_ds_type 			= $_SESSION['s_aap_ds_type'];
	$s_ds_usertype 		= $_SESSION['s_ds_usertype'];

	$search_type 		= $_REQUEST['search_type'];
	$search_text		= $_REQUEST['search_text'];
	$searchword 		= $_REQUEST['searchword'];
	$ds_usertype 		= $_REQUEST['ds_usertype'];

	$PageNo 			= $_REQUEST['pageno'];
	$PageSize 			= $_REQUEST['PageSize'];
	if (empty($PageNo)) {$PageNo=1;}else{$PageNo=$PageNo;}
	if (empty($PageSize)) {
		$PageSize=4; //목록개수설정
	}else{$PageSize=$PageSize;}

	$pagestartpoint=($PageNo-1)*$PageSize;
	$sql_from = "";
	$add_sql = " WHERE s.ds_delind='N'";
	$orderby = "order by s.dt_insert desc";

	$ds_type = "A0";	//A0 : 매입
	$add_sql.= " AND s.ds_type = '$ds_type' and s.ds_dealer_type = 'A0'"; //AAP 매물만

	if($s_aap_ds_type == "D0"){	//딜러
		$sql_ds_type = " AND cd_dealer_p = '$s_cd_dealer' ";
	}else{	//딜러-관리자
		$sql_ds_type = " AND cd_dealer = '$s_cd_dealer' ";
	}

	if($searchword != ""){
		if($searchtype=="" || $searchtype==null){
			$add_sql.= " AND (s.nm_name like '$searchword%' or a.nm_number like '$searchword%') ";
		}else{
			$add_sql.= " AND $searchtype like '$searchword%'";
		}
	}

	if($ds_usertype != ""){
		$add_sql.= " AND s.ds_usertype = '$ds_usertype'";
	}


	$add_sql.= " AND s.ds_status in ('Z0') AND s.ds_status_contract in ('B0')";

	$sql_from = $sql_from. " inner join ( ";
	$sql_from = $sql_from. "  select cd_sale from tender_master where ds_delind = 'N' and ds_status in ('Z0') ".$sql_ds_type." group by cd_sale) h ";
	$sql_from = $sql_from. " on (s.cd_sale = h.cd_sale)  ";
//	$add_sql.= "	and h.cd_sale is not null ";

	if(!empty($search_text) && !empty($search_type)) {
		$add_sql.= " AND $search_type like '$search_text%' ";
	}

	$sql = "select count(*) from sale_master s
				inner join car_master a on ( s.cd_car = a.cd_car )
				LEFT OUTER JOIN car_model b on (a.cd_model = b.cd_model)
				LEFT OUTER JOIN car_brand d on (a.cd_brand = d.cd_brand)
				$sql_from
				$add_sql";
	// echo $sql;
	// exit;
	// echo "select count(*) from sale_master s inner join car_master a on (	s.cd_car = a.cd_car ) $sql_from $sql";
	if($_SERVER['REMOTE_ADDR'] == '220.86.25.88') {
		// echo "select count(*) from sale_master s inner join car_master a on (	s.cd_car = a.cd_car ) $sql_from $sql";
	}
	$totalrows = @mysql_result(mysql_query($sql),0,0);

?>
<?php include("include/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="car--list--wrap ty2">
			<form id="frmSearch" name="frmSearch" method="get">
			<input type="hidden" id="PageSort" name="PageSort">
			<input type="hidden" id="PageSize" name="PageSize">
			<div class="title--wrapper">
				<div class="d-flex justify-content-end sch--m--wrapper">
					<div>
						<select id="searchtype" name="searchtype" class="form-select" style="width:120px;">
							<option value="">선택</option>
							<option value="s.nm_name"<? if($searchtype =="s.nm_name"){?> selected<?}?>>소유주명</option>
							<option value="a.nm_number"<? if($searchtype =="a.nm_number"){?> selected<?}?>>차량번호</option>
							<option value="d.nm_brand"<? if($searchtype =="d.nm_brand"){?> selected<?}?>>제조사</option>
							<option value="b.nm_model"<? if($searchtype =="b.nm_model"){?> selected<?}?>>모델명</option>
						</select>
					</div>
					<div class="ms-2 sch--m--wrap">
						<input type="text" id="searchword" name="searchword" class="form-control" placeholder="검색어를 입력하세요" value="<?=$searchword?>">
						<button type="button" class="sch--t--btn" id="btn_search"></button>
					</div>
				</div>
			</div>
			</form>
			<ul>

			<?php
				if($totalrows>0) {
					$sql = "SELECT
								a.*
								, b.nm_model
								, d.nm_brand
								, getCode('car_master','ds_type',a.ds_type) as nm_type
								, getCode('car_master','ds_open',a.ds_open) as nm_open
								, getCode('car_master','ds_sales',a.ds_sales) as nm_sales
								, getCode('car_master','ds_transmission',a.ds_transmission) as nm_transmission
								, getCode('member_master','ds_area',s.ds_area) as nm_area
								, getCode('sale_master','ds_status',s.ds_status) as nm_status
								, s.cd_sale
								, s.nm_name nm_name_sale
								, s.ds_status ds_status_sale
								, s.ds_aap_status ds_aap_status_sale
								, case when s.ds_status in ('B0','C0') then '미승인' when s.ds_status in ('D0') then '반려' when s.ds_status in ('E0','F0') then case when s.ds_status = 'E0' then '입찰대기' else '입찰중' end when s.ds_status in ('G0') then '입찰마감' when s.ds_status in ('Z0') then '낙찰' when s.ds_status in ('ZS') then '구매확정' else '' end as nm_status_sale
								-- , case when a.ds_accident in ('A0') then '유'  when a.ds_accident in ('B0') then '무' end as nm_act_yn_name
								, getCode('car_master','ds_accident',a.ds_accident) as nm_act_yn_name
								-- , getRemainTime(".LIMIT_TIME_TENDER.", s.dt_approve, now()) as time_limit_tender
								, getRemainTime(".LIMIT_TIME_SELL.", s.dt_approve, now()) as time_limit_sell
								, getRemainTime(s.ds_auction_time, s.dt_approve, now()) as time_limit_tender
								, ifnull((select min(nm_price) from tender_master where ds_delind = 'N' and cd_sale = s.cd_sale ".$sql_ds_type."), '') nm_price_tender
								FROM sale_master s
									inner join car_master a on (s.cd_car = a.cd_car)
									left outer join car_model b on (a.cd_model = b.cd_model)
									left outer join car_brand d on (a.cd_brand = d.cd_brand)
									$sql_from
									$add_sql $orderby LIMIT $pagestartpoint,$PageSize";
					// echo $sql;
					$r = mysql_query($sql);


					$i=1;
					while($col = mysql_fetch_array($r)) {

						$tmpNum =$totalrows-($PageSize*($PageNo-1));
						$no = ($tmpNum - $i)+1;

						// $car_image = "/images/common/bg_no_photo.png";
						// if(file_exists($_SERVER['DOCUMENT_ROOT'].UPLOAD_PATH_CAR."nm_file_main/ratio/".$col['nm_file_front'])) {
						// 	$car_image = UPLOAD_PATH_CAR."nm_file_main/ratio/".$col['nm_file_front'];
						// }
						$car_image = getCarBasicThumbImage($col['cd_car'], $col['nm_file_front'], '차량전면', false, 'car_basic');

			?>

					<li class="" cd_sale="<?=$col['cd_sale']?>">
						<a href="delay_view.php?cd_sale=<?=$col['cd_sale']?>&ListPageNo=<?=$PageNo?>&PageSort=<?=$PageSort?>&PageSize=<?=$PageSort?>&searchtype=<?=$searchtype?>&searchword=<?=$searchword?>">
							<div class="car--box--wrap ty2">
								<div class="thumb--wrap">
									<?=$car_image?>
								</div>



								<div class="car--info">
									<h2 class="car--name"><?=$col['nm_brand']." ".$col['nm_model']?></h2>
									<div class="car--inner--info">
										<div>
											<span><?=$col['nm_mileage']?>km</span>
											<span><?=$col['nm_regdate_mm']?>년 <?=$col['nm_regdate_yyyy']?>월식</span>
										</div>
										<div>
											<span>사고유무: <?=$col['nm_act_yn_name']?></span>
											<span><?=$col['nm_area']?></span>
										</div>
									</div>
									<div class="status--wrap">
										<span class="ready">거래연기</span>
										<!--
											상태값 클래스
											status--companion : 반려
											tr--delay : 연기
											tr--fail : 거래 실패
											sell--fail : 판매 실패
											ready : 대기
										-->
										<!-- <span class="status--companion">반려</span> -->
									</div>
								</div>

							</div>
						</a>
					</li>

			<?php
						$i++;
					}
				}else{
			?>
					<li class="">
						<div class="car--box--wrap ty2">
							등록된 정보가 없습니다.
						</div>
					</li>
			<?php
				}
			?>
			</ul>
		</div>
		<div class="col-12 d-flex justify-content-center">
			<?php
				$url = "./$list_url?searchtype=$searchtype&searchword=$searchword&ds_status=$ds_status&ds_usertype=$ds_usertype&PageSort=$PageSort";
				ListMobilePg($url ,$PageNo,$PageSize,10,$totalrows,$leftboard[1],$rootpath);
			?>
		</div>
	</section>


</main>

<script>
function go_search() {
	$("#frmSearch").submit();
}
$(function() {
	$("#btn_search").on("click", function() {
		go_search();
	});
});
</script>

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