<?php

include_once '../config/database.php';

/* =========================================================
   GET QR CODE
========================================================= */

$code = trim($_GET['code']);

/* =========================================================
   GET PARTICIPANT
========================================================= */

$query = mysqli_query(

    $conn,

    "SELECT

        participants.*,

        participant_collection.status

     FROM participant_qr

     INNER JOIN participants

     ON participant_qr.participant_id =
        participants.id

     LEFT JOIN participant_collection

     ON participant_collection.participant_id =
        participants.id

     WHERE UPPER(participant_qr.qr_code)=UPPER('$code')"
);

/* =========================================================
   SQL DEBUG
========================================================= */

if(!$query){

    die(mysqli_error($conn));

}

/* =========================================================
   QR NOT FOUND
========================================================= */

if(mysqli_num_rows($query) == 0){

    echo '

    <div class="admin-card">

        <div class="badge badge--rejected"
             style="
             font-size:20px;
             padding:14px 22px;
             ">

            QR NOT FOUND

        </div>

    </div>

    ';

    exit;

}

$row = mysqli_fetch_assoc($query);

/* =========================================================
   ALREADY COLLECTED
========================================================= */

if($row['status'] == 'collected'){

    echo '

    <div class="admin-card">

        <div class="badge badge--rejected"
             style="
             font-size:20px;
             padding:14px 22px;
             margin-bottom:20px;
             ">

            RACE PACK SUDAH DIAMBIL

        </div>

        <h2>'.$row['full_name'].'</h2>

        <p>
            BIB:
            '.$row['bib_number'].'
        </p>

        <p>
            Distance:
            '.$row['distance_category'].'
        </p>

        <p>
            Category:
            '.$row['participant_category'].'
        </p>

    </div>

    ';

    exit;

}

/* =========================================================
   READY FOR COLLECTION
========================================================= */

echo '

<div class="admin-card">

    <div class="badge badge--verified"
         style="
         font-size:20px;
         padding:14px 22px;
         margin-bottom:20px;
         ">

        READY FOR COLLECTION

    </div>

    <h2>'.$row['full_name'].'</h2>

    <p>
        BIB:
        '.$row['bib_number'].'
    </p>

    <p>
        Distance:
        '.$row['distance_category'].'
    </p>

    <p>
        Category:
        '.$row['participant_category'].'
    </p>

    <br>

    <button

    onclick="collectParticipant('.$row['id'].')"

    class="admin-btn admin-btn--verify"

    style="
    font-size:18px;
    padding:14px 26px;
    border:none;
    cursor:pointer;
    ">

        MARK AS COLLECTED

    </button>

</div>

';