<?php $bp = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<!-- News Management -->
<div class="admin-section">
    <div class="admin-section-header">
        <a href="<?= $bp ?>/admin/news/create" class="admin-btn admin-btn-primary">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
            Add New News
        </a>
    </div>

    <!-- Filters -->
    <div class="admin-card" style="margin-bottom: 20px;">
        <div class="admin-card-body" style="padding: 16px;">
            <form method="GET" action="<?= $bp ?>/admin/news" style="display:flex; gap:12px; flex-wrap:wrap; align-items:flex-end;">
                <div style="flex:1; min-width:200px;">
                    <label style="display:block; font-weight:600; margin-bottom:4px; font-size:13px; color:#555;">Search</label>
                    <input type="text" name="search" value="<?= htmlspecialchars($search ?? '', ENT_QUOTES, 'UTF-8') ?>" placeholder="Search by title or slug..."
                        style="width:100%; padding:8px 12px; border:1px solid #ddd; border-radius:6px; font-size:14px; outline:none;">
                </div>
                <div style="min-width:180px;">
                    <label style="display:block; font-weight:600; margin-bottom:4px; font-size:13px; color:#555;">Branch</label>
                    <select name="branch_id" style="width:100%; padding:8px 12px; border:1px solid #ddd; border-radius:6px; font-size:14px; outline:none;">
                        <option value="">All Branches</option>
                        <option value="general" <?= ($filterBranch ?? '') === 'general' ? 'selected' : '' ?>>General (No Branch)</option>
                        <?php foreach ($branches ?? [] as $b): ?>
                        <option value="<?= (int) $b['id'] ?>" <?= ($filterBranch ?? '') == $b['id'] ? 'selected' : '' ?>><?= htmlspecialchars($b['name'], ENT_QUOTES, 'UTF-8') ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div style="min-width:140px;">
                    <label style="display:block; font-weight:600; margin-bottom:4px; font-size:13px; color:#555;">Status</label>
                    <select name="status" style="width:100%; padding:8px 12px; border:1px solid #ddd; border-radius:6px; font-size:14px; outline:none;">
                        <option value="">All</option>
                        <option value="1" <?= ($filterStatus ?? '') === '1' ? 'selected' : '' ?>>Active</option>
                        <option value="0" <?= ($filterStatus ?? '') === '0' ? 'selected' : '' ?>>Inactive</option>
                    </select>
                </div>
                <button type="submit" class="admin-btn admin-btn-secondary" style="padding:8px 16px;">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
                    Filter
                </button>
                <?php if (!empty($search) || !empty($filterBranch) || !empty($filterStatus)): ?>
                <a href="<?= $bp ?>/admin/news" class="admin-btn admin-btn-secondary" style="padding:8px 16px; text-decoration:none;">Clear</a>
                <?php endif; ?>
            </form>
        </div>
    </div>

    <!-- News Table -->
    <?php if (!empty($news)): ?>
    <div class="admin-card">
        <div class="admin-card-body">
            <div style="margin-bottom:12px; font-size:13px; color:#888;">
                Showing <?= count($news) ?> of <?= (int) ($totalNews ?? 0) ?> news articles
            </div>
            <div class="admin-table-responsive">
                <table class="admin-table" data-search="true">
                    <thead>
                        <tr>
                            <th style="width:40px;">#</th>
                            <th>Title</th>
                            <th>Branch</th>
                            <th>Status</th>
                            <th>Date</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($news as $idx => $item): ?>
                        <tr>
                            <td style="color:#999; font-size:13px;"><?= ($idx + 1) + (($page - 1) * 15) ?></td>
                            <td>
                                <strong><?= htmlspecialchars(mb_substr($item['title'], 0, 80) . (mb_strlen($item['title']) > 80 ? '...' : ''), ENT_QUOTES, 'UTF-8') ?></strong>
                                <?php if (!empty($item['title_hi'])): ?>
                                <br><small style="color:#888; font-size:12px;"><?= htmlspecialchars(mb_substr($item['title_hi'], 0, 60) . (mb_strlen($item['title_hi']) > 60 ? '...' : ''), ENT_QUOTES, 'UTF-8') ?></small>
                                <?php endif; ?>
                                <?php if (!empty($item['image'])): ?>
                                <br><small style="color:#4A6CF7; font-size:11px;">📷 Has image</small>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if (!empty($item['branch_name'])): ?>
                                    <?= htmlspecialchars($item['branch_name'], ENT_QUOTES, 'UTF-8') ?>
                                <?php else: ?>
                                    <span style="background:#E8F5E9; color:#2E7D32; padding:2px 8px; border-radius:4px; font-size:12px; font-weight:600;">General</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($item['active']): ?>
                                    <span style="background:#E8F5E9; color:#2E7D32; padding:3px 10px; border-radius:12px; font-size:12px; font-weight:600;">Active</span>
                                <?php else: ?>
                                    <span style="background:#FFEBEE; color:#C62828; padding:3px 10px; border-radius:12px; font-size:12px; font-weight:600;">Inactive</span>
                                <?php endif; ?>
                            </td>
                            <td style="font-size:13px; color:#666;">
                                <?= date('M d, Y', strtotime($item['created_at'])) ?>
                            </td>
                            <td>
                                <div class="admin-actions">
                                    <a href="<?= $bp ?>/admin/news/<?= (int) $item['id'] ?>/edit" class="admin-btn-icon" title="Edit">
                                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M11 4H4a2 2 0 00-2 2v14a2 2 0 002 2h14a2 2 0 002-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 013 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>
                                    </a>
                                    <form method="POST" action="<?= $bp ?>/admin/news/<?= (int) $item['id'] ?>/toggle-publish" style="display:inline;">
                                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                        <button type="submit" class="admin-btn-icon" title="<?= $item['active'] ? 'Unpublish' : 'Publish' ?>" style="border:none; background:none; cursor:pointer; padding:6px; border-radius:6px; color:<?= $item['active'] ? '#F59E0B' : '#10B981' ?>;">
                                            <?php if ($item['active']): ?>
                                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M18 6L6 18"/><path d="M6 6l12 12"/></svg>
                                            <?php else: ?>
                                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><polyline points="20 6 9 17 4 12"/></svg>
                                            <?php endif; ?>
                                        </button>
                                    </form>
                                    <button type="button" class="admin-btn-icon admin-btn-delete" title="Delete"
                                            data-delete-url="<?= $bp ?>/admin/news/<?= (int) $item['id'] ?>/delete">
                                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6m3 0V4a2 2 0 012-2h4a2 2 0 012 2v2"/></svg>
                                    </button>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    <?php if (($totalPages ?? 1) > 1): ?>
    <div style="display:flex; justify-content:center; gap:6px; margin-top:20px; flex-wrap:wrap;">
        <?php
        $queryParams = [];
        if (!empty($search)) $queryParams['search'] = $search;
        if (!empty($filterBranch)) $queryParams['branch_id'] = $filterBranch;
        if (!empty($filterStatus)) $queryParams['status'] = $filterStatus;
        $queryString = !empty($queryParams) ? '&' . http_build_query($queryParams) : '';
        ?>

        <?php if ($page > 1): ?>
        <a href="<?= $bp ?>/admin/news?page=<?= $page - 1 ?><?= $queryString ?>"
           style="padding:8px 14px; border:1px solid #ddd; border-radius:6px; text-decoration:none; color:#555; font-size:14px;">&laquo; Prev</a>
        <?php endif; ?>

        <?php for ($i = 1; $i <= $totalPages; $i++): ?>
            <?php if ($i === $page): ?>
            <span style="padding:8px 14px; background:#4A6CF7; color:#fff; border-radius:6px; font-size:14px; font-weight:600;"><?= $i ?></span>
            <?php elseif (abs($i - $page) <= 2 || $i === 1 || $i === $totalPages): ?>
            <a href="<?= $bp ?>/admin/news?page=<?= $i ?><?= $queryString ?>"
               style="padding:8px 14px; border:1px solid #ddd; border-radius:6px; text-decoration:none; color:#555; font-size:14px;"><?= $i ?></a>
            <?php elseif (abs($i - $page) === 3): ?>
            <span style="padding:8px 6px; color:#999;">...</span>
            <?php endif; ?>
        <?php endfor; ?>

        <?php if ($page < $totalPages): ?>
        <a href="<?= $bp ?>/admin/news?page=<?= $page + 1 ?><?= $queryString ?>"
           style="padding:8px 14px; border:1px solid #ddd; border-radius:6px; text-decoration:none; color:#555; font-size:14px;">Next &raquo;</a>
        <?php endif; ?>
    </div>
    <?php endif; ?>

    <?php else: ?>
    <div class="admin-card">
        <div class="admin-card-body">
            <p class="admin-empty-text">No news articles found. <a href="<?= $bp ?>/admin/news/create">Add your first news article</a>.</p>
        </div>
    </div>
    <?php endif; ?>
</div>
