<?php
session_start();
if (!isset($_SESSION['staff_logged_in'])) { header("Location: login.php"); exit(); }
include '../db_connect.php';

$msg = "";

if (isset($_POST['upload'])) {
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $report_name = $_POST['report_name'];
    
    $target_dir = "../assets/reports/";
    $file_name = time() . "_" . basename($_FILES["file"]["name"]);
    $target_file = $target_dir . $file_name;
    
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
        // CRITICAL: We explicitly insert 'Pending' here
        $stmt = $conn->prepare("INSERT INTO lab_reports (patient_name, patient_phone, report_name, file_path, status) VALUES (?, ?, ?, ?, 'Pending')");
        $stmt->bind_param("ssss", $name, $phone, $report_name, $file_name);
        
        if($stmt->execute()) {
            $msg = "✅ Uploaded! Status set to 'Pending'. Waiting for Doctor approval.";
        } else {
            $msg = "Database Error: " . $conn->error;
        }
    } else {
        $msg = "Error uploading file.";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Upload Reports</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen">
    <nav class="bg-teal-800 text-white p-4 shadow flex justify-between">
        <h1 class="font-bold text-xl">Upload Lab Reports</h1>
        <a href="dashboard.php" class="bg-white text-teal-800 px-3 py-1 rounded text-sm font-bold">← Dashboard</a>
    </nav>

    <div class="container mx-auto p-6 max-w-2xl">
        <div class="bg-white p-8 rounded shadow-md border-t-4 border-purple-500">
            <h2 class="text-xl font-bold mb-4">📂 Add Patient Report</h2>
            
            <?php if($msg) echo "<p class='bg-blue-100 text-blue-800 p-3 rounded mb-4 border border-blue-200 font-bold'>$msg</p>"; ?>
            
            <form method="POST" enctype="multipart/form-data" class="space-y-4">
                <div>
                    <label class="block text-sm font-bold text-gray-600">Patient Phone</label>
                    <input type="number" name="phone" required class="w-full border p-2 rounded">
                </div>
                <div>
                    <label class="block text-sm font-bold text-gray-600">Patient Name</label>
                    <input type="text" name="name" required class="w-full border p-2 rounded">
                </div>
                <div>
                    <label class="block text-sm font-bold text-gray-600">Report Title</label>
                    <input type="text" name="report_name" required placeholder="e.g. CBC Blood Test" class="w-full border p-2 rounded">
                </div>
                <div>
                    <label class="block text-sm font-bold text-gray-600">File</label>
                    <input type="file" name="file" required class="w-full border p-2 rounded">
                </div>
                <button type="submit" name="upload" class="w-full bg-purple-600 text-white font-bold py-3 rounded hover:bg-purple-700">
                    Upload & Send to Doctor
                </button>
            </form>
        </div>
    </div>
</body>
</html>