<?php
/**
 * @author Amasty Team
 * @copyright Copyright (c) Amasty (https://www.amasty.com)
 * @package Free Gift Base for Magento 2
 */
/**
 * @var $block Magento\Downloadable\Block\Catalog\Product\Links
 * @var $escaper Magento\Framework\Escaper
 * */
?>

<?php $_linksPurchasedSeparately = $block->getLinksPurchasedSeparately(); ?>
<?php if ($block->getProduct()->isSaleable() && $block->hasLinks()): ?>
    <?php $_links = $block->getLinks(); ?>
    <?php $_linksLength = 0; ?>
    <?php $_isRequired = $block->getLinkSelectionRequired(); ?>
    <legend class="legend links-title">
        <?= $escaper->escapeHtml($block->getLinksTitle()) ?>
    </legend>
    <br>
    <div class="field downloads<?php if ($_isRequired) {
        echo ' required';
                               } ?><?php if (!$_linksPurchasedSeparately) {
        echo ' downloads-no-separately';
                               } ?>">
        <label class="label">
            <?= $escaper->escapeHtml($block->getLinksTitle()) ?>
        </label>
        <div class="control" id="downloadable-links-list"
             data-container-for="downloadable-links">
            <?php foreach ($_links as $_link): ?>
                <?php $_linksLength++; ?>
                <div class="field choice" data-role="link">
                    <?php if ($_linksPurchasedSeparately): ?>
                        <input type="checkbox"
                            <?php if ($_isRequired): ?>
                                data-validate="{'validate-one-checkbox-required-by-name':'downloadable-links-list'}"
                            <?php endif; ?>
                               name="links[]"
                               id="links_<?= $escaper->escapeHtml($_link->getId()) ?>"
                               value="<?= $escaper->escapeHtml($_link->getId()) ?>"
                                      <?= $escaper->escapeHtml($block->getLinkCheckedValue($_link)) ?> />
                    <?php endif; ?>
                    <label class="label" for="links_<?= $escaper->escapeHtml($_link->getId()) ?>">
                        <?= $escaper->escapeHtml($_link->getTitle()) ?>
                        <?php if ($_link->getSampleFile() || $_link->getSampleUrl()): ?>
                            <a class="sample link"
                               title="<?=  $escaper->escapeHtml(__('sample')) ?>"
                               href="<?= $escaper->escapeUrl($block->getLinkSampleUrl($_link)) ?>"
                                <?= $block->getIsOpenInNewWindow() ? 'target="_blank"' : '' ?>>
                                <?=  $escaper->escapeHtml(__('sample')) ?>
                            </a>
                        <?php endif; ?>
                    </label>
                </div>
            <?php endforeach; ?>
            <?php if ($_linksPurchasedSeparately && $_linksLength > 1): ?>
                <div class="field choice downloads-all">
                    <input type="checkbox"
                           data-notchecked="<?= $escaper->escapeHtml(__('Select all')) ?>"
                           data-checked="<?= $escaper->escapeHtml(__('Unselect all')) ?>"
                           id="links_all" />
                    <label class="label" for="links_all">
                        <?= $escaper->escapeHtml(__('Select all')) ?>
                    </label>
                </div>
            <?php endif; ?>
        </div>
        <?php if ($_isRequired): ?>
            <span id="links-advice-container"></span>
        <?php endif;?>
    </div>
<?php endif; ?>
