<?php

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

	$cd_dealer_p = $_REQUEST["cd_dealer"];
?>
<? include("../inc/inc_top.php") ?>
<? include("../inc/inc_header.php") ?>

<section id="contents">
	<h2>딜러 회원관리</h2>

<? include("../inc/inc_header_dealer.php") ?>

	<div class="area_title">
		<h3><em>▶</em> 부관리자 리스트</h3>
		<a href="dealer_view3.php?cd_dealer=<?=$cd_dealer?>" class="rnd_btn ty4 gray">+ 더보기</a>
	</div>
	<div class="tbl_wrap">
		<table class="tbl_list">
			<colgroup>
				<col width="7%">
				<col width="24%">
				<col width="24%">
				<col width="*">
				<col width="15%">
			</colgroup>
			<thead>
			<tr>
				<th>번호</th>
				<th>아이디</th>
				<th>이름</th>
				<th>연락처</th>
				<th>등록일</th>
			</tr>
			</thead>
<?
		$PageSize=5; //목록개수설정
		$pagestartpoint=0;

		$sql_base = "where ds_delind='N'";

		$ds_type = "D1";
		$sql = $sql_base. "	and ds_type = '$ds_type' ";
		$sql = $sql. "	and cd_dealer_p = '$cd_dealer_p' ";

		$r = mysql_query("SELECT * FROM dealer_master $sql ORDER BY cd_dealer desc  LIMIT $pagestartpoint,$PageSize");
		$i=1;
		while($col = mysql_fetch_array($r)) {
?>
			<tr onclick="location.href='./dealer_view3_detail.php?cd_dealer=<?=$col['cd_dealer']?>&cd_dealer_p=<?=$col['cd_dealer_p']?>&PageNo=<?=$PageNo?>';" style="cursor: pointer; cursor: hand;">
				<td><?=$i?></td>
				<td><?=$col['cd_dealerid']?></td>
				<td><?=$col['nm_name']?></td>
				<td><?=$col['nm_hp_01']."-".$col['nm_hp_02']."-".$col['nm_hp_03']?></td>
				<td><?=$col['dt_insert']?></td>
			</tr>
<?
			$i++;
		}
?>
		</table>
	</div>

	<div class="area_title">
		<h3><em>▶</em> 입찰요청 리스트</h3>
		<a href="dealer_view4.php?cd_dealer=<?=$cd_dealer?>" class="rnd_btn ty4 gray">+ 더보기</a>
	</div>
	<div class="tbl_wrap">
		<table class="tbl_list ty2">
			<colgroup>
				<col width="7%">
				<col width="15%">
				<col width="11%">
				<col width="*">
				<col width="12%">
				<col width="10%">
				<col width="12%">
				<col width="15%">
			</colgroup>
			<thead>
			<tr>
				<th>번호</th>
				<th>판매자</th>
				<th>지역</th>
				<th>차종</th>
				<th>주행거리</th>
				<th>년식</th>
				<th>등록일</th>
				<th>입찰 마감시간</th>
			</tr>
			</thead>
<?

	$sql = "where s.ds_delind='N'";

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

	$sql = $sql. "	and s.ds_area in (
						select
						   SUBSTRING_INDEX (SUBSTRING_INDEX(nm_area,',',numbers.n),',',-1)
						from
						   (select  1 n union  all  select 2
							union  all  select  3  union  all select 4
							union  all  select  5  union  all  select  6
							union  all  select  7  union  all  select  8
							union  all  select  9 union  all  select  10
							union  all  select  11 union  all  select  12
							union  all  select  13 union  all  select  14
							union  all  select  15 union  all  select  16
							union  all  select  17 union  all  select  18
							union  all  select  19 union  all  select  20
							) numbers INNER  JOIN dealer_master
							on CHAR_LENGTH ( nm_area ) - CHAR_LENGTH ( REPLACE ( nm_area ,  ',' ,  '' ))>= numbers . n-1
						where cd_dealer = $cd_dealer_p
		) ";

	$sql1 = "	and cd_dealer_p = '$cd_dealer_p' ";

	$sql = $sql. "	and s.ds_status in ('E0','F0')";

	$sql = $sql. "	and s.cd_sale not in (select cd_sale from tender_master where ds_delind = 'N' and cd_dealer_p = '$cd_dealer_p' group by cd_sale)";

	$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
							, 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
							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)) {
?>
			<tr onclick="location.href='./dealer_view4_detail.php?list_url=<?=$list_url?>&cd_sale=<?=$col['cd_sale']?>&cd_dealer=<?=$cd_dealer_p?>&ds_status_sale=<?=$col['ds_status_sale']?>&ListPageNo=<?=$PageNo?>';" style="cursor: pointer; cursor: hand;">
				<td><?=$i?></td>
				<td><?=$col['nm_name_sale']?></td>
				<td><?=$col['nm_area']?></td>
				<td><?=$col['nm_brand']." ".$col['nm_model']?></td>
				<td><?=$col['nm_mileage']?>km</td>
				<td><?=$col['ds_year']?></td>
				<td><?=$col['dt_insert']?></td>
				<td><em class="txt_bold"><?//=$col[nm_status]?></em></td>
			</tr>
<?
		$i++;
	}
?>
		</table>
	</div>

	<div class="area_title">
		<h3><em>▶</em> 입찰중 리스트</h3>
		<a href="dealer_view4.php?cd_dealer=<?=$cd_dealer?>" class="rnd_btn ty4 gray">+ 더보기</a>
	</div>
	<div class="tbl_wrap">
		<table class="tbl_list ty2">
			<colgroup>
				<col width="7%">
				<col width="15%">
				<col width="11%">
				<col width="*">
				<col width="12%">
				<col width="10%">
				<col width="12%">
				<col width="15%">
			</colgroup>
			<thead>
			<tr>
				<th>번호</th>
				<th>판매자</th>
				<th>지역</th>
				<th>차종</th>
				<th>주행거리</th>
				<th>년식</th>
				<th>등록일</th>
				<th>상태</th>
			</tr>
			</thead>
<?

	$sql = "where s.ds_delind='N'";

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

	$sql1 = "	and cd_dealer_p = '$cd_dealer_p' ";

	$sql = $sql. "	and s.ds_status in ('E0','F0')";
//	$sql = $sql. "	and s.cd_sale in (select cd_sale from tender_master where ds_delind = 'N' ".$sql1." group by cd_sale)";	//20191001이전 지연 쿼리
	$sql = $sql. "	and exists(select cd_sale from tender_master where cd_sale = s.cd_sale and ds_delind = 'N' ".$sql1." )";	//20191001 cd_sale_cd_user_cd_dealer index 설정 후 쿼리 변경

	$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
							, 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
							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)) {
?>
			<tr onclick="location.href='./dealer_view4_detail.php?list_url=<?=$list_url?>&cd_sale=<?=$col['cd_sale']?>&cd_dealer=<?=$cd_dealer_p?>&ds_status_sale=<?=$col['ds_status_sale']?>&ListPageNo=<?=$PageNo?>';" style="cursor: pointer; cursor: hand;">
				<td><?=$i?></td>
				<td><?=$col['nm_name_sale']?></td>
				<td><?=$col['nm_area']?></td>
				<td><?=$col['nm_brand']." ".$col['nm_model']?></td>
				<td><?=$col['nm_mileage']?>km</td>
				<td><?=$col['ds_year']?></td>
				<td><?=$col['dt_insert']?></td>
				<td><em class="txt_bold"><?=$col['nm_status_sale']?></em></td>
			</tr>
<?
		$i++;
	}
?>
		</table>
	</div>
</section>

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