<?php
header('Content-Type: application/json');
include '../../Connections/repnew.php';
$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {
    die("Connessione fallita: " . $conn->connect_error);
}

$method = $_POST['method'];

if ($method == 'show_notification') {
    $sql = "SELECT r.idreports, p.products_refnumber, r.reportsNumberLab, r.reportsRating 
            FROM reports AS r
            JOIN products p ON r.idproducts = p.idproducts 
            WHERE r.seen = 0
            ORDER BY r.idreports DESC 
            LIMIT 1";
    $result = $conn->query($sql);
    $notifications = [];
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $notifications[] = $row;
        }
    }
    $conn->close();
    die(json_encode($notifications));
}else if($method == 'update_notification'){
    
    $idreports = $_POST['idreports'];
    $sql = "UPDATE reports SET seen = 1 WHERE seen = 0 AND idreports = $idreports";
    $conn->query($sql);
    $conn->close();
    die(json_encode('success'));
}
