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

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Admin : View Task</title>
    <?php include("headerlink.php")?>
    <link rel="stylesheet" href="https://cdn.datatables.net/1.11.4/css/jquery.dataTables.min.css">

    <style>
    .member-container {
        display: flex;
        /* Use flexbox for layout */
        align-items: center;
        /* Vertically center content */
        margin-bottom: 10px;
        /* Add margin for spacing between containers */
    }

    .member-name {
        flex: 1;
        /* Allow member name to expand and fill available space */
    }

    .delete-button {
        margin-left: 10px;
        /* Add margin to the left of the delete button for spacing */
    }
    </style>


</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">
                            <h1 class="m-0 fw-bold text-uppercase">Group</h1>
                        </div>
                        <div class="col-sm-6">
                            <ol class="breadcrumb float-sm-right">
                                <li class="breadcrumb-item"><a href="#">Home</a></li>
                                <li class="breadcrumb-item active">Group</li>
                                <li class="breadcrumb-item active">View All</li>
                            </ol>
                        </div>
                    </div>
                </div>
            </div>

            <section class="content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="card shadow">
                                <div class="card-header" style="background: #343a40; ">
                                    <h3 class="card-title text-white text-bold text-uppercase">View All Group</h3>
                                </div>

                                <div class="card-body">
                                    <div id="example2_wrapper" class="dataTables_wrapper dt-bootstrap4">
                                        <div class="row">
                                            <div class="col-sm-12 col-md-6"></div>
                                            <div class="col-sm-12 col-md-6"></div>
                                        </div>
                                        <div class="row">
                                            <div class="col-sm-12">
                                                <table class="table-responsive">
                                                    <table id="example2"
                                                        class="table table-bordered text-center table-hover dataTable dtr-inline display"
                                                        aria-describedby="example2_info">
                                                        <thead>
                                                            <tr>
                                                                <th>Date &amp; time</th>
                                                                <th>Name Of Group</th>
                                                                <th>Action</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            <?php
                                                            include('connection.php');
                                                            $sql = "SELECT * FROM groups";
                                                            $query = mysqli_query($conn, $sql);
                                                            while ($row = mysqli_fetch_assoc($query)) {
                                                            ?>
                                                            <tr>
                                                                <td>1/2/23</td>
                                                                <td class="view-details-button fw-bold text-danger text-uppercase"
                                                                    style="text-decoration: none; " data-toggle="modal"
                                                                    data-target="#detailsModal<?php echo $row['id'];?> ">
                                                                    <?php echo $row['group_name']?>
                                                                </td>
                                                                <td>
                                                                    <a href="delete_group.php?id=<?php echo $row['id']; ?>"
                                                                        class="btn btn-danger btn-sm">
                                                                        <i class="fa fa-trash" aria-hidden="true"></i>
                                                                    </a>
                                                                    <button class="btn btn-success btn-sm add-member"
                                                                        data-group-id="<?php echo $row['id']; ?>"
                                                                        data-toggle="modal"
                                                                        data-target="#openModal<?php echo $row['id'];?> ">
                                                                        <i class="fa fa-plus-circle"
                                                                            aria-hidden="true"></i>
                                                                    </button>
                                                                </td>

                                                            </tr>
                                                            <div class="modal fade"
                                                                id="openModal<?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 fw-bold"
                                                                                id="exampleModalLabel ">Add New Member
                                                                            </h5>
                                                                            <button type="button" class="close"
                                                                                data-dismiss="modal" aria-label="Close">
                                                                                <span aria-hidden="true">&times;</span>
                                                                            </button>
                                                                        </div>
                                                                        <div class="modal-body">
                                                                            <form method="POST" action="add-member.php">
                                                                                <div class="form-group">
                                                                                    <label for="newMemberName">Member
                                                                                        Name</label>
                                                                                    <input type="text"
                                                                                        class="form-control"
                                                                                        id="newMemberName"
                                                                                        name="newMemberName" required>
                                                                                </div>
                                                                                <input type="hidden" id="groupId"
                                                                                    value="<?php echo $row['id']?>"
                                                                                    name="groupId">
                                                                                <button type="submit"
                                                                                    class="btn btn-dark fw-bold">Add
                                                                                    Member</button>
                                                                            </form>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                            <?php }?>
                                                        </tbody>
                                                    </table>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </div>

    <script>
    $(document).ready(function() {
        $('.add-member').click(function() {
            var groupId = $(this).data('group-id');
            $('#groupId').val(groupId);
        });
    });
    </script>

    <?php
      include('connection.php');
      $sql = "SELECT * FROM groups";
      $query = mysqli_query($conn, $sql);
      while ($row = mysqli_fetch_assoc($query)) {
        $members = explode(',', $row['member']);
     ?>
    <div class="modal fade" id="detailsModal<?php echo $row['id'];?>">
        <div class="modal-dialog modal-md">
            <div class="modal-content">
                <div class="modal-header" style="background: #343a40; ">
                    <h6 class="modal-title fw-bold text-white text-uppercase">Group Name:<?php echo $row['group_name']?>
                    </h6>
                    <button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form method="POST" action="" enctype="multipart/form-data">
                        <div class="card-body">
                            <input type="hidden" id="id" name="id" value="<?php echo $row['id']?>">
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group">
                                        <label for="exampleInputName">All Member</label>
                                        <!-- Loop through members and display them with delete buttons -->
                                        <?php foreach ($members as $member): ?>
                                        <?php
                                        $trimmedMember = trim($member);
                                        if (!empty($trimmedMember)): // Check if member is not empty
                                        ?>
                                        <div class="member-container">
                                            <span class="member-name"><?php echo $trimmedMember; ?></span>
                                            <button class="btn btn-danger btn-sm my-2 delete-button"
                                                onclick="removeMember(<?php echo $row['id']; ?>, '<?php echo $trimmedMember; ?>')">Delete</button>
                                        </div>
                                        <?php endif; ?>
                                        <?php endforeach; ?>


                                    </div>
                                </div>


                            </div>

                        </div>


                    </form>
                </div>

            </div>

        </div>

    </div>
    <?php }?>

    <!-- Page specific script -->
    <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() {
        <?php if (isset($_GET['error'])) { ?>
        Swal.fire({
            title: 'Error',
            text: '<?php echo $_GET['error']; ?>',
            icon: 'error',
            confirmButtonText: 'OK',
            confirmButtonClass: 'btn btn-success btn-lg',
        });
        <?php } ?>
        <?php if (isset($_GET['success'])) { ?>
        Swal.fire({
            title: 'Success',
            text: '<?php echo $_GET['success']; ?>',
            icon: 'success',
            confirmButtonText: 'OK',
            confirmButtonClass: 'btn btn-success btn-lg',
        });
        <?php } ?>
    });
    </script>

    <script>
    function removeMember(groupId, memberName) {
        if (confirm(`Are you sure you want to remove ${memberName} from the group?`)) {
            $.post('remove-member.php', {
                groupId: groupId,
                memberName: memberName
            }, function(response) {
                if (response === 'success') {
                    location.reload(); // Reload the page to reflect the changes
                } else {
                    alert('Failed to remove member. Please try again.');
                }
            });
        }
    }
    </script>



    <script>
    // Use noConflict to avoid conflicts with other scripts
    var $j = jQuery.noConflict();
    $j(document).ready(function() {
        $j('#example2').DataTable();
    });

    function displayFileName() {
        var input = document.getElementById('video_file');
        var fileName = input.files[0] ? input.files[0].name : '';
        var label = input.nextElementSibling; // Get the label element
        label.innerText = fileName;
    }


    function displayFileName() {
        var input = document.getElementById('image_file');
        var fileName = input.files[0] ? input.files[0].name : '';
        var label = input.nextElementSibling; // Get the label element
        label.innerText = fileName;
    }

    function displayFileName() {
        var input = document.getElementById('audio_file');
        var fileName = input.files[0] ? input.files[0].name : '';
        var label = input.nextElementSibling; // Get the label element
        label.innerText = fileName;
    }

    function displayFileName() {
        var input = document.getElementById('pdf_file');
        var fileName = input.files[0] ? input.files[0].name : '';
        var label = input.nextElementSibling; // Get the label element
        label.innerText = fileName;
    }
    </script>


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

</body>

</html>