<?php
require_once 'config.php';
require_once 'database.php';
require_once 'auth.php';
require_once 'functions.php';
require_once 'vendor-functions.php';

$auth = new Auth();

// Require vendor login
if (!$auth->isLoggedIn() || $_SESSION['user_role'] != 'vendor') {
    header('Location: ../user/login.php');
    exit();
}

$db = Database::getInstance();
$conn = $db->getConnection();
$user_id = $_SESSION['user_id'];

// Get vendor details
$vendor = VendorFunctions::getVendorByUserId($user_id);

if (!$vendor) {
    header('Location: ../vendor/register.php');
    exit();
}

if (!isset($page_title)) {
    $page_title = 'Vendor Dashboard';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $page_title . ' - ' . SITE_NAME; ?></title>
    
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome 6 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <!-- Custom CSS -->
    <?php
    $root_path = str_repeat('../', substr_count($_SERVER['PHP_SELF'], '/') - 1);
    ?>
    <link rel="stylesheet" href="<?php echo $root_path; ?>css/admin.css">
    <link rel="stylesheet" href="<?php echo $root_path; ?>css/responsive.css">
</head>
<body>
    <div class="admin-wrapper">
        <!-- Sidebar -->
        <nav class="admin-sidebar">
            <div class="sidebar-header">
                <div class="sidebar-logo">
                    <span class="logo-care">Care</span><span class="logo-world">world</span>
                </div>
                <div class="sidebar-tagline">Vendor Panel</div>
                <?php if($vendor['status'] == 'pending'): ?>
                <span class="sidebar-admin-badge" style="background: var(--warning);">Pending Approval</span>
                <?php elseif($vendor['status'] == 'active'): ?>
                <span class="sidebar-admin-badge" style="background: var(--success);">Active</span>
                <?php else: ?>
                <span class="sidebar-admin-badge" style="background: var(--danger);"><?php echo ucfirst($vendor['status']); ?></span>
                <?php endif; ?>
            </div>
            
            <ul class="sidebar-menu">
                <li class="<?php echo basename($_SERVER['PHP_SELF']) == 'dashboard.php' ? 'active' : ''; ?>">
                    <a href="dashboard.php"><i class="fas fa-dashboard"></i> Dashboard</a>
                </li>
                <li class="<?php echo basename($_SERVER['PHP_SELF']) == 'products.php' ? 'active' : ''; ?>">
                    <a href="products.php"><i class="fas fa-box"></i> My Products</a>
                </li>
                <li class="<?php echo basename($_SERVER['PHP_SELF']) == 'orders.php' ? 'active' : ''; ?>">
                    <a href="orders.php"><i class="fas fa-shopping-cart"></i> Orders</a>
                </li>
                <li class="<?php echo basename($_SERVER['PHP_SELF']) == 'earnings.php' ? 'active' : ''; ?>">
                    <a href="earnings.php"><i class="fas fa-dollar-sign"></i> Earnings</a>
                </li>
                <li class="<?php echo basename($_SERVER['PHP_SELF']) == 'profile.php' ? 'active' : ''; ?>">
                    <a href="profile.php"><i class="fas fa-store"></i> Store Profile</a>
                </li>
                <li class="<?php echo basename($_SERVER['PHP_SELF']) == 'settings.php' ? 'active' : ''; ?>">
                    <a href="settings.php"><i class="fas fa-cog"></i> Settings</a>
                </li>
                <li class="logout">
                    <a href="../logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a>
                </li>
            </ul>
            
            <div class="sidebar-footer">
                <div class="sidebar-user">
                    <div class="sidebar-user-avatar">
                        <?php echo strtoupper(substr($vendor['store_name'], 0, 1)); ?>
                    </div>
                    <div>
                        <div class="sidebar-user-name"><?php echo htmlspecialchars($vendor['store_name']); ?></div>
                        <div class="sidebar-user-role">Vendor</div>
                    </div>
                </div>
            </div>
        </nav>
        
        <main class="admin-main">
            <!-- Top Bar -->
            <div class="admin-topbar">
                <button class="topbar-toggle" id="sidebarToggle">
                    <i class="fas fa-bars"></i>
                </button>
                
                <div class="topbar-breadcrumb">
                    <a href="dashboard.php">Vendor</a>
                    <span class="sep">/</span>
                    <span class="current"><?php echo $page_title; ?></span>
                </div>
                
                <div class="topbar-search">
                    <input type="text" placeholder="Search...">
                </div>
                
                <div class="topbar-actions">
                    <button class="topbar-btn">
                        <i class="fas fa-bell"></i>
                        <span class="badge">3</span>
                    </button>
                    
                    <a href="profile.php" class="topbar-user">
                        <div class="topbar-user-avatar">
                            <?php echo strtoupper(substr($vendor['store_name'], 0, 1)); ?>
                        </div>
                        <span class="topbar-user-name"><?php echo htmlspecialchars($vendor['store_name']); ?></span>
                        <i class="fas fa-chevron-down topbar-user-arrow"></i>
                    </a>
                </div>
            </div>