<?php
/*
 * @ https://hospedagem.matrixch.store 
 * @ PHP 7.4
 * @ Decoder version: 1.0.2
 * @ Release: 17/06/2024
*/

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\">\n    <div class=\"modal-dialog\">\n        <div class=\"modal-content\">\n            <div class=\"modal-header\">\n                <h2>Confirm</h2>\n            </div>\n            <div class=\"modal-body\">\n                Do you really want to delete?\n            </div>\n            <div class=\"modal-footer\">\n                <button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Cancel</button>\n                <a class=\"btn btn-danger btn-ok\">Delete</a>\n            </div>\n        </div>\n    </div>\n</div>\n";
if (isset($_GET["create"])) {
    echo "        <div class=\"col-md-8 mx-auto\">\n            <div class=\"card-body\">\n                <div class=\"card bg-primary text-white\">\n                    <div class=\"card-header card-header-warning\">\n                        <center>\n                            <h2><i class=\"icon icon-bullhorn\"></i> Add User</h2>\n                        </center>\n                    </div>\n                    \n                    <div class=\"card-body\">\n                        <div class=\"col-12\">\n                            <h3>Add User</h3>\n                        </div>\n                            <form method=\"post\">\n                                <div class=\"form-group\">\n                                    <label class=\"form-label \" for=\"title\">DNS</label>\n\t\t\t\t\t\t\t\t\t<select class=\"form-control\" name=\"dns_id\">\n                                       <option selected=\"selected\">Choose one</option>\n                                       ";
    $dnss = $db->select("dns", "*", "", "");
    foreach ($dnss as $dns) {
        echo "                                           <option value=\"";
        echo $dns["id"];
        echo "\">";
        echo $dns["title"];
        echo "</option>\n                                       ";
    }
    echo "                                     </select> \n                                </div>\n                                <div class=\"form-group\">\n                                    <label class=\"form-label \" for=\"dns\">MAC Address (will auto format)</label>\n                                        <input class=\"form-control\" id=\"mac\" name=\"mac_address\" placeholder=\"MAC Address\" type=\"text\"/>\n                                </div>\n                                <script>\n                                    document.getElementById(\"mac\").addEventListener('keyup', function() { \n                                      this.value = \n                                        (this.value.toUpperCase()\n                                        .replace(/[^\\d|A-Z]/g, '')\n                                        .match(/.{1,2}/g) || [])\n                                        .join(\":\")\n                                    });\n                                </script>\n                                <div class=\"form-group\">\n                                    <label class=\"form-label \" for=\"title\">Username</label>\n                                        <input class=\"form-control\" id=\"description\" name=\"username\" placeholder=\"Username\" type=\"text\"/>\n                                </div>\n                                <div class=\"form-group\">\n                                    <label class=\"form-label \" for=\"dns\">Password</label>\n                                        <input class=\"form-control\" id=\"description\" name=\"password\" placeholder=\"Password\" type=\"text\"/>\n                                </div>\n                                <div class=\"form-group\">\n                                    <label class=\"form-label \" for=\"dns\">Parental Pin</label>\n                                        <input class=\"form-control\" id=\"description\" name=\"pin\" placeholder=\"Parental Pin\" type=\"text\" value=\"0000\"/>\n                                </div>\n                                <div class=\"form-group\">\n                                    <label class=\"form-label \" for=\"link\">Link</label>\n                                        <input class=\"form-control\" id=\"link\" name=\"link\" placeholder=\"Link\" type=\"text\"/>\n                                </div>\n                                <div class=\"form-group\">\n                                    <center>\n                                        <button class=\"btn btn-info \" name=\"submit\" type=\"submit\">\n                                            <i class=\"icon icon-check\"></i> Submit\n                                        </button>\n                                    </center>\n                                </div>\n                            </form>\n                    </div>\n                </div>\n            </div>\n        </div>\n";
} else {
    if (isset($_GET["update"])) {
        echo "        <div class=\"col-md-8 mx-auto\">\n            <div class=\"card-body\">\n                <div class=\"card bg-primary text-white\">\n                    <div class=\"card-header card-header-warning\">\n                        <center>\n                            <h2><i class=\"icon icon-bullhorn\"></i> Edit Current User</h2>\n                        </center>\n                    </div>\n                    \n                    <div class=\"card-body\">\n                        <div class=\"col-12\">\n                            <h3>Edit User</h3>\n                        </div>\n                            <form method=\"post\">\n                                <div class=\"form-group\">\n                                    <label class=\"form-label \" for=\"title\">DNS ID</label>\n                                        <input class=\"form-control\" id=\"description\" name=\"id\" value=\"";
        echo $resU[0]["id"];
        echo "\" readonly type=\"text\" />\n                                </div>\n                                <div class=\"form-group\">\n                                    <label class=\"form-label \" for=\"dns\">MAC Address</label>\n                                        <input class=\"form-control\" id=\"description\" name=\"mac_address\" value=\"";
        echo $resU[0]["mac_address"];
        echo "\" type=\"text\"/>\n                                </div>\n                                <div class=\"form-group\">\n                                    <label class=\"form-label \" for=\"title\">Username</label>\n                                        <input class=\"form-control\" id=\"description\" name=\"username\" placeholder=\"Username\" value=\"";
        echo $resU[0]["username"];
        echo "\" type=\"text\"/>\n                                </div>\n                                <div class=\"form-group\">\n                                    <label class=\"form-label \" for=\"dns\">Password</label>\n                                        <input class=\"form-control\" id=\"description\" name=\"password\" placeholder=\"Password\" value=\"";
        echo $resU[0]["password"];
        echo "\" type=\"text\"/>\n                                </div>\n                                <div class=\"form-group\">\n                                    <label class=\"form-label \" for=\"dns\">Parental Pin</label>\n                                        <input class=\"form-control\" id=\"description\" name=\"pin\" placeholder=\"Parental Pin\" value=\"";
        echo $resU[0]["pin"];
        echo "\" type=\"text\"/>\n                                </div>\n                                <div class=\"form-group\">\n                                    <label class=\"form-label \" for=\"link\">Link</label>\n                                        <input class=\"form-control\" id=\"link\" name=\"link\" placeholder=\"Link\" value=\"";
        echo $resU[0]["link"];
        echo "\" type=\"text\"/>\n                                </div>\n                                <div class=\"form-group\">\n                                    <center>\n                                        <button class=\"btn btn-info \" name=\"submitU\" type=\"submit\">\n                                            <i class=\"icon icon-check\"></i> Submit\n                                        </button>\n                                    </center>\n                                </div>\n                            </form>\n                    </div>\n                </div>\n            </div>\n        </div>\n";
    } else {
        echo "        <div class=\"col-md-12 mx-auto\">\n            <div class=\"card-body\">\n                <div class=\"card bg-primary text-white\">\n                    <div class=\"card-header card-header-warning\">\n                        <center>\n                            <h2><i class=\"icon icon-bullhorn\"></i> User Management</h2>\n                        </center>\n                    </div>\n                    <div class=\"card-body\">\n                        <center>\n                            <a href=\"";
        echo basename($_SERVER["SCRIPT_NAME"]);
        echo "?create=1\" class=\"btn btn-info btn-sm\" type=\"button\">Add New</a>\n                        </center>\n                        <table class=\"table table-striped\">\n                            <thead>\n                                <tr>\n                                    <th>DNS</th>\n                                    <th>MAC Address</th>\n                                    <th>Username</th>\n                                    <th>Password</th>\n                                    <th>Parental Pin</th>\n                                    <th>Link</th>\n                                    <th>Action</th>\n                                </tr>\n                            </thead>\n                            <tbody>\n                                ";
        foreach ($res as $row) {
            echo "                                <tr>\n                                    <td>";
            echo $row["dns_id"];
            echo "</td>\n                                    <td>";
            echo $row["mac_address"];
            echo "</td>\n                                    <td>";
            echo $row["username"];
            echo "</td>\n                                    <td>";
            echo $row["password"];
            echo "</td>\n                                    <td>";
            echo $row["pin"];
            echo "</td>\n                                    <td>";
            echo $row["link"];
            echo "</td>\n                                    <td>\n                                        <a href=\"";
            echo basename($_SERVER["SCRIPT_NAME"]);
            echo "?update=";
            echo $row["id"];
            echo "\" class=\"btn btn-warning btn-sm\">Edit</a>\n                                        <a href=\"";
            echo basename($_SERVER["SCRIPT_NAME"]);
            echo "?delete=";
            echo $row["id"];
            echo "\" class=\"btn btn-danger btn-sm\" data-toggle=\"modal\" data-target=\"#confirm-delete\">Delete</a>\n                                    </td>\n                                </tr>\n                                ";
        }
        echo "                            </tbody>\n                        </table>\n                    </div>\n                </div>\n            </div>\n        </div>\n";
    }
}
include "includes/footer.php";
