<?php include('../include/headscript.php'); ?>
<?php include("../class/company.php"); ?>

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

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <?php include('../include/seo.php'); ?>

    <link rel="shortcut icon" href="../assets/images/favicon.ico">
    <link href="../assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link href="../assets/css/icons.css" rel="stylesheet" type="text/css">
    <link href="../assets/css/style.css" rel="stylesheet" type="text/css">
    <link href="https://cdn.jsdelivr.net/npm/boxicons@2.0.7/css/boxicons.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@10/dist/sweetalert2.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@10/dist/sweetalert2.min.css">
    <script src="../assets/js/jquery.min.js"></script>
    <link rel="stylesheet" href="../assets/plugins/select2/select2.min.css">
    <script src="../assets/plugins/select2/select2.min.js"></script>
    <style>
        /* Custom styles here */
    </style>
</head>

<body class="fixed-left">

    <style>
        #ajax_preloader {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: transparent;
            z-index: 9999999;
        }

        .select2-container--open {
            z-index: 9999;
        }
    </style> 

    <div id="wrapper">
        <?php include('../include/navigationbar.php'); ?>

        <div class="content-page">
            <div class="content">

                <?php include('../include/topbar.php'); ?>

                <div class="page-content-wrapper ">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-sm-12">
                                <div class="page-title-box">
                                    <h4 class="page-title">Notifications</h4>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xl-12">
                                <div class="card">
                                    <div class="card-body">
                                        <h5 class="header-title pb-3 mt-0">Records</h5>
                                        <div class="table-responsive">
                                            <table class="table table-striped table-custom">
                                                <thead>
                                                    <tr>
                                                        <th>ID</th>
                                                        <th>Title</th>
                                                        <th>Description</th>
                                                        <th>Actions</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                    // Database connection
                                                    $conn = new mysqli($servername, $username, $password, $database);
                                                    if ($conn->connect_error) {
                                                        die("Connection failed: " . $conn->connect_error);
                                                    }

                                                    // Query for rows with processed = 2
                                                    $sql = "SELECT * FROM notifications";
                                                    $result = $conn->query($sql);

                                                    $all_notifications = [];
                                                    if ($result->num_rows > 0) {
                                                        while ($row = $result->fetch_assoc()) {
                                                            $all_notifications[] = $row;
                                                        }
                                                    }

                                                    $user_id = $_SESSION['iduserlogin'];
                                                    $all_notification_ids = array_column($all_notifications, 'id');
                                                    $all_notification_ids_str = implode(',', $all_notification_ids);

                                                    $sql = "SELECT notification_id 
                                                            FROM seen_notifications 
                                                            WHERE notification_id IN ($all_notification_ids_str)
                                                            AND user_id = $user_id";
                                                    $result = $conn->query($sql);
                                                    $seen_notification_ids = [];
                                                    if ($result->num_rows > 0) {
                                                        while ($row = $result->fetch_assoc()) {
                                                            $seen_notification_ids[] = $row['notification_id'];
                                                        }
                                                    }
                                                    
                                                    $seen_notifications = [];
                                                    foreach($all_notifications as $notification){
                                                        if(in_array($notification['id'], $seen_notification_ids)){
                                                            $seen_notifications[] = $notification;
                                                        }
                                                    }

                                                    $unseen_notifications = array_udiff($all_notifications, $seen_notifications, function ($a, $b) {
                                                        return $a['id'] <=> $b['id'];
                                                    });
                                                    

                                                    // unseen notifications with mark as seen button and different color , seen without that
                                                    if(!empty($all_notifications)){
                                                        foreach($all_notifications as $notification){
                                                            if(in_array($notification, $unseen_notifications)){
                                                                echo "<tr style='background-color: #f8d7da;'>";
                                                            }else{
                                                                echo "<tr>";
                                                            }
                                                            echo "<td>" . $notification['id'] . "</td>";
                                                            echo "<td>" . $notification['title'] . "</td>";
                                                            echo "<td>" . $notification['description'] . "</td>";
                                                            if(in_array($notification, $unseen_notifications)){
                                                                echo "<td>
                                                                        <button class='btn btn-warning btn-sm' onclick='mark_as_seen(" . $notification['id'] . ")'>
                                                                        <i class='bx bx-check'></i> Mark as seen
                                                                        </button>
                                                                      </td>";
                                                            }else{
                                                                echo "<td></td>";
                                                            }
                                                            echo "</tr>";
                                                        }
                                                    }

                                                    $conn->close();
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div> <!-- container -->
                </div> <!-- Page content Wrapper -->
            </div> <!-- content -->

            <?php include('../include/footer.php'); ?>
        </div>
    </div> 

    <script>
        function mark_as_seen(id){
            $.ajax({
                url:'<?php echo USERAREA_PATH;?>include/getNotifications.php',
                type:'POST',
                data:{
                    method:'markAsSeen',
                    notification_id:id
                },
                success:function(response){
                    if(response.code == 'success'){
                        Swal.fire({
                            title: 'Success',
                            text: 'Notification marked as seen',
                            icon: 'success',
                            confirmButtonText: 'OK'
                        }).then((result) => {
                            location.reload();
                        });
                    }else{
                        Swal.fire({
                            title: 'Error',
                            text: 'An error occurred',
                            icon: 'error',
                            confirmButtonText: 'OK'
                        });
                    }
                }
            })
        }
    </script>

    <script src="../assets/js/common_helper.js"></script>
    <script src="../assets/plugins/alertify/js/alertify.js"></script>

</body>

</html>