<?php
include_once 'auth.php';

$totalParticipants = mysqli_num_rows(
    mysqli_query(
        $conn,
        "SELECT id FROM participants"
    )
);

$total7K = mysqli_num_rows(
    mysqli_query(
        $conn,
        "SELECT id FROM participants
         WHERE distance_category='7K'"
    )
);

$total9K = mysqli_num_rows(
    mysqli_query(
        $conn,
        "SELECT id FROM participants
         WHERE distance_category='9K'"
    )
);

$pendingPayment = mysqli_num_rows(
    mysqli_query(
        $conn,
        "SELECT id FROM participants
         WHERE payment_status='pending'"
    )
);

$verifiedCount = mysqli_num_rows(
    mysqli_query(
        $conn,
        "SELECT id FROM participants
         WHERE payment_status='verified'"
    )
);

$rejectedCount = mysqli_num_rows(
    mysqli_query(
        $conn,
        "SELECT id FROM participants
         WHERE payment_status='rejected'"
    )
);

/* Recent registrations */
$recent = mysqli_query(
    $conn,
    "SELECT * FROM participants
     ORDER BY id DESC LIMIT 5"
);

?>

<!-- WELCOME -->
<div class="page-header">

    <div>

        <h1 class="page-title">
            <?php echo tr(
                'Welcome back',
                'Selamat datang'
            ); ?>, <span><?php echo htmlspecialchars($_SESSION['admin_username'] ?? 'Admin'); ?></span>
        </h1>

    </div>

</div>

<!-- STAT CARDS -->
<div class="stats-grid">

    <!-- Total -->
    <div class="stat-card stat-card--total">

        <div class="stat-card__header">

            <div class="stat-card__icon">
                <svg viewBox="0 0 24 24">
                    <path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/>
                    <circle cx="9" cy="7" r="4"/>
                    <path d="M23 21v-2a4 4 0 0 0-3-3.87"/>
                    <path d="M16 3.13a4 4 0 0 1 0 7.75"/>
                </svg>
            </div>

        </div>

        <div class="stat-card__value" data-count="<?php echo $totalParticipants; ?>">
            <?php echo $totalParticipants; ?>
        </div>

        <div class="stat-card__label">
            <?php echo tr('Total Participants','Total Peserta'); ?>
        </div>

    </div>

    <!-- 7K -->
    <div class="stat-card stat-card--7k">

        <div class="stat-card__header">

            <div class="stat-card__icon">
                <svg viewBox="0 0 24 24">
                    <polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/>
                </svg>
            </div>

        </div>

        <div class="stat-card__value" data-count="<?php echo $total7K; ?>">
            <?php echo $total7K; ?>
        </div>

        <div class="stat-card__label">
            <?php echo tr('7K Runners','Peserta 7K'); ?>
        </div>

    </div>

    <!-- 9K -->
    <div class="stat-card stat-card--9k">

        <div class="stat-card__header">

            <div class="stat-card__icon">
                <svg viewBox="0 0 24 24">
                    <circle cx="12" cy="12" r="10"/>
                    <polyline points="12 6 12 12 16 14"/>
                </svg>
            </div>

        </div>

        <div class="stat-card__value" data-count="<?php echo $total9K; ?>">
            <?php echo $total9K; ?>
        </div>

        <div class="stat-card__label">
            <?php echo tr('9K Runners','Peserta 9K'); ?>
        </div>

    </div>

    <!-- Pending -->
    <div class="stat-card stat-card--pending">

        <div class="stat-card__header">

            <div class="stat-card__icon">
                <svg viewBox="0 0 24 24">
                    <circle cx="12" cy="12" r="10"/>
                    <line x1="12" y1="8" x2="12" y2="12"/>
                    <line x1="12" y1="16" x2="12.01" y2="16"/>
                </svg>
            </div>

        </div>

        <div class="stat-card__value" data-count="<?php echo $pendingPayment; ?>">
            <?php echo $pendingPayment; ?>
        </div>

        <div class="stat-card__label">
            <?php echo tr('Pending Payments','Pembayaran Pending'); ?>
        </div>

    </div>

    <!-- Verified -->
    <div class="stat-card stat-card--verified">

        <div class="stat-card__header">

            <div class="stat-card__icon">
                <svg viewBox="0 0 24 24">
                    <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/>
                    <polyline points="22 4 12 14.01 9 11.01"/>
                </svg>
            </div>

        </div>

        <div class="stat-card__value" data-count="<?php echo $verifiedCount; ?>">
            <?php echo $verifiedCount; ?>
        </div>

        <div class="stat-card__label">
            <?php echo tr('Verified','Terverifikasi'); ?>
        </div>

    </div>

    <!-- Rejected -->
    <div class="stat-card stat-card--rejected">

        <div class="stat-card__header">

            <div class="stat-card__icon">
                <svg viewBox="0 0 24 24">
                    <circle cx="12" cy="12" r="10"/>
                    <line x1="15" y1="9" x2="9" y2="15"/>
                    <line x1="9" y1="9" x2="15" y2="15"/>
                </svg>
            </div>

        </div>

        <div class="stat-card__value" data-count="<?php echo $rejectedCount; ?>">
            <?php echo $rejectedCount; ?>
        </div>

        <div class="stat-card__label">
            <?php echo tr('Rejected','Ditolak'); ?>
        </div>

    </div>

</div>

<!-- RECENT REGISTRATIONS -->
<div class="section-card">

    <div class="section-card__header">

        <span class="section-card__title">
            <?php echo tr(
                'Recent Registrations',
                'Pendaftaran Terbaru'
            ); ?>
        </span>

        <a href="index.php?page=participants"
           class="admin-btn">
            <?php echo tr('View All','Lihat Semua'); ?>
        </a>

    </div>

    <div style="overflow:auto">

        <table class="admin-table">

            <thead>

                <tr>

                    <th>BIB</th>

                    <th>
                        <?php echo tr('Name','Nama'); ?>
                    </th>

                    <th>
                        <?php echo tr('Distance','Jarak'); ?>
                    </th>

                    <th>
                        <?php echo tr('Category','Kategori'); ?>
                    </th>

                    <th>
                        <?php echo tr('Status','Status'); ?>
                    </th>

                </tr>

            </thead>

            <tbody>

                <?php while($row = mysqli_fetch_assoc($recent)){ ?>

                <tr>

                    <td data-label="BIB"><?php echo $row['bib_number']; ?></td>

                    <td data-label="<?php echo tr('Name','Nama'); ?>"><?php echo $row['full_name']; ?></td>

                    <td data-label="<?php echo tr('Distance','Jarak'); ?>"><?php echo $row['distance_category']; ?></td>

                    <td data-label="<?php echo tr('Category','Kategori'); ?>"><?php echo $row['participant_category']; ?></td>

                    <td data-label="<?php echo tr('Status','Status'); ?>">
                        <?php
                        $s = $row['payment_status'];
                        $cls = 'badge--' . $s;
                        echo "<span class=\"badge $cls\">"
                            . ucfirst($s)
                            . "</span>";
                        ?>
                    </td>

                </tr>

                <?php } ?>

            </tbody>

        </table>

    </div>

</div>