<?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>
<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 Notice for user!!</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 Notice For User</h3>
                            <ol class="breadcrumb float-sm-right" style="background-color: black;">
                                <li class="breadcrumb-item"><a href="#">Home</a></li>
                                <li class="breadcrumb-item">Notice Update</li>
                                <li class="breadcrumb-item active">Add Notice</li>
                            </ol>
                        </div>
                        <!-- /.card-header -->
                        <div class="card-body">
                            <form action="user_notice_code.php" method="post">
                                <div class="row">
                                    <div class="col-sm-12">
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Notice<span style="color:red; font-size:25px;">*<span></label>
                                            <textarea class="form-control" rows="1" name="user_notice" placeholder="Enter ..." required></textarea>

                                        </div>
                                    </div>
                                    <div class="col-sm-2">

                                        <button type="submit" style="margin-top: 45px;width:95%; margin-left:5%; background:black;" name="add" class="btn btn-dark">Create</button>

                                    </div>
                                </div>

                            </form>

                            <br>
                            <table id="example1" class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>Sr.no</th>
                                        <th>Date</th>

                                        <th>Notice</th>

                                        <th>Status</th>
                                        <th>Action</th>

                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    include('connection.php');
                                    $i = 0;
                                    $sql = "SELECT * FROM user_notice ORDER BY date_time";
                                    $res = mysqli_query($conn, $sql);
                                    while ($row = mysqli_fetch_assoc($res)) {
                                        $i++;

                                    ?>

                                        <tr>
                                            <td><?php echo $i; ?></td>
                                            <td><?php echo $row['date_time'];  ?></td>

                                            <td><?php echo $row['user_notice'];  ?></td>
                                            <td>

                                                <form method="POST" action="user_notice_code.php">

                                                    <input type="hidden" name="item_id" value="<?php echo $row['id']; ?>">
                                                    <?php if ($row['status'] == 1) { ?>
                                                        <input type="submit" name="block" value="Block" style="font-weight: 600;font-size: 12px;height: 30px;" class="btn btn-danger">
                                                    <?php } else { ?>
                                                        <input type="submit" name="activate" value="Activate" style="font-weight: 600;font-size: 12px;height: 30px;" class="btn btn-success">
                                                    <?php } ?>
                                                    <!-- <?php echo ($row['status'] == 1) ? "<div class='test rounded-circle'></div>" : "<div class='test2 rounded-circle'></div>"; ?> -->
                                                </form>

                                            </td>
                                            <td>
                                                <button type="button" style="font-weight: 600;font-size: 12px;height: 30px; background:black;" class="btn btn-danger delete-btn" data-record-id="<?php echo $row['id']; ?>">
                                                    Delete
                                                </button>
                                            </td>
                                            <!-- <td><button type="submit" id="approve_button" class="btn btn-success">Active</button></td> -->
                                        </tr>
                                    <?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@11"></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_user_notice.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').then(() => {
                                    window.location.href = "add_user_notice.php";
                                });
                            } else {
                                Swal.fire('Error!', 'Failed to delete the record.',
                                    'error');
                            }
                        }
                    });
                }
            });
        });
    });
</script>
<!-- Sweet Alert  start-->
<?php if (isset($_REQUEST['msg']) && $_REQUEST['msg'] == 1) { ?>
    <script>
        Swal.fire(
            'Good job!',
            'Notice Added Successfully!!',
            'success'
        ).then(() => {
            window.location.href = "add_user_notice.php";
        });
    </script>
<?php } ?>

<?php if (isset($_REQUEST['msg']) && $_REQUEST['msg'] == 2) { ?>
    <script>
        Swal.fire(
            'Oops...',
            'Something went wrong!!',
            'error'
        ).then(() => {
            window.location.href = "add_user_notice.php";
        });
    </script>
<?php } ?>

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