<?php
/**
 * Super Admin — Edit Announcement
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$announcement = $announcement ?? [];
$branches = $branches ?? [];
?>
<div class="admin-section">
    <div style="margin-bottom:20px;">
        <a href="<?= $bp ?>/super-admin/announcements" style="color:#6c3fe8;text-decoration:none;font-size:0.85rem;">← Back to Announcements</a>
    </div>

    <form method="POST" action="<?= $bp ?>/super-admin/announcements/<?= (int) ($announcement['id'] ?? 0) ?>" enctype="multipart/form-data" style="max-width:720px;">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

        <div class="admin-card" style="background:#fff;border-radius:12px;padding:24px;margin-bottom:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <h2 style="margin:0 0 20px;font-size:1.1rem;color:#2c1e4a;">Edit Announcement</h2>

            <div style="display:grid;grid-template-columns:1fr 1fr;gap:14px;margin-bottom:14px;">
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Title <span style="color:#e84118;">*</span></label>
                    <input type="text" name="title" required value="<?= htmlspecialchars($announcement['title'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Type</label>
                    <select name="type" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                        <option value="general" <?= ($announcement['type'] ?? '') === 'general' ? 'selected' : '' ?>>General</option>
                        <option value="info" <?= ($announcement['type'] ?? '') === 'info' ? 'selected' : '' ?>>Info</option>
                        <option value="urgent" <?= ($announcement['type'] ?? '') === 'urgent' ? 'selected' : '' ?>>Urgent</option>
                    </select>
                </div>
            </div>

            <div style="margin-bottom:14px;">
                <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Short Text <span style="color:#e84118;">*</span></label>
                <input type="text" name="text" required maxlength="1000" value="<?= htmlspecialchars($announcement['text'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
            </div>

            <div style="margin-bottom:14px;">
                <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Full Content</label>
                <textarea name="content" rows="4" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;resize:vertical;"><?= htmlspecialchars($announcement['content'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
            </div>

            <div style="display:grid;grid-template-columns:1fr 1fr;gap:14px;margin-bottom:14px;">
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Scope</label>
                    <select name="branch_id" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                        <option value="">🌍 Global (all branches)</option>
                        <?php foreach ($branches as $b): ?>
                            <option value="<?= (int) $b['id'] ?>" <?= ($announcement['branch_id'] ?? '') == $b['id'] ? 'selected' : '' ?>>📍 <?= htmlspecialchars($b['branch_code'], ENT_QUOTES, 'UTF-8') ?> — <?= htmlspecialchars($b['name'], ENT_QUOTES, 'UTF-8') ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Link</label>
                    <input type="url" name="link" value="<?= htmlspecialchars($announcement['link'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
            </div>

            <div style="margin-bottom:14px;">
                <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Replace Image (optional)</label>
                <?php if (!empty($announcement['image'])): ?>
                    <img src="<?= $bp ?>/<?= htmlspecialchars($announcement['image'], ENT_QUOTES, 'UTF-8') ?>" alt="Current" style="max-height:80px;border-radius:6px;border:1px solid #ddd;margin-bottom:6px;display:block;">
                <?php endif; ?>
                <input type="file" name="image" accept="image/jpeg,image/png,image/gif,image/webp" style="width:100%;font-size:0.85rem;padding:8px;border:1px solid #ddd;border-radius:6px;">
            </div>

            <div>
                <label style="display:flex;align-items:center;gap:8px;cursor:pointer;">
                    <input type="checkbox" name="is_active" value="1" <?= !empty($announcement['is_active']) ? 'checked' : '' ?> style="width:18px;height:18px;accent-color:#6c3fe8;">
                    <span style="font-size:0.88rem;font-weight:600;color:#2c1e4a;">Active</span>
                </label>
            </div>
        </div>

        <div style="display:flex;gap:12px;justify-content:flex-end;">
            <a href="<?= $bp ?>/super-admin/announcements" style="padding:12px 24px;border:1px solid #ddd;border-radius:8px;text-decoration:none;color:#666;font-weight:600;font-size:0.9rem;">Cancel</a>
            <button type="submit" style="padding:12px 28px;background:linear-gradient(135deg,#6c3fe8,#4a2db8);color:#fff;border:none;border-radius:8px;font-weight:600;font-size:0.9rem;cursor:pointer;">Save Changes</button>
        </div>
    </form>
</div>
