<?php
session_start();
require('../include/connect.php');
if (isset($_SESSION['username'])) {
    $Manv = $_SESSION['username'];
    $query = "SELECT * from data_user where Maso_NV = '$Manv';";
    $resultkq = mysqli_query($link, $query);
    $rows = mysqli_fetch_array($resultkq);
    $bophan = $rows['Bophan'];
    $congty = $rows['Thuoc_congty'];
    $thuockhoi = $rows['Thuockhoi'];
    if ($_GET['Type'] == '1') {
        $find_notifications = "SELECT * from notice where (grouptype='0' and (seen NOT LIKE '%[$Manv]%' OR seen IS NULL OR seen ='')) OR (grouptype='1' and groupby='$congty' and (seen NOT LIKE '%[$Manv]%' OR seen IS NULL OR seen ='')) OR (grouptype='2' and groupby='$thuockhoi' and (seen NOT LIKE '%[$Manv]%' OR seen IS NULL OR seen ='')) OR (grouptype='3' and groupby='$bophan' and (seen NOT LIKE '%[$Manv]%' OR seen IS NULL OR seen =''));";
        $result = mysqli_query($link, $find_notifications);
        $count_active = '';
        while ($rows2 = mysqli_fetch_assoc($result)) {
            $count_active = mysqli_num_rows($result);
        }

        if (!empty($count_active)) {
            header('Content-Type: application/json');
            echo json_encode($count_active);
        } else {
            header('Content-Type: application/json');
            echo json_encode('error');
        }
    } else if ($_GET['Type'] == '2') {
        $find_notifications = "SELECT * from notice where (grouptype='0' and (seen NOT LIKE '%[$Manv]%' OR seen IS NULL OR seen ='')) OR (grouptype='1' and groupby='$congty' and (seen NOT LIKE '%[$Manv]%' OR seen IS NULL OR seen ='')) OR (grouptype='2' and groupby='$thuockhoi' and (seen NOT LIKE '%[$Manv]%' OR seen IS NULL OR seen ='')) OR (grouptype='3' and groupby='$bophan' and (seen NOT LIKE '%[$Manv]%' OR seen IS NULL OR seen ='')) ORDER BY Time DESC LIMIT 5;";
        $result = mysqli_query($link, $find_notifications);
        $notifications_data = array();
        while ($rows = mysqli_fetch_assoc($result)) {
            $notifications_data[] = array(
                "n_id" => $rows['n_id'],
                "notifications_name" => $rows['notifications_name'],
                "message" => $rows['message'],
                "time" => $rows['time']
            );
        }
        header('Content-Type: application/json');
        echo json_encode($notifications_data);
    } else if ($_GET['Type'] == '3') {
        $deactive_notifications = "SELECT * from notice where (grouptype='0' and (seen LIKE '%[$Manv]%')) OR (grouptype='1' and groupby='$congty' and (seen LIKE '%[$Manv]%')) OR (grouptype='2' and groupby='$thuockhoi' and (seen LIKE '%[$Manv]%')) OR (grouptype='3' and groupby='$bophan' and (seen LIKE '%[$Manv]%')) ORDER BY Time DESC LIMIT 0,5";
        $result = mysqli_query($link, $deactive_notifications);
        $deactive_notifications_dump = array();
        while ($rows = mysqli_fetch_assoc($result)) {
            $deactive_notifications_dump[] = array(
                "n_id" => $rows['n_id'],
                "notifications_name" => $rows['notifications_name'],
                "message" => $rows['message'],
                "time" => $rows['time']
            );
        }
        header('Content-Type: application/json');
        echo json_encode($deactive_notifications_dump);
    }
} else {
    header('location:../template/index.php?mod=error');   //redirect đến trang error.php
    exit();
}
