<?php
session_start();
if (!isset($_SESSION['doctor_logged_in'])) { header("Location: login.php"); exit(); }
include '../db_connect.php';
date_default_timezone_set('Asia/Kolkata');

$doctor_name = $_SESSION['doctor_name']; // Auto-detect logged in doctor
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>New Prescription</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen p-6">

    <nav class="flex justify-between items-center mb-6">
        <div class="flex items-center gap-2">
            <a href="dashboard.php" class="bg-gray-600 text-white px-3 py-1 rounded text-sm">← Back</a>
            <h1 class="text-2xl font-bold text-teal-800">Write New Prescription</h1>
        </div>
        <div class="bg-white px-4 py-2 rounded shadow text-sm font-bold text-teal-700">
            👨‍⚕️ <?php echo $doctor_name; ?>
        </div>
    </nav>

    <div class="max-w-4xl mx-auto bg-white rounded-lg shadow-xl overflow-hidden border-t-4 border-teal-600">
        <form action="print_rx.php" method="POST" target="_blank" class="p-8">
            
            <h3 class="text-gray-500 font-bold uppercase text-sm border-b pb-2 mb-4">Patient Details</h3>
            <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
                <div class="col-span-2">
                    <label class="text-xs font-bold text-gray-600">Patient Name</label>
                    <input type="text" name="patient_name" required class="w-full border p-2 rounded bg-gray-50">
                </div>
                <div>
                    <label class="text-xs font-bold text-gray-600">Age (Yrs)</label>
                    <input type="number" name="age" required class="w-full border p-2 rounded bg-gray-50">
                </div>
                <div>
                    <label class="text-xs font-bold text-gray-600">Gender</label>
                    <select name="gender" class="w-full border p-2 rounded bg-gray-50">
                        <option>Male</option>
                        <option>Female</option>
                        <option>Other</option>
                    </select>
                </div>
                <div>
                    <label class="text-xs font-bold text-gray-600">Mobile</label>
                    <input type="text" name="mobile" class="w-full border p-2 rounded bg-gray-50">
                </div>
                <div>
                    <label class="text-xs font-bold text-gray-600">OPD No / ID</label>
                    <input type="text" name="opd_number" class="w-full border p-2 rounded bg-gray-50">
                </div>
                <div class="col-span-2">
                    <label class="text-xs font-bold text-gray-600">Date</label>
                    <input type="date" name="visit_date" value="<?php echo date('Y-m-d'); ?>" class="w-full border p-2 rounded bg-gray-50">
                </div>
            </div>

            <h3 class="text-gray-500 font-bold uppercase text-sm border-b pb-2 mb-4">Clinical Notes</h3>
            <div class="mb-4">
                <label class="text-xs font-bold text-gray-600">Diagnosis / Symptoms</label>
                <textarea name="diagnosis" rows="2" class="w-full border p-2 rounded" placeholder="e.g. Acute Appendicitis, Fever..."></textarea>
            </div>

            <div class="mb-6">
                <label class="block text-lg font-bold text-teal-800 mb-1">💊 Rx (Medicines)</label>
                <div class="text-xs text-gray-400 mb-2">Format: Medicine Name -- Dosage -- Duration (One per line)</div>
                <textarea name="medicine_text" rows="8" required class="w-full border-2 border-teal-100 p-4 rounded font-mono text-lg focus:border-teal-500 outline-none" placeholder="Tab Dolo 650 --- 1-0-1 --- 3 Days&#10;Inj Tetanus --- Stat"></textarea>
            </div>

            <div class="grid grid-cols-2 gap-4 mb-6">
                <div>
                    <label class="text-xs font-bold text-gray-600">Advice / Instructions</label>
                    <textarea name="advice" rows="2" class="w-full border p-2 rounded" placeholder="Drink plenty of water..."></textarea>
                </div>
                <div>
                    <label class="text-xs font-bold text-gray-600">Next Visit Date</label>
                    <input type="date" name="next_visit" class="w-full border p-2 rounded">
                </div>
            </div>

            <div class="flex justify-end gap-4 pt-4 border-t">
                <button type="reset" class="px-6 py-3 bg-gray-200 text-gray-700 font-bold rounded hover:bg-gray-300">Clear</button>
                <button type="submit" name="generate" class="px-8 py-3 bg-teal-600 text-white font-bold rounded hover:bg-teal-700 shadow-lg flex items-center gap-2">
                    <span>Generate & Print PDF</span>
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2.5 0a.5.5 0 01.5.5v3a.5.5 0 01-.5.5h-6a.5.5 0 01-.5-.5v-3a.5.5 0 01.5-.5h6z"></path></svg>
                </button>
            </div>

        </form>
    </div>
</body>
</html>