<?php
// pages/components/template_usage.php — BeezUI Template Usage List
require_once BEEZUI_ROOT . '/components/index.php';

use Components\Base;
use Components\Button;

if (!class_exists('template_manager')) {
    require_once(MH_DIR_CONFIG . 'config_editor/classes/template_manager.php');
}

$template_id = mh_get('maintemplate_id');
$usage = template_manager::get_template_usage($template_id);
$usageCount = count($usage);

$title = mh_get('title', $template_id);
?>
<div class="flex flex-col h-full max-h-[inherit]">
    <div class="flex-1 overflow-y-auto p-6">
        <div class="mb-6">
            <h3 class="text-lg font-medium text-primary">Inhaltsnutzung</h3>
            <p class="text-sm text-secondary mt-1">
                "<?= Base::h($title) ?>" wird derzeit in <?= $usageCount ?> E-Mails oder Vorlagen verwendet.
            </p>
        </div>

        <?php if ($usageCount > 0): ?>
            <div class="overflow-hidden border border-divider rounded-lg">
                <table class="min-w-full divide-y divide-divider">
                    <thead class="bg-surface-secondary">
                        <tr>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-secondary uppercase tracking-wider">Name</th>
                            <th scope="col" class="px-6 py-3 text-right text-xs font-medium text-secondary uppercase tracking-wider">Nutzung</th>
                        </tr>
                    </thead>
                    <tbody class="bg-surface divide-y divide-divider">
                        <?php foreach ($usage as $item): ?>
                            <?php

                            if (!empty($item['editor_dialog'])) {
                                $dialog = $item['editor_dialog'];
                                if (($dialog['type'] ?? '') === 'beezui') {
                                    $linkAttrs = [
                                        'hx-get' => $dialog['url'],
                                        'hx-target' => '#' . ($dialog['modal_id'] ?? 'config-modal') . '-body',
                                        'hx-swap' => 'innerHTML',
                                        'command' => 'show-modal',
                                        'commandfor' => ($dialog['modal_id'] ?? 'config-modal'),
                                        'data-title' => $dialog['title'] ?? '',
                                        'data-size' => $dialog['size'] ?? 'full',
                                        'data-body-class' => $dialog['body_class'] ?? ''
                                    ];
                                }
                            }
                            ?>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                    <?php if (!empty($item['url'])): ?>
                                        <button <?= Base::attrs($linkAttrs) ?> class="text-brand hover:text-brand-emphasis inline-flex items-center gap-1">
                                            <?= Base::h($item['name']) ?>
                                            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-4">
                                                <path stroke-linecap="round" stroke-linejoin="round" d="M13.5 6H5.25A2.25 2.25 0 0 0 3 8.25v10.5A2.25 2.25 0 0 0 5.25 21h10.5A2.25 2.25 0 0 0 18 18.75V10.5m-10.5 6L21 3m0 0h-5.25M21 3v5.25" />
                                            </svg>
                                        </button>
                                    <?php else: ?>
                                        <span class="text-primary"><?= Base::h($item['name']) ?></span>
                                    <?php endif; ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-secondary text-right">
                                    <?= Base::h($item['type']) ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php else: ?>
            <div class="py-12 text-center border-2 border-dashed border-divider rounded-lg">
                <p class="text-sm text-secondary">Dieses Template wird derzeit in keinen E-Mails oder Vorlagen verwendet.</p>
            </div>
        <?php endif; ?>
    </div>
</div>
