<?php 
session_start(); 
require 'db_connect.php';

// Fetch PDFs
try {
    $pdfs = $users_db->query("SELECT * FROM pdf_downloads ORDER BY created_at DESC")->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) { $pdfs = []; }

// Fetch Software
try {
    $sw_list = $users_db->query("SELECT * FROM software_downloads ORDER BY created_at DESC")->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) { $sw_list = []; }
?>
<!DOCTYPE html>
<html lang="en" class="scroll-smooth">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Downloads | Astrologer Subhajit Banerjee</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .bg-vedic-dark { background-color: #064e3b; } 
        .bg-vedic-light { background-color: #ecfdf5; } 
        .text-vedic-gold { color: #fbbf24; }
        .pdf-scroll::-webkit-scrollbar { width: 6px; }
        .pdf-scroll::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 4px; }
        .pdf-scroll::-webkit-scrollbar-thumb { background: #10b981; border-radius: 4px; }
        .pdf-scroll::-webkit-scrollbar-thumb:hover { background: #059669; }
    </style>
</head>
<body class="bg-gray-50 text-gray-800 font-sans antialiased flex flex-col min-h-screen">

    <nav class="bg-vedic-dark text-white shadow-lg sticky top-0 z-50">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between py-2 items-center min-h-[120px]">
                <div class="flex-shrink-0 font-bold text-2xl tracking-wider flex items-center">
                    <img src="logofpa.jpg" alt="Logo" class="w-[100px] h-[100px] object-cover rounded-full mr-4 bg-white border-2 border-vedic-gold">
                    Astro Subhajit
                </div>
                <div class="hidden md:flex space-x-8 font-medium">
                    <a href="index.php" class="hover:text-vedic-gold transition">Home</a>
                    <a href="about.php" class="hover:text-vedic-gold transition">About Us</a>
                    <a href="consultations.php" class="hover:text-vedic-gold transition">Consultations</a>
                    <a href="horoscope.php" class="hover:text-vedic-gold transition">Your Horoscope</a>
                    <a href="downloads.php" class="text-vedic-gold border-b-2 border-vedic-gold transition">Downloads</a>
                    <a href="contact.php" class="hover:text-vedic-gold transition">Contact</a>
                </div>
                <div>
                    <?php if(isset($_SESSION['client_id'])): ?>
                        <a href="dashboard.php" class="bg-yellow-400 hover:bg-yellow-300 text-yellow-900 px-6 py-2 rounded-full font-bold shadow-md transition flex items-center whitespace-nowrap">
                            <i class="fa-solid fa-circle-user mr-2"></i> My Dashboard
                        </a>
                    <?php else: ?>
                        <a href="login.php" class="bg-emerald-700 hover:bg-emerald-600 text-white px-6 py-2 rounded-full font-semibold shadow-md transition flex items-center whitespace-nowrap">
                            <i class="fa-solid fa-user mr-2"></i> Client Login
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </nav>

    <div class="bg-emerald-900 py-12 border-b-4 border-yellow-400 text-center">
        <h1 class="text-4xl font-extrabold text-white mb-2">Resource Center</h1>
        <p class="text-lg text-emerald-200">Download software and access astrological literature.</p>
    </div>

    <!-- MAIN CONTENT: 2-Column Grid Layout -->
    <main class="flex-grow max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12 w-full">
        <div class="grid grid-cols-1 lg:grid-cols-2 gap-12 items-start">
            
            <!-- LEFT COLUMN: Software Downloads -->
            <div class="w-full">
                <h2 class="text-3xl font-bold mb-6 text-emerald-900 border-b-2 border-yellow-400 pb-2">Software Downloads</h2>
                <div class="space-y-6">
                    <?php foreach ($sw_list as $sw): ?>
                    <div class="bg-white p-6 rounded-xl shadow-lg border border-emerald-100 hover:shadow-2xl transition">
                        <h3 class="text-xl font-bold text-gray-900"><?= htmlspecialchars($sw['title']) ?></h3>
                        <p class="text-sm text-gray-600 mb-4"><?= htmlspecialchars($sw['description']) ?></p>
                        <div class="flex gap-2 mb-4">
                            <span class="badge bg-blue-100 text-blue-800 px-3 py-1 rounded text-xs font-semibold"><?= htmlspecialchars($sw['os_compatibility']) ?></span>
                            <span class="badge bg-gray-100 text-gray-600 px-3 py-1 rounded text-xs font-semibold"><?= htmlspecialchars($sw['file_size']) ?></span>
                        </div>
                        <a href="<?= htmlspecialchars($sw['file_path']) ?>" download class="bg-[#064e3b] text-white w-full py-3 rounded-lg text-center block font-bold hover:bg-emerald-800 transition shadow-md">
                            <i class="fa-solid fa-download mr-2"></i> Download Software
                        </a>
                    </div>
                    <?php endforeach; ?>
                    
                    <?php if (empty($sw_list)): ?>
                        <div class="p-8 text-center text-gray-500 bg-white rounded-xl border">No software uploaded yet.</div>
                    <?php endif; ?>
                </div>
            </div>

            <!-- RIGHT COLUMN: Astrology Library PDFs -->
            <div class="w-full">
                <div class="flex justify-between items-end mb-6 border-b-2 border-yellow-400 pb-2">
                    <h2 class="text-3xl font-bold text-emerald-900">Astrology Library</h2>
                    <span class="bg-emerald-100 text-emerald-800 text-xs font-bold px-3 py-1 rounded-full uppercase tracking-wide"><?= count($pdfs) ?> Files</span>
                </div>
                
                <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
                    <?php if (count($pdfs) > 0): ?>
                        <ul class="divide-y divide-gray-100 max-h-[700px] overflow-y-auto pdf-scroll">
                            <?php foreach ($pdfs as $pdf): ?>
                                <li class="p-5 hover:bg-emerald-50 transition flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4">
                                    <div class="flex items-start">
                                        <div class="bg-red-100 text-red-600 rounded-lg p-3 mr-4 flex-shrink-0">
                                            <i class="fa-solid fa-file-pdf text-2xl"></i>
                                        </div>
                                        <div>
                                            <h3 class="font-bold text-gray-900 text-lg"><?= htmlspecialchars($pdf['title']) ?></h3>
                                            <p class="text-gray-500 text-sm mt-1"><?= htmlspecialchars($pdf['description']) ?></p>
                                        </div>
                                    </div>
                                    <a href="<?= htmlspecialchars($pdf['file_path']) ?>" download class="w-full sm:w-auto bg-gray-100 hover:bg-emerald-600 hover:text-white text-gray-700 font-semibold py-2 px-4 rounded transition flex items-center justify-center whitespace-nowrap">
                                        <i class="fa-solid fa-download mr-2"></i> Download
                                    </a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php else: ?>
                        <div class="p-16 text-center">
                            <div class="w-20 h-20 bg-gray-50 rounded-full flex items-center justify-center text-gray-300 text-4xl mx-auto mb-4">
                                <i class="fa-regular fa-folder-open"></i>
                            </div>
                            <h3 class="text-xl font-bold text-gray-900 mb-2">No Files Uploaded Yet</h3>
                            <p class="text-gray-500 max-w-sm mx-auto">The PDF library is currently empty.</p>
                        </div>
                    <?php endif; ?>
                </div>
            </div>

        </div>
    </main>

    <footer class="bg-[#022c22] text-emerald-100 py-12 border-t-4 border-yellow-400 mt-auto">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 grid grid-cols-1 md:grid-cols-3 gap-8">
            <div>
                <h3 class="text-xl font-bold text-white mb-4 flex items-center">
                    <img src="logofpa.jpg" alt="Future Point Astrology Logo" class="w-8 h-8 object-cover rounded-full mr-3 border border-yellow-400">
                    Future Point Astrology
                </h3>
                <p class="text-sm leading-relaxed mb-4 text-emerald-200">
                    Authentic Vedic Astrology and Gemology consultations by Subhajit Banerjee. Guiding you toward stability and success.
                </p>
            </div>
            <div>
                <h4 class="text-lg font-semibold text-white mb-4 border-b border-emerald-800 pb-2 inline-block">Quick Links</h4>
                <ul class="space-y-2 text-sm">
                    <li><a href="index.php" class="hover:text-yellow-400 transition">Home</a></li>
                    <li><a href="about.php" class="hover:text-yellow-400 transition">About Us</a></li>
                    <li><a href="consultations.php" class="hover:text-yellow-400 transition">Consultations</a></li>
                    <li><a href="dashboard.php" class="hover:text-yellow-400 transition">Client Dashboard</a></li>
                </ul>
            </div>
            <div>
                <h4 class="text-lg font-semibold text-white mb-4 border-b border-emerald-800 pb-2 inline-block">Legal Policies</h4>
                <ul class="space-y-2 text-sm">
                    <li><a href="terms.php" class="hover:text-yellow-400 transition">Terms & Conditions</a></li>
                    <li><a href="privacy.php" class="hover:text-yellow-400 transition">Privacy Policy</a></li>
                    <li><a href="refunds.php" class="hover:text-yellow-400 transition">Return & Refund Policy</a></li>
                </ul>
            </div>
        </div>
        <div class="max-w-7xl mx-auto px-4 mt-10 pt-6 border-t border-emerald-800 text-center text-xs text-emerald-400">
            © 2026 Future Point Astrology. All rights reserved.
        </div>
    </footer>

</body>
</html>