<?php
require("header.php")
?>
<style>
    .btn-group>.btn:not(:last-child):not(.dropdown-toggle) {
        background: black;
    }

    .btn-group>.btn-group:not(:first-child)>.btn {
        background: black;
    }

    .page-item.active .page-link {
        background: black;
    }

    .dt-button-collection .dropdown-menu .buttons-columnVisibility {
        background-color: black;
    }
</style>
<?php
if (isset($_GET['url'])) {
    $url = $_GET['url'];
    $response = getWebsiteStatus($url);
    echo $response;
}

function getWebsiteStatus($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);

    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $httpCode === 200 ? "online" : "offline";
}
?>

<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <!-- <h1>Add Salt</h1> -->
                </div>
                <div class="col-sm-6">

                </div>
            </div>
        </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <!-- /.card -->

                    <div class="card card-secondary">
                        <div class="card-header" style="background-color: black;">
                            <h3 class="card-title"> Add Website</h3>
                            <ol class="breadcrumb float-sm-right" style="background-color: black;">
                                <li class="breadcrumb-item"><a href="#">Home</a></li>
                                <li class="breadcrumb-item ">Inventory</li>
                                <li class="breadcrumb-item active">Add Website</li>
                            </ol>
                        </div>
                        <!-- /.card-header -->
                        <div class="card-body">

                            <table id="example1" class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>Date Time</th>
                                        <th>Added BY</th>
                                        <th>Website URL</th>
                                        <th>Title</th>
                                        <th>IP Address</th>
                                        <th>Status</th>
                                        <th>Check</th>
                                        <th>Action</th>

                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    include('connection.php');
                                    $sql = "SELECT * FROM add_website";
                                    $res = mysqli_query($conn, $sql);
                                    while ($row = mysqli_fetch_assoc($res)) {

                                    ?>

                                        <tr data-record-id="<?php echo $row['id']; ?>">
                                            <td><?php echo $row['date_time'] ?></td>
                                            <td><?php echo $row['added_by'] ?></td>
                                            <td><?php echo $row['website_url'];  ?></td>
                                            <td><?php echo $row['title'];  ?></td>
                                            <td><?php echo $row['ip_address'] ?></td>
                                            <!-- Add a class and data-website-url attribute to the status cell -->
                                            <td class="status-cell" style="color:purple; font-weight:700;" data-website-url="<?php echo $row['website_url']; ?>"></td>
                                            <td>
                                                <button type="button" class="btn btn-success recheck-btn" data-toggle="modal" data-target="#exampleModal<?php echo $row['id']; ?>" data-website="<?php echo $row['website_url']; ?>">
                                                    Recheck
                                                </button>
                                            </td>
                                            <td>
                                                <button type="button" class="btn btn-danger delete-btn" data-record-id="<?php echo $row['id']; ?>">
                                                    Delete
                                                </button>
                                            </td>
                                        </tr>
                                        <!-- Modal -->

                                        <!-- Modal -->
                                        <div class="modal fade" id="exampleModal<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                            <div class="modal-dialog" role="document">
                                                <div class="modal-content">
                                                    <div class="modal-header" style="background-color: black;">
                                                        <h5 class="modal-title" id="exampleModalLabel">verify url</h5>
                                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                            <span aria-hidden="true">&times;</span>
                                                        </button>
                                                    </div>
                                                    <div class="modal-body">
                                                        <div class="row">
                                                            <div class="form-group col-sm-12">
                                                                <label for="name">Website URL:</label>
                                                                <input type="text" class="form-control" value="<?php echo $row['website_url']; ?>" readonly>
                                                            </div>
                                                        </div>
                                                        <div class="row">
                                                            <div class="form-group col-sm-12">
                                                                <label for="status">Website Status:</label>
                                                                <input type="text" class="form-control website-status" readonly>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>



                                        <?php
                                    }
                                        ?>

                                </tbody>

                            </table>

                        </div>
                    </div>
                    <!-- /.card-body -->

                </div>

                <!-- /.card -->
            </div>

            <!-- /.col -->
        </div>
        <!-- /.row -->

        <!-- /.container-fluid -->
    </section>
    <!-- /.content -->
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10.15.5/dist/sweetalert2.all.min.js"></script>
<script>
    $(document).ready(function() {
        // Handle delete button click
        $('.delete-btn').click(function() {
            var recordId = $(this).data('record-id');

            // Show a confirmation dialog before deletion
            Swal.fire({
                title: 'Are you sure?',
                text: 'You will not be able to recover this record!',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonText: 'Yes, delete it!',
                cancelButtonText: 'Cancel'
            }).then((result) => {
                if (result.isConfirmed) {
                    // User confirmed deletion, send an AJAX request to delete the record
                    $.ajax({
                        type: 'POST',
                        url: 'delete_website.php', // Create a PHP script to handle record deletion
                        data: {
                            record_id: recordId
                        },
                        success: function(response) {
                            if (response === 'success') {
                                // Deletion was successful, remove the table row
                                $('tr[data-record-id="' + recordId + '"]').remove();
                                Swal.fire('Deleted!', 'The record has been deleted.',
                                    'success');
                            } else {
                                Swal.fire('Error!', 'Failed to delete the record.',
                                    'error');
                            }
                        }
                    });
                }
            });
        });
    });
</script>


<script>
    $(document).ready(function() {
        // Function to update the status cell for a given website URL
        function updateStatusCell(websiteUrl, status) {
            $(".status-cell[data-website-url='" + websiteUrl + "']").text(status);
        }

        // Handle Recheck button click
        $(".recheck-btn").click(function() {
            var websiteUrl = $(this).data("website");
            var modalId = $(this).data("target");
            var statusInput = $(modalId + " .modal-body .website-status");

            $.ajax({
                type: "POST",
                url: "check_website_status.php",
                data: {
                    websiteUrl: websiteUrl
                },
                success: function(response) {
                    statusInput.val(response);
                    updateStatusCell(websiteUrl, response); // Update the status cell
                }
            });
        });

        // Automatically update status cells on page load
        $(".status-cell").each(function() {
            var websiteUrl = $(this).data("website-url");

            $.ajax({
                type: "POST",
                url: "check_website_status.php",
                data: {
                    websiteUrl: websiteUrl
                },
                success: function(response) {
                    updateStatusCell(websiteUrl, response);
                }
            });
        });
    });
</script>

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