<?php $isEdit = !empty($announcement); $a = $announcement ?? []; ?>
<div class="row justify-content-center"><div class="col-lg-9"><div class="card">
    <div class="card-header"><h5 class="mb-0"><?= $isEdit ? lang('comm.edit_announcement') : lang('comm.create_announcement') ?></h5></div>
    <div class="card-body">
        <form method="POST" action="<?= $isEdit ? '/communications/announcements/' . $a['id'] : '/communications/announcements' ?>" data-ajax="true" enctype="multipart/form-data">
            <?= csrf_field() ?>
            <div class="row g-3">
                <div class="col-md-8">
                    <label class="form-label">Title (EN) <span class="text-danger">*</span></label>
                    <input type="text" name="title_en" class="form-control" value="<?= e($a['title_en'] ?? '') ?>" required>
                    <div class="invalid-feedback" data-field="title_en"></div>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Category <span class="text-danger">*</span></label>
                    <select name="category" class="form-select" required>
                        <?php foreach (['general','maintenance','event','policy','financial','emergency','other'] as $c): ?>
                        <option value="<?= $c ?>" <?= selected($c, $a['category'] ?? 'general') ?>><?= ucfirst($c) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="mt-3">
                <label class="form-label">Title (AR)</label>
                <input type="text" name="title_ar" class="form-control" value="<?= e($a['title_ar'] ?? '') ?>" dir="rtl">
            </div>
            <div class="row g-3 mt-1">
                <div class="col-md-3">
                    <label class="form-label">Priority <span class="text-danger">*</span></label>
                    <select name="priority" class="form-select" required>
                        <?php foreach (['low','normal','high','urgent'] as $p): ?>
                        <option value="<?= $p ?>" <?= selected($p, $a['priority'] ?? 'normal') ?>><?= ucfirst($p) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Audience <span class="text-danger">*</span></label>
                    <select name="target_audience" class="form-select" required>
                        <?php foreach (['all_members','specific_types','staff','board','public'] as $ta): ?>
                        <option value="<?= $ta ?>" <?= selected($ta, $a['target_audience'] ?? 'all_members') ?>><?= ucwords(str_replace('_',' ',$ta)) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Publish Date <span class="text-danger">*</span></label>
                    <input type="text" name="publish_date" class="form-control datetimepicker" value="<?= e($a['publish_date'] ?? date('Y-m-d H:i')) ?>" required>
                    <div class="invalid-feedback" data-field="publish_date"></div>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Expiry Date</label>
                    <input type="text" name="expiry_date" class="form-control datetimepicker" value="<?= e($a['expiry_date'] ?? '') ?>">
                </div>
            </div>
            <div class="mt-3">
                <label class="form-label">Target Membership Types</label>
                <select name="target_membership_types[]" class="form-select select2" multiple>
                    <?php
                    $selectedTypes = json_decode($a['target_membership_types'] ?? '[]', true) ?: [];
                    foreach ($memTypes as $mt): ?>
                    <option value="<?= $mt['id'] ?>" <?= in_array($mt['id'], $selectedTypes) ? 'selected' : '' ?>><?= e($mt['name_en']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="mt-3">
                <label class="form-label">Body (EN) <span class="text-danger">*</span></label>
                <textarea name="body_en" class="form-control" rows="6" required><?= e($a['body_en'] ?? '') ?></textarea>
                <div class="invalid-feedback" data-field="body_en"></div>
            </div>
            <div class="mt-3">
                <label class="form-label">Body (AR)</label>
                <textarea name="body_ar" class="form-control" rows="6" dir="rtl"><?= e($a['body_ar'] ?? '') ?></textarea>
            </div>
            <div class="row g-3 mt-1">
                <div class="col-md-6">
                    <label class="form-label">Image</label>
                    <input type="file" name="image" class="form-control" accept="image/*">
                    <?php if (!empty($a['image_path'])): ?><small class="text-muted">Current: <?= e($a['image_path']) ?></small><?php endif; ?>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Attachment</label>
                    <input type="file" name="attachment" class="form-control" accept=".pdf,.doc,.docx,.jpg,.png">
                    <?php if (!empty($a['attachment_path'])): ?><small class="text-muted">Current: <?= e($a['attachment_path']) ?></small><?php endif; ?>
                </div>
            </div>
            <div class="row g-3 mt-2">
                <div class="col-auto">
                    <div class="form-check"><input class="form-check-input" type="checkbox" name="is_published" value="1" id="ann_pub" <?= checked(1, $a['is_published'] ?? 0) ?>><label class="form-check-label" for="ann_pub">Published</label></div>
                </div>
                <div class="col-auto">
                    <div class="form-check"><input class="form-check-input" type="checkbox" name="is_pinned" value="1" id="ann_pin" <?= checked(1, $a['is_pinned'] ?? 0) ?>><label class="form-check-label" for="ann_pin">Pinned</label></div>
                </div>
                <div class="col-auto">
                    <div class="form-check"><input class="form-check-input" type="checkbox" name="send_notification" value="1" id="ann_notif" <?= checked(1, $a['send_notification'] ?? 0) ?>><label class="form-check-label" for="ann_notif">Send Notification</label></div>
                </div>
            </div>
            <div class="mt-4 d-flex gap-2">
                <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i><?= lang('common.save') ?></button>
                <a href="/communications/announcements" class="btn btn-secondary"><?= lang('common.cancel') ?></a>
            </div>
        </form>
    </div>
</div></div></div>