<?php

require '../config/database.php';

$data = json_decode(file_get_contents("php://input"), true);

$title = $data['title'];
$description = $data['description'];
$hazard = $data['hazard_level'];
$section = $data['section_id'];
$assigned = $data['assigned_user_id'];
$creator = $_SESSION['user_id'];

$created = date('Y-m-d H:i:s');

switch($hazard){

    case 'A':
        $due = date('Y-m-d H:i:s', strtotime('+24 hours'));
        break;

    case 'B':
        $due = date('Y-m-d H:i:s', strtotime('+7 days'));
        break;

    case 'C':
        $due = date('Y-m-d H:i:s', strtotime('+14 days'));
        break;
}

$stmt = $pdo->prepare("
INSERT INTO safety_concerns
(title, description, hazard_level, section_id, assigned_user_id, created_by, status, created_at, due_date)
VALUES (?,?,?,?,?,?, 'OPEN', ?, ?)
");

$stmt->execute([
$title,
$description,
$hazard,
$section,
$assigned,
$creator,
$created,
$due
]);

echo json_encode([
"status"=>"success"
]);