<?php

include_once 'auth.php';

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

/* =========================================================
   SEARCH
========================================================= */

$search = isset($_GET['search'])
    ? trim($_GET['search'])
    : '';

/* =========================================================
   PARTICIPANTS
========================================================= */

$participants = mysqli_query(

    $conn,

    "SELECT

        participants.*,
        participant_rfid.rfid_uid

     FROM participants

     LEFT JOIN participant_rfid

     ON participant_rfid.participant_id =
        participants.id

     WHERE participants.payment_status='verified'

     AND (

        participants.full_name LIKE '%$search%'

        OR

        participants.bib_number LIKE '%$search%'

     )

     ORDER BY participants.id DESC

     LIMIT 20"
);

?>

<div class="page-header">

    <h1 class="page-title">
        RFID Assignment
    </h1>

</div>

<!-- SEARCH -->
<div class="admin-card racepack-search">

    <form method="GET"
          class="racepack-search__form">

        <input type="hidden"
               name="page"
               value="rfid">

        <input type="text"
               name="search"
               placeholder="Search name or BIB"
               value="<?php echo htmlspecialchars($search); ?>"
               class="admin-input">

        <button class="admin-btn admin-btn--primary">

            Search

        </button>

    </form>

</div>

<div class="rfid-grid">

    <!-- LEFT -->
    <div>

        <div class="admin-card rfid-live-card">

            <div class="rfid-live-label">

                LIVE RFID UID

            </div>

            <div class="rfid-uid"
                 id="rfid-live">

                Waiting RFID...

            </div>

            <div class="rfid-status">

                <span class="badge badge--pending">

                    Tap RFID Card

                </span>

            </div>

        </div>

    </div>

    <!-- RIGHT -->
    <div>

        <div class="admin-card rfid-table-card">

            <div class="racepack-card-header">

                <h2>

                    Verified Participants

                </h2>

            </div>

            <div style="overflow:auto;">

                <table class="admin-table rfid-table">

                    <thead>

                        <tr>

                            <th>BIB</th>
                            <th>Name</th>
                            <th>RFID UID</th>
                            <th>Action</th>

                        </tr>

                    </thead>

                    <tbody>

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

                        <tr>

                            <td data-label="BIB">

                                <?php echo $row['bib_number']; ?>

                            </td>

                            <td data-label="Name">

                                <?php echo $row['full_name']; ?>

                            </td>

                            <td data-label="RFID">

                                <?php

                                if($row['rfid_uid']){

                                    echo '

                                    <span class="rfid-badge rfid-badge--active">

                                        '.$row['rfid_uid'].'

                                    </span>

                                    ';

                                }else{

                                    echo '

                                    <span class="rfid-badge rfid-badge--empty">

                                        EMPTY

                                    </span>

                                    ';
                                }

                                ?>

                            </td>

                            <td data-label="Action">

                                <div class="rfid-action">

                                    <button
                                    type="button"
                                    onclick="assignRFID(<?php echo $row['id']; ?>)"
                                    class="admin-btn admin-btn--verify">

                                        ASSIGN

                                    </button>

                                    <button
                                    type="button"
                                    onclick="replaceRFID(<?php echo $row['id']; ?>)"
                                    class="admin-btn">

                                        REPLACE

                                    </button>

                                    <button
                                    type="button"
                                    onclick="removeRFID(<?php echo $row['id']; ?>)"
                                    class="admin-btn admin-btn--reject">

                                        REMOVE

                                    </button>

                                </div>

                            </td>

                        </tr>

                        <?php } ?>

                    </tbody>

                </table>

            </div>

        </div>

    </div>

</div>

<script>

/* =========================================================
   LIVE RFID POLLING
========================================================= */

setInterval(() => {

    fetch('latest_uid.php')

    .then(response => response.text())

    .then(data => {

        if(data.trim() != ''){

            document.getElementById(

                'rfid-live'

            ).innerHTML = data;

        }

    });

}, 1000);

/* =========================================================
   ASSIGN RFID
========================================================= */

function assignRFID(id){

    fetch(

        'save_rfid.php?id=' + id

    )

    .then(response => response.text())

    .then(data => {

        if(data.trim() != 'OK'){

            alert(data);

            return;

        }

        location.reload();

    });

}

/* =========================================================
   REPLACE RFID
========================================================= */

function replaceRFID(id){

    fetch(

        'replace_rfid.php?id=' + id

    )

    .then(() => {

        location.reload();

    });

}

/* =========================================================
   REMOVE RFID
========================================================= */

function removeRFID(id){

    if(!confirm('Remove RFID?')){

        return;

    }

    fetch(

        'remove_rfid.php?id=' + id

    )

    .then(() => {

        location.reload();

    });

}

</script>