<?php
$sub_menu = '650420';
include_once('./_common.php');

auth_check($auth[$sub_menu], 'r');

$g5['title'] = '존재하지 않는 상품 차단 로그';
include_once(G5_ADMIN_PATH.'/admin.head.php');

// 검색 파라미터
$s_mb_id      = isset($_GET['s_mb_id'])      ? trim((string)$_GET['s_mb_id'])      : '';
$s_it_name    = isset($_GET['s_it_name'])     ? trim((string)$_GET['s_it_name'])    : '';
$s_action     = isset($_GET['s_action'])      ? trim((string)$_GET['s_action'])     : '';
$s_date_from  = isset($_GET['s_date_from'])   ? trim((string)$_GET['s_date_from'])  : '';
$s_date_to    = isset($_GET['s_date_to'])     ? trim((string)$_GET['s_date_to'])    : '';

$page  = max(1, (int)(isset($_GET['page']) ? $_GET['page'] : 1));
$limit = 30;
$offset = ($page - 1) * $limit;

// WHERE 절 조립
$where_parts = array('1=1');
if ($s_mb_id !== '') {
    $safe = function_exists('sql_real_escape_string') ? sql_real_escape_string($s_mb_id) : addslashes($s_mb_id);
    $where_parts[] = "mb_id LIKE '%{$safe}%'";
}
if ($s_it_name !== '') {
    $safe = function_exists('sql_real_escape_string') ? sql_real_escape_string($s_it_name) : addslashes($s_it_name);
    $where_parts[] = "(vpn_it_name LIKE '%{$safe}%' OR shop_it_id LIKE '%{$safe}%')";
}
if ($s_action !== '') {
    $safe = function_exists('sql_real_escape_string') ? sql_real_escape_string($s_action) : addslashes($s_action);
    $where_parts[] = "action_type = '{$safe}'";
}
if ($s_date_from !== '') {
    $safe = preg_replace('/[^0-9\-]/', '', $s_date_from);
    $where_parts[] = "DATE(created_at) >= '{$safe}'";
}
if ($s_date_to !== '') {
    $safe = preg_replace('/[^0-9\-]/', '', $s_date_to);
    $where_parts[] = "DATE(created_at) <= '{$safe}'";
}
$where = implode(' AND ', $where_parts);

$total_row = sql_fetch("SELECT COUNT(*) AS cnt FROM vpnlink_invalid_product_log WHERE {$where}", false);
$total = isset($total_row['cnt']) ? (int)$total_row['cnt'] : 0;
$total_pages = $total > 0 ? (int)ceil($total / $limit) : 1;

$result = sql_query("SELECT * FROM vpnlink_invalid_product_log WHERE {$where} ORDER BY id DESC LIMIT {$limit} OFFSET {$offset}", false);
$rows = array();
if ($result) {
    while ($r = sql_fetch_array($result)) {
        $rows[] = $r;
    }
}

// URL 유지용 쿼리스트링
function ipl_qs($extra = array()) {
    $params = array();
    foreach (array('s_mb_id','s_it_name','s_action','s_date_from','s_date_to') as $k) {
        if (isset($_GET[$k]) && $_GET[$k] !== '') {
            $params[$k] = $_GET[$k];
        }
    }
    foreach ($extra as $k => $v) {
        $params[$k] = $v;
    }
    return http_build_query($params);
}

$action_label = array(
    'profile_hidden' => '프로필 숨김',
    'order_blocked'  => '주문 차단',
);
?>

<style>
/* ── 공통 변수 ── */
:root {
    --ipl-h: 30px;
    --ipl-radius: 3px;
    --ipl-border: #c8cdd3;
    --ipl-bg-input: #fff;
    --ipl-bg-form: #f4f6f8;
    --ipl-color-label: #555;
    --ipl-color-text: #333;
    --ipl-border-form: #dde1e6;
    --ipl-color-stat: #6c757d;
    --ipl-color-td-border: #e4e8ec;
    --ipl-color-hover: #f0f4f8;
    --ipl-color-th-bg: #3a4049;
}
[data-theme="dark"] {
    --ipl-border: #454545;
    --ipl-bg-input: #3c3c3c;
    --ipl-bg-form: #252526;
    --ipl-color-label: #aaaaaa;
    --ipl-color-text: #cccccc;
    --ipl-border-form: #454545;
    --ipl-color-stat: #888;
    --ipl-color-td-border: #3a3a3a;
    --ipl-color-hover: #2e2e2e;
    --ipl-color-th-bg: #252526;
}

