<?php
// Home page with featured store products at the bottom if ENABLE_STORE is true
// Based on home_default.php
?>

<div class="container py-2">
    <div class="jumbotron">
        <h4 class="display-6"><?php echo htmlspecialchars(SITE_WELCOME, ENT_QUOTES, 'UTF-8'); ?></h4>
        <hr class="my-4">
        <p><?php echo htmlspecialchars(SITE_WELCOME_DETAILS, ENT_QUOTES, 'UTF-8'); ?></p>
        <?php if (defined('ALERT_MESSAGE') && ALERT_MESSAGE !== ''): ?>
            <div class="alert alert-warning" role="alert">
                <?php echo htmlspecialchars(ALERT_MESSAGE, ENT_QUOTES, 'UTF-8'); ?></div>
        <?php endif; ?>
    </div>
</div>

<div class="row g-4 py-2">
    <div class="col-md-4">
        <a href="<?php echo (isLoggedIn() || ALLOW_GUEST_TICKETS) ? getCleanUrl('ticket_create') : getCleanUrl('login'); ?>"
            class="card-link">
            <div class="card h-100">
                <div class="card-body text-center">
                    <i class="bi bi-ticket-detailed text-primary" style="font-size: 3rem;"></i>
                    <h3 class="card-title mt-3">
                        <?php echo $lang_frontend['HOME_SUBMIT_TICKET_TITLE'] ?? 'Submit a Ticket'; ?>
                    </h3>
                    <p class="card-text">
                        <?php echo $lang_frontend['HOME_SUBMIT_TICKET_DESC'] ?? "Submit a support ticket and we'll get back to you."; ?>
                        <?php if (!isLoggedIn() && !ALLOW_GUEST_TICKETS): ?>
                            <br><small
                                class="text-muted"><?php echo $lang_frontend['HOME_LOGIN_REQUIRED_NOTE'] ?? '(Login required)'; ?></small>
                        <?php endif; ?>
                    </p>
                </div>
            </div>
        </a>
    </div>

    <div class="col-md-4">
        <a href="<?php echo (isLoggedIn() || ALLOW_GUEST_TICKETS) ? getCleanUrl('tickets') : getCleanUrl('login'); ?>"
            class="card-link">
            <div class="card h-100">
                <div class="card-body text-center">
                    <i class="bi bi-list-check text-primary" style="font-size: 3rem;"></i>
                    <h3 class="card-title mt-3">
                        <?php echo $lang_frontend['HOME_VIEW_TICKETS_TITLE'] ?? 'View Your Tickets'; ?>
                    </h3>
                    <p class="card-text">
                        <?php echo $lang_frontend['HOME_VIEW_TICKETS_DESC'] ?? 'Check the status of your support tickets.'; ?>
                        <?php if (!isLoggedIn() && !ALLOW_GUEST_TICKETS): ?>
                            <br><small
                                class="text-muted"><?php echo $lang_frontend['HOME_LOGIN_REQUIRED_NOTE'] ?? '(Login required)'; ?></small>
                        <?php endif; ?>
                    </p>
                </div>
            </div>
        </a>
    </div>
    <?php if (defined('ENABLE_STORE') && ENABLE_STORE): ?>
        <div class="col-md-4">
            <a href="<?php echo getCleanUrl('store'); ?>" class="card-link">
                <div class="card h-100">
                    <div class="card-body text-center">
                        <i class="bi bi-cart text-primary" style="font-size: 3rem;"></i>
                        <h3 class="card-title mt-3"><?php echo $lang_frontend['PAGE_TITLE_STORE'] ?? 'Store'; ?></h3>
                        <p class="card-text">
                            <?php echo $lang_frontend['STORE_SUBHEADING_DISCOVER'] ?? 'Discover our products and service'; ?>
                        </p>
                    </div>
                </div>
            </a>
        </div>
    <?php endif; ?>
    <?php if (defined('ENABLE_PROJECTS') && ENABLE_PROJECTS): ?>
        <div class="col-md-4">
            <a href="<?php echo getCleanUrl('projects'); ?>" class="card-link">
                <div class="card h-100">
                    <div class="card-body text-center">
                        <i class="bi bi-check-all text-primary" style="font-size: 3rem;"></i>
                        <h3 class="card-title mt-3"><?php echo $lang_frontend['HOME_PROJECTS_TITLE'] ?? 'Projects'; ?></h3>
                        <p class="card-text">
                            <?php echo $lang_frontend['HOME_PROJECTS_DESC'] ?? 'View your active projects and tasks.'; ?>
                        </p>
                    </div>
                </div>
            </a>
        </div>
    <?php endif; ?>


    <?php if (defined('ENABLE_FAQS') && ENABLE_FAQS): ?>
        <div class="col-md-4">
            <a href="<?php echo getCleanUrl('faq'); ?>" class="card-link">
                <div class="card h-100">
                    <div class="card-body text-center">
                        <i class="bi bi-question-circle text-primary" style="font-size: 3rem;"></i>
                        <h3 class="card-title mt-3"><?php echo $lang_frontend['HOME_FAQ_TITLE'] ?? 'FAQ'; ?></h3>
                        <p class="card-text">
                            <?php echo $lang_frontend['HOME_FAQ_DESC'] ?? 'Find answers to frequently asked questions.'; ?>
                        </p>
                    </div>
                </div>
            </a>
        </div>
    <?php endif; ?>

    <?php if (!isLoggedIn()): ?>
        <?php if (ALLOW_REGISTER): ?>
            <div class="col-md-4">
                <a href="<?php echo getCleanUrl('register'); ?>" class="card-link">
                    <div class="card h-100">
                        <div class="card-body text-center">
                            <i class="bi bi-person-plus text-primary" style="font-size: 3rem;"></i>
                            <h3 class="card-title mt-3">
                                <?php echo $lang_frontend['HOME_CREATE_ACCOUNT_TITLE'] ?? 'Create an Account'; ?>
                            </h3>
                            <p class="card-text">
                                <?php echo $lang_frontend['HOME_CREATE_ACCOUNT_DESC'] ?? 'Register to access all features.'; ?>
                            </p>
                        </div>
                    </div>
                </a>
            </div>
        <?php endif; ?>
    <?php endif; ?>


    <?php if (defined('ENABLE_CONTACT') && ENABLE_CONTACT): ?>
        <div class="col-md-4">
            <a href="<?php echo getCleanUrl('contact'); ?>" class="card-link">
                <div class="card h-100">
                    <div class="card-body text-center">
                        <i class="bi bi-envelope text-primary" style="font-size: 3rem;"></i>
                        <h3 class="card-title mt-3"><?php echo $lang_frontend['HOME_CONTACT_US_TITLE'] ?? 'Contact Us'; ?>
                        </h3>
                        <p class="card-text">
                            <?php echo $lang_frontend['HOME_CONTACT_US_DESC'] ?? 'Get in touch with our team.'; ?>
                        </p>
                    </div>
                </div>
            </a>
        </div>
    <?php endif; ?>
