<?php

include_once '../config/database.php';
include_once '../template/lang.php';
include_once 'auth.php';

/* =========================================================
   STATUS FILTER
========================================================= */

$status = isset($_GET['status'])
    ? $_GET['status']
    : 'pending';

/* =========================================================
   ALLOWED STATUS
========================================================= */

$allowed_status = [
    'pending',
    'verified',
    'rejected'
];

if(!in_array($status, $allowed_status)){

    $status = 'pending';

}

/* =========================================================
   QUERY
========================================================= */

$result = mysqli_query(
    $conn,
    "SELECT * FROM participants
     WHERE payment_status='$status'
     ORDER BY id DESC"
);

?>

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

    <h1 class="page-title">
        <span><?php echo ucfirst($status); ?></span>
        <?php echo tr('Participants','Peserta'); ?>
    </h1>

    <!-- FILTER TABS -->
    <div class="filter-tabs">

        <a href="index.php?page=participants&status=pending"
           class="filter-tab <?php echo ($status === 'pending') ? 'active' : ''; ?>">
            Pending
        </a>

        <a href="index.php?page=participants&status=verified"
           class="filter-tab filter-tab--verified <?php echo ($status === 'verified') ? 'active' : ''; ?>">
            Verified
        </a>

        <a href="index.php?page=participants&status=rejected"
           class="filter-tab filter-tab--rejected <?php echo ($status === 'rejected') ? 'active' : ''; ?>">
            Rejected
        </a>

    </div>

</div>

<!-- TABLE -->
<div class="admin-table-wrap">

    <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>

                <th>Payment</th>

                <th>KTP</th>

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

            </tr>

        </thead>

        <tbody>

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

            <?php

            /* =========================================
               FORMAT WHATSAPP NUMBER
            ========================================== */

            $wa = preg_replace(
                '/^0/',
                '62',
                $row['whatsapp']
            );

            /* =========================================
               WHATSAPP MESSAGE
            ========================================== */

            $message =
                        "Halo ".$row['full_name'].",\n\n".

                        "Pendaftaran Anda untuk UK Trail Run 2026 telah TERKONFIRMASI.\n\n".

                        "Nomor BIB: ".$row['bib_number']."\n".
                        "Kategori Jarak: ".$row['distance_category']."\n".
                        "Kategori Peserta: ".$row['participant_category']."\n\n".

                        "Silakan mempersiapkan diri untuk hari perlombaan.\n".
                        "Sampai jumpa di garis start!\n\n".

                        "Gabung Grup WhatsApp Resmi:\n".
                        "https://chat.whatsapp.com/JlBFNSMqQxtFJft9UMDF4g";

            ?>

            <tr>

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

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

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

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

                <!-- STATUS -->
                <td data-label="<?php echo tr('Status','Status'); ?>">

                    <?php

                    if($row['payment_status'] == 'verified'){

                        echo '
                        <span class="badge badge--verified">
                            Verified
                        </span>
                        ';

                    }elseif($row['payment_status'] == 'rejected'){

                        echo '
                        <span class="badge badge--rejected">
                            Rejected
                        </span>
                        ';

                    }else{

                        echo '
                        <span class="badge badge--pending">
                            Pending
                        </span>
                        ';
                    }

                    ?>

                </td>

                <!-- PAYMENT -->
                <td data-label="Payment">

                    <a href="../uploads/payment/<?php echo $row['payment_file']; ?>"
                       target="_blank"
                       class="admin-btn">

                        Payment

                    </a>

                </td>

                <!-- KTP -->
                <td data-label="KTP">

                    <a href="../uploads/ktp/<?php echo $row['ktp_file']; ?>"
                       target="_blank"
                       class="admin-btn">

                        KTP

                    </a>

                </td>

                <!-- ACTION -->
                <td data-label="<?php echo tr('Action','Aksi'); ?>">

                    <div class="admin-actions">

                        <!-- VERIFY -->
                        <a href="update_status.php?id=<?php echo $row['id']; ?>&status=verified"
                           class="admin-btn admin-btn--verify"

                           onclick="return confirm(
                               'Move participant to VERIFIED?'
                           );">

                            Verify

                        </a>

                        <!-- PENDING -->
                        <a href="update_status.php?id=<?php echo $row['id']; ?>&status=pending"
                           class="admin-btn"

                           onclick="return confirm(
                               'Move participant to PENDING?'
                           );">

                            Pending

                        </a>

                        <!-- REJECT -->
                        <a href="update_status.php?id=<?php echo $row['id']; ?>&status=rejected"
                           class="admin-btn admin-btn--reject"

                           onclick="return confirm(
                               'Move participant to REJECTED?'
                           );">

                            Reject

                        </a>

                        <!-- WHATSAPP -->
                        <?php if($row['payment_status'] == 'verified'){ ?>

                        <a href="https://wa.me/<?php echo $wa; ?>?text=<?php echo urlencode($message); ?>"
                           target="_blank"
                           class="admin-btn">

                            WhatsApp

                        </a>

                        <?php } ?>

                    </div>

                </td>

            </tr>

            <?php } ?>

        </tbody>

    </table>

</div>