<?php
include "includes/header.php";
$table_name = "playlist";
$res = $db->select($table_name, "*", "", "");

if (isset($_POST["submit"])) {
    unset($_POST["submit"]);
    $db->insert($table_name, $_POST);
    $db->close();
    echo "<script>window.location.href='" . basename($_SERVER["SCRIPT_NAME"]) . "?status=1'</script>";
}

$resU = @$db->select($table_name, "*", "id = :id", "", [":id" => $_GET["update"]]);

if (isset($_POST["submitU"])) {
    unset($_POST["submitU"]);
    $updateData = $_POST;
    $db->update($table_name, $updateData, "id = :id", [":id" => $_GET["update"]]);
    echo "<script>window.location.href='" . basename($_SERVER["SCRIPT_NAME"]) . "?status=1'</script>";
}

if (isset($_GET["delete"])) {
    $db->delete($table_name, "id = :id", [":id" => $_GET["delete"]]);
    echo "<script>window.location.href='" . basename($_SERVER["SCRIPT_NAME"]) . "?status=2'</script>";
}

echo '<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h2>Confirm</h2>
            </div>
            <div class="modal-body">
                Do you really want to delete?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <a id="confirm-delete-btn" class="btn btn-danger btn-ok">Delete</a>
            </div>
        </div>
    </div>
</div>';

echo '<script>
    $(document).ready(function() {
        var deleteUrl;
        $(".btn-danger").on("click", function() {
            deleteUrl = $(this).attr("href");
            $("#confirm-delete").modal("show");
        });

        $("#confirm-delete-btn").on("click", function() {
            window.location.href = deleteUrl;
        });
    });
</script>';

if (isset($_GET["create"])) {
    echo '<div class="col-12 col-md-8 mx-auto">
            <div class="card-body">
                <div class="card bg-primary text-white">
                    <div class="card-header card-header-warning text-center">
                        <h2><i class="icon icon-bullhorn"></i> Add User</h2>
                    </div>
                    <div class="card-body">
                        <form method="post">
                            <div class="form-group">
                                <label class="form-label" for="dns_id">DNS</label>
                                <select class="form-control" name="dns_id">
                                    <option selected="selected">Choose one</option>';
                                    $dnss = $db->select("dns", "*", "", "");
                                    foreach ($dnss as $dns) {
                                        echo '<option value="' . $dns["id"] . '">' . $dns["title"] . '</option>';
                                    }
    echo '                    </select>
                            </div>
                            <div class="form-group">
                                <label class="form-label" for="mac">MAC Address (will auto format)</label>
                                <input class="form-control" id="mac" name="mac_address" placeholder="MAC Address" type="text"/>
                            </div>
                            <script>
                                document.getElementById("mac").addEventListener("keyup", function() {
    // Remove todos os caracteres que não sejam números ou letras de A a F
    let mac = this.value.toUpperCase().replace(/[^A-F0-9]/g, "");
    
    // Formata o MAC em pares de dois, adicionando ":" após cada par, mas limitando a 17 caracteres
    if (mac.length > 12) {
        mac = mac.substring(0, 12); // Limita a string a 12 caracteres antes da formatação
    }
    
    // Formata o MAC com o ":" após cada par de caracteres
    this.value = mac.match(/.{1,2}/g)?.join(":") || "";
});
                            </script>
                            <div class="form-group">
                                <label class="form-label" for="username">Username</label>
                                <input class="form-control" id="username" name="username" placeholder="Username" type="text"/>
                            </div>
                            <div class="form-group">
                                <label class="form-label" for="password">Password</label>
                                <input class="form-control" id="password" name="password" placeholder="Password" type="text"/>
                            </div>
                            <div class="form-group">
                                <label class="form-label" for="pin">Parental Pin</label>
                                <input class="form-control" id="pin" name="pin" placeholder="Parental Pin" type="text" value="0000"/>
                            </div>
                            <div class="form-group">
                                <label class="form-label" for="link">Link</label>
                                <input class="form-control" id="link" name="link" placeholder="Link" type="text"/>
                            </div>
                            <div class="form-group text-center">
                                <button class="btn btn-info" name="submit" type="submit">
                                    <i class="icon icon-check"></i> Submit
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>';
} elseif (isset($_GET["update"])) {
    echo '<div class="col-12 col-md-8 mx-auto">
            <div class="card-body">
                <div class="card bg-primary text-white">
                    <div class="card-header card-header-warning text-center">
                        <h2><i class="icon icon-bullhorn"></i> Edit Current User</h2>
                    </div>
                    <div class="card-body">
                        <form method="post">
                            <div class="form-group">
                                <label class="form-label" for="id">DNS ID</label>
                                <input class="form-control" id="id" name="id" value="' . $resU[0]["id"] . '" readonly type="text"/>
                            </div>
                            <div class="form-group">
                                <label class="form-label" for="mac_address">MAC Address</label>
                                <input class="form-control" id="mac_address" name="mac_address" value="' . $resU[0]["mac_address"] . '" type="text"/>
                            </div>
                            <div class="form-group">
                                <label class="form-label" for="username">Username</label>
                                <input class="form-control" id="username" name="username" value="' . $resU[0]["username"] . '" type="text"/>
                            </div>
                            <div class="form-group">
                                <label class="form-label" for="password">Password</label>
                                <input class="form-control" id="password" name="password" value="' . $resU[0]["password"] . '" type="text"/>
                            </div>
                            <div class="form-group">
                                <label class="form-label" for="pin">Parental Pin</label>
                                <input class="form-control" id="pin" name="pin" value="' . $resU[0]["pin"] . '" type="text"/>
                            </div>
                            <div class="form-group">
                                <label class="form-label" for="link">Link</label>
                                <input class="form-control" id="link" name="link" value="' . $resU[0]["link"] . '" type="text"/>
                            </div>
                            <div class="form-group text-center">
                                <button class="btn btn-info" name="submitU" type="submit">
                                    <i class="icon icon-check"></i> Submit
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>';
} else {
    echo '<div class="col-12 mx-auto">
            <div class="card-body">
                <div class="card bg-primary text-white">
                    <div class="card-header card-header-warning text-center">
                        <h2><i class="icon icon-bullhorn"></i> User Management</h2>
                    </div>
                    <div class="card-body text-center">
                        <a href="' . basename($_SERVER["SCRIPT_NAME"]) . '?create=1" class="btn btn-info btn-sm" type="button">Add New</a>
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>DNS</th>
                                        <th>MAC Address</th>
                                        <th>Username</th>
                                        <th>Password</th>
                                        <th>Parental Pin</th>
                                        <th>Link</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>';
                                    foreach ($res as $row) {
                                        echo '<tr>
                                                <td>' . $row["dns_id"] . '</td>
                                                <td>' . $row["mac_address"] . '</td>
                                                <td>' . $row["username"] . '</td>
                                                <td>' . $row["password"] . '</td>
                                                <td>' . $row["pin"] . '</td>
                                                <td>' . $row["link"] . '</td>
                                                <td>
                                                    <a href="' . basename($_SERVER["SCRIPT_NAME"]) . '?update=' . $row["id"] . '" class="btn btn-warning btn-sm">Edit</a>
                                                    <a href="' . basename($_SERVER["SCRIPT_NAME"]) . '?delete=' . $row["id"] . '" class="btn btn-danger btn-sm">Delete</a>
                                                </td>
                                            </tr>';
                                    }
    echo '                    </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>';
}

include "includes/footer.php";
?>
