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

include_once("$board_skin_path/auction.lib.php");

add_stylesheet('<link rel="stylesheet" href="'.$board_skin_url.'/style.css">', 0);//add_stylesheet('css 구문', 출력순서); 숫자가 작을 수록 먼저 출력됨
add_javascript('<script src="'.G5_JS_URL.'/viewimageresize.js"></script>');

$sql = " select wr_subject from $write_table where wr_id = '$wr_id' ";
$row = sql_fetch($sql);

$g5['title'] = get_text($row[wr_subject])." 경매입찰내역";

include_once(G5_PATH.'/head.sub.php');

// 경매정보
$info = get_info_auction($wr_id);

// 경매정보
if($info[order_by]=="desc"){$order_title="내림";} else {$order_title="오름";}

if ($info[status] >= 2) {
    $order_info = "입찰금액 {$order_title}차순 정렬";
    $orderby = "td_tender_point {$info[order_by]}";
} else {
    $order_info = "입찰일시 내림차순 정렬";
    $orderby = "td_datetime desc";
}

$row = sql_fetch(" select count(*) as cnt from $tender_table where wr_id = '$wr_id' ");
$total_count = $total = $row[cnt];
$tender_scroll_threshold = 15;
$tender_is_many_rows = ($total > $tender_scroll_threshold);

$rows = $config[cf_page_rows];
// threshold 이하이면 모든 행을 한 페이지에 표시 (페이지 이동 없이 전체 노출)
if ($total <= $tender_scroll_threshold) {
    $rows = max($total, 1);
}
$total_page  = ceil($total_count / $rows);  // 전체 페이지 계산

// 페이지가 없으면 첫 페이지 (1 페이지)
if ($page == ""){$page = 1;}

