<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h4 class="mb-1"><?= e($meeting['meeting_number']) ?> — <?= e($meeting['title_en']) ?></h4>
        <span class="text-muted"><?= format_date($meeting['meeting_date']) ?> | <?= e($meeting['location'] ?? '') ?></span>
    </div>
    <div class="d-flex gap-2">
        <?php if (auth_can('board.manage')): ?>
        <a href="/board/meetings/<?= $meeting['id'] ?>/edit" class="btn btn-warning btn-sm"><i class="fas fa-edit me-1"></i>Edit</a>
        <?php endif; ?>
        <a href="/board/meetings" class="btn btn-secondary btn-sm"><i class="fas fa-arrow-left me-1"></i>Back</a>
    </div>
</div>

<div class="row g-3">
    <div class="col-md-3">
        <div class="card">
            <div class="card-body text-center">
                <div class="mb-2"><?= status_badge($meeting['status']) ?></div>
                <div class="mb-1"><strong>Type:</strong> <?= ucfirst(str_replace('_', ' ', $meeting['meeting_type'])) ?></div>
                <div class="mb-1"><strong>Quorum:</strong> <?= $meeting['quorum_met'] ? '<span class="text-success">Met</span>' : '<span class="text-danger">Not Met</span>' ?> (<?= $meeting['attendees_count'] ?>/<?= $meeting['quorum_required'] ?>)</div>
                <?php if ($meeting['start_time']): ?>
                <div><strong>Time:</strong> <?= e($meeting['start_time']) ?> - <?= e($meeting['end_time'] ?? '?') ?></div>
                <?php endif; ?>
                <?php if (auth_can('board.manage') && $meeting['status'] !== 'completed'): ?>
                <hr>
                <form method="POST" action="/board/meetings/<?= $meeting['id'] ?>/status" data-ajax="true">
                    <?= csrf_field() ?>
                    <select name="status" class="form-select form-select-sm mb-2">
                        <option value="scheduled">Scheduled</option>
                        <option value="in_progress">In Progress</option>
                        <option value="completed">Completed</option>
                        <option value="cancelled">Cancelled</option>
                        <option value="postponed">Postponed</option>
                    </select>
                    <button type="submit" class="btn btn-sm btn-primary w-100">Update Status</button>
                </form>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <div class="col-md-9">
        <ul class="nav nav-tabs mb-3" role="tablist">
            <li class="nav-item"><a class="nav-link active" data-bs-toggle="tab" href="#tab-agenda">Agenda</a></li>
            <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-attendance">Attendance</a></li>
            <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-minutes">Minutes</a></li>
            <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-resolutions">Resolutions</a></li>
        </ul>
        <div class="tab-content">
            <div class="tab-pane fade show active" id="tab-agenda">
                <div class="card">
                    <div class="card-body p-0">
                        <table class="table table-sm mb-0">
                            <thead><tr><th width="50">#</th><th>Title</th><th>Status</th></tr></thead>
                            <tbody>
                            <?php foreach ($agendaItems as $ai): ?>
                            <tr>
                                <td><?= $ai['agenda_number'] ?></td>
                                <td><?= e($ai['title_en']) ?><?php if ($ai['description']): ?><br><small class="text-muted"><?= e($ai['description']) ?></small><?php endif; ?></td>
                                <td><?= status_badge($ai['status']) ?></td>
                            </tr>
                            <?php endforeach; ?>
                            <?php if (empty($agendaItems)): ?><tr><td colspan="3" class="text-center text-muted py-3">No agenda items</td></tr><?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="tab-pane fade" id="tab-attendance">
                <div class="card">
                    <div class="card-body">
                        <?php if (auth_can('board.manage')): ?>
                        <form method="POST" action="/board/meetings/<?= $meeting['id'] ?>/attendance" data-ajax="true">
                            <?= csrf_field() ?>
                            <table class="table table-sm">
                                <thead><tr><th>Board Member</th><th>Position</th><th>Attendance</th></tr></thead>
                                <tbody>
                                <?php
                                $existingAtt = [];
                                foreach ($attendees as $a) $existingAtt[$a['board_member_id']] = $a['attendance'];
                                foreach ($boardMembers as $bm):
                                    $current = $existingAtt[$bm['id']] ?? '';
                                ?>
                                <tr>
                                    <td><?= e($bm['full_name_en']) ?></td>
                                    <td><?= e($bm['position_title'] ?? '') ?></td>
                                    <td>
                                        <select name="attendance[<?= $bm['id'] ?>]" class="form-select form-select-sm">
                                            <option value="">-- Not recorded --</option>
                                            <option value="present" <?= selected('present', $current) ?>>Present</option>
                                            <option value="absent" <?= selected('absent', $current) ?>>Absent</option>
                                            <option value="excused" <?= selected('excused', $current) ?>>Excused</option>
                                            <option value="proxy" <?= selected('proxy', $current) ?>>Proxy</option>
                                        </select>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                                </tbody>
                            </table>
                            <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i>Save Attendance</button>
                        </form>
                        <?php else: ?>
                        <table class="table table-sm">
                            <thead><tr><th>Name</th><th>Attendance</th></tr></thead>
                            <tbody>
                            <?php foreach ($attendees as $a): ?>
                            <tr><td><?= e($a['full_name_en']) ?></td><td><?= status_badge($a['attendance']) ?></td></tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <div class="tab-pane fade" id="tab-minutes">
                <div class="card">
                    <div class="card-body">
                        <?php if ($meeting['minutes_text']): ?>
                        <div class="border rounded p-3 mb-3" style="white-space:pre-wrap"><?= e($meeting['minutes_text']) ?></div>
                        <?php if (!$meeting['minutes_approved'] && auth_can('board.manage')): ?>
                        <form method="POST" action="/board/meetings/<?= $meeting['id'] ?>/approve-minutes" data-ajax="true">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-success"><i class="fas fa-check me-1"></i>Approve Minutes</button>
                        </form>
                        <?php elseif ($meeting['minutes_approved']): ?>
                        <span class="badge bg-success"><i class="fas fa-check me-1"></i>Minutes Approved <?= format_datetime($meeting['minutes_approved_at']) ?></span>
                        <?php endif; ?>
                        <?php else: ?>
                        <p class="text-muted">No minutes recorded yet. <a href="/board/meetings/<?= $meeting['id'] ?>/edit">Edit meeting</a> to add minutes.</p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <div class="tab-pane fade" id="tab-resolutions">
                <div class="card">
                    <div class="card-body p-0">
                        <table class="table table-sm mb-0">
                            <thead><tr><th>#</th><th>Title</th><th>Status</th><th>Votes</th></tr></thead>
                            <tbody>
                            <?php foreach ($resolutions as $r): ?>
                            <tr>
                                <td><a href="/board/resolutions/<?= $r['id'] ?>"><?= e($r['resolution_number']) ?></a></td>
                                <td><?= e($r['title_en']) ?></td>
                                <td><?= status_badge($r['status']) ?></td>
                                <td><?= $r['votes_for'] ?>F / <?= $r['votes_against'] ?>A / <?= $r['votes_abstain'] ?>Ab</td>
                            </tr>
                            <?php endforeach; ?>
                            <?php if (empty($resolutions)): ?><tr><td colspan="4" class="text-center text-muted py-3">No resolutions</td></tr><?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php if (auth_can('board.resolutions')): ?>
                    <div class="card-footer">
                        <a href="/board/resolutions/create?meeting_id=<?= $meeting['id'] ?>&term_id=<?= $meeting['term_id'] ?>" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i>Create Resolution from Meeting</a>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>