<?php
if (!isset($conn)) require_once __DIR__.'/bootstrap.php';
$drawer_cats = mysqli_query($conn,"SELECT * FROM categories WHERE status=1 AND (parent_id=0 OR parent_id IS NULL) ORDER BY sort_order ASC,id ASC LIMIT 18");
$home_parent = mysqli_query($conn,"SELECT id FROM categories WHERE (name_en LIKE '%Home Appliances%' OR name_ar LIKE '%الأجهزة المنزلية%') AND (parent_id=0 OR parent_id IS NULL) LIMIT 1");
$home_parent_id = 0;
if($home_parent && mysqli_num_rows($home_parent)){ $tmp=mysqli_fetch_assoc($home_parent); $home_parent_id=(int)$tmp['id']; }
$home_children = $home_parent_id ? mysqli_query($conn,"SELECT * FROM categories WHERE status=1 AND parent_id=$home_parent_id ORDER BY sort_order ASC,id ASC") : false;
$drawer_brands = mysqli_query($conn,"SELECT * FROM brands ORDER BY exclusive DESC,sort_order ASC,id ASC LIMIT 10");
$flag = ($lang=='ar') ? '🇮🇶' : (($lang=='ku') ? '🇹🇯' : '🇬🇧');
$langName = ($lang=='ar') ? 'AR' : (($lang=='ku') ? 'KU' : 'EN');
$ph = array(
  'ar'=>array('ابحث عن مروحة','ابحث عن تلفاز','ابحث عن خلاط','ابحث عن لابتوب','ابحث عن مبردة'),
  'en'=>array('Search fan','Search TV','Search mixer','Search laptop','Search air cooler'),
  'ku'=>array('گەڕان بۆ فەن','گەڕان بۆ تەلەفزیۆن','گەڕان بۆ میکسەر','گەڕان بۆ لاپتۆپ','گەڕان بۆ کولەر')
);
$placeholders = isset($ph[$lang]) ? $ph[$lang] : $ph['en'];
?>
<!DOCTYPE html>
<html lang="<?php echo e($lang); ?>" dir="<?php echo e($dir); ?>">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>Tariq Alagha - Home of International Brands</title>
<meta name="description" content="Tariq Alagha online electronics shopping in Iraq">
<link rel="manifest" href="manifest.webmanifest">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Alexandria:wght@300;400;500;600;700;800;900&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/style.css?v=30">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
</head>
<body class="<?php echo is_rtl($lang)?'rtl':'ltr'; ?>">
<header class="site-header electro-header">
  <div class="mobile-head container">
    <button class="round-menu" id="mobileMenuBtn" type="button" aria-label="Menu"><i class="fa-solid fa-bars"></i></button>
    <a class="logo-svg" href="index.php?lang=<?php echo e($lang); ?>"><span>TARIQ ALAGHA</span><small>HOME OF INTERNATIONAL BRANDS</small></a>
    <a class="cart-icon" href="cart.php?lang=<?php echo e($lang); ?>"><i class="fa-solid fa-cart-shopping"></i><b id="cartCountMobile"><?php echo cart_count(); ?></b></a>
  </div>
  <div class="container desktop-head">
    <a class="logo-svg" href="index.php?lang=<?php echo e($lang); ?>"><span>TARIQ ALAGHA</span><small>HOME OF INTERNATIONAL BRANDS</small></a>
    <form class="search-box" action="search.php" method="get">
      <i class="fa-solid fa-magnifying-glass"></i>
      <input id="smartSearchInput" data-placeholders="<?php echo e(implode('|',$placeholders)); ?>" type="text" name="q" value="<?php echo isset($_GET['q'])?e($_GET['q']):''; ?>" placeholder="<?php echo e(t('search_placeholder')); ?>">
      <input type="hidden" name="lang" value="<?php echo e($lang); ?>">
      <button><i class="fa-solid fa-magnifying-glass"></i><?php echo e(t('search')); ?></button>
    </form>
    <nav class="head-actions">
      <?php if(isset($_SESSION['user_id'])): ?>
        <a href="account.php?lang=<?php echo e($lang); ?>"><?php echo e(t('profile')); ?></a>
        <a href="logout.php"><?php echo e(t('logout')); ?></a>
      <?php else: ?>
        <a href="login.php?lang=<?php echo e($lang); ?>"><?php echo e(t('login_signup')); ?></a>
      <?php endif; ?>
      <a href="contact.php?lang=<?php echo e($lang); ?>"><?php echo e(t('need_help')); ?></a>
      <a class="heart" href="account.php?lang=<?php echo e($lang); ?>"><i class="fa-regular fa-heart"></i></a>
      <a class="cart-icon" href="cart.php?lang=<?php echo e($lang); ?>"><i class="fa-solid fa-cart-shopping"></i><b id="cartCountTop"><?php echo cart_count(); ?></b></a>
      <details class="language-dropdown" id="languageDropdown">
        <summary class="language-current" aria-label="Change language">
          <span class="flag-circle"><?php echo e($flag); ?></span>
          <span><?php echo e($langName); ?></span>
          <i class="fa-solid fa-chevron-down"></i>
        </summary>
        <div class="language-menu">
          <a href="<?php echo e(lang_url('en')); ?>"><span>🇬🇧</span> English</a>
          <a href="<?php echo e(lang_url('ar')); ?>"><span>🇮🇶</span> العربية</a>
          <a href="<?php echo e(lang_url('ku')); ?>"><span>🇹🇯</span> کوردی</a>
        </div>
      </details>
    </nav>
  </div>
