<?php
if (!defined("_GNUBOARD_")) exit;

add_stylesheet('<link rel="stylesheet" href="'.$skin_url.'/style.css" media="screen">', 0);

if(isset($wset['ihead']) && $wset['ihead']) {
	add_stylesheet('<link rel="stylesheet" href="'.G5_CSS_URL.'/head/'.$wset['ihead'].'.css" media="screen">', 0);
	$head_class = 'list-head';
} else {
	$head_class = (isset($wset['icolor']) && $wset['icolor']) ? 'tr-head border-'.$wset['icolor'] : 'tr-head border-black';
}

if($header_skin) include_once('./header.php');

if(!function_exists('_oi_grade_badge')) {
	function _oi_grade_badge($g) {
		$g = strtoupper(trim((string)$g)); if($g==='') return '';
		$c = array('BASIC'=>'#6c757d','PLUS'=>'#0d6efd','PRO'=>'#198754','ULTRA'=>'#6f42c1','MAX'=>'#dc3545');
		$bg = isset($c[$g]) ? $c[$g] : '#555';
		return '<span style="display:inline-block;padding:1px 8px;font-size:11px;font-weight:700;letter-spacing:.4px;background:'.htmlspecialchars($bg,ENT_QUOTES).';color:#fff;vertical-align:middle;">'.htmlspecialchars($g,ENT_QUOTES).'</span>';
	}
}
if(!function_exists('_oi_flag')) {
	function _oi_flag($s) {
		static $m=array('KOR'=>'KR','USA'=>'US','JPN'=>'JP','CHN'=>'CN','GBR'=>'GB','DEU'=>'DE','FRA'=>'FR','SGP'=>'SG','HKG'=>'HK','TWN'=>'TW','AUS'=>'AU','NLD'=>'NL','CAN'=>'CA','IND'=>'IN','RUS'=>'RU','VNM'=>'VN','THA'=>'TH','IDN'=>'ID','MYS'=>'MY','PHL'=>'PH','BRA'=>'BR','MEX'=>'MX','NOR'=>'NO','SWE'=>'SE','FIN'=>'FI','CHE'=>'CH','AUT'=>'AT','ESP'=>'ES','ITA'=>'IT','POL'=>'PL','UKR'=>'UA');
		$s=strtoupper(trim((string)$s));
		$c=isset($m[$s])?$m[$s]:(strlen($s)===2?$s:'');
		if(strlen($c)!==2) return '';
		$l=strtolower($c);
		return '<img src="https://flagcdn.com/20x15/'.$l.'.png" srcset="https://flagcdn.com/40x30/'.$l.'.png 2x" width="20" height="15" alt="'.htmlspecialchars($c,ENT_QUOTES).'" style="display:inline-block;vertical-align:middle;border-radius:2px;border:1px solid rgba(0,0,0,.08);">';
	}
}
?>
<style>
.oi-info-cell{display:flex;align-items:flex-start;gap:8px}
.oi-prod-icon{width:36px;height:36px;display:flex;align-items:center;justify-content:center;border-radius:6px;background:#1e293b;flex-shrink:0}
.oi-prod-icon svg{width:20px;height:20px}
.oi-info-col{flex:1;min-width:0}
.oi-badges-row{display:flex;align-items:center;gap:4px;flex-wrap:wrap;margin-bottom:4px}
.oi-spec-row{display:flex;align-items:center;gap:5px;flex-wrap:wrap;margin:3px 0}
.oi-spec-b{display:inline-flex;align-items:center;gap:2px;font-size:11px;color:#374151}
.oi-spec-b b{color:#6b7280;font-weight:600;font-size:10px}
[data-site-theme="dark"] .oi-spec-b{color:#d1d5db}
[data-site-theme="dark"] .oi-spec-b b{color:#9ca3af}
.oi-order-no-wrap{display:inline-flex;align-items:center;gap:4px;flex-wrap:wrap}
.oi-no-label{font-size:10px;color:#9ca3af;font-weight:500;margin-right:2px;white-space:nowrap}
.oi-view-btn{padding:0 6px!important;height:20px!important;line-height:18px!important;font-size:11px!important;font-weight:normal!important;border-radius:2px!important;background:#f3f4f6!important;border:1px solid #d1d5db!important;color:#374151!important}
.oi-view-btn:hover,.oi-view-btn:focus{background:#e5e7eb!important;color:#111827!important}
[data-site-theme="dark"] .oi-view-btn{background:#2a2a32!important;border-color:#4a4a52!important;color:#c4c4cc!important}
[data-site-theme="dark"] .oi-view-btn:hover{background:#363640!important;color:#e4e4e7!important}
.oi-ext-sum{margin-top:3px;font-size:11px;color:#0b6e4f;font-weight:600}
[data-site-theme="dark"] .oi-ext-sum{color:#34d399}
.oi-date-t{font-size:11px;color:#6b7280}
[data-site-theme="dark"] .oi-date-t{color:#9ca3af}
.oi-country-name{font-size:11px;color:#6b7280;vertical-align:middle}
[data-site-theme="dark"] .oi-country-name{color:#9ca3af}
.mp-clickable-row{cursor:pointer}
.mp-clickable-row:hover>td{background:rgba(0,0,0,.03)}
[data-site-theme="dark"] .mp-clickable-row:hover>td{background:rgba(255,255,255,.04)}
.oi4a-status-badge{display:inline-block;padding:2px 7px;border-radius:10px;font-size:11px;font-weight:600;white-space:nowrap}
.oi4a-status-won{background:#cce5ff;color:#004085}
.oi4a-status-failed{background:#e2e3e5;color:#383d41}
.oi4a-status-default{background:#fff3cd;color:#856404}
</style>

<div class="well well-sm">
	<i class="fa fa-bell fa-lg"></i> 입찰번호 또는 경매상품 링크를 누르시면 경매 상세내역을 조회하실 수 있습니다.
</div>

<div class="table-responsive">
<table class="div-table table bsk-tbl bg-white">
<tbody>
<tr class="<?php echo $head_class;?>">
	<th scope="col"><span>상품 정보</span></th>
	<th scope="col"><span>낙찰방식</span></th>
	<th scope="col"><span>경매진행기간</span></th>
	<th scope="col"><span>입찰참여일시</span></th>
	<th scope="col"><span>입찰금액</span></th>
	<th scope="col"><span>현재상태</span></th>
	<th scope="col"><span class="last">최종결과</span></th>
</tr>
<?php for ($i=0; $i < count($list); $i++):
	$_od_href = $list[$i]['od_href'];
	$_od_href_raw = str_replace('&amp;', '&', $_od_href);

	$_auction_pcnum = trim((string)($list[$i]['auction_pcnum'] ?? ''));
	$_row_it  = array();
	$_specs   = array();
	$_grade   = '';
	$_country_name  = '';
	$_country_short = '';

	if ($_auction_pcnum !== '') {
		$_pcnum_e = sql_escape_string($_auction_pcnum);
		$_row_it = sql_fetch("
			SELECT pt_id, it_spec_grade, it_spec_os, it_spec_cpu, it_spec_gpu,
			       it_spec_ram, it_spec_ram_type,
			       it_spec_ssd, it_spec_ssd_type,
			       it_spec_hdd, it_spec_hdd_type,
			       it_spec_remote_ip_country_code, it_spec_remote_sw
			FROM {$g5['g5_shop_item_table']}
			WHERE it_id = '{$_pcnum_e}' LIMIT 1
		");
	}

	if ($_row_it) {
		$_grade = trim((string)($_row_it['it_spec_grade'] ?? ''));
		$_rmv = array("(R)","(TM)","with Radeon Graphics","with Radeon Vega Graphics","CPU","Core ","Eight-Processor");
		$_os  = trim(str_ireplace(array("Microsoft","Windows"),array("","Win"),(string)($_row_it['it_spec_os']??'')));
		$_cpu = trim(explode("\xC5\xBD",trim(str_ireplace($_rmv,array_fill(0,count($_rmv),''),(string)($_row_it['it_spec_cpu']??''))))[0]);
		$_gpu = trim(explode("\xC5\xBD",trim(str_replace(array("(R)","(TM)"),"",(string)($_row_it['it_spec_gpu']??''))))[0]);
		$_ram = trim((($_row_it['it_spec_ram_type']??'')?$_row_it['it_spec_ram_type'].' ':'').(string)($_row_it['it_spec_ram']??''));
		$_ssd = (($_row_it['it_spec_ssd']??'')&&$_row_it['it_spec_ssd']!=='0 GB')?trim((($_row_it['it_spec_ssd_type']??'')?$_row_it['it_spec_ssd_type'].' ':'').(string)($_row_it['it_spec_ssd']??'')):'';
		$_hdd = (($_row_it['it_spec_hdd']??'')&&$_row_it['it_spec_hdd']!=='0 GB')?trim((($_row_it['it_spec_hdd_type']??'')?$_row_it['it_spec_hdd_type'].' ':'').(string)($_row_it['it_spec_hdd']??'')):'';
		if ($_os)  $_specs[] = array('OS',  $_os);
		if ($_cpu) $_specs[] = array('CPU', $_cpu);
		if ($_gpu) $_specs[] = array('GPU', $_gpu);
		if ($_ram) $_specs[] = array('RAM', $_ram);
		if ($_ssd) $_specs[] = array('SSD', $_ssd);
		if ($_hdd) $_specs[] = array('HDD', $_hdd);
		if (!empty($_row_it['it_spec_remote_ip_country_code']))
			$_country_short = trim((string)$_row_it['it_spec_remote_ip_country_code']);
		if (empty($_country_name)) {
			$_sw = @json_decode($_row_it['it_spec_remote_sw'] ?? '', true);
			if (is_array($_sw) && !empty($_sw[0]['RemoteIP_CountryName__0']))
				$_country_name = trim((string)$_sw[0]['RemoteIP_CountryName__0']);
		}
	}

	$_bid_res = $list[$i]['td_bid_result'] ?? '-';
	$_bid_cls = 'oi4a-status-default';
	if ($_bid_res === '본인낙찰')  $_bid_cls = 'oi4a-status-won';
	elseif ($_bid_res === '타인낙찰') $_bid_cls = 'oi4a-status-failed';
?>
<tr class="mp-clickable-row<?php echo ($i==0)?' tr-line':''; ?>" data-href="<?php echo htmlspecialchars($_od_href_raw,ENT_QUOTES); ?>">
	<td>
		<div class="oi-info-cell">
			<div class="oi-prod-icon">
				<svg viewBox="0 0 24 24" fill="none" stroke="#a78bfa" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="2" y="3" width="20" height="14" rx="2"/><path d="M8 21h8M12 17v4"/><path d="M7 10l2 2 4-4"/></svg>
			</div>
			<div class="oi-info-col">
				<div class="oi-badges-row">
					<span style="display:inline-block;padding:1px 8px;font-size:11px;font-weight:700;letter-spacing:.4px;background:#7c3aed;color:#fff;vertical-align:middle;">RPC경매</span>
					<?php echo _oi_grade_badge($_grade); ?>
					<span style="display:inline-block;padding:1px 6px;font-size:11px;font-weight:600;background:#374151;color:#d1d5db;border:1px solid #4b5563;vertical-align:middle;"><?php echo htmlspecialchars($list[$i]['pcnum'],ENT_QUOTES); ?></span>
					<?php if (!empty($_country_short)): ?>
					<span style="line-height:1;vertical-align:middle;"><?php echo _oi_flag($_country_short); ?></span>
					<?php endif; ?>
					<?php if (!empty($_country_name)): ?>
					<span class="oi-country-name"><?php echo htmlspecialchars(ucfirst(strtolower($_country_name)),ENT_QUOTES); ?></span>
					<?php endif; ?>
				</div>
				<?php if (!empty($_specs)): ?>
				<div class="oi-spec-row">
					<?php foreach($_specs as $_sp): ?><span class="oi-spec-b"><b><?php echo $_sp[0]; ?></b><?php echo htmlspecialchars($_sp[1]); ?></span><?php endforeach; ?>
				</div>
				<?php endif; ?>
				<div>
				<?php
					// 판매자 표시 (통신판매중개 플랫폼: 상품별 판매자 명시)
					$_sl_pt_id = isset($_row_it['pt_id']) ? (string)$_row_it['pt_id'] : '';
					$_sl_pt_id = is_string($_sl_pt_id) ? trim($_sl_pt_id) : '';
					if ($_sl_pt_id !== '' && function_exists('get_pt_seller_name')) {
						$_sl_nick = trim((string)get_pt_seller_name($_sl_pt_id));
						if ($_sl_nick !== '') {
							echo '<div style="font-size:11px; color:#8a97a5; margin-top:2px;">판매자 <b style="font-weight:600;">'.get_text(cut_str($_sl_nick, 18, '…')).'</b>';
							if (function_exists('get_pt_manner_temperature')) echo ' · 매너온도 '.get_pt_manner_temperature($_sl_pt_id);
							echo '</div>';
						}
					}
				?>
				<span class="oi-order-no-wrap">
						<span class="oi-no-label">입찰번호</span><a href="<?php echo $_od_href; ?>"><?php echo $list[$i]['td_id']; ?></a>
						<a href="<?php echo $_od_href; ?>" class="btn btn-black btn-xs oi-view-btn">보기</a>
					</span>
					<?php if (!empty($list[$i]['extension_summary_text'])): ?>
					<div class="oi-ext-sum"><?php echo $list[$i]['extension_summary_text']; ?></div>
					<?php endif; ?>
				</div>
			</div>
		</div>
	</td>
	<td class="text-center"><?php echo $list[$i]['auction_type']; ?></td>
	<td class="text-center" style="font-size:12px;"><?php echo $list[$i]['auction_term']; ?></td>
	<td class="text-center"><span class="oi-date-t"><?php echo $list[$i]['datetime']; ?></span></td>
	<td class="text-right"><?php echo display_point($list[$i]['td_tender_point']); ?></td>
	<td class="text-center"><?php echo $list[$i]['od_status']; ?></td>
	<td class="text-center"><span class="oi4a-status-badge <?php echo $_bid_cls; ?>"><?php echo htmlspecialchars($_bid_res,ENT_QUOTES); ?></span></td>
</tr>
<?php endfor; ?>
<?php if ($i == 0) { ?>
<tr><td colspan="7" class="text-center">경매입찰 주문 내역이 없습니다.</td></tr>
<?php } ?>
</tbody>
</table>
</div>

<div class="text-center">
	<ul class="pagination pagination-sm en">
		<?php echo apms_paging($write_pages, $page, $total_page, $list_page); ?>
	</ul>
</div>

<?php if($setup_href) { ?>
<p class="text-center">
	<a class="btn btn-color btn-sm win_memo" href="<?php echo $setup_href;?>"><i class="fa fa-cogs"></i> 스킨설정</a>
</p>
<?php } ?>

<script>
$(document).on("click",".mp-clickable-row",function(e){
	if($(e.target).closest("a,button,input,label").length)return;
	var h=$(this).data("href");if(h)location.href=h;
});
</script>
