<div class="row justify-content-center">
    <div class="col-lg-8">
        <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">Code <span class="text-danger">*</span></label>
                            <input type="text" name="committee_code" class="form-control" value="<?= e($committee['committee_code'] ?? '') ?>" required>
                            <div class="invalid-feedback" data-field="committee_code"></div>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label"><?= lang('board.name') ?> (EN) <span class="text-danger">*</span></label>
                            <input type="text" name="name_en" class="form-control" value="<?= e($committee['name_en'] ?? '') ?>" required>
                            <div class="invalid-feedback" data-field="name_en"></div>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label"><?= lang('board.name') ?> (AR) <span class="text-danger">*</span></label>
                            <input type="text" name="name_ar" class="form-control" value="<?= e($committee['name_ar'] ?? '') ?>" required dir="rtl">
                            <div class="invalid-feedback" data-field="name_ar"></div>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label"><?= lang('board.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'], $committee['term_id'] ?? '') ?>><?= e($t['term_name_en']) ?></option>
                                <?php endforeach; ?>
                            </select>
                            <div class="invalid-feedback" data-field="term_id"></div>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Chairperson</label>
                            <select name="chairperson_board_member_id" class="form-select">
                                <option value="">-- None --</option>
                                <?php foreach ($boardMembers as $bm): ?>
                                <option value="<?= $bm['id'] ?>" <?= selected($bm['id'], $committee['chairperson_board_member_id'] ?? '') ?>><?= e($bm['full_name_en']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Description (EN)</label>
                            <textarea name="description_en" class="form-control" rows="3"><?= e($committee['description_en'] ?? '') ?></textarea>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Description (AR)</label>
                            <textarea name="description_ar" class="form-control" rows="3" dir="rtl"><?= e($committee['description_ar'] ?? '') ?></textarea>
                        </div>
                        <?php if ($committee): ?>
                        <div class="col-12">
                            <div class="form-check">
                                <input type="checkbox" name="is_active" class="form-check-input" value="1" <?= checked(1, $committee['is_active'] ?? 1) ?>>
                                <label class="form-check-label">Active</label>
                            </div>
                        </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/committees" class="btn btn-secondary"><?= lang('common.cancel') ?></a>
                    </div>
                </form>
            </div>
        </div>
        <?php if ($committee && !empty($committeeMembers)): ?>
        <div class="card mt-3">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h6 class="mb-0">Committee Members</h6>
            </div>
            <div class="card-body p-0">
                <table class="table table-sm mb-0">
                    <thead><tr><th>Name</th><th>Role</th><th>Joined</th><th>Actions</th></tr></thead>
                    <tbody>
                    <?php foreach ($committeeMembers as $cm): ?>
                    <tr>
                        <td><?= e($cm['full_name_en']) ?></td>
                        <td><?= e($cm['role_in_committee'] ?? '-') ?></td>
                        <td><?= format_date($cm['joined_date']) ?></td>
                        <td>
                            <form method="POST" action="/board/committees/<?= $committee['id'] ?>/remove-member" class="d-inline" data-ajax="true">
                                <?= csrf_field() ?>
                                <input type="hidden" name="board_member_id" value="<?= $cm['board_member_id'] ?>">
                                <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Remove?')"><i class="fas fa-times"></i></button>
                            </form>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>
        <?php if ($committee): ?>
        <div class="card mt-3">
            <div class="card-header"><h6 class="mb-0">Add Committee Member</h6></div>
            <div class="card-body">
                <form method="POST" action="/board/committees/<?= $committee['id'] ?>/add-member" data-ajax="true" class="row g-2">
                    <?= csrf_field() ?>
                    <div class="col-md-5">
                        <select name="board_member_id" class="form-select" required>
                            <option value="">-- Select Board Member --</option>
                            <?php foreach ($boardMembers as $bm): ?>
                            <option value="<?= $bm['id'] ?>"><?= e($bm['full_name_en']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <input type="text" name="role_in_committee" class="form-control" placeholder="Role (optional)">
                    </div>
                    <div class="col-md-3">
                        <button type="submit" class="btn btn-primary w-100"><i class="fas fa-plus me-1"></i>Add</button>
                    </div>
                </form>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>