<?php
/**
 * Single company page.
 *
 * @package ALFASMART
 */

get_header();

while (have_posts()) :
    the_post();

    $post_id        = get_the_ID();
    $logo_url       = get_the_post_thumbnail_url($post_id, 'full') ?: alfasmart_img('group2.webp');
    $category       = alfasmart_get_field('company_category', alfasmart_text('Our Group', 'مجموعتنا'), $post_id);
    $hero_title     = alfasmart_get_field('company_hero_title', get_the_title(), $post_id);
    $website_url    = alfasmart_get_field('company_website', alfasmart_url('contact'), $post_id);
    $visit_text     = alfasmart_get_field('company_visit_button_text', alfasmart_text('Visit Website', 'زيارة الموقع'), $post_id);
    $hero_copy      = alfasmart_get_field('company_hero_description', get_the_excerpt(), $post_id);
    $video_file     = alfasmart_get_image_url(alfasmart_get_field('company_video_file', null, $post_id), 'video.mp4');
    $video_cover    = alfasmart_get_image_url(alfasmart_get_field('company_video_cover', null, $post_id), 'serv-video-cover.webp');
    $video_alt      = alfasmart_get_field('company_video_cover_alt', get_the_title(), $post_id);
    $services       = (array) alfasmart_get_field('company_services', array(), $post_id);
    $solution_items = (array) alfasmart_get_field('company_solutions_items', array(), $post_id);
    $solutions_img  = alfasmart_get_image_url(alfasmart_get_field('company_solutions_image', null, $post_id), 'serv-what-we-do.webp');
    $why_items      = (array) alfasmart_get_field('company_why_items', array(), $post_id);
    ?>
    <section class="as-service-detail-hero" aria-labelledby="as-company-detail-title">
        <div class="video-shape as-service-detail-video" aria-hidden="true">
            <video class="as-inner-hero-video" src="<?php echo esc_url(alfasmart_img('breadcrumb.mp4')); ?>" autoplay muted loop playsinline></video>
        </div>
        <div class="container as-section-container">
            <div class="row as-layout-row as-service-detail-hero-row">
                <div class="col-12 col-md-auto">
                    <figure class="as-service-logo-card as-reveal">
                        <img src="<?php echo esc_url($logo_url); ?>" alt="<?php echo esc_attr(get_the_title()); ?>">
                    </figure>
                </div>
                <div class="col-12 col-lg as-reveal">
                    <div class="as-service-detail-copy">
                        <p class="as-service-pill"><?php echo esc_html($category); ?></p>
                        <h1 id="as-company-detail-title"><?php echo esc_html($hero_title); ?></h1>
                        <?php if ($hero_copy) : ?>
                            <p><?php echo esc_html($hero_copy); ?></p>
                        <?php endif; ?>
                        <?php if ($website_url) : ?>
                            <a href="<?php echo esc_url($website_url); ?>" class="as-primary-button as-magnetic">
                                <?php echo esc_html($visit_text); ?> <i class="fa-solid fa-arrow-right" aria-hidden="true"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <section class="as-service-video-section" aria-label="<?php esc_attr_e('Company video', 'alfasmart'); ?>">
        <div class="container as-section-container">
            <a class="as-video-frame as-service-video-frame as-reveal" href="<?php echo esc_url($video_file); ?>" data-fancybox="company-video" data-type="html5video" aria-label="<?php esc_attr_e('Play company video', 'alfasmart'); ?>">
                <div class="serv-img-cover">
                    <img src="<?php echo esc_url($video_cover); ?>" alt="<?php echo esc_attr($video_alt); ?>">
                </div>
                <span class="as-play-button" aria-hidden="true"><i class="fa-solid fa-play"></i></span>
            </a>
        </div>
    </section>

    <?php if ($services) : ?>
        <section class="as-service-detail-services" aria-labelledby="as-company-services-title">
            <div class="container as-section-container">
                <div class="as-service-line-heading as-reveal">
                    <span aria-hidden="true"></span>
                    <h2 id="as-company-services-title"><?php echo esc_html(alfasmart_get_field('company_services_title', alfasmart_text('Our Services', 'خدماتنا'), $post_id)); ?></h2>
                </div>
                <div class="row as-card-row as-service-detail-grid">
                    <?php foreach ($services as $service) : ?>
                        <div class="col-12 col-lg-6">
                            <article class="as-service-detail-card as-reveal">
                                <?php alfasmart_render_icon($service['icon_type'] ?? 'icon', $service['icon'] ?? '', $service['icon_image'] ?? null, '', 'fa-solid fa-layer-group'); ?>
                                <h3><?php echo esc_html($service['title'] ?? ''); ?></h3>
                                <p><?php echo esc_html($service['description'] ?? ''); ?></p>
                            </article>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </section>
    <?php endif; ?>

    <section class="as-service-solutions-section" aria-labelledby="as-company-solutions-title">
        <div class="as-service-solutions-bg">
            <div class="container as-section-container">
                <div class="row as-service-solutions-row">
                    <div class="col-12 col-lg-6 as-service-solutions-copy as-reveal">
                        <p class="as-service-solutions-kicker"><?php echo esc_html(alfasmart_get_field('company_solutions_kicker', alfasmart_text('What We Do', 'ماذا نقدم'), $post_id)); ?></p>
                        <h2 id="as-company-solutions-title"><?php echo esc_html(alfasmart_get_field('company_solutions_title', '', $post_id)); ?></h2>
                        <p><?php echo esc_html(alfasmart_get_field('company_solutions_description', '', $post_id)); ?></p>
                        <?php if ($solution_items) : ?>
                            <div class="row as-service-solutions-list">
                                <?php foreach ($solution_items as $item) : ?>
                                    <div class="col-12 col-sm-6">
                                        <article class="as-service-solution-item">
                                            <span><img src="<?php echo esc_url(alfasmart_get_image_url($item['image'] ?? null, 'general-trading.webp')); ?>" alt=""></span>
                                            <h3><?php echo esc_html($item['title'] ?? ''); ?></h3>
                                            <p><?php echo esc_html($item['description'] ?? ''); ?></p>
                                        </article>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                        <?php if ($website_url) : ?>
                            <a href="<?php echo esc_url($website_url); ?>" class="as-yellow-button as-magnetic">
                                <?php echo esc_html($visit_text); ?> <i class="fa-solid fa-arrow-right" aria-hidden="true"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                    <div class="col-12 col-lg-6 as-service-solutions-media as-reveal">
                        <div class="why-serv-cover">
                            <img src="<?php echo esc_url($solutions_img); ?>" alt="<?php echo esc_attr(alfasmart_get_field('company_solutions_image_alt', '', $post_id)); ?>">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <section class="as-service-growth-section" aria-labelledby="as-company-expansion-title">
        <div class="container as-section-container">
            <div class="row as-layout-row as-service-growth-row">
                <div class="col-12 col-lg-6">
                    <div class="as-service-line-heading as-reveal">
                        <span aria-hidden="true"></span>
                        <h2 id="as-company-expansion-title"><?php echo esc_html(alfasmart_get_field('company_expansion_title', alfasmart_text('Expansion Plan', 'خطة التوسع'), $post_id)); ?></h2>
                    </div>
                    <article class="as-service-plan-card as-reveal">
                        <p><?php echo wp_kses_post(nl2br(esc_html(alfasmart_get_field('company_expansion_description', '', $post_id)))); ?></p>
                    </article>
                </div>
                <div class="col-12 col-lg-6">
                    <div class="as-service-line-heading as-reveal">
                        <span aria-hidden="true"></span>
                        <h2><?php echo esc_html(alfasmart_get_field('company_why_title', alfasmart_text('Why Choose Us', 'لماذا تختارنا'), $post_id)); ?></h2>
                    </div>
                    <?php if ($why_items) : ?>
                        <div class="as-service-choice-list">
                            <?php foreach ($why_items as $item) : ?>
                                <article class="as-service-choice-card as-reveal">
                                    <?php alfasmart_render_icon($item['icon_type'] ?? 'icon', $item['icon'] ?? '', $item['icon_image'] ?? null, '', 'fa-solid fa-bullseye'); ?>
                                    <p><?php echo esc_html($item['text'] ?? ''); ?></p>
                                </article>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </section>

    <section class="as-about-light-cta-section as-service-cta-panel" aria-labelledby="as-company-cta-title">
        <div class="container as-section-container">
            <div class="as-about-light-cta as-reveal">
                <h2 id="as-company-cta-title"><?php echo esc_html(alfasmart_get_field('company_cta_title', alfasmart_text('Let\'s Build the Future Together', 'لنبني المستقبل معاً'), $post_id)); ?></h2>
                <p><?php echo esc_html(alfasmart_get_field('company_cta_description', '', $post_id)); ?></p>
                <div>
                    <a href="<?php echo esc_url(alfasmart_get_field('company_cta_primary_url', alfasmart_url('contact'), $post_id)); ?>" class="as-primary-button as-magnetic">
                        <?php echo esc_html(alfasmart_get_field('company_cta_primary_text', alfasmart_text('Contact Us', 'اتصل بنا'), $post_id)); ?> <i class="fa-solid fa-arrow-right" aria-hidden="true"></i>
                    </a>
                    <a href="<?php echo esc_url(alfasmart_get_field('company_cta_secondary_url', get_post_type_archive_link('companies') ?: home_url('/companies/'), $post_id)); ?>" class="as-outline-button as-magnetic">
                        <?php echo esc_html(alfasmart_get_field('company_cta_secondary_text', alfasmart_text('Explore Our Companies', 'استكشف شركاتنا'), $post_id)); ?>
                    </a>
                </div>
            </div>
        </div>
    </section>
    <?php
endwhile;

get_footer();
