<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (!isset($_SESSION['patient_id'])) {
    header("Location: ../login.php");
    exit();
}
include '../../include/config.php';

$user_id = $_SESSION['patient_id'];
$user_name = $_SESSION['patient_name'];

// Get all prescriptions for this patient
$prescriptions = $conn->query("
    SELECT p.*, d.name as doctor_name, d.specialization 
    FROM prescriptions p
    LEFT JOIN doctors d ON p.doctor_id = d.doctor_id
    WHERE p.patient_id = '$user_id'
    ORDER BY p.prescribed_date DESC, p.created_at DESC
");

$page_title = 'My Prescriptions | UHD';
include '../../include/header.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Prescriptions - UHD</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
    <style>
        body { background: linear-gradient(135deg, #0a3d2f, #064e3b); font-family: 'Segoe UI', sans-serif; }
        
        .prescription-card {
            background: white;
            border-radius: 20px;
            padding: 20px;
            margin-bottom: 20px;
            transition: all 0.3s ease;
            border-left: 4px solid #d4a017;
            cursor: pointer;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
        }
        
        .prescription-card:hover {
            transform: translateX(5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.15);
        }
        
        .medication-name {
            font-size: 1.1rem;
            font-weight: 700;
            color: #0a3d2f;
            margin-bottom: 5px;
        }
        
        .status-badge {
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 0.7rem;
            font-weight: 600;
            display: inline-block;
        }
        
        .status-active { background: #28a745; color: white; }
        .status-completed { background: #17a2b8; color: white; }
        .status-cancelled { background: #6c757d; color: white; }
        
        .header-section {
            background: rgba(255,255,255,0.08);
            backdrop-filter: blur(10px);
            border-radius: 24px;
            padding: 25px;
            margin-bottom: 30px;
            border: 1px solid rgba(255,255,255,0.1);
        }
        
        .btn-refill {
            background: #d4a017;
            color: #0a3d2f;
            border: none;
            border-radius: 30px;
            padding: 6px 18px;
            font-size: 0.75rem;
            font-weight: 600;
        }
        
        .btn-refill:hover {
            background: #b8860b;
            color: white;
        }
        
        .back-btn {
            background: rgba(255,255,255,0.15);
            border: 1px solid rgba(255,255,255,0.2);
            color: white;
            border-radius: 40px;
            padding: 8px 20px;
            text-decoration: none;
        }
        
        .back-btn:hover {
            background: #d4a017;
            color: #0a3d2f;
        }
        
        @media (max-width: 768px) {
            .prescription-card { padding: 15px; }
            .medication-name { font-size: 1rem; }
        }
    </style>
</head>
<body>

<div class="container mt-4">
    <!-- Header Section -->
    <div class="header-section text-white">
        <div class="row align-items-center">
            <div class="col-md-8">
                <h2 class="fw-bold mb-2"><i class="fas fa-prescription-bottle me-2" style="color: #d4a017;"></i> My Prescriptions</h2>
                <p class="mb-0 opacity-75">View all your prescribed medications and refill requests.</p>
            </div>
            <div class="col-md-4 text-center mt-3 mt-md-0">
                <a href="index.php" class="back-btn"><i class="fas fa-arrow-left me-2"></i> Back to Medical Hub</a>
            </div>
        </div>
    </div>

    <!-- Prescriptions List -->
    <?php if ($prescriptions && $prescriptions->num_rows > 0): ?>
        <div class="row">
            <?php while($p = $prescriptions->fetch_assoc()): 
                $status_class = '';
                $status_text = '';
                $status = $p['status'] ?? 'active';
                
                if($status == 'active') {
                    $status_class = 'status-active';
                    $status_text = 'Active';
                } elseif($status == 'completed') {
                    $status_class = 'status-completed';
                    $status_text = 'Completed';
                } else {
                    $status_class = 'status-cancelled';
                    $status_text = 'Cancelled';
                }
            ?>
                <div class="col-md-6">
                    <div class="prescription-card">
                        <div class="d-flex justify-content-between align-items-start">
                            <div>
                                <div class="medication-name"><?= htmlspecialchars($p['medication']) ?></div>
                                <small class="text-muted">Prescribed by: Dr. <?= htmlspecialchars($p['doctor_name'] ?? 'Doctor') ?></small>
                            </div>
                            <span class="status-badge <?= $status_class ?>"><?= $status_text ?></span>
                        </div>
                        
                        <div class="mt-3">
                            <div class="row">
                                <div class="col-6">
                                    <small class="text-muted"><i class="fas fa-chart-line me-1"></i> Dosage</small>
                                    <div class="fw-semibold"><?= htmlspecialchars($p['dosage'] ?? 'As prescribed') ?></div>
                                </div>
                                <div class="col-6">
                                    <small class="text-muted"><i class="fas fa-clock me-1"></i> Frequency</small>
                                    <div class="fw-semibold"><?= htmlspecialchars($p['frequency'] ?? 'As needed') ?></div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="mt-2">
                            <small class="text-muted"><i class="fas fa-hourglass-half me-1"></i> Duration</small>
                            <div class="small"><?= htmlspecialchars($p['duration'] ?? 'Not specified') ?></div>
                        </div>
                        
                        <div class="mt-2">
                            <small class="text-muted"><i class="fas fa-calendar-alt me-1"></i> Prescribed Date</small>
                            <div class="small"><?= date('M d, Y', strtotime($p['prescribed_date'] ?? $p['created_at'])) ?></div>
                        </div>
                        
                        <?php if(!empty($p['instructions'])): ?>
                            <div class="mt-2">
                                <small class="text-muted"><i class="fas fa-info-circle me-1"></i> Instructions</small>
                                <div class="small"><?= htmlspecialchars(substr($p['instructions'], 0, 100)) ?></div>
                            </div>
                        <?php endif; ?>
                        
                        <?php if($status == 'active'): ?>
                            <div class="mt-3">
                                <button class="btn-refill" onclick="requestRefill(<?= $p['id'] ?>, '<?= addslashes($p['medication']) ?>')">
                                    <i class="fas fa-sync-alt me-1"></i> Request Refill
                                </button>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endwhile; ?>
        </div>
        
        <!-- Info Footer -->
        <div class="text-center mt-4">
            <small class="text-white-50">
                <i class="fas fa-shield-alt me-1"></i> Prescriptions are secure | 
                <i class="fas fa-clock ms-2 me-1"></i> Refill requests processed within 24 hours
            </small>
        </div>
        
    <?php else: ?>
        <!-- Empty State -->
        <div class="alert alert-info text-center py-5" style="background: rgba(255,255,255,0.1); border: none; backdrop-filter: blur(5px);">
            <i class="fas fa-prescription fa-4x mb-3" style="color: #d4a017;"></i>
            <h4 class="text-white mb-2">No Prescriptions Yet</h4>
            <p class="text-white-50 mb-3">You don't have any active prescriptions at the moment.</p>
            <p class="small text-white-50">When a doctor prescribes medication, it will appear here.</p>
            <a href="../consultations.php" class="btn-appointment mt-2" style="background: #d4a017; color: #0a3d2f; padding: 8px 20px; border-radius: 40px; text-decoration: none; display: inline-block;">
                <i class="fas fa-video me-2"></i> Consult a Doctor
            </a>
        </div>
    <?php endif; ?>
</div>

<script>
function requestRefill(id, medication) {
    if(confirm(`Request a refill for ${medication}?`)) {
        alert(`Refill request for ${medication} has been sent to your doctor. You will be notified when approved.`);
        // Here you would make an AJAX call to process the refill request
    }
}
</script>

<style>
.btn-appointment {
    background: #d4a017;
    color: #0a3d2f;
    border: none;
    border-radius: 40px;
    padding: 8px 20px;
    text-decoration: none;
    display: inline-block;
}
.btn-appointment:hover {
    background: #b8860b;
    color: white;
}
</style>

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