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

	$HEADER_LOC = 'main';
	$nav_title = "입찰중";

	$list_url 		= "prepare_list.php";

	$s_staff_user 		= $_SESSION['s_staff_user'];

	$searchtype 		= $_REQUEST['searchtype'];
	$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=8; //목록개수설정
	}else{$PageSize=$PageSize;}

	$pagestartpoint=($PageNo-1)*$PageSize;
	$add_sql = " WHERE s.ds_delind='N' ";
	//입찰중이거나 낙찰한 매물
	$add_sql.= " AND (s.ds_status = 'E0') "; // or ( s.ds_status = 'Z0' and (s.ds_status_contract is null or s.ds_status_contract = '') and s.ds_status_contract_step1 != 'Y' ) )";

	// echo $sql;
	$add_sql.= " AND s.cd_user = '$s_staff_user' AND is_staff = 'Y' ";

	$ds_type = "A0";	//A0 : 매입
	$add_sql.= " and s.ds_type = '$ds_type' ";

	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'";
	}

	$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)
				$add_sql";
	// echo $sql;

	$totalrows = @mysql_result(mysql_query($sql),0,0);

	$orderby = "order by s.dt_insert desc";

?>
<?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 valur="">선택</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) {
					$r = mysql_query("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.nm_price nm_price_sale
									, s.ds_status ds_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 '입찰중' 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.nm_act_yn in ('Y') then '유'  else '무' end as nm_act_yn_name
									, getCode('car_master','ds_accident',a.ds_accident) as nm_act_yn_name
									, getRemainTime(".LIMIT_TIME_SELL.", s.dt_approve, now()) as time_limit_sell
									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)
										$add_sql $orderby LIMIT $pagestartpoint,$PageSize");
					$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="prepare_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"><?=$col['nm_status_sale']?></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 mt-5">
			<?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") ?>
