<?php
$prefillMeetingId = $_GET['meeting_id'] ?? ($resolution['meeting_id'] ?? '');
$prefillTermId = $_GET['term_id'] ?? ($resolution['term_id'] ?? '');
?>
<div class="row justify-content-center">
    <div class="col-lg-10">
        <div class="card">
            <div class="card-header"><h5 class="mb-0"><?= e($title) ?></h5></div>
            <div class="card-body">
                <form method="POST" action="<?= e($formAction) ?>" data-ajax="true">
                    <?= csrf_field() ?>
                    <div class="row g-3">
                        <div class="col-md-4">
                            <label class="form-label">Term <span class="text-danger">*</span></label>
                            <select name="term_id" class="form-select" required>
                                <option value="">-- Select --</option>
                                <?php foreach ($terms as $t): ?>
                                <option value="<?= $t['id'] ?>" <?= selected($t['id'], $prefillTermId) ?>><?= e($t['term_name_en']) ?></option>
                                <?php endforeach; ?>
                            </select><div class="invalid-feedback" data-field="term_id"></div>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Meeting (optional)</label>
                            <select name="meeting_id" class="form-select">
                                <option value="">-- Standalone --</option>
                                <?php foreach ($meetings as $m): ?>
                                <option value="<?= $m['id'] ?>" <?= selected($m['id'], $prefillMeetingId) ?>><?= e($m['meeting_number'] . ' - ' . $m['title_en']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Resolution Date <span class="text-danger">*</span></label>
                            <input type="date" name="resolution_date" class="form-control" value="<?= e($resolution['resolution_date'] ?? date('Y-m-d')) ?>" required>
                            <div class="invalid-feedback" data-field="resolution_date"></div>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Title (EN) <span class="text-danger">*</span></label>
                            <input type="text" name="title_en" class="form-control" value="<?= e($resolution['title_en'] ?? '') ?>" required>
                            <div class="invalid-feedback" data-field="title_en"></div>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Title (AR)</label>
                            <input type="text" name="title_ar" class="form-control" value="<?= e($resolution['title_ar'] ?? '') ?>" dir="rtl">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Category</label>
                            <input type="text" name="category" class="form-control" value="<?= e($resolution['category'] ?? '') ?>" placeholder="e.g. finance, membership">
                        </div>
                        <?php if ($resolution): ?>
                        <div class="col-md-4">
                            <label class="form-label">Status</label>
                            <select name="status" class="form-select">
                                <?php foreach (['draft','approved','rejected','implemented','cancelled','superseded'] as $s): ?>
                                <option value="<?= $s ?>" <?= selected($s, $resolution['status']) ?>><?= ucfirst($s) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <?php endif; ?>
                        <div class="col-md-4">
                            <label class="form-label">Implementation Deadline</label>
                            <input type="date" name="implementation_deadline" class="form-control" value="<?= e($resolution['implementation_deadline'] ?? '') ?>">
                        </div>
                        <div class="col-12">
                            <div class="form-check">
                                <input type="checkbox" name="requires_implementation" class="form-check-input" value="1" <?= checked(1, $resolution['requires_implementation'] ?? 0) ?>>
                                <label class="form-check-label">Requires Implementation</label>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Resolution Text (EN) <span class="text-danger">*</span></label>
                            <textarea name="resolution_text" class="form-control" rows="6" required><?= e($resolution['resolution_text'] ?? '') ?></textarea>
                            <div class="invalid-feedback" data-field="resolution_text"></div>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Resolution Text (AR)</label>
                            <textarea name="resolution_text_ar" class="form-control" rows="6" dir="rtl"><?= e($resolution['resolution_text_ar'] ?? '') ?></textarea>
                        </div>
                        <?php if ($resolution): ?>
                        <div class="col-12">
                            <label class="form-label">Implementation Notes</label>
                            <textarea name="implementation_notes" class="form-control" rows="3"><?= e($resolution['implementation_notes'] ?? '') ?></textarea>
                        </div>
                        <?php endif; ?>
                    </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="/board/resolutions" class="btn btn-secondary"><?= lang('common.cancel') ?></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>