<?php $title = isset($announcement) ? 'Edit Post' : 'New Post'; ?>
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white font-weight-bold"><?= isset($announcement) ? 'Edit Post' : 'New Post' ?></h1>
</div>
<div class="glass-card">
    <form method="POST" action="<?= $baseUrl ?>/announcements/<?= isset($announcement) ? $announcement->id . '/update' : 'store' ?>" enctype="multipart/form-data">
        <div class="row">
            <div class="col-md-8 form-group">
                <label style="color: rgba(255,255,255,0.7);">Title</label>
                <input type="text" name="title" class="form-control glass-input" value="<?= htmlspecialchars($announcement->title ?? '') ?>" required>
            </div>
            <div class="col-md-4 form-group">
                <label style="color: rgba(255,255,255,0.7);">Post Type</label>
                <select name="post_type" class="form-control glass-input">
                    <option value="announcement" <?= (($announcement->post_type ?? 'announcement') === 'announcement') ? 'selected' : '' ?>>Announcement</option>
                    <option value="news" <?= (($announcement->post_type ?? '') === 'news') ? 'selected' : '' ?>>News</option>
                    <option value="article" <?= (($announcement->post_type ?? '') === 'article') ? 'selected' : '' ?>>Article</option>
                    <option value="event_update" <?= (($announcement->post_type ?? '') === 'event_update') ? 'selected' : '' ?>>Event Update</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label style="color: rgba(255,255,255,0.7);">Excerpt (short summary for cards)</label>
            <textarea name="excerpt" class="form-control glass-input" rows="2"><?= htmlspecialchars($announcement->excerpt ?? '') ?></textarea>
        </div>
        <div class="form-group">
            <label style="color: rgba(255,255,255,0.7);">Content</label>
            <textarea name="content" id="contentEditor" class="form-control glass-input" rows="15"><?= htmlspecialchars($announcement->content ?? '') ?></textarea>
        </div>
        <div class="row">
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Category (optional)</label>
                <select name="category_id" class="form-control glass-input">
                    <option value="">-- Uncategorized --</option>
                    <?php if (!empty($categories)): ?>
                        <?php foreach ($categories as $cat): ?>
                        <option value="<?= $cat->id ?>" <?= (($announcement->category_id ?? '') == $cat->id) ? 'selected' : '' ?>><?= htmlspecialchars($cat->name) ?></option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
            </div>
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Image (thumbnail)</label>
                <input type="file" name="image" class="form-control glass-input" accept="image/*">
                <?php if (isset($announcement) && $announcement->image): ?>
                <div class="mt-2">
                    <img src="<?= $baseUrl ?>/<?= $announcement->image ?>" style="max-height: 80px; border-radius: 8px;">
                    <small style="color: rgba(255,255,255,0.5);">Current image</small>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <div class="row">
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Link URL (optional - for "Learn More")</label>
                <input type="url" name="link_url" class="form-control glass-input" value="<?= htmlspecialchars($announcement->link_url ?? '') ?>" placeholder="https://...">
            </div>
        </div>
        <div class="row">
            <div class="col-md-3 form-group">
                <label style="color: rgba(255,255,255,0.7);">Priority</label>
                <select name="priority" class="form-control glass-input">
                    <option value="low" <?= (($announcement->priority ?? '') === 'low') ? 'selected' : '' ?>>Low</option>
                    <option value="normal" <?= (($announcement->priority ?? '') === 'normal') ? 'selected' : '' ?>>Normal</option>
                    <option value="high" <?= (($announcement->priority ?? '') === 'high') ? 'selected' : '' ?>>High</option>
                    <option value="urgent" <?= (($announcement->priority ?? '') === 'urgent') ? 'selected' : '' ?>>Urgent</option>
                </select>
            </div>
            <div class="col-md-3 form-group">
                <label style="color: rgba(255,255,255,0.7);">Visibility</label>
                <select name="visibility" class="form-control glass-input">
                    <option value="public" <?= (($announcement->visibility ?? 'public') === 'public') ? 'selected' : '' ?>>Public (anyone)</option>
                    <option value="private" <?= (($announcement->visibility ?? '') === 'private') ? 'selected' : '' ?>>Private (members only)</option>
                </select>
            </div>
            <div class="col-md-3 form-group">
                <label style="color: rgba(255,255,255,0.7);">Status</label>
                <select name="status" class="form-control glass-input">
                    <option value="draft" <?= (($announcement->status ?? '') === 'draft') ? 'selected' : '' ?>>Draft</option>
                    <option value="published" <?= (($announcement->status ?? '') === 'published') ? 'selected' : '' ?>>Published</option>
                </select>
            </div>
        </div>
        <div class="text-right mt-3">
            <a href="<?= $baseUrl ?>/announcements" class="glass-btn mr-2">Cancel</a>
            <button type="submit" class="glass-btn glass-btn-primary"><?= isset($announcement) ? 'Update' : 'Create' ?></button>
        </div>
    </form>
</div>

<!-- TinyMCE -->
<script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script>
<script>
tinymce.init({
    selector: '#contentEditor',
    height: 400,
    menubar: false,
    plugins: 'advlist autolink lists link image charmap preview anchor searchreplace visualblocks code fullscreen insertdatetime media table code help wordcount',
    toolbar: 'undo redo | blocks | bold italic underline strikethrough | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | removeformat | code',
    content_style: 'body { font-family: Nunito, sans-serif; font-size: 14px; color: #333; }',
    branding: false,
    promotion: false,
});
</script>
