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

	$list_url = "sell_list.php";

	$cd_user = $_SESSION['s_cd_user'];

	$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;
	$sql = "where s.ds_delind='N'";

	$sql = $sql. "	and s.cd_user='$cd_user' ";

	$searchtype = $_REQUEST['searchtype'];
	$searchword = $_REQUEST['searchword'];
	$ds_usertype = $_REQUEST['ds_usertype'];
	$nm_contract_date_month = $_REQUEST['nm_contract_date_month'];
	$nm_contract_date_year = $_REQUEST['nm_contract_date_year'];

	$ds_status = 'ZS';

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

	if(empty($nm_contract_date_year) && empty($nm_contract_date_month)) {
		$nm_contract_date_year = date("Y");
		$nm_contract_date_month = date("m");
	}

	$sql = $sql. "	and left(nm_contract_date, 7) = '".$nm_contract_date_year."-".$nm_contract_date_month."'";

	if($ds_usertype != ""){
		$sql = $sql. "	and s.ds_usertype = '$ds_usertype'";
	}

	$sql = $sql. "	and s.ds_status in ('ZS')";

	$orderby = " order by s.cd_sale desc ";
	$totalrows = @mysql_result(mysql_query("select count(*) from sale_master s inner join car_master a on (	s.cd_car = a.cd_car ) $sql"),0,0);

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

<section id="wrap">
	<section id="visual" class="visual_mypage">
		<h2>마이페이지</h2>
		<p>쉽고 간편한 내차팔기, 견적산출, 최종 매각까지 어떤 수수료도 요구하지 않습니다.</p>
	</section>
	<form id="frmCompete" name="frmCompete" method="get">
	<section id="content">
		<?
			// /common/lib/func.php 1662에 정의됨
			$is_upro_member = getUproMember($_SESSION['s_cd_user']);
			$depth_info = array("NAME" => array("입찰정보", "거래완료"));
			if($is_upro_member) {
				include_once($_SERVER['DOCUMENT_ROOT']."/mypage/inc_menu_member_company.php");
			}else {
				include_once($_SERVER['DOCUMENT_ROOT']."/mypage/inc_menu_main_member.php");
			}
		?>
		<!-- <ul class="location">
			<li><a href="/" class="home">홈</a></li>
			<li><span class="depth1">입찰정보</span>
				<ul class="sub_depth">
          <li><a href="/mypage/mypage.php">마이페이지</a></li>
          <li><a href="/sell/sell_step1.php">내찰팔기</a></li>
					<li><a href="/mypage/sell_list.php">전체 입찰내역</a></li>
          <li><a href="/mypage/sell_list_04.php">낙찰내역</a></li>
          <li><a href="/mypage/sell_list_03.php">거래완료</a></li>
          <li><a href="/mypage/sell_list_02.php">유찰내역</a></li>
					<li><a href="/mypage/sell_list_05.php">거래실패/연기</a></li>
					<li><a href="/mypage/qna_list.php?cd_board=02&ds_kind=A">문의내역</a></li>
				</ul>
			</li>
			<li><span class="depth2">거래완료</span></li>
		</ul> -->

		<h3 class="date__title">
      <div>거래완료</div>
      <div class="date__selector">
        <select id="nm_contract_date_year" name="nm_contract_date_year">
          <?
            for($i=2021; $i>=0; $i--){
          ?>
            <option value="<?=$i?>" <?=($nm_contract_date_year == $i ? "selected" : "") ?>><?=$i ?></option>
          <?
            }
          ?>
        </select>
        년
        <select id="nm_contract_date_month" name="nm_contract_date_month">
          <?
            for($i=1; $i<=12; $i++){
          ?>
            <option value="<?=sprintf("%02d", $i);?>"  <?=($nm_contract_date_month == sprintf("%02d", $i) ? "selected" : "") ?>><?=sprintf("%02d", $i);?></option>
          <?
            }
          ?>
        </select>
        월
      </div>
    </h3>
		<table class="tbl_v">
			<colgroup>
				<col width="7%">
				<col width="*">
				<col width="10%">
                <col width="10%">
				<col width="10%">
				<col width="15%">
				<col width="15%">
			</colgroup>
			<thead>
			<tr>
				<th>번호</th>
				<th>차종</th>
                <th>차량번호</th>
				<th>년식</th>
				<th>지역</th>
				<th>거래완료일</th>
				<th>처리상태</th>
			</tr>
			</thead>
			<tbody>
<?
if($totalrows>0) {

$r = mysql_query("SELECT
						a.*
						, b.nm_model
						, c.nm_grade
						, d.nm_brand
						, e.nm_model_sub
						, 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.nm_contract_date nm_contract_date_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') and s.ds_status_contract in ('B0') then '거래연기' when s.ds_status in ('Z0') and s.ds_status_contract in ('C0') then '거래실패' when s.ds_status in ('Z0') then '낙찰' when s.ds_status in ('ZE') 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
						, 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_grade c on (a.cd_grade = c.cd_grade)
							left outer join car_brand d on (a.cd_brand = d.cd_brand)
							left outer join car_model_sub e on (a.cd_model_sub = e.cd_model_sub)
							$sql $orderby LIMIT $pagestartpoint,$PageSize");

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

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

?>

			<tr onclick="location.href='./<? if($col[ds_status_sale]=="ZS"){ ?>sell_view2.php<? }else{ ?>sell_view.php<? } ?>?list_url=<?=$list_url?>&cd_sale=<?=$col[cd_sale]?>&ds_status_sale=<?=$col[ds_status_sale]?>&ListPageNo=<?=$PageNo?>&nm_contract_date_year=<?=$nm_contract_date_year?>&nm_contract_date_month=<?=$nm_contract_date_month?>';" style="cursor:pointer;">
				<td><?=$no?></td>
				<td><?=$col[nm_brand]." ".$col[nm_model]?></td>
                <td><?=$col[nm_number]?></td>
				<td><?=$col[ds_year]?>년</td>
				<td><?=$col[nm_area]?></td>
				<td><?=$col[nm_contract_date_sale]?></td>
				<td>
				<? if($col[time_limit_sell]=="0분" && ($col[ds_status_sale] == "E0" || $col[ds_status_sale] == "F0")){ ?>
				거래마감
				<? }else{ ?>
				<?=$col[nm_status_sale]?>
				<? } ?>
				</td>
			</tr>
<?
	$i++;
}
}else{
?>
			<tr>
				<td colspan="7">등록된 정보가 없습니다.</td>
			</tr>
<?
}
?>
			</tbody>
		</table>

		<div class="paging">
			<?
					$url = "./sell_list.php?searchtype=$searchtype&searchword=$searchword&ds_status=$ds_status&ds_usertype=$ds_usertype&nm_contract_date_year=$nm_contract_date_year&nm_contract_date_month=$nm_contract_date_month";
					ListPg1($url ,$PageNo,$PageSize,10,$totalrows,$leftboard[1],$rootpath);
			?>
		</div>

<!--		<div class="btn_right">
			<a href="/sell/sell.php" class="button">내차팔기</a>
		</div>-->
	</section>
	</form>
</section>
<script>
$(function() {
	$("#nm_contract_date_month, #nm_contract_date_year").on("change", function() {
		$("#frmCompete").submit();
	});
})
</script>
<? include("../include/inc_footer.php") ?>
<? include("../include/inc_bottom.php") ?>
<?
mysql_close($connect);
?>
