<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<?php /* @var $block \Magento\Downloadable\Block\Catalog\Product\Links */ ?>
<?php $_linksPurchasedSeparately = $block->getLinksPurchasedSeparately(); ?>
<?php if ($block->getProduct()->isSaleable() && $block->hasLinks()) :?>
    <?php $_links = $block->getLinks(); ?>
    <?php $_linksLength = 0; ?>
    <?php $_isRequired = $block->getLinkSelectionRequired(); ?>
    <div class="field _mg-v-20 downloads<?php if ($_isRequired) { echo ' required'; } ?><?php if (!$_linksPurchasedSeparately) { echo ' downloads-no-separately'; } ?>">
        <label class="label _text-strong"><span><?= $block->escapeHtml($block->getLinksTitle()) ?></span></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"
                                [checked]="downloadable_links_select_all == true"
                               <?php if ($_isRequired) : ?>data-validate="{'validate-one-checkbox-required-by-name':'downloadable-links-list'}" <?php endif; ?>
                               name="links[]"
                               id="links_<?= $block->escapeHtmlAttr($_link->getId()) ?>"
                               value="<?= $block->escapeHtmlAttr($_link->getId()) ?>" <?= $block->escapeHtml($block->getLinkCheckedValue($_link)) ?> />
                    <?php endif; ?>
                    <label class="label" for="links_<?= $block->escapeHtmlAttr($_link->getId()) ?>">
                        <span><?= $block->escapeHtml($_link->getTitle()) ?></span>
                        <?php if ($_link->getSampleFile() || $_link->getSampleUrl()) : ?>
                            <a class="sample link"
                               href="<?= $block->escapeUrl($block->getLinkSampleUrl($_link)) ?>" <?= $block->getIsOpenInNewWindow() ? 'target="_blank"' : '' ?>>
                                <?= $block->escapeHtml(__('sample')) ?>
                            </a>
                        <?php endif; ?>
                        <?php if ($_linksPurchasedSeparately) : ?>
                            <?= /* @noEscape */ $block->getLinkPrice($_link) ?>
                        <?php endif; ?>
                    </label>
                </div>
            <?php endforeach; ?>
            <?php if ($_linksPurchasedSeparately && $_linksLength > 1) : ?>
                <div class="field choice downloads-all">
                    <input type="checkbox"
                           on="change:AMP.setState({downloadable_links_select_all: !downloadable_links_select_all, downloads_all_text: (downloadable_links_select_all ? 
                            '<?= $block->escapeHtmlAttr(__('Select all')) ?>' : '<?= $block->escapeHtmlAttr(__('Unselect all')) ?>')})"
                           id="links_all" />
                    <label class="label" for="links_all" [text]="downloads_all_text ? downloads_all_text : '<?= $block->escapeHtml(__('Select all')) ?>'"><span><?= $block->escapeHtml(__('Select all')) ?></span></label>
                </div>
            <?php endif; ?>
        </div>
        <?php if ($_isRequired) : ?>
            <span id="links-advice-container"></span>
        <?php endif;?>
    </div>
<?php endif; ?>
