<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Admin : Add Contact</title>
    <?php include("headerlink.php")?>
</head>

<body class="hold-transition sidebar-mini layout-fixed">
    <div class="wrapper">

        <div class="preloader flex-column justify-content-center align-items-center">
            <img class="animation__shake" src="dist/img/AdminLTELogo.png" alt="AdminLTELogo" height="60" width="60">
        </div>

        <?php include("navbar.php")?>


        <?php include("sidebar.php")?>

        <div class="content-wrapper">

            <div class="content-header">
                <div class="container-fluid">
                    <div class="row mb-2">
                        <div class="col-sm-6">
                        
                        </div>
                        <div class="col-sm-6">
                            <ol class="breadcrumb float-sm-right">
                               
                            </ol>
                        </div>
                    </div>
                </div>
            </div>

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

                            <div class="card shadow">
                                <div class="card-header" style="background: #343a40;">
                                    <h2 class="card-title text-white text-bold text-uppercase">All
                                        Show Data
                                    </h2>
                                </div>
                                <div class="card-body">



                                    <table id="example1" class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th>Date Time</th>
                                                <th>Website URL</th>
                                                <th>Title</th>
                                                <th>IP Address</th>
                                                <th>Status</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['website_url'];  ?></td>

                                                <td><?php echo $row['title'];  ?></td>
                                                <td><?php echo $row['ip_address']?></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 bg-dark">
                                                            <h5 class="modal-title text-uppercase"
                                                                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>

        </div>
        <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.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() {
    $(".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);
            }
        });
    });
});
</script>
        <?php include("footer.php")?>

    </div>


    <?php include("footerlink.php")?>
</body>

</html>