<?php
include 'includes/db.php'; // MUST BE FIRST

$success = false;
$error = '';

if(isset($_POST['submit_request'])){

    if(!$conn){
        die("Database connection failed");
    }

    $name    = $conn->real_escape_string($_POST['patient_name']);
    $phone   = $conn->real_escape_string($_POST['phone']);
    $email   = $conn->real_escape_string($_POST['email']);
    $address = $conn->real_escape_string($_POST['address']);
    $notes   = $conn->real_escape_string($_POST['notes']);
    $lat     = $conn->real_escape_string($_POST['latitude']);
    $lng     = $conn->real_escape_string($_POST['longitude']);

    // Upload prescription
    $fileName = '';
    if(!empty($_FILES['prescription']['name'])){
        $dir = "uploads/prescriptions/";
        if(!is_dir($dir)){
            mkdir($dir, 0777, true);
        }

        $fileName = time().'_'.basename($_FILES['prescription']['name']);
        move_uploaded_file($_FILES['prescription']['tmp_name'], $dir.$fileName);
    }

    $sql = "
        INSERT INTO home_sampling_requests
        (patient_name, phone, email, address, notes, prescription_file, latitude, longitude, status)
        VALUES
        ('$name','$phone','$email','$address','$notes','$fileName','$lat','$lng','new')
    ";

    if($conn->query($sql)){
        $success = true;
    }else{
        $error = $conn->error;
    }
}
?>

<?php include 'includes/header.php'; ?>

<div class="home-sampling-wrapper">
    <h2>Home Sampling Request</h2>

    <?php if($success): ?>
        <div class="success-msg">
            ✅ Your request has been sent successfully. We will contact you shortly.
        </div>
    <?php elseif(!empty($error)): ?>
        <div class="error-msg">
            ❌ Error: <?= htmlspecialchars($error) ?>
        </div>
    <?php endif; ?>

    <form method="post" enctype="multipart/form-data">
        <input type="text" name="patient_name" placeholder="Patient Name" required>
        <input type="text" name="phone" placeholder="Phone Number" required>
        <input type="email" name="email" placeholder="Email (optional)">

        <textarea name="address" placeholder="Home Address" required></textarea>
        <textarea name="notes" placeholder="Notes (optional)"></textarea>

        <div class="file-upload">
            📎 Upload Prescription:
            <input type="file" name="prescription" accept=".jpg,.png,.pdf">
        </div>

        <input type="hidden" name="latitude" id="lat">
        <input type="hidden" name="longitude" id="lng">

        <button type="submit" name="submit_request">Submit Request</button>
    </form>
</div>

<script>
navigator.geolocation.getCurrentPosition(
    function(pos){
        document.getElementById("lat").value = pos.coords.latitude;
        document.getElementById("lng").value = pos.coords.longitude;
    },
    function(){
        console.log("Location access denied");
    }
);
</script>

<style>
.home-sampling-wrapper{
    max-width:520px;
    margin:40px auto;
    background:#fff;
    padding:25px;
    border-radius:10px;
    box-shadow:0 6px 20px rgba(0,0,0,.08);
    font-family:Segoe UI,Tahoma,sans-serif;
}
.home-sampling-wrapper h2{
    text-align:center;
    margin-bottom:20px;
}
.home-sampling-wrapper input,
.home-sampling-wrapper textarea{
    width:100%;
    padding:12px;
    margin-bottom:14px;
    border:1px solid #ccc;
    border-radius:6px;
}
.file-upload{
    border:1px dashed #ccc;
    padding:12px;
    margin-bottom:15px;
}
.home-sampling-wrapper button{
    width:100%;
    padding:14px;
    background:#25A9C2;
    color:#fff;
    border:none;
    border-radius:6px;
    font-size:16px;
    cursor:pointer;
}
.home-sampling-wrapper button:hover{
    background:#1e40af;
}
.success-msg{
    background:#dcfce7;
    color:#166534;
    padding:12px;
    margin-bottom:15px;
    border-radius:6px;
    text-align:center;
}
.error-msg{
    background:#fee2e2;
    color:#991b1b;
    padding:12px;
    margin-bottom:15px;
    border-radius:6px;
    text-align:center;
}
</style>

<?php include 'includes/footer.php'; ?>
