<!--
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_MobikulCore
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
-->

<div class="page-main-actions">
    <div class="page-actions" data-ui-id="page-actions-toolbar-content-header">
        <div class="page-actions-inner" data-title="Joust Duffle Bag">
            <div class="page-actions-buttons">
                <button
                    onclick="printBarcode()"
                    id="print"
                    title="Print"
                    type="button"
                    class="action- scalable action-secondary">
                    <span>&#128438;</span>
                    <?= $block->escapeHtml(__("Print"));?>
                </button>
                <a
                    href="<?= $block->escapeUrl($block->getUrl('catalog/product/index')) ?>"
                    id="back"
                    title="Back"
                    class="action-scalable action-primary">
                    <span>&#8592;</span>
                    <?= /* @noEscape*/ __("Back");?>
                </a>
            </div>
        </div>
    </div>
</div>

<?php
    $barcodeAttribute  = 'sku';
    $productCollection = $block->_filter->getCollection($block->_collectionFactory->create());
    $productCollection->addAttributeToSelect("name");
if ($barcodeAttribute != "sku") {
    $productCollection->addAttributeToSelect($barcodeAttribute);
}
    $target = $block->_storeManager->getStore()
        ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)."/"."barcode"."/"."product"."/";
    $targetDir = $block->_directoryList->getPath("media");
?>

<div id="printable">
    <table>
        <tbody>
            <?php
            foreach ($productCollection as $product) {
                $fileName = str_replace([" ", "#", "/"], "_", $product->getData($barcodeAttribute)).".png";
                $basePath = $targetDir."/"."barcode"."/"."product"."/".$product->getId()."/";
                if (!$block->fileDriver->isFile($basePath.$fileName)) {
                    $block->_ioFile->mkdir($basePath, 0777);
                    $block->_helperBarcode->generatebarcode(
                        $basePath.$fileName,
                        $product->getData($barcodeAttribute),
                        20,
                        "horizontal",
                        "code128",
                        false,
                        1
                    );
                }
                echo '<tr>';
                echo '<td>'.$block->escapeHtml($product->getId()).'</td>';
                echo '<td>'.$block->escapeHtml($product->getName()).'</td>';
                echo '<td>'.$block->escapeHtml($product->getData($barcodeAttribute)).'</td>';
                echo '<td>
                    <img src="'.$block->escapeHtml($target.$product->getId())."/".$block->escapeHtml($fileName).'"/>
                    </td>';
                echo '</tr>';
            }
            if (!count($productCollection)) {
                echo '<tr><td colspan="4">'.$block->escapeHtml(__("No BarCode Found")).'</td></tr>';
            }
            ?>
        </tbody>
    </table>
</div>
