<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h4 class="mb-1"><?= e($resolution['resolution_number']) ?></h4>
        <p class="text-muted mb-0"><?= e($resolution['title_en']) ?></p>
    </div>
    <div class="d-flex gap-2">
        <?php if (auth_can('board.resolutions')): ?>
        <a href="/board/resolutions/<?= $resolution['id'] ?>/edit" class="btn btn-warning btn-sm"><i class="fas fa-edit me-1"></i>Edit</a>
        <?php endif; ?>
        <a href="/board/resolutions" 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-4">
        <div class="card">
            <div class="card-body">
                <table class="table table-sm table-borderless mb-0">
                    <tr><td class="text-muted">Status</td><td><?= status_badge($resolution['status']) ?></td></tr>
                    <tr><td class="text-muted">Date</td><td><?= format_date($resolution['resolution_date']) ?></td></tr>
                    <tr><td class="text-muted">Term</td><td><?= e($resolution['term_name_en'] ?? '-') ?></td></tr>
                    <tr><td class="text-muted">Meeting</td><td><?php if ($resolution['meeting_number']): ?><a href="/board/meetings/<?= $resolution['meeting_id'] ?>"><?= e($resolution['meeting_number']) ?></a><?php else: ?>Standalone<?php endif; ?></td></tr>
                    <tr><td class="text-muted">Category</td><td><?= e($resolution['category'] ?? '-') ?></td></tr>
                    <tr><td class="text-muted">Votes</td><td><span class="text-success"><?= $resolution['votes_for'] ?>F</span> / <span class="text-danger"><?= $resolution['votes_against'] ?>A</span> / <span class="text-muted"><?= $resolution['votes_abstain'] ?>Ab</span></td></tr>
                    <?php if ($resolution['requires_implementation']): ?>
                    <tr><td class="text-muted">Impl. Deadline</td><td><?= format_date($resolution['implementation_deadline']) ?: '-' ?></td></tr>
                    <?php if ($resolution['implemented_at']): ?>
                    <tr><td class="text-muted">Implemented</td><td><?= format_datetime($resolution['implemented_at']) ?></td></tr>
                    <?php endif; ?>
                    <?php endif; ?>
                </table>
                <?php if ($resolution['status'] === 'approved' && $resolution['requires_implementation'] && !$resolution['implemented_at'] && auth_can('board.resolutions')): ?>
                <hr>
                <form method="POST" action="/board/resolutions/<?= $resolution['id'] ?>/implement" data-ajax="true">
                    <?= csrf_field() ?>
                    <textarea name="implementation_notes" class="form-control form-control-sm mb-2" rows="2" placeholder="Implementation notes..."></textarea>
                    <button type="submit" class="btn btn-success btn-sm w-100"><i class="fas fa-check me-1"></i>Mark Implemented</button>
                </form>
                <?php endif; ?>
                <?php if ($supersededBy): ?>
                <div class="alert alert-warning mt-2 mb-0 small">Superseded by <a href="/board/resolutions/<?= $supersededBy['id'] ?>"><?= e($supersededBy['resolution_number']) ?></a></div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <div class="col-md-8">
        <div class="card mb-3">
            <div class="card-header"><h6 class="mb-0">Resolution Text</h6></div>
            <div class="card-body" style="white-space:pre-wrap"><?= e($resolution['resolution_text']) ?></div>
        </div>

        <div class="card">
            <div class="card-header"><h6 class="mb-0">Voting</h6></div>
            <div class="card-body">
                <?php if (auth_can('board.resolutions') && $resolution['status'] === 'draft'): ?>
                <form method="POST" action="/board/resolutions/<?= $resolution['id'] ?>/vote" data-ajax="true">
                    <?= csrf_field() ?>
                    <table class="table table-sm">
                        <thead><tr><th>Board Member</th><th>Position</th><th>Vote</th><th>Comments</th></tr></thead>
                        <tbody>
                        <?php
                        $existingVotes = [];
                        $existingComments = [];
                        foreach ($votes as $v) { $existingVotes[$v['board_member_id']] = $v['vote']; $existingComments[$v['board_member_id']] = $v['comments']; }
                        foreach ($boardMembers as $bm):
                            $curVote = $existingVotes[$bm['id']] ?? '';
                            $curComment = $existingComments[$bm['id']] ?? '';
                        ?>
                        <tr>
                            <td><?= e($bm['full_name_en']) ?></td>
                            <td><small class="text-muted"><?= e($bm['position_title'] ?? '') ?></small></td>
                            <td>
                                <select name="votes[<?= $bm['id'] ?>]" class="form-select form-select-sm">
                                    <option value="">--</option>
                                    <option value="for" <?= selected('for', $curVote) ?>>For</option>
                                    <option value="against" <?= selected('against', $curVote) ?>>Against</option>
                                    <option value="abstain" <?= selected('abstain', $curVote) ?>>Abstain</option>
                                </select>
                            </td>
                            <td><input type="text" name="vote_comments[<?= $bm['id'] ?>]" class="form-control form-control-sm" value="<?= e($curComment) ?>" placeholder="Optional"></td>
                        </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                    <button type="submit" class="btn btn-primary"><i class="fas fa-vote-yea me-1"></i>Save Votes</button>
                </form>
                <?php else: ?>
                <table class="table table-sm">
                    <thead><tr><th>Board Member</th><th>Vote</th><th>Comments</th></tr></thead>
                    <tbody>
                    <?php foreach ($votes as $v): ?>
                    <tr>
                        <td><?= e($v['full_name_en']) ?></td>
                        <td><?= status_badge($v['vote'] === 'for' ? 'approved' : ($v['vote'] === 'against' ? 'rejected' : 'pending')) ?> <?= ucfirst($v['vote']) ?></td>
                        <td><?= e($v['comments'] ?? '') ?></td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if (empty($votes)): ?><tr><td colspan="3" class="text-center text-muted py-3">No votes recorded</td></tr><?php endif; ?>
                    </tbody>
                </table>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>