</div>

<?php if (defined('ENABLE_STORE') && ENABLE_STORE): ?>
    <?php
    // Fetch featured products
    try {
        $stmt = $pdo->prepare("SELECT p.*, pc.name as category_name FROM products p LEFT JOIN product_categories pc ON p.category_id = pc.id WHERE p.is_active = 1 AND p.hide_from_store = 0 AND p.is_featured = 1 ORDER BY p.sort_order, p.name LIMIT 8");
        $stmt->execute();
        $featured_products = $stmt->fetchAll();
        $currency_info = get_active_currency();
        $currency_symbol = $currency_info['symbol'];
    } catch (Exception $e) {
        $featured_products = [];
    }
    ?>
    <?php if (!empty($featured_products)): ?>
        <div class="container py-5">
            <h2 class="mb-4 text-center"><?php echo $lang_frontend['HOME_FEATURED_PRODUCTS_HEADING'] ?? 'Featured Products'; ?>
            </h2>
            <div class="row">
                <?php foreach ($featured_products as $product): ?>
                    <div class="col-lg-4 col-md-4 col-sm-12 mb-4">
                        <div class="card product-card h-100">
                            <div class="product-image" style="background-image: url('<?php
                            if (!empty($product['image'])) {
                                $img = htmlspecialchars($product['image']);
                                if (strpos($img, 'uploads/products/') === 0) {
                                    echo BASE_URL . '/' . $img;
                                } else {
                                    echo BASE_URL . '/uploads/products/' . $img;
                                }
                            } else {
                                echo BASE_URL . '/assets/store.png';
                            }
                            ?>'); height: 180px; background-size: cover; background-position: center;"></div>
                            <div class="card-body d-flex flex-column">
                                <div class="mb-2">
                                    <small class="text-muted">
                                        <i
                                            class="bi bi-<?php echo $product['type'] === 'physical' ? 'box' : ($product['type'] === 'digital' ? 'download' : 'arrow-repeat'); ?> me-1"></i>
                                        <?php echo ucfirst($product['type']); ?>
                                        <?php if ($product['category_name']): ?>
                                            • <?php echo htmlspecialchars($product['category_name']); ?>
                                        <?php endif; ?>
                                    </small>
                                </div>
                                <h6 class="card-title"><?php echo htmlspecialchars($product['name']); ?></h6>
                                <?php if ($product['short_description']): ?>
                                    <p class="card-text text-muted small"><?php echo htmlspecialchars($product['short_description']); ?>
                                    </p>
                                <?php endif; ?>
                                <div class="mt-auto">
                                    <div class="price-display mb-2">
                                        <?php echo $currency_symbol; ?>             <?php echo number_format($product['price'], 2); ?>
                                    </div>
                                    <a href="<?php echo getCleanUrl('product/' . $product['id']); ?>"
                                        class="btn btn-outline-primary btn-sm w-100">
                                        <i
                                            class="bi bi-eye me-1"></i><?php echo $lang_frontend['PRODUCT_CARD_BUTTON_VIEW'] ?? 'View'; ?>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    <?php endif; ?>
<?php endif; ?>