<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
// $html->check_user_type("ADMIN");
$db = new db_safeguard();

$leads_res = $db->query("work_requests", "SELECT * FROM work_requests WHERE type != 'JOBCARD'  ORDER BY jc_no DESC");

if ($leads_res->num_rows > 0) {

    while ($leads = $leads_res->fetch_assoc()) {
        $user_res = $db->query("users", "SELECT * FROM users WHERE record_id = {$leads['user_id']}");
        $user = $user_res->fetch_assoc();

        $table_data = $table_data . "
        <tr>
            <td>{$leads['jc_no']}</td>
            <td>{$leads['contact_number']}</td>
            <td>{$leads['address']}</td>
            <td>{$leads['type']}</td>
            <td>{$user['username']}</td>
            <td>{$leads['status']}</td>
            <td><input type='input' class='submit_btn' style='margin: 0.5vw auto;' value='ASSIGN' onclick='window.location.href = `assign_jobcard.php?jc_no={$leads['jc_no']}`' readonly></td>
        </tr>
    ";
    }

} else {

    $table_data = $table_data . "
        <tr>
            <td colspan='6' style='text-align: center;'>NO LEADS FOUND</td>
        </tr>
    ";

}

?>
<style>

    .form_down h1 {
        margin-bottom: 1.5vw;
        font-size: 2em;
        color: #333;
    }

    table {
        width: 98%;
        border-collapse: collapse;
        background-color: #ffffff;
        border-radius: 8px;
        overflow: hidden;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    }

    th {
        background-color: #2c3e50;
        color: #ffffff;
        text-align: left;
        padding: 0.8vw;
        font-size: 1em;
        text-transform: uppercase;
        letter-spacing: 0.05em;
    }

    td {
        padding: 0.8vw;
        font-size: 1em;
        color: #333;
        border-bottom: 1px solid #e0e0e0;
    }

    tr:nth-child(even) {
        background-color: #f7f9fb;
    }

    tr:hover {
        background-color: #eef3f7;
    }

    .submit_btn {
        background-color: #f39c12;
        color: #ffffff;
        border: none;
        padding: 1vw;
        border-radius: 6px;
        font-size: 1em;
        cursor: pointer;
        transition: background-color 0.2s ease;
    }

    .submit_btn:hover {
        background-color: #d68910;
    }

    .submit_btn:active {
        transform: scale(0.97);
    }
</style>

<div class="form_down">
    <h1>UNASSIGNED LEADS</h1>
    <table>
        <tr>
            <th>JOBCARD NO</th>
            <th>CONTACT NUMBER</th>
            <th>AREA</th>
            <th>TYPE</th>
            <th>CREATED BY</th>
            <th>STATUS</th>
            <th></th>
        </tr>
        <?php
        echo $table_data;
        ?>
    </table>
    <br><br>
</div>