<?php
// ১. সেশন চেক করে স্টার্ট করা
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// ২. ডাটাবেস কানেকশন ইনক্লুড করা
require_once 'db.php';

// ৩. ডাটাবেস থেকে সেটিংস তথ্য নিয়ে আসা (ID = 1 এর জন্য)
try {
    $stmt = $pdo->query("SELECT * FROM settings WHERE id = 1 LIMIT 1");
    $settings = $stmt->fetch(PDO::FETCH_ASSOC);

    // ৪. ডাটাবেসের ভ্যালুগুলো ভেরিয়েবলে সেট করা (যাতে নিচে HTML এ ব্যবহার করা যায়)
    // যদি ডাটাবেসে তথ্য না থাকে, তবে ডিফল্ট ভ্যালু দেখাবে
    $site_name     = $settings['site_name'] ?? 'IzhaanPrinters'; 
    $contact_phone = $settings['contact_calls'] ?? '01700000000'; // contact_calls কলাম ব্যবহার করা হয়েছে
    $contact_email = $settings['contact_email'] ?? 'info@izhaanprinters.com';
    $trade_license = $settings['trade_license'] ?? 'TRAD-12345678';
    $site_address  = $settings['address'] ?? 'ঢাকা, বাংলাদেশ';

} catch (PDOException $e) {
    // ডাটাবেস এরর হলে ডিফল্ট ভ্যালু
    $site_name     = 'IzhaanPrinters';
    $contact_phone = '01700000000';
    $contact_email = 'info@izhaanprinters.com';
    $trade_license = 'TRAD-12345678';
}

// ৫. ইউজার লগইন চেক লজিক
$is_logged_in = isset($_SESSION['customer_id']);
$account_link = $is_logged_in ? 'profile.php' : 'login.php';
$account_text = $is_logged_in ? 'প্রোফাইল' : 'একাউন্ট';

