<? $advertisers = $args['advertiser']; ?>

<?
$extensionToClassMap = [
    'pdf' => 'advert-item__icon_pdf',
    'docx' => 'advert-item__icon_doc',
    'doc' => 'advert-item__icon_doc',
];
?>
<? if ($advertisers->have_posts()) : ?>
    <? while ($advertisers->have_posts()) : ?>

        <div class="advert-item">
            <div class="advert-item__block">
                <?
                $advertisers->the_post();
                the_content();
                $documents = get_field('documents');
                $documents = array_filter($documents, function($item) {
                    return isset($item['document']) && $item['document'] !== false;
                });
                $contacts = get_field('contacts');
                ?>
            </div>
            <? if ($documents) : ?>
                <div class="advert-item__block">
                    <h2 class="advert-item__block-title_margin-lg subtitle-18">Документы</h2>
                    <div class="advert-item__documents-list">
                        <? foreach ($documents as $document) : ?>
                            <? if (!empty($document['document'])) : ?>
                                <? $extension = pathinfo($document['document']['url'] ?? '', PATHINFO_EXTENSION); ?>
                                <a href="<?= $document['document']['url'] ?>" target="_blank" class="advert-item__document">
                                    <span class="advert-item__icon <?= $extensionToClassMap[$extension] ?? '' ?>"></span>
                                    <span class="advert-item__document-description text-13"><?= $document['document']['title'] ?></span>
                                </a>
                            <? endif; ?>
                        <? endforeach; ?>
                    </div>
                </div>
            <? endif; ?>
            <div class="advert-item__block">
                <h2 class="advert-item__block-title_margin-lg subtitle-18">Контакты</h2>
                <div class="advert-item__contacts">
                    <? if ($contacts) : ?>
                        <? foreach ($contacts as $contact) : ?>
                            <div class="advert-item__contact-item">
                                <div class="advert-item__avatar-container"><img src="<?= $contact['image']['url'] ?>" alt="" class="advert-item__contacts-avatar"></div>
                                <div class="advert-item__contact-info">
                                    <p class="advert-item__contact-name subtitle-13"><?= $contact['name'] ?></p>
                                    <p class="advert-item__contact-position text-13"><?= $contact['position'] ?></p>
                                    <p class="advert-item__contact-phone subtitle-13">Тел: <a href="tel:79263663700"> +7 (926) 366 37 00</a></p>
                                    <p class="advert-item__contact-email text-13"><a href="mailto:pr@agroexpert.press">pr@agroexpert.press</a></p>
                                </div>
                            </div>
                        <? endforeach; ?>
                    <? endif; ?>
                </div>
            </div>
        </div>
    <? endwhile; ?>
<? endif; ?>