<?php
/**
 * Announcement Create/Edit Form — Gmind Institute ERP
 * Uses $announcement for edit mode (null for create)
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$isEdit = !empty($announcement);
$pageTitle = $isEdit ? 'Edit Announcement' : 'Create Announcement';
$formAction = $isEdit ? $bp . '/admin/announcements/update/' . (int)$announcement['id'] : $bp . '/admin/announcements/store';
?>
<div class="admin-page-header" style="margin-bottom:24px;">
    <h1 style="margin:0;font-size:22px;font-weight:700;"><?= htmlspecialchars($pageTitle, ENT_QUOTES, 'UTF-8') ?></h1>
</div>

<div class="admin-form-container" style="max-width:720px;">
    <form method="POST" action="<?= htmlspecialchars($formAction, ENT_QUOTES, 'UTF-8') ?>" class="admin-form" enctype="multipart/form-data">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">

        <div class="admin-form-group" style="margin-bottom:16px;">
            <label for="title" class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Title <span style="color:#c62828;">*</span></label>
            <input type="text" id="title" name="title" class="admin-form-control" style="width:100%;padding:8px 12px;border:1px solid #ccc;border-radius:6px;" value="<?= htmlspecialchars($announcement['title'] ?? '', ENT_QUOTES, 'UTF-8') ?>" required>
        </div>

        <div class="admin-form-group" style="margin-bottom:16px;">
            <label for="content" class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Content <span style="color:#c62828;">*</span></label>
            <textarea id="content" name="content" class="admin-form-control" style="width:100%;padding:8px 12px;border:1px solid #ccc;border-radius:6px;min-height:150px;" rows="6" required><?= htmlspecialchars($announcement['content'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
        </div>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
            <div class="admin-form-group" style="margin-bottom:16px;">
                <label for="type" class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Type <span style="color:#c62828;">*</span></label>
                <select id="type" name="type" class="admin-form-control" style="width:100%;padding:8px 12px;border:1px solid #ccc;border-radius:6px;" required>
                    <option value="general" <?= ($announcement['type'] ?? 'general') === 'general' ? 'selected' : '' ?>>General</option>
                    <option value="exam" <?= ($announcement['type'] ?? '') === 'exam' ? 'selected' : '' ?>>Exam</option>
                    <option value="admission" <?= ($announcement['type'] ?? '') === 'admission' ? 'selected' : '' ?>>Admission</option>
                    <option value="event" <?= ($announcement['type'] ?? '') === 'event' ? 'selected' : '' ?>>Event</option>
                </select>
            </div>

            <div class="admin-form-group" style="margin-bottom:16px;">
                <label for="branch_id" class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Branch (Optional)</label>
                <select id="branch_id" name="branch_id" class="admin-form-control" style="width:100%;padding:8px 12px;border:1px solid #ccc;border-radius:6px;">
                    <option value="">— All Branches —</option>
                    <?php if (!empty($branches)): ?>
                        <?php foreach ($branches as $br): ?>
                            <option value="<?= (int)$br['id'] ?>" <?= (string)($announcement['branch_id'] ?? '') === (string)$br['id'] ? 'selected' : '' ?>>
                                <?= htmlspecialchars($br['name'], ENT_QUOTES, 'UTF-8') ?>
                            </option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
            </div>
        </div>

        <div class="admin-form-group" style="margin-bottom:16px;">
            <label for="image" class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Image</label>
            <?php if ($isEdit && !empty($announcement['image'])): ?>
                <div style="margin-bottom:8px;">
                    <img src="<?= htmlspecialchars($announcement['image'], ENT_QUOTES, 'UTF-8') ?>" alt="Current image" style="max-width:200px;max-height:120px;border-radius:6px;border:1px solid #e0e0e0;">
                    <p style="margin:4px 0 0;font-size:12px;color:#999;">Current image. Upload a new one to replace.</p>
                </div>
            <?php endif; ?>
            <input type="file" id="image" name="image" class="admin-form-control" style="width:100%;padding:8px 12px;border:1px solid #ccc;border-radius:6px;" accept="image/*">
        </div>

        <div class="admin-form-group" style="margin-bottom:16px;">
            <label style="display:flex;align-items:center;gap:8px;cursor:pointer;font-weight:600;">
                <input type="checkbox" name="is_active" value="1" <?= !empty($announcement['is_active']) || !$isEdit ? 'checked' : '' ?> style="width:18px;height:18px;">
                Is Active
            </label>
        </div>

        <div style="display:flex;gap:12px;margin-top:24px;">
            <button type="submit" class="admin-btn admin-btn-primary"><?= $isEdit ? 'Update Announcement' : 'Create Announcement' ?></button>
            <a href="<?= $bp ?>/admin/announcements" class="admin-btn admin-btn-secondary">Cancel</a>
        </div>
    </form>
</div>
