<?php
include 'config.php';

header('Content-Type: text/html; charset=utf-8');

function e($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

function pick(array $row, array $keys, $default = '-') {
    foreach ($keys as $k) {
        if (isset($row[$k]) && $row[$k] !== '') return $row[$k];
    }
    return $default;
}

function formatMoney($value) {
    if ($value === null || $value === '') return 'TK 0';
    if (is_numeric($value)) return 'TK ' . number_format((float)$value, 0);
    return 'TK ' . e($value);
}

// Get filters from URL
$type = trim($_GET['type'] ?? ($_POST['type'] ?? 'owner'));
$time_filter = trim($_GET['time'] ?? ($_POST['time'] ?? 'all'));

$owner_username = '';
$owner_res = $conn->query("SELECT username FROM admin LIMIT 1");
if ($owner_res && $owner_res->num_rows > 0) {
    $owner_row = $owner_res->fetch_assoc();
    $owner_username = $owner_row['username'] ?? '';
}

$logs = [];

// Date filter condition
$date_sql = "";
if ($time_filter === 'today') {
    $date_sql = " AND DATE(refunded_at) = CURDATE()";
}

if ($type === 'owner') {
    $stmt = $conn->prepare("SELECT * FROM refund_logs WHERE moderator_username = ? $date_sql ORDER BY refunded_at DESC");
    $stmt->bind_param("s", $owner_username);
    $stmt->execute();
    $res = $stmt->get_result();
    $logs = $res ? $res->fetch_all(MYSQLI_ASSOC) : [];
    $stmt->close();
} elseif ($type === 'moderator') {
    $stmt = $conn->prepare("SELECT * FROM refund_logs WHERE moderator_username != ? $date_sql ORDER BY refunded_at DESC");
    $stmt->bind_param("s", $owner_username);
    $stmt->execute();
    $res = $stmt->get_result();
    $logs = $res ? $res->fetch_all(MYSQLI_ASSOC) : [];
    $stmt->close();
}

$totalAmount = 0;
foreach ($logs as $row) {
    $amt = pick($row, ['refund_amount', 'amount', 'refunded_amount', 'money'], 0);
    if (is_numeric($amt)) $totalAmount += (float)$amt;
}

function tabActive($current, $tab) {
    return $current === $tab ? 'active' : '';
}

$avatarColors = ['#6366F1', '#EC4899', '#F59E0B', '#10B981', '#3B82F6', '#8B5CF6', '#EF4444', '#14B8A6'];
function avatarColor($name, $palette) {
    $sum = 0;
    foreach (str_split((string)$name) as $ch) $sum += ord($ch);
    return $palette[$sum % count($palette)];
}

$backUrl = 'https://skmesports.fun/Elitegoxadmin/ZabbarAdminPHP/index.php';
require_once "header.php";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Refund History</title>
<style>
*{box-sizing:border-box; -webkit-tap-highlight-color:transparent;}
html,body{ margin:0; padding:0; }
body{
    background:#F8F9FC;
    font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    color:#0F172A;
    -webkit-font-smoothing:antialiased;
}

@keyframes fadeUp{
    from{ opacity:0; transform:translateY(8px); }
    to{ opacity:1; transform:translateY(0); }
}

/* ---------- Top bar ---------- */
.topbar{
    position:sticky;
    top:0;
    z-index:20;
    display:flex;
    align-items:center;
    gap:13px;
    padding:16px 18px;
    background:rgba(255,255,255,.78);
    backdrop-filter:blur(18px) saturate(180%);
    -webkit-backdrop-filter:blur(18px) saturate(180%);
    border-bottom:1px solid rgba(15,23,42,.06);
}
.back{
    width:40px;height:40px;
    border:none;
    background:#FFFFFF;
    border-radius:13px;
    color:#0F172A;
    font-size:18px;
    cursor:pointer;
    display:flex;align-items:center;justify-content:center;
    text-decoration:none;
    box-shadow:0 1px 2px rgba(15,23,42,.04), 0 4px 10px rgba(15,23,42,.06);
    border:1px solid #F0F1F5;
    transition:transform .15s ease, box-shadow .15s ease;
}
.back:active{
    transform:scale(.92);
    box-shadow:0 1px 2px rgba(15,23,42,.04);
}
.title-block{ display:flex; flex-direction:column; }
.title{
    font-size:18px;
    font-weight:800;
    letter-spacing:-.3px;
    color:#0F172A;
}
.subtitle{
    font-size:11.5px;
    color:#94A3B8;
    font-weight:600;
    margin-top:1px;
    letter-spacing:.1px;
}

/* ---------- Tabs ---------- */
.tabs-container {
    position:sticky;
    top:72px;
    z-index:15;
    background:#F8F9FC;
    padding:12px 14px 0;
    display:flex;
    flex-direction:column;
    gap:8px;
}
.tabs{
    display:flex;
    gap:6px;
}
.tab{
    flex:1;
    text-align:center;
    text-decoration:none;
    color:#94A3B8;
    font-weight:700;
    font-size:13px;
    padding:10px 8px;
    border-radius:10px;
    background:#EEF0F5;
    transition:all .2s ease;
}
.tab.active{
    color:#4F46E5;
    background:#FFFFFF;
    box-shadow:0 1px 3px rgba(15,23,42,.06);
}

/* ---------- Summary strip ---------- */
.summary{
    display:flex;
    gap:10px;
    padding:14px;
    max-width:640px;
    margin:0 auto;
}
.stat{
    flex:1;
    background:#FFFFFF;
    border:1px solid #EFF1F5;
    border-radius:18px;
    padding:13px 15px;
    box-shadow:0 1px 2px rgba(15,23,42,.03);
    position:relative;
    overflow:hidden;
}
.stat::before{
    content:"";
    position:absolute;
    top:0; left:0; right:0; height:3px;
    background:linear-gradient(90deg,#4F46E5,#818CF8);
}
.stat.accent::before{
    background:linear-gradient(90deg,#16A34A,#4ADE80);
}
.stat .k{
    font-size:10.5px;
    color:#94A3B8;
    font-weight:700;
    text-transform:uppercase;
    letter-spacing:.5px;
    margin-bottom:5px;
}
.stat .v{
    font-size:18px;
    font-weight:800;
    color:#0F172A;
    letter-spacing:-.3px;
}
.stat.accent .v{ color:#16A34A; }

/* ---------- Wrap ---------- */
.wrap{ padding:0 14px 28px; max-width:640px; margin:0 auto; }

/* ---------- Card ---------- */
.card{
    background:#FFFFFF;
    border-radius:22px;
    margin-bottom:14px;
    overflow:hidden;
    border:1px solid #EFF1F5;
    box-shadow:0 1px 2px rgba(15,23,42,.03), 0 10px 28px -14px rgba(15,23,42,.10);
    transition:box-shadow .25s ease, transform .25s ease;
    animation:fadeUp .4s ease both;
}
.card:hover{
    box-shadow:0 1px 2px rgba(15,23,42,.04), 0 18px 38px -14px rgba(15,23,42,.16);
    transform:translateY(-2px);
}

/* Header */
.card-head{
    padding:17px 17px 15px;
    display:flex;
    align-items:flex-start;
    gap:12px;
    border-bottom:1px solid #F5F6F9;
}
.num-box{
    width:38px;height:38px;min-width:38px;
    background:linear-gradient(135deg,#1E293B,#0F172A);
    border-radius:12px;
    display:flex;align-items:center;justify-content:center;
    color:#fff;font-size:13px;font-weight:800;
    font-variant-numeric:tabular-nums;
    box-shadow:0 3px 8px rgba(15,23,42,.25);
}
.head-mid{
    flex:1;
    min-width:0;
    padding-top:1px;
}
.match-title{
    color:#0F172A;font-size:14.5px;font-weight:800;
    line-height:1.3;
    letter-spacing:-.15px;
    overflow:hidden;text-overflow:ellipsis;
    display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;
}
.match-id{
    color:#94A3B8;font-size:11.5px;font-weight:700;margin-top:3px;
    font-variant-numeric:tabular-nums;
}
.amount-pill{
    background:linear-gradient(135deg,#F0FDF4,#DCFCE7);
    color:#15803D;
    border:1px solid #BBF7D0;
    font-size:12.5px;font-weight:800;
    padding:8px 14px;
    border-radius:999px;
    white-space:nowrap;
    font-variant-numeric:tabular-nums;
}

/* Body */
.card-body{
    padding:15px 17px;
}
.row-date-status{
    display:flex;
    justify-content:space-between;
    align-items:center;
    margin-bottom:14px;
}
.date-row{
    display:flex;align-items:center;gap:6px;
    color:#64748B;font-size:12.5px;font-weight:600;
}
.date-row .ic{ font-size:13px; }
.status-pill{
    background:linear-gradient(135deg,#FEF2F2,#FEE2E2);
    color:#B91C1C;
    border:1px solid #FECACA;
    font-size:10.5px;font-weight:800;
    padding:6px 12px;border-radius:999px;
    letter-spacing:.5px;
}

.divider-label{
    display:flex;align-items:center;justify-content:space-between;
    margin-bottom:12px;
}
.label-text{
    font-size:11px;
    color:#A0A7B4;
    font-weight:700;
    text-transform:uppercase;
    letter-spacing:.6px;
}
.play-icon{
    width:28px;height:28px;
    display:flex;align-items:center;justify-content:center;
    background:#F8F9FB;
    border-radius:50%;
    color:#64748B;font-size:12px;
    border:1px solid #EFF1F5;
}

.ref-by-row{
    display:flex;align-items:center;gap:10px;
    margin-bottom:14px;
}
.avatar{
    width:34px;height:34px;
    border-radius:11px;
    color:#fff;
    display:flex;align-items:center;justify-content:center;
    font-size:13px;font-weight:800;
    flex-shrink:0;
    box-shadow:0 3px 8px rgba(0,0,0,.15);
}
.ref-name{
    font-size:13.5px;
    font-weight:700;
    color:#1E293B;
}

.amount-line{
    display:flex;align-items:center;justify-content:space-between;
    background:#FAFBFD;
    border:1px solid #F1F3F6;
    padding:11px 13px;
    border-radius:13px;
}
.amount-line .lbl{
    font-size:12.5px;color:#64748B;font-weight:700;
    display:flex;align-items:center;gap:6px;
}
.amount-line .val{
    font-size:14.5px;color:#0F172A;font-weight:800;
    font-variant-numeric:tabular-nums;
}

/* Email strip */
.email-strip{
    background:linear-gradient(135deg,#FFFBEB,#FEF9E7);
    border-top:1px solid #FEF3C7;
    padding:12px 17px;
    display:flex;align-items:center;gap:7px;
    color:#B45309;font-size:12px;font-weight:700;
    word-break:break-all;
}
.email-strip .ic{ font-size:12px; opacity:.85; }

/* Footer */
.footer-brand{
    padding:10px 17px;
    display:flex;align-items:center;gap:6px;
    color:#B0B5C0;font-size:11px;font-weight:700;
    letter-spacing:.5px;
    text-transform:uppercase;
    background:#FCFCFD;
}

/* Empty state */
.empty{
    text-align:center;
    padding:64px 24px;
    background:#FFFFFF;
    border-radius:22px;
    border:1px solid #EFF1F5;
    margin-top:4px;
}
.empty .icon{ font-size:36px; margin-bottom:12px; opacity:.5; }
.empty .msg{ font-size:14.5px; color:#64748B; font-weight:700; }

@media (max-width:400px){
    .title{ font-size:16.5px; }
    .stat .v{ font-size:16px; }
}
</style>
</head>
<body>

<div class="topbar">
    <a href="<?php echo e($backUrl); ?>" class="back">←</a>
    <div class="title-block">
        <div class="title">Refund History</div>
        <div class="subtitle">SKM eSPORTS · Admin Panel</div>
    </div>
</div>

<div class="tabs-container">
    <!-- User Type Tabs -->
    <div class="tabs">
        <a class="tab <?php echo tabActive($type, 'owner'); ?>" href="?type=owner&time=<?php echo $time_filter; ?>">Owner History</a>
        <a class="tab <?php echo tabActive($type, 'moderator'); ?>" href="?type=moderator&time=<?php echo $time_filter; ?>">Moderator History</a>
    </div>
    
    <!-- Time Filter Tabs -->
    <div class="tabs">
        <a class="tab <?php echo tabActive($time_filter, 'today'); ?>" href="?type=<?php echo $type; ?>&time=today">Today History</a>
        <a class="tab <?php echo tabActive($time_filter, 'all'); ?>" href="?type=<?php echo $type; ?>&time=all">All History</a>
    </div>
</div>

<div class="summary">
    <div class="stat">
        <div class="k">Total Refunds</div>
        <div class="v"><?php echo count($logs); ?></div>
    </div>
    <div class="stat accent">
        <div class="k">Total Amount</div>
        <div class="v"><?php echo e(formatMoney($totalAmount)); ?></div>
    </div>
</div>

<div class="wrap">
<?php if (!empty($logs)): ?>
    <?php foreach ($logs as $i => $row):
        $title = pick($row, ['match_title', 'title', 'match_name', 'name'], 'Unknown Match');
        $match_id = pick($row, ['match_id', 'id', 'match_no', 'match_code'], '-');
        $amount = pick($row, ['refund_amount', 'amount', 'refunded_amount', 'money'], 0);
        $refunded_at = pick($row, ['refunded_at', 'created_at', 'date', 'time'], '-');
        $refunded_by = pick($row, ['moderator_username', 'refunded_by', 'username', 'admin_username'], '-');
        $email = pick($row, ['email', 'user_email', 'account_email'], '-');
        $status = pick($row, ['status', 'refund_status'], 'REFUNDED');
        $initial = strtoupper(mb_substr((string)$refunded_by, 0, 1));
        if ($initial === '') $initial = 'A';
        $color = avatarColor($refunded_by, $avatarColors);
    ?>
    <div class="card" style="animation-delay:<?php echo min($i * 0.04, 0.4); ?>s">
        <div class="card-head">
            <div class="num-box"><?php echo $i + 1; ?></div>
            <div class="head-mid">
                <div class="match-title"><?php echo e($title); ?></div>
                <div class="match-id">#<?php echo e($match_id); ?></div>
            </div>
            <div class="amount-pill"><?php echo e(formatMoney($amount)); ?></div>
        </div>

        <div class="card-body">
            <div class="row-date-status">
                <div class="date-row"><span class="ic">🕒</span> <?php echo e($refunded_at); ?></div>
                <div class="status-pill"><?php echo e(strtoupper($status)); ?></div>
            </div>

            <div class="divider-label">
                <div class="label-text">Refunded By</div>
                <div class="play-icon">▶</div>
            </div>

            <div class="ref-by-row">
                <div class="avatar" style="background:<?php echo e($color); ?>"><?php echo e($initial); ?></div>
                <div class="ref-name"><?php echo e($refunded_by); ?></div>
            </div>

            <div class="amount-line">
                <div class="lbl">💸 Refund Amount</div>
                <div class="val"><?php echo e(formatMoney($amount)); ?></div>
            </div>
        </div>

        <div class="email-strip">
            <span class="ic">✉</span> <?php echo e($email); ?>
        </div>

        <div class="footer-brand">
            🎮 SKM eSPORTS
        </div>
    </div>
    <?php endforeach; ?>
<?php else: ?>
    <div class="empty">
        <div class="icon">📭</div>
        <div class="msg">No refund history found</div>
    </div>
<?php endif; ?>
</div>

</body>
</html>
<?php $conn->close(); ?>