</header>
<nav class="main-nav">
  <div class="container nav-inner">
    <button class="shop-all" id="catMenuBtn" type="button"><i class="fa-solid fa-bars"></i><?php echo e(t('shop_all')); ?></button>
    <a href="brands.php?lang=<?php echo e($lang); ?>"><?php echo e(t('brands')); ?></a>
    <a href="new-arrivals.php?lang=<?php echo e($lang); ?>"><?php echo e(t('new_arrivals')); ?></a>
    <a href="best-sellers.php?lang=<?php echo e($lang); ?>"><?php echo e(t('best_sellers')); ?></a>
    <a class="hot-link" href="offers.php?lang=<?php echo e($lang); ?>"><?php echo e(t('hot_deals')); ?></a>
  </div>
</nav>
<aside class="side-drawer electro-drawer" id="sideDrawer">
  <button class="drawer-close" type="button" id="drawerClose">×</button>
  <div class="drawer-grid">
    <div class="drawer-col main-cats">
      <h3><?php echo e(t('categories')); ?></h3>
      <?php if($drawer_cats): while($c=mysqli_fetch_assoc($drawer_cats)): ?>
        <a href="category.php?id=<?php echo (int)$c['id']; ?>&lang=<?php echo e($lang); ?>"><?php echo e(category_name($c,$lang)); ?><span>›</span></a>
      <?php endwhile; endif; ?>
    </div>
    <div class="drawer-col sub-cats">
      <h3><?php echo is_rtl($lang)?'الأجهزة المنزلية':'Home Appliances'; ?></h3>
      <?php if($home_children && mysqli_num_rows($home_children)): while($sc=mysqli_fetch_assoc($home_children)): ?>
        <a href="category.php?id=<?php echo (int)$sc['id']; ?>&lang=<?php echo e($lang); ?>"><?php echo e(category_name($sc,$lang)); ?></a>
      <?php endwhile; else: ?>
        <a href="category.php?id=<?php echo (int)$home_parent_id; ?>&lang=<?php echo e($lang); ?>"><?php echo is_rtl($lang)?'كل الأجهزة المنزلية':'All Home Appliances'; ?></a>
      <?php endif; ?>
    </div>
    <div class="drawer-col brand-cats">
      <h3><?php echo e(t('brands')); ?></h3>
      <?php if($drawer_brands): while($b=mysqli_fetch_assoc($drawer_brands)): ?>
        <a href="brand.php?id=<?php echo (int)$b['id']; ?>&lang=<?php echo e($lang); ?>"><?php echo e($b['name']); ?></a>
      <?php endwhile; endif; ?>
    </div>
  </div>
</aside>
<div class="drawer-backdrop" id="drawerBackdrop"></div>
