<?php
session_start();
if (!isset($_SESSION['staff_logged_in'])) { header("Location: login.php"); exit(); }
include '../db_connect.php';

// --- IMPORTANT: SET TIMEZONE TO IST (INDIA) ---
date_default_timezone_set('Asia/Kolkata');

// Initialize variables with IST Dates
$search = "";
$doctor = "";
$from_date = date('Y-m-01'); // 1st of current month (IST)
$to_date = date('Y-m-d');    // Today (IST)

// Build Query based on Filters
$sql = "SELECT * FROM patient_entries WHERE 1=1";

if (isset($_GET['filter'])) {
    if (!empty($_GET['search'])) {
        $search = $conn->real_escape_string($_GET['search']);
        $sql .= " AND (patient_name LIKE '%$search%' OR phone LIKE '%$search%')";
    }
    if (!empty($_GET['doctor'])) {
        $doctor = $conn->real_escape_string($_GET['doctor']);
        $sql .= " AND doctor = '$doctor'";
    }
    if (!empty($_GET['from_date']) && !empty($_GET['to_date'])) {
        $from_date = $_GET['from_date'];
        $to_date = $_GET['to_date'];
        $sql .= " AND visit_date BETWEEN '$from_date' AND '$to_date'";
    }
}

$sql .= " ORDER BY id DESC";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Patient History | Reception</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-lg flex justify-between items-center">
        <div class="flex items-center gap-2">
            <a href="dashboard.php" class="text-white hover:bg-teal-700 px-3 py-1 rounded transition">← Back to Dashboard</a>
            <h1 class="text-xl font-bold border-l border-teal-600 pl-4 ml-2">Patient History</h1>
        </div>
        <div class="flex items-center gap-4">
            <span class="text-xs bg-teal-900 px-2 py-1 rounded text-gray-300">
                IST Time: <?php echo date('h:i A'); ?>
            </span>
            <a href="logout.php" class="bg-red-500 hover:bg-red-600 px-4 py-1 rounded text-sm font-bold">Logout</a>
        </div>
    </nav>

    <div class="container mx-auto p-6">
        
        <div class="bg-white p-6 rounded-lg shadow-md mb-8 border-t-4 border-blue-500">
            <h2 class="text-lg font-bold text-gray-700 mb-4">🔍 Filter & Search Records</h2>
            <form method="GET" class="grid grid-cols-1 md:grid-cols-5 gap-4 items-end">
                
                <div class="md:col-span-2">
                    <label class="text-xs font-bold text-gray-500 uppercase">Search (Name / Phone)</label>
                    <input type="text" name="search" value="<?php echo htmlspecialchars($search); ?>" placeholder="Enter name or mobile..." class="w-full border p-2 rounded focus:ring-2 focus:ring-blue-500 outline-none">
                </div>

                <div>
                    <label class="text-xs font-bold text-gray-500 uppercase">Doctor</label>
                    <select name="doctor" class="w-full border p-2 rounded bg-white">
                        <option value="">All Doctors</option>
                        <option value="Dr. Gajanan Padghan" <?php if($doctor == 'Dr. Gajanan Padghan') echo 'selected'; ?>>Dr. Gajanan Padghan</option>
                        <option value="Dr. Anita Padghan" <?php if($doctor == 'Dr. Anita Padghan') echo 'selected'; ?>>Dr. Anita Padghan</option>
                    </select>
                </div>

                <div>
                    <label class="text-xs font-bold text-gray-500 uppercase">From Date</label>
                    <input type="date" name="from_date" value="<?php echo $from_date; ?>" class="w-full border p-2 rounded">
                </div>
                <div>
                    <label class="text-xs font-bold text-gray-500 uppercase">To Date</label>
                    <input type="date" name="to_date" value="<?php echo $to_date; ?>" class="w-full border p-2 rounded">
                </div>

                <div class="md:col-span-5 flex gap-2">
                    <button type="submit" name="filter" class="bg-blue-600 text-white px-6 py-2 rounded font-bold hover:bg-blue-700 transition">Apply Filters</button>
                    <a href="history.php" class="bg-gray-500 text-white px-6 py-2 rounded font-bold hover:bg-gray-600 transition">Reset</a>
                </div>
            </form>
        </div>

        <div class="bg-white rounded-lg shadow-md overflow-hidden">
            <div class="p-4 bg-gray-50 border-b flex justify-between items-center">
                <h3 class="font-bold text-gray-700">Total Records Found: <?php echo $result->num_rows; ?></h3>
                <button onclick="window.print()" class="text-blue-600 font-bold hover:underline text-sm flex items-center gap-1">
                    🖨️ Print List
                </button>
            </div>
            
            <div class="overflow-x-auto">
                <table class="w-full text-left border-collapse">
                    <thead class="bg-gray-100 text-xs text-gray-600 uppercase">
                        <tr>
                            <th class="p-4 border-b">Date</th>
                            <th class="p-4 border-b">Receipt #</th>
                            <th class="p-4 border-b">Patient Name</th>
                            <th class="p-4 border-b">Phone</th>
                            <th class="p-4 border-b">Doctor</th>
                            <th class="p-4 border-b">Fee</th>
                            <th class="p-4 border-b text-center">Receipt</th>
                        </tr>
                    </thead>
                    <tbody class="text-sm text-gray-700">
                        <?php
                        if ($result->num_rows > 0) {
                            while($row = $result->fetch_assoc()) {
                                echo '<tr class="border-b hover:bg-gray-50 transition">';
                                echo '<td class="p-4 whitespace-nowrap">' . date('d-M-Y', strtotime($row['visit_date'])) . '</td>';
                                echo '<td class="p-4 font-mono text-xs text-gray-500">' . $row['id'] . '</td>';
                                echo '<td class="p-4 font-bold">' . $row['patient_name'] . '</td>';
                                echo '<td class="p-4">' . $row['phone'] . '</td>';
                                echo '<td class="p-4 text-teal-700 font-medium">' . $row['doctor'] . '</td>';
                                echo '<td class="p-4 font-bold">₹' . $row['consultation_fee'] . '</td>';
                                echo '<td class="p-4 text-center">';
                                if(!empty($row['pdf_file'])) {
                                    echo '<a href="../assets/receipts/' . $row['pdf_file'] . '" target="_blank" class="bg-red-100 text-red-600 px-3 py-1 rounded text-xs font-bold hover:bg-red-200">PDF</a>';
                                } else {
                                    echo '<span class="text-gray-400 text-xs">N/A</span>';
                                }
                                echo '</td>';
                                echo '</tr>';
                            }
                        } else {
                            echo '<tr><td colspan="7" class="p-8 text-center text-gray-500">No records found for the selected filters.</td></tr>';
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>

    </div>
</body>
</html>