<?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 = $_SESSION['doctor_name'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Prescription History</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 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">← Dashboard</a>
            <h1 class="text-2xl font-bold text-teal-800">Rx History</h1>
        </div>
        <a href="new_rx.php" class="bg-teal-600 text-white px-4 py-2 rounded font-bold">+ New Rx</a>
    </nav>

    <div class="bg-white rounded shadow overflow-hidden">
        <table class="w-full text-left text-sm">
            <thead class="bg-teal-700 text-white">
                <tr>
                    <th class="p-3">Date</th>
                    <th class="p-3">Patient</th>
                    <th class="p-3">Diagnosis</th>
                    <th class="p-3">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $sql = "SELECT * FROM prescriptions WHERE doctor_name = '$doctor' ORDER BY id DESC LIMIT 20";
                $res = $conn->query($sql);
                if($res->num_rows > 0) {
                    while($row = $res->fetch_assoc()) {
                        echo "<tr class='border-b hover:bg-gray-50'>";
                        echo "<td class='p-3'>" . date("d M Y", strtotime($row['visit_date'])) . "</td>";
                        echo "<td class='p-3 font-bold'>{$row['patient_name']} <span class='text-gray-500 text-xs'>({$row['age']}/{$row['gender']})</span></td>";
                        echo "<td class='p-3 text-gray-600'>{$row['diagnosis']}</td>";
                        echo "<td class='p-3'>
                            <form action='print_rx.php' method='POST' target='_blank'>
                                <input type='hidden' name='patient_name' value='{$row['patient_name']}'>
                                <input type='hidden' name='age' value='{$row['age']}'>
                                <input type='hidden' name='gender' value='{$row['gender']}'>
                                <input type='hidden' name='mobile' value='{$row['mobile']}'>
                                <input type='hidden' name='opd_number' value='{$row['opd_number']}'>
                                <input type='hidden' name='visit_date' value='{$row['visit_date']}'>
                                <input type='hidden' name='diagnosis' value='{$row['diagnosis']}'>
                                <input type='hidden' name='medicine_text' value='{$row['medicine_text']}'>
                                <input type='hidden' name='advice' value='{$row['advice']}'>
                                <input type='hidden' name='next_visit' value='{$row['next_visit']}'>
                                <button type='submit' name='generate' class='text-blue-600 hover:underline'>Reprint</button>
                            </form>
                        </td>";
                        echo "</tr>";
                    }
                } else {
                    echo "<tr><td colspan='4' class='p-4 text-center text-gray-400'>No history found.</td></tr>";
                }
                ?>
            </tbody>
        </table>
    </div>
</body>
</html>