<?php
/**
 * Branding Materials List — Gmind Institute ERP
 * Lists all branding materials with upload/delete actions
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
?>
<div class="admin-page-header" style="display:flex;justify-content:space-between;align-items:center;margin-bottom:24px;">
    <h1 style="margin:0;font-size:22px;font-weight:700;">Branding Materials</h1>
    <a href="<?= $bp ?>/admin/branch-branding/upload" class="admin-btn admin-btn-primary">+ Upload Material</a>
</div>

<div class="admin-table-wrapper">
    <table class="admin-table">
        <thead>
            <tr>
                <th>Branch</th>
                <th>Material Type</th>
                <th>Original Name</th>
                <th>File Path</th>
                <th>Uploaded On</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php if (!empty($materials)): ?>
                <?php foreach ($materials as $mat): ?>
                <tr>
                    <td><?= htmlspecialchars($mat['branch_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                    <td>
                        <?php
                        $mtype = $mat['material_type'] ?? 'other';
                        $typeBadgeColors = [
                            'banner'   => ['bg' => '#e3f2fd', 'text' => '#1565c0'],
                            'logo'     => ['bg' => '#e8f5e9', 'text' => '#2e7d32'],
                            'brochure' => ['bg' => '#fff3e0', 'text' => '#e65100'],
                            'standee'  => ['bg' => '#f3e5f5', 'text' => '#7b1fa2'],
                            'other'    => ['bg' => '#f5f5f5', 'text' => '#616161'],
                        ];
                        $tbc = $typeBadgeColors[$mtype] ?? $typeBadgeColors['other'];
                        ?>
                        <span style="display:inline-block;padding:3px 10px;border-radius:12px;font-size:12px;font-weight:600;background:<?= $tbc['bg'] ?>;color:<?= $tbc['text'] ?>;">
                            <?= ucfirst(htmlspecialchars($mtype, ENT_QUOTES, 'UTF-8')) ?>
                        </span>
                    </td>
                    <td><?= htmlspecialchars($mat['original_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                    <td>
                        <a href="<?= htmlspecialchars($mat['file_path'] ?? '#', ENT_QUOTES, 'UTF-8') ?>" target="_blank" style="color:#1565c0;text-decoration:none;font-size:13px;">
                            <?= htmlspecialchars(basename($mat['file_path'] ?? ''), ENT_QUOTES, 'UTF-8') ?>
                        </a>
                    </td>
                    <td><?= htmlspecialchars($mat['created_at'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                    <td>
                        <form method="POST" action="<?= $bp ?>/admin/branch-branding/delete/<?= (int)$mat['id'] ?>" style="display:inline;" onsubmit="return confirm('Are you sure you want to delete this material?');">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">
                            <button type="submit" class="admin-btn admin-btn-sm admin-btn-danger" title="Delete">Delete</button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr><td colspan="6" style="text-align:center;color:#999;">No branding materials found. Upload your first material.</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>
