<?php
/* 
Template Name: قائمة الطعام (Menu) 
*/

get_header(); ?>

<style>
/* تنسيق صورة التصنيف */
.menu-category {
    text-align: center;
    margin-bottom: 40px;
}

.menu-category img {
    width: 100%;
    height: auto;
    max-height: 400px; /* تحكم بارتفاع الصورة */
    object-fit: cover;
}

/* تنسيق قائمة الأطباق */
.menu-items {
    list-style: none;
    padding: 0;
    margin: 20px auto;
    max-width: 800px;
}

.menu-items li {
    padding: 15px;
    border-bottom: 1px solid #ddd;
}

.menu-items li h2 {
    font-size: 22px;
    margin: 5px 0;
}

.menu-items li .ingredients {
    font-size: 16px;
    color: #666;
}
</style>

<div class="menu-container">
    <?php
    // جلب كل التصنيفات الخاصة بالمنتجات
    $terms = get_terms(array(
        'taxonomy'   => 'product_cat',
        'hide_empty' => false, 
    ));

    foreach ($terms as $term) :
        $thumbnail_id = get_term_meta($term->term_id, 'thumbnail_id', true);
        $image_url = wp_get_attachment_url($thumbnail_id);
    ?>

        <!-- صورة التصنيف بحجم كامل -->
        <div class="menu-category">
            <?php if ($image_url) : ?>
                <img src="<?php echo esc_url($image_url); ?>" alt="<?php echo esc_attr($term->name); ?>">
            <?php endif; ?>
            <h1><?php echo esc_html($term->name); ?></h1>
        </div>

        <!-- جلب المنتجات داخل التصنيف -->
        <ul class="menu-items">
            <?php
            $products = new WP_Query(array(
                'post_type'      => 'product',
                'posts_per_page' => -1,
                'tax_query'      => array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field'    => 'term_id',
                        'terms'    => $term->term_id,
                    ),
                ),
            ));

            if ($products->have_posts()) :
                while ($products->have_posts()) : $products->the_post();
                    global $product;
                    $ingredients = get_post_meta(get_the_ID(), 'product_ingredients', true); // تأكد من أن لديك مكون "المكونات" مضافًا للمنتج
            ?>
                    <li>
                        <h2><?php the_title(); ?></h2>
                        <?php if ($ingredients) : ?>
                            <p class="ingredients"><?php echo esc_html($ingredients); ?></p>
                        <?php endif; ?>
                    </li>
            <?php
                endwhile;
                wp_reset_postdata();
            endif;
            ?>
        </ul>

    <?php endforeach; ?>
</div>

<?php get_footer(); ?>