// ৬. কার্ট কাউন্ট
$cart_count = isset($_SESSION['cart']) ? count($_SESSION['cart']) : 0;
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($site_name) ?> | প্রিমিয়াম প্রিন্টিং সার্ভিস</title>
    
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    
    <!-- Google Fonts: Pacifico (Stylish), Hind Siliguri (Bangla), Poppins (English) -->
    <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&family=Pacifico&family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#1E3A8A', // Deep Blue
                        secondary: '#F59E0B', // Orange/Gold
                        dark: '#1F2937',
                        light: '#F3F4F6'
                    },
                    fontFamily: {
                        bangla:['"Hind Siliguri"', 'sans-serif'],
                        english: ['"Poppins"', 'sans-serif'],
                        stylish: ['"Pacifico"', 'cursive'] // New Stylish Font
                    }
                }
            }
        }
    </script>
    <style>
        body { font-family: 'Hind Siliguri', sans-serif; background-color: #F9FAFB; }
        .en-font { font-family: 'Poppins', sans-serif; }
        .logo-font { font-family: 'Pacifico', cursive; } /* Custom Class */
        input:focus { outline: none; }
        #mobileSidebar { transition: transform 0.3s ease-in-out; }
    </style>
</head>
<body class="text-gray-800 antialiased flex flex-col min-h-screen">

    <!-- Top Bar (Now visible on mobile too) -->
    <div class="bg-primary text-white py-1.5 border-b border-blue-900">
        <!-- মোবাইলের জন্য কলাম এবং ডেস্কটপের জন্য রো (Row) ব্যবহার করা হয়েছে -->
        <div class="container mx-auto px-4 flex flex-col md:flex-row justify-between items-center gap-1.5 md:gap-0">
            
            <!-- Left: Phone & Email -->
            <div class="flex flex-wrap justify-center items-center gap-3 md:gap-5 text-[11px] md:text-xs">
                <!-- Phone Number -->
                <a href="tel:<?= htmlspecialchars($contact_phone) ?>" class="en-font flex items-center gap-1.5 hover:text-secondary transition">
                    <i class="fa-solid fa-phone text-secondary"></i> <?= htmlspecialchars($contact_phone) ?>
                </a>
                
                <!-- Email Address -->
                <a href="mailto:<?= htmlspecialchars($contact_email) ?>" class="en-font flex items-center gap-1.5 hover:text-secondary transition">
                    <i class="fa-solid fa-envelope text-secondary"></i> <?= htmlspecialchars($contact_email) ?>
                </a>
            </div>

            <!-- Right: License -->
            <div class="flex items-center text-[11px] md:text-xs mt-0.5 md:mt-0 opacity-90">
                <span class="flex items-center gap-1.5 en-font">
                    <i class="fa-solid fa-file-contract text-secondary"></i> License: <?= htmlspecialchars($trade_license) ?>
                </span>
            </div>
            
        </div>
    </div>

    <!-- Main Header (Sticky) -->
    <header class="bg-white shadow-sm sticky top-0 z-50">
        <div class="container mx-auto px-4 py-3">
            
            <!-- Row 1: Logo, Menu, Cart -->
            <div class="flex justify-between items-center">
                
                <!-- Left: Menu & Logo -->
                <div class="flex items-center gap-3">
                    <!-- Mobile Menu Button -->
                    <button id="mobileMenuBtn" class="lg:hidden text-gray-700 text-2xl focus:outline-none mr-2">
                        <i class="fa-solid fa-bars"></i>
                    </button>

                    <!-- Stylish Logo -->
                    <a href="index.php" class="text-2xl md:text-3xl flex items-center gap-1 hover:scale-105 transition transform duration-300">
                        <span class="text-primary logo-font tracking-wide text-3xl md:text-4xl">Izhaan</span>
                        <span class="text-secondary en-font font-bold text-xl md:text-2xl mt-1">Printers</span>
                    </a>
                </div>

                <!-- Center: Desktop Search -->
                <div class="hidden md:flex flex-1 max-w-lg mx-8">
                    <form action="search.php" method="GET" class="w-full flex items-center bg-gray-100 rounded-full border border-gray-200 focus-within:border-primary focus-within:ring-1 focus-within:ring-primary transition-all overflow-hidden">
                        <input type="text" name="q" placeholder="কি প্রিন্ট করতে চান?..." class="bg-transparent px-5 py-2 w-full text-sm font-bangla outline-none text-gray-700">
                        <button type="submit" class="bg-primary text-white px-6 py-2.5 hover:bg-blue-800 transition">
                            <i class="fa-solid fa-magnifying-glass"></i>
                        </button>
                    </form>
                </div>

                <!-- Right: Desktop Menu & Cart -->
                <div class="flex items-center gap-5">
                    <!-- Desktop Nav Links -->
                    <nav class="hidden lg:flex space-x-6 font-medium text-gray-600 items-center text-sm">
                        <a href="index.php" class="hover:text-primary transition font-bangla">হোম</a>
                        <a href="category.php" class="hover:text-primary transition font-bangla">ক্যাটাগরি</a>
                        
                        <!-- Dynamic Account Link -->
                        <a href="<?= $account_link ?>" class="hover:text-primary transition font-bangla font-bold text-primary">
                            <?= $account_text ?>
                        </a>
                        
                        <a href="contact.php" class="hover:text-primary transition font-bangla">যোগাযোগ</a>
                    </nav>

                    <!-- Cart Icon -->
                    <a href="cart.php" class="relative text-gray-600 hover:text-primary transition flex items-center gap-1">
                        <div class="bg-gray-100 p-2 rounded-full hover:bg-blue-50 transition">
                            <i class="fa-solid fa-cart-shopping text-xl"></i>
                        </div>
                        <?php $cart_count = isset($_SESSION['cart']) ? count($_SESSION['cart']) : 0; ?>
                        <span class="absolute -top-1 -right-1 bg-secondary text-white text-[10px] font-bold h-5 w-5 flex items-center justify-center rounded-full en-font shadow-sm border-2 border-white">
                            <?= $cart_count ?>
                        </span>
                    </a>
                </div>
            </div>

            <!-- Row 2: Mobile Search Bar -->
            <div class="mt-3 md:hidden">
                <form action="search.php" method="GET" class="flex items-center bg-gray-100 rounded-lg border border-gray-200 focus-within:border-secondary transition-all overflow-hidden">
                    <div class="pl-3 text-gray-400"><i class="fa-solid fa-magnifying-glass"></i></div>
                    <input type="text" name="q" placeholder="প্রোডাক্ট খুঁজুন..." class="bg-transparent px-3 py-2.5 w-full text-sm font-bangla outline-none text-gray-700">
                </form>
            </div>
        </div>
    </header>

    <!-- Mobile Sidebar (Drawer) -->
    <div id="sidebarOverlay" class="fixed inset-0 bg-black bg-opacity-50 z-[60] hidden transition-opacity"></div>
    
    <div id="mobileSidebar" class="fixed inset-y-0 left-0 w-72 bg-white z-[70] transform -translate-x-full shadow-2xl overflow-y-auto">
        <div class="p-5 flex flex-col h-full">
            <!-- Sidebar Header -->
            <div class="flex justify-between items-center mb-6 border-b pb-4">
                <!-- Sidebar Logo -->
                <span class="text-2xl flex items-center gap-1">
                    <span class="text-primary logo-font">Izhaan</span><span class="text-secondary en-font font-bold text-lg mt-1">Printers</span>
                </span>
                <button id="closeSidebarBtn" class="w-8 h-8 flex items-center justify-center bg-gray-100 rounded-full text-gray-500 hover:text-red-500 hover:bg-red-50 transition">
                    <i class="fa-solid fa-times"></i>
                </button>
            </div>

            <!-- Menu Links -->
            <nav class="flex flex-col space-y-1 font-bangla text-base">
                <a href="index.php" class="flex items-center gap-3 px-3 py-3 rounded-lg hover:bg-blue-50 text-gray-700 hover:text-primary transition font-medium">
                    <i class="fa-solid fa-house w-5 text-gray-400"></i> হোম
                </a>
                
                <a href="cart.php" class="flex items-center gap-3 px-3 py-3 rounded-lg hover:bg-blue-50 text-gray-700 hover:text-primary transition font-medium">
                    <i class="fa-solid fa-cart-shopping w-5 text-gray-400"></i> কার্ট
                </a>
                
                <!-- Dynamic Account Link For Mobile -->
                <a href="<?= $account_link ?>" class="flex items-center gap-3 px-3 py-3 rounded-lg hover:bg-blue-50 text-primary transition font-bold">
                    <i class="fa-solid fa-user w-5 text-primary"></i> <?= $account_text ?>
                </a>

              
            </nav>

            <!-- Bottom Contact Info -->
            <div class="mt-auto pt-6 border-t bg-gray-50 -mx-5 px-5 pb-5">
                <p class="text-xs text-gray-500 mb-2 font-bangla mt-4">অর্ডারের জন্য কল করুন</p>
                <a href="tel:<?= htmlspecialchars($contact_phone) ?>" class="flex items-center gap-3 text-primary font-bold en-font text-lg bg-white p-3 rounded-lg border border-gray-200 shadow-sm mb-3">
                    <div class="w-8 h-8 bg-primary text-white rounded-full flex items-center justify-center text-sm"><i class="fa-solid fa-phone"></i></div>
                    <?= htmlspecialchars($contact_phone) ?>
                </a>
                <!-- Email Also added to sidebar bottom -->
                <a href="mailto:<?= htmlspecialchars($contact_email) ?>" class="flex items-center gap-3 text-gray-600 font-medium en-font text-sm bg-white p-3 rounded-lg border border-gray-200 shadow-sm">
                    <div class="w-8 h-8 bg-gray-100 text-secondary rounded-full flex items-center justify-center text-sm"><i class="fa-solid fa-envelope"></i></div>
                    <span class="truncate"><?= htmlspecialchars($contact_email) ?></span>
                </a>
            </div>
        </div>
    </div>
    
    <!-- Mobile Menu Toggle Script -->
    <script>
        const menuBtn = document.getElementById('mobileMenuBtn');
        const sidebar = document.getElementById('mobileSidebar');
        const overlay = document.getElementById('sidebarOverlay');
        const closeBtn = document.getElementById('closeSidebarBtn');

        function openSidebar() {
            sidebar.classList.remove('-translate-x-full');
            overlay.classList.remove('hidden');
        }

        function closeSidebar() {
            sidebar.classList.add('-translate-x-full');
            overlay.classList.add('hidden');
        }

        if(menuBtn) menuBtn.addEventListener('click', openSidebar);
        if(closeBtn) closeBtn.addEventListener('click', closeSidebar);
        if(overlay) overlay.addEventListener('click', closeSidebar);
    </script>