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

include "includes/header.php";
$table_name = "dns";
$res = $db->select($table_name, "*", "id IN (SELECT MIN(id) FROM $table_name WHERE title != 'new' GROUP BY url)", "");

$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($_POST["submit"])) {
    unset($_POST["submit"]);
    $db->insert($table_name, $_POST);
    $db->close();
    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 bg-danger text-white\">
                <h2>⚠️ Confirm Deletion</h2>
            </div>
            <div class=\"modal-body\">
                <p class=\"lead\">🗑️ Do you really want to delete this DNS record?</p>
            </div>
            <div class=\"modal-footer\">
                <button type=\"button\" class=\"btn btn-secondary\" data-dismiss=\"modal\">❌ Cancel</button>
                <a class=\"btn btn-danger btn-ok\">✅ Delete</a>
            </div>
        </div>
    </div>
</div>";

if (isset($_GET["create"])) {
    echo "<div class=\"col-md-8 mx-auto\">
            <div class=\"card-body\">
                <div class=\"card bg-primary text-white\">
                    <div class=\"card-header card-header-warning\">
                        <center>
                            <h2><i class=\"fa fa-globe\"></i>Servidores  DNS</h2>
                        </center>
                    </div>
                    
                    <div class=\"card-body\">
                        <div class=\"col-12\">
                            <h3>➕ Create New DNS</h3>
                        </div>
                            <form method=\"post\">
                                <div class=\"form-group\">
                                    <label class=\"form-label\" for=\"title\">📝 Title</label>
                                    <input class=\"form-control\" id=\"description\" name=\"title\" placeholder=\"Enter title\" type=\"text\"/>
                                </div>
                                <div class=\"form-group\">
                                    <label class=\"form-label\" for=\"dns\">🔗 DNS Address</label>
                                    <input class=\"form-control\" id=\"description\" name=\"url\" placeholder=\"Enter DNS (e.g., 8.8.8.8)\" type=\"text\"/>
                                </div>
                                <div class=\"form-group\">
                                    <center>
                                        <button class=\"btn btn-success\" name=\"submit\" type=\"submit\">
                                            <i class=\"fa fa-save\"></i> 💾 Save DNS
                                        </button>
                                    </center>
                                </div>
                            </form>
                    </div>
                </div>
            </div>
        </div>";
} else {
    if (isset($_GET["update"])) {
        echo "<div class=\"col-md-8 mx-auto\">
            <div class=\"card-body\">
                <div class=\"card bg-primary text-white\">
                    <div class=\"card-header card-header-warning\">
                        <center>
                            <h2><i class=\"fa fa-globe\"></i>Servidores  DNS</h2>
                        </center>
                    </div>
                    
                    <div class=\"card-body\">
                        <div class=\"col-12\">
                            <h3>✏️ Edit DNS Record</h3>
                        </div>
                            <form method=\"post\">
                                <div class=\"form-group\">
                                    <label class=\"form-label\" for=\"title\">📝 Title</label>
                                    <input class=\"form-control\" id=\"description\" name=\"title\" placeholder=\"Title\" value=\"" . $resU[0]["title"] . "\" type=\"text\"/>
                                </div>
                                <div class=\"form-group\">
                                    <label class=\"form-label\" for=\"dns\">🔗 DNS Address</label>
                                    <input class=\"form-control\" id=\"description\" name=\"url\" placeholder=\"DNS\" value=\"" . $resU[0]["url"] . "\" type=\"text\"/>
                                </div>
                                <div class=\"form-group\">
                                    <center>
                                        <button class=\"btn btn-warning\" name=\"submitU\" type=\"submit\">
                                            <i class=\"fa fa-refresh\"></i> 🔄 Update
                                        </button>
                                    </center>
                                </div>
                            </form>
                    </div>
                </div>
            </div>
        </div>";
    } else {
        echo "<div class=\"col-md-12 mx-auto\">
            <div class=\"card-body\">
                <div class=\"card bg-primary text-white\">
                    <div class=\"card-header card-header-warning\">
                        <center>
                            <h2><i class=\"fa fa-globe\"></i> Servidores DNS</h2>
                        </center>
                    </div>

                    <div class=\"card-body\">
                        <div class=\"col-12\">
                            <center>
                                <a id=\"button\" href=\"./" . basename($_SERVER["SCRIPT_NAME"]) . "?create\" class=\"btn btn-success\">
                                    <i class=\"fa fa-plus\"></i> 🆕 New DNS
                                </a>
                            </center>
                        </div>
                        <br>
                        <div class=\"table-responsive\">
                            <table class=\"table table-striped table-hover table-sm\">
                            <thead class=\"thead-dark\">
                                <tr>
                                    <th>#️⃣ ID</th>
                                    <th>📛 Title</th>
                                    <th>🔗 DNS Address</th>
                                    <th>⚙️ Actions</th>
                                </tr>
                            </thead>";
        foreach ($res as $row) {
            echo "<tbody>
                                <tr>
                                    <td>" . $row["id"] . "</td>
                                    <td>" . $row["title"] . "</td>
                                    <td><code>" . $row["url"] . "</code></td>
                                    <td>
                                        <a class=\"btn btn-info btn-sm\" href=\"./" . basename($_SERVER["SCRIPT_NAME"]) . "?update=" . $row["id"] . "\">
                                            <i class=\"fa fa-edit\"></i> ✏️
                                        </a>
                                        &nbsp;
                                        <a class=\"btn btn-danger btn-sm\" href=\"#\" data-href=\"./" . basename($_SERVER["SCRIPT_NAME"]) . "?delete=" . $row["id"] . "\" data-toggle=\"modal\" data-target=\"#confirm-delete\">
                                            <i class=\"fa fa-trash\"></i> 🗑️
                                        </a>
                                    </td>
                                </tr>
                            </tbody>";
        }
        echo "</table>
                        </div>
                    </div>
                </div>
            </div>
        </div>";
    }
}

include "includes/footer.php";
echo "</body>
</html>";