<?php if (!defined('ABSPATH')) exit; // Exit if accessed directly  
?>
<div class="nbd-header-with-form">
    <h2 class="nbd-title-page" style="display: flex;align-items: center;gap: 10px;">
        <?php esc_html_e('Manager Global Templates', 'web-to-print-online-designer'); ?>
        <a href="<?php echo esc_url(admin_url('edit-tags.php?taxonomy=template_tag&post_type=product')); ?>" target="_blank" class="button-primary"><?php esc_html_e('Template Tags', 'web-to-print-online-designer'); ?></a>
    </h2>
</div>
<div class="wrap postbox nbdesigner-manager-product">
    <div style="margin: 10px 0; display: flex; align-items: center; gap: 10px; flex-wrap: wrap;">
        <?php foreach ($cats as $cat):
            $link_cat = add_query_arg(array('cat_id' => $cat->id), admin_url('admin.php?page=nbd-templates'));
        ?>
            <a href="<?php echo esc_url($link_cat); ?>" class="button <?php echo (isset($cat_id) && $cat_id == $cat->id) ? 'button-primary' : ''; ?>"><?php esc_html_e($cat->name); ?></a>
        <?php endforeach; ?>
        <a href="<?php echo esc_url(admin_url('admin.php?page=nbd-templates')); ?>" class="button <?php echo (empty($cat_id)) ? 'button-primary' : ''; ?>"><?php esc_html_e('All', 'web-to-print-online-designer'); ?></a>
    </div>
    <?php if (!empty($templates->items) && is_array($templates->items) && count($templates->items)): ?>
        <div>
            <?php
            foreach ($templates->items as $key => $val):
            ?>
                <div class="nbdesigner-product nbdesigner-template-global" data-id="<?php echo esc_attr($val->id); ?>">
                    <a class="nbdesigner-product-title"><span><?php esc_html_e($val->name); ?></span></a>
                    <div class="nbdesigner-product-inner">
                        <div class="nbdesigner-product-link">
                            <img src="<?php echo esc_url($val->thumbnail); ?>" alt="<?php esc_attr_e($val->name); ?>" />
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        <div class="tablenav top">
            <div class="tablenav-pages">
                <span class="displaying-num"><?php esc_html_e($number_pro . ' ' . esc_html__('Products', 'web-to-print-online-designer')); ?></span>
                <?php echo ($paging->html()); ?>
            </div>
        </div>
    <?php else: ?>
        <p><?php esc_html_e('No templates found.', 'web-to-print-online-designer'); ?></p>
    <?php endif; ?>
</div>

<!-- Popup chọn sản phẩm -->
<div id="nbd-assign-product-modal">
    <div class="nbd-modal-inner">
        <div class="nbd-modal-header">
            <strong><?php esc_html_e('Select Product', 'web-to-print-online-designer'); ?></strong>
            <button id="nbd-modal-close" class="nbd-modal-close-btn" type="button">&times;</button>
        </div>
        <input type="text" id="nbd-product-search" placeholder="<?php esc_attr_e('Search product…', 'web-to-print-online-designer'); ?>" />
        <div id="nbd-product-results">
            <p><?php esc_html_e('Type to search products…', 'web-to-print-online-designer'); ?></p>
        </div>
        <div class="nbd-modal-footer">
            <button id="nbd-modal-cancel" type="button" class="button"><?php esc_html_e('Cancel', 'web-to-print-online-designer'); ?></button>
        </div>
    </div>
</div>