$from_record = ($page - 1) * $rows; // 시작 열을 구함
?>
<style>
    @media screen and (max-width: 768px) {
        body {font-size: 12px;}
    }
    body {
        margin: 0;
        padding: 0;
        background: #fff;   /* 하단 여백이 흰색으로 보이지 않도록 tender-wrap과 동일한 색 */
        font-family: 'Noto Sans KR', 'Noto Sans JP', 'Noto Sans SC', 'Apple SD Gothic Neo', 'Malgun Gothic', 'Segoe UI', Arial, sans-serif;
        font-size: 12px;
        line-height: 1.6;
        color: #333;
        overflow-y: <?php echo $tender_is_many_rows ? 'auto' : 'hidden'; ?>;
    }
    .tender-wrap {
        border: 0;
        border-radius: 0;
        background: #fff;
        overflow: visible;
        box-shadow: none;
    }
    .tender-paging { margin: 0; padding: 0; } /* 하단 여백 제거 */
    .tender-head {
        height:auto;
        font-size: 12px;
        margin-bottom: 0;
        padding: 10px 12px;
        background: #f9fbff;
        border-bottom: 1px solid #eef3f8;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
        flex-wrap: wrap;
    }
    .tender-head-left,
    .tender-head-right {
        margin: 0;
        padding: 0;
        line-height: 1.5;
    }
    .tender-head-right {
        color: #4b5563;
        font-weight: 600;
    }
    #tender_list_tb {
        margin:0 auto;
        width:100%;
        border-collapse:collapse;
        font-size: 12px;
        font-family: inherit;
    }
    #tender_list_tb th {
        background-color:#fbfcfe;
        color: #4b5563;
        font-weight: 600;
        padding: 10px 8px;
        border-bottom: 1px solid #eef3f8;
    }
    #tender_list_tb td {
        padding: 10px 8px;
        text-align:center;
        border-bottom:1px solid #f1f4f8;
        color: #333;
    }
    #tender_list_tb tr:last-child td {
        border-bottom: 0;
    }
    #tender_list_tb td.align_right{text-align:right;}
    .tender-title {
        color: #333;
        font-size: 12px;
        font-weight: bold;
    }
    .tender-order-info {
        color: #777;
        font-size: 12px;
    }
    #tender_list_tb tr.tender-highlight td {
        background: #fff3e8;
        font-weight: 700;
    }
    .tender-paging {
        text-align: center;
        margin: 10px 0 0;
    }

    .tender-wrap, .tender-head, #tender_list_tb th, #tender_list_tb td {
        transition: background-color 0.25s ease, color 0.15s ease, border-color 0.2s ease;
    }

    [data-site-theme="dark"] body { background: #252535 !important; color: #e2e8f0 !important; }
    [data-site-theme="dark"] .tender-wrap { background: #252535 !important; border: 0 !important; }
    [data-site-theme="dark"] .tender-head { background: #1e1e2e !important; border-bottom-color: #2d3748 !important; }
    [data-site-theme="dark"] .tender-title { color: #e2e8f0 !important; }
    [data-site-theme="dark"] .tender-order-info { color: #64748b !important; }
    [data-site-theme="dark"] .tender-head-right { color: #94a3b8 !important; }
    [data-site-theme="dark"] #tender_list_tb th { background: #1e1e2e !important; color: #94a3b8 !important; border-bottom-color: #2d3748 !important; }
    [data-site-theme="dark"] #tender_list_tb td { color: #cbd5e1 !important; border-bottom-color: #2d3748 !important; }
    [data-site-theme="dark"] #tender_list_tb tr.tender-highlight td { background: #2a1800 !important; color: #ffb74d !important; }
</style>

<div id="tender_list_meta" data-total="<?php echo (int)$total; ?>" data-scroll-threshold="<?php echo (int)$tender_scroll_threshold; ?>"></div>

<div class="tender-wrap">
<div class="tender-head">
    <div class="tender-head-left">
        <span class="tender-title">입찰내역</span>
        <span class="tender-order-info">(<?php echo $order_info?>)</span>
    </div>

    <div class="tender-head-right">
    총 참여수 : <?php echo number_format($total)?>
    </div>
</div>



<table id="tender_list_tb">
    <tr>
        <th> 입찰접수번호 </th>
        <th> 입찰일시 </th>
        <th> 입찰금액 </th>
        <th> 입찰자 </th>
    </tr>
<?php
if (!$total) {
    echo "<tr><td colspan=4> 입찰내역이 없습니다. </td></tr>";
}

$qry = sql_query(" select * from $tender_table where wr_id = '$wr_id' order by $orderby limit $from_record, $rows");
//$num = mysql_num_rows($qry);
$k = 0;
while ($row = sql_fetch_array($qry)) {
    $num = $total_count - ($page - 1) * $config[cf_page_rows] - $k;

//경매정보 보기 권한 설정
    //$is_view = ($is_admin || $info[status] != 1 || $row[mb_id] == $member[mb_id]);//경매 입찰자아이디=로그인아이디
    $is_view_info = ($is_admin || $write[mb_id] == $member[mb_id] || $row[mb_id] == $member[mb_id]);//경매 등록자아이디=로그인아이디, 로그인아이디=입찰아이디는 입찰정보를 봄.
    $is_view = ($info[tender_method]=="공개" && $info[status] >= 1);//공개일때 정보보기.
//경매정보 보기 권한 설정

    if ($is_view_info || $is_view){$tender_point = number_format($row[td_tender_point]);} else {$tender_point = '*****';}
    $is_highlight = ($row[td_tender_point] == $info[auction_off_point]);
    $k++;
?>
    <tr class="<?php echo $is_highlight ? 'tender-highlight' : ''; ?>">
        <td> <?php echo $num?> </td>
        <td>
            <?php
                if ($is_view_info || $is_view) {
                    echo substr($row[td_datetime],2,17);
                } else {
                    echo substr($row[td_datetime],2,17);
                }
            ?>
        </td>
        <td><?php echo $tender_point?> Point</td>
        <td>
            <?php
            if ($is_view_info || $is_view) {
                //if($is_view_info){echo get_sideview($row[mb_id], $row[mb_nick], $row[mb_email], $row[mb_homepage]);} else {echo($row[mb_nick]);}
                echo masking('N',$row[mb_nick]);
            } else {
                echo '*****';
            }
            ?>
        </td>
    </tr>
<?php } ?>

</table>
</div>

<?php
$qstr = "bo_table=$bo_table&wr_id=$wr_id";
?>

<div class="tender-paging">
<?php echo get_paging($config[cf_write_pages], $page, $total_page, "$_SERVER[PHP_SELF]?$qstr&page=");?>
</div>

<script>
(function() {
    function reportHeight() {
        var wrap   = document.querySelector('.tender-wrap');
        var paging = document.querySelector('.tender-paging');
        var h = (wrap   ? wrap.offsetHeight   : 0)
              + (paging ? paging.offsetHeight : 0);
        if (h > 0) {
            try { window.parent.postMessage({ type: 'tenderFrameHeight', h: h }, '*'); } catch(e) {}
        }
    }
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function() { setTimeout(reportHeight, 0); });
    } else {
        setTimeout(reportHeight, 0);
    }
    /* 폰트·이미지 로딩 완료 후 재측정 */
    window.addEventListener('load', function() { setTimeout(reportHeight, 50); });
})();
</script>

<?php
include_once(G5_PATH.'/tail.sub.php');
?>
