<?php
include_once('./_common.php');

//경매정보 라이브러리
include_once (G5_SKIN_PATH.'/board/auction/auction.lib.php');

if (!$is_member)
    goto_url(G5_BBS_URL."/login.php?url=".urlencode(G5_SHOP_URL.'/wishlist.php'));

if(USE_G5_THEME && defined('G5_THEME_PATH')) {
    require_once(G5_SHOP_PATH.'/yc/wishlist.php');
    return;
}

$inquiry_type = isset($_GET['inquiry_type']) ? trim((string)$_GET['inquiry_type']) : '';
$is_vpn_wishlist = ($inquiry_type === 'vpn');
$is_rpc_wishlist = ($inquiry_type === 'rpc');
$is_auction_wishlist = ($inquiry_type === 'auction');

$auction_table = isset($g5['write_auction_table']) ? $g5['write_auction_table'] : '';
$auction_exists_condition = $auction_table
	? "exists (select 1 from {$auction_table} aw where aw.pcnum = b.it_id)"
	: '0=1';

$wishlist_filter_sql = '';
if ($is_vpn_wishlist) {
	$wishlist_filter_sql = " and LEFT(b.ca_id, 2) IN ('30', '40') ";
} else if ($is_rpc_wishlist) {
	$wishlist_filter_sql = " and LEFT(b.ca_id, 2) NOT IN ('30', '40') and not {$auction_exists_condition} ";
} else if ($is_auction_wishlist) {
	$wishlist_filter_sql = " and {$auction_exists_condition} ";
}

$list = array();
$sql  = " select a.wi_id, a.wi_time, b.*
			from {$g5['g5_shop_wish_table']} a
			inner join {$g5['g5_shop_item_table']} b on ( a.it_id = b.it_id ) ";
$sql .= " where a.mb_id = '{$member['mb_id']}' {$wishlist_filter_sql} order by a.wi_id desc ";
$result = sql_query($sql);
for ($i=0; $row = sql_fetch_array($result); $i++) {

	$list[$i] = $row;

	$list[$i]['out_cd'] = '';
	$sql = " select count(*) as cnt from {$g5['g5_shop_item_option_table']} where it_id = '{$row['it_id']}' and io_type = '0' ";
	$tmp = sql_fetch($sql);
	if($tmp['cnt'])
		$list[$i]['out_cd'] = 'no';

	$list[$i]['price'] = get_price($row);

	if ($row['it_tel_inq']) $list[$i]['out_cd'] = 'tel';

	$list[$i]['is_soldout'] = is_soldout($row['it_id']);

	$category_head = substr((string)$row['ca_id'], 0, 2);
	$auction_row = array();
	if ($auction_table) {
		$auction_sql = " select wr_id from {$auction_table} where pcnum = '{$row['it_id']}' order by wr_id desc limit 1 ";
		$auction_row = sql_fetch($auction_sql);
	}

	if (!empty($auction_row['wr_id'])) {
		$auction_detail_sql = " select wr_id, wr_1, wr_2, wr_8, wr_subject from {$auction_table} where wr_id = '{$auction_row['wr_id']}' limit 1 ";
		$auction_detail = sql_fetch($auction_detail_sql);
		if (function_exists('get_info_auction') && !empty($auction_detail['wr_id'])) {
			get_info_auction($auction_detail['wr_id'], $auction_detail);
			$auction_detail = sql_fetch($auction_detail_sql);
		}

		$auction_status = '경매취소';
		switch((string)$auction_detail['wr_8']) {
			case '0': $auction_status = '경매시작전'; break;
			case '1': $auction_status = '경매진행중'; break;
			case '2': $auction_status = '경매종료[낙찰]'; break;
			case '3': $auction_status = '경매종료[유찰]'; break;
		}

		$auction_term = '-';
		if (!empty($auction_detail['wr_1']) && !empty($auction_detail['wr_2'])) {
			$auction_term = substr($auction_detail['wr_1'],2,17).' ('.get_yoil($auction_detail['wr_1']).')';
			$auction_term .= ' ~ ';
			$auction_term .= substr($auction_detail['wr_2'],2,17).' ('.get_yoil($auction_detail['wr_2']).')';
		}

		$list[$i]['auction_wr_id'] = $auction_detail['wr_id'];
		$list[$i]['auction_pcnum'] = 'RPC-A'.$row['it_id'];
		$list[$i]['auction_term'] = $auction_term;
		$list[$i]['auction_status'] = $auction_status;

		$list[$i]['item_type_label'] = '원격제어컴퓨터(경매)';
		$list[$i]['item_href'] = G5_BBS_URL.'/board.php?bo_table=auction&amp;wr_id='.$auction_row['wr_id'].'&amp;s=&amp;pcnum='.$row['it_id'];
	} else if (in_array($category_head, array('30', '40'))) {
		$list[$i]['item_type_label'] = 'VPN';
		$list[$i]['item_href'] = './item.php?it_id='.$row['it_id'];
	} else {
		$list[$i]['item_type_label'] = '원격제어컴퓨터(일반)';
		$list[$i]['item_href'] = './item.php?it_id='.$row['it_id'];
	}
}

// Page ID
$pid = ($pid) ? $pid : 'wishlist';
$at = apms_page_thema($pid);
include_once(G5_LIB_PATH.'/apms.thema.lib.php');

// 스킨 체크
list($member_skin_path, $member_skin_url) = apms_skin_thema('member', $member_skin_path, $member_skin_url);

// 설정값 불러오기
$is_wishlist_sub = false;
@include_once($member_skin_path.'/config.skin.php');

if ($is_vpn_wishlist) {
	$g5['title'] = $member['mb_nick'].'님의 VPN 위시리스트';
} else if ($is_rpc_wishlist) {
	$g5['title'] = $member['mb_nick'].'님의 원격제어컴퓨터(일반) 위시리스트';
} else if ($is_auction_wishlist) {
	$g5['title'] = $member['mb_nick'].'님의 원격제어컴퓨터(경매) 위시리스트';
} else {
	$g5['title'] = $member['mb_nick'].'님의 위시리스트';
}

if($is_wishlist_sub) {
	include_once(G5_PATH.'/head.sub.php');
	if(!USE_G5_THEME) @include_once(THEMA_PATH.'/head.sub.php');
} else {
	include_once('./_head.php');
}

$skin_path = $member_skin_path;
$skin_url = $member_skin_url;

// 스킨설정
$wset = (G5_IS_MOBILE) ? apms_skin_set('member_mobile') : apms_skin_set('member');

$setup_href = '';
if(is_file($skin_path.'/setup.skin.php') && ($is_demo || $is_designer)) {
	$setup_href = './skin.setup.php?skin=member&amp;ts='.urlencode(THEMA);
}

include_once($skin_path.'/wishlist.skin.php');

if($is_wishlist_sub) {
	if(!USE_G5_THEME) @include_once(THEMA_PATH.'/tail.sub.php');
	include_once(G5_PATH.'/tail.sub.php');
} else {
	include_once('./_tail.php');
}
?>