/* ── 래퍼 ── */
.ipl-wrap { max-width:1200px; margin:0 auto; padding:10px; }
.ipl-wrap h2 { color:var(--ipl-color-text); }
.ipl-wrap > p { color:var(--ipl-color-stat); font-size:13px; margin-bottom:14px; }

/* ── 검색 폼 ── */
.ipl-search {
    background: var(--ipl-bg-form);
    border: 1px solid var(--ipl-border-form);
    border-radius: 5px;
    padding: 12px 16px;
    margin-bottom: 16px;
}
.ipl-fields {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px 16px;
}
.ipl-field {
    display: flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
}
.ipl-field label {
    font-size: 12px;
    font-weight: 600;
    color: var(--ipl-color-label);
    white-space: nowrap;
    flex-shrink: 0;
}
.ipl-field input[type="text"],
.ipl-field input[type="date"],
.ipl-field select {
    height: var(--ipl-h);
    line-height: var(--ipl-h);
    padding: 0 8px;
    border: 1px solid var(--ipl-border);
    border-radius: var(--ipl-radius);
    font-size: 12px;
    background: var(--ipl-bg-input);
    color: var(--ipl-color-text);
    box-sizing: border-box;
    vertical-align: middle;
}
.ipl-field input[type="text"] { width: 160px; }
.ipl-field input[type="date"] { width: 130px; }
.ipl-field select { width: 110px; }
.ipl-sep {
    color: var(--ipl-color-label);
    font-size: 13px;
    line-height: var(--ipl-h);
}
.ipl-btns {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-left: auto;
}
.ipl-btn-search,
.ipl-btn-reset {
    height: var(--ipl-h);
    padding: 0 14px;
    border-radius: var(--ipl-radius);
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    border: none;
    white-space: nowrap;
    display: inline-flex;
    align-items: center;
}
.ipl-btn-search { background: #007bff; color: #fff; }
.ipl-btn-search:hover { background: #0069d9; }
.ipl-btn-reset  { background: #6c757d; color: #fff; text-decoration: none; }
.ipl-btn-reset:hover  { background: #5a6268; color: #fff; }

/* ── 통계/페이저 ── */
.ipl-stat { color:var(--ipl-color-stat); font-size:13px; margin-bottom:8px; }
.ipl-pager { text-align:center; margin:16px 0; }
.ipl-pager a, .ipl-pager span {
    display:inline-block; padding:4px 10px; margin:0 2px;
    border:1px solid var(--ipl-border); border-radius:var(--ipl-radius);
    text-decoration:none; color:var(--ipl-color-text);
    background: var(--ipl-bg-input);
}
.ipl-pager .current { background:#094771; color:#3794ff; border-color:#007acc; }

/* ── 결과 테이블 ── */
.ipl-table { width:100%; border-collapse:collapse; font-size:13px; }
.ipl-table th {
    background: var(--ipl-color-th-bg);
    color:#d0d4d8; padding:8px 10px; text-align:center;
    font-weight:600; font-size:12px;
}
.ipl-table td {
    padding:7px 10px;
    border-bottom:1px solid var(--ipl-color-td-border);
    vertical-align:top;
    color: var(--ipl-color-text);
}
.ipl-table tbody tr:hover td { background: var(--ipl-color-hover); }

/* ── 배지 ── */
.badge-profile { background:#17a2b8; color:#fff; padding:2px 8px; border-radius:3px; font-size:11px; }
.badge-order   { background:#dc3545; color:#fff; padding:2px 8px; border-radius:3px; font-size:11px; }
</style>

<div class="ipl-wrap">
<h2 style="margin-bottom:12px;">존재하지 않는 상품 차단 로그</h2>
<p style="color:#6c757d;font-size:13px;margin-bottom:14px;">
  g5_shop_item 테이블에 없는 상품으로 인한 프로필 숨김 및 주문 차단 이력을 조회합니다.
</p>

<form method="get" class="ipl-search">
<input type="hidden" name="page" value="1">
<div class="ipl-fields">
  <div class="ipl-field">
    <label>회원 ID</label>
    <input type="text" name="s_mb_id" value="<?php echo htmlspecialchars($s_mb_id); ?>" placeholder="회원 ID 검색">
  </div>
  <div class="ipl-field">
    <label>상품명/번호</label>
    <input type="text" name="s_it_name" value="<?php echo htmlspecialchars($s_it_name); ?>" placeholder="상품명 또는 번호">
  </div>
  <div class="ipl-field">
    <label>날짜 범위</label>
    <input type="date" name="s_date_from" value="<?php echo htmlspecialchars($s_date_from); ?>">
    <span class="ipl-sep">~</span>
    <input type="date" name="s_date_to"   value="<?php echo htmlspecialchars($s_date_to); ?>">
  </div>
  <div class="ipl-field">
    <label>유형</label>
    <select name="s_action">
      <option value="">전체</option>
      <option value="profile_hidden" <?php if($s_action==='profile_hidden') echo 'selected'; ?>>프로필 숨김</option>
      <option value="order_blocked"  <?php if($s_action==='order_blocked')  echo 'selected'; ?>>주문 차단</option>
    </select>
  </div>
  <div class="ipl-btns">
    <button type="submit" class="ipl-btn-search">검색</button>
    <a href="?page=1" class="ipl-btn-reset">초기화</a>
  </div>
</div>
</form>

<div class="ipl-stat">총 <strong><?php echo number_format($total); ?></strong>건 | 페이지 <?php echo $page; ?>/<?php echo $total_pages; ?></div>

<table class="ipl-table">
<thead>
<tr>
  <th style="width:60px">번호</th>
  <th style="width:150px">발생 일시</th>
  <th style="width:160px">회원 ID</th>
  <th style="width:120px">VPN 상품명</th>
  <th style="width:100px">상품 번호</th>
  <th style="width:100px">유형</th>
  <th>내용</th>
</tr>
</thead>
<tbody>
<?php if (count($rows) < 1): ?>
<tr><td colspan="7" style="text-align:center;padding:24px;color:#888;">조회 결과가 없습니다.</td></tr>
<?php else: foreach ($rows as $r): ?>
<tr>
  <td style="text-align:center"><?php echo (int)$r['id']; ?></td>
  <td style="text-align:center"><?php echo htmlspecialchars($r['created_at']); ?></td>
  <td><?php echo htmlspecialchars($r['mb_id']); ?></td>
  <td><?php echo htmlspecialchars($r['vpn_it_name']); ?></td>
  <td style="text-align:center"><?php echo htmlspecialchars($r['shop_it_id']); ?></td>
  <td style="text-align:center">
    <?php
    $act = $r['action_type'];
    $lbl = isset($action_label[$act]) ? $action_label[$act] : htmlspecialchars($act);
    $cls = ($act === 'profile_hidden') ? 'badge-profile' : 'badge-order';
    echo "<span class=\"{$cls}\">{$lbl}</span>";
    ?>
  </td>
  <td><?php echo htmlspecialchars($r['user_message']); ?></td>
</tr>
<?php endforeach; endif; ?>
</tbody>
</table>

<?php if ($total_pages > 1): ?>
<div class="ipl-pager">
<?php
$qs_base = ipl_qs();
for ($p = 1; $p <= $total_pages; $p++):
    $qs = ipl_qs(array('page' => $p));
    if ($p === $page):
        echo "<span class=\"current\">{$p}</span>";
    else:
        echo "<a href=\"?{$qs}\">{$p}</a>";
    endif;
endfor;
?>
</div>
<?php endif; ?>

</div>

<?php include_once(G5_ADMIN_PATH.'/admin.tail.php'); ?>
