<?php
require("header.php");
?>

<div class="content-wrapper">
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <!-- <h1>Panel Setting</h1> -->
                </div>
                <div class="col-sm-6">
                </div>
            </div>
        </div>
    </section>

    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-3">
                    <div class="card card-secondary">
                        <div class="card-header"  style="background-color: black;">
                            <h3 class="card-title">Enter Website URLs</h3>
                        </div>
                        <div class="card-body">
                            <form action="check_web_status.php" method="post">
                                <div class="form-group">
                                    <label for="websites">Enter Website URLs (one per line):</label>
                                    <textarea class="form-control" name="websites" rows="12" cols="50"></textarea>
                                </div>
                                <button type="submit" class="btn btn-dark"  style="background-color: black;">Check Status</button>
                            </form>

                        </div>
                    </div>
                </div>
                <div class="col-md-9">
                    <div class="card card-secondary">
                        <div class="card-header"  style="background-color: black;">
                            <h3 class="card-title">Status Results</h3>
                        </div>
                        <div class="card-body">
                            <?php if (isset($_GET['error'])) { ?>
                                <div class="alert alert-denger  col-12">
                                    <p class="error"><?php echo $_GET['error']; ?></p>
                                </div>



                            <?php } ?>
                            <?php if (isset($_GET['success'])) { ?>
                                <div class="alert alert-success  col-12">
                                    <p class="success"><?php echo $_GET['success']; ?></p>
                                </div>


                            <?php } ?>
                            <?php
                            if (isset($_POST['websites'])) {
                                $websites = explode("\n", $_POST['websites']);

                                echo '<div class="mt-4">'; // Add some margin-top
                                echo "<h2>Status and IP Address Results:</h2>";

                                echo '<div class="table-responsive">'; // Wrap the table in a responsive container
                                echo "<table class='table'>";
                                echo "<tr><th>Website URL</th><th>Title</th><th>Status</th><th>IP Address</th><th>Action</th></tr>";

                                foreach ($websites as $website) {
                                    $website = trim($website);

                                    if (!empty($website)) {
                                        $website = rtrim($website, '/'); // Remove trailing slashes

                                        $variations = array(
                                            'http://' . $website,
                                            'http://www.' . $website,
                                            'https://' . $website,
                                            'https://www.' . $website,
                                        );

                                        foreach ($variations as $variation) {
                                            $ch = curl_init($variation);
                                            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                                            $response = curl_exec($ch);

                                            $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

                                            preg_match("/<title>(.*?)<\/title>/i", $response, $titleMatches);
                                            $title = isset($titleMatches[1]) ? $titleMatches[1] : "N/A";

                                            $ipAddress = gethostbyname(parse_url($variation, PHP_URL_HOST));

                                            echo "<tr>";
                                            echo "<td>$variation</td>";
                                            echo "<td>$title</td>";
                                            echo "<td>";
                                            if ($status == 200) {
                                                echo "<span class='text-success'>Online</span>"; // Apply success color
                                            } else {
                                                echo "<span class='text-danger'>Offline</span>"; // Apply danger color
                                            }
                                            echo "</td>";
                                            echo "<td>$ipAddress</td>"; // Display IP address
                                            echo "<td>";
                                            echo "<button class='btn btn-dark btn-sm' onclick='sendData(\"$variation\", \"$title\", \"$status\", \"$ipAddress\", this.parentNode.parentNode)'>Send to Database</button>";

                                            echo "</td>";
                                            echo "</tr>";

                                            curl_close($ch);
                                        }
                                    }
                                }

                                echo "</table>";
                                echo '</div>'; // Close the table-responsive container
                                echo '</div>'; // Close the spacing div
                            }
                            ?>

                            <script>
                                function sendData(url) {
                                    // Implement your code to send data to the database here
                                    // You can use JavaScript or AJAX to perform the database operation

                                    alert("Sending data to the database: " + url);
                                }
                            </script>
                            <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.7.27/dist/sweetalert2.all.min.js"></script>
                            <script>
                                function sendData(url, title, status, ipAddress, row) {
                                    var data = {
                                        url: url,
                                        title: title,
                                        status: status,
                                        ipAddress: ipAddress
                                    };

                                    var xhr = new XMLHttpRequest();
                                    xhr.open("POST", "website_process.php", true);
                                    xhr.setRequestHeader("Content-Type", "application/json");
                                    xhr.onreadystatechange = function() {
                                        if (xhr.readyState === XMLHttpRequest.DONE) {
                                            if (xhr.status === 200) {
                                                var response = xhr.responseText;
                                                if (response.startsWith("Error")) {
                                                    // Use Swal to show an error message
                                                    Swal.fire({
                                                        icon: 'error',
                                                        title: 'Error',
                                                        text: response
                                                    });
                                                } else {
                                                    var insertedId = response;
                                                    // Use Swal to show a success message
                                                    Swal.fire({
                                                        icon: 'success',
                                                        title: 'Success',
                                                        text: "Data inserted successfully with ID: " + insertedId
                                                    });

                                                    row.style.display = "none"; // Hide the row (display: none)

                                                    // Mark the row as removed using a custom data attribute
                                                    row.setAttribute("data-removed", "true");
                                                }
                                            } else {
                                                // Use Swal to show an error message
                                                Swal.fire({
                                                    icon: 'error',
                                                    title: 'Error',
                                                    text: "Error: " + xhr.statusText
                                                });
                                            }
                                        }

                                    };
                                    xhr.send(JSON.stringify(data));
                                }
                            </script>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

<?php
require("footer.php");
?>