<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h4 class="mb-1"><?= e($transfer['transfer_number']) ?> <?= status_badge($transfer['status']) ?></h4>
        <p class="text-muted mb-0"><?= e($transfer['type_name'] ?? '') ?> — <?= ucfirst(str_replace('_', ' ', $transfer['reason'])) ?></p>
    </div>
    <a href="/transfers" class="btn btn-secondary btn-sm"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<div class="row g-3">
    <div class="col-md-4">
        <div class="card">
            <div class="card-header"><h6 class="mb-0">Transfer Details</h6></div>
            <div class="card-body">
                <table class="table table-sm table-borderless mb-0">
                    <tr><td class="text-muted">From</td><td><strong><?= e($transfer['from_name'] ?? '') ?></strong><br><small><?= e($transfer['from_number'] ?? '') ?></small></td></tr>
                    <tr><td class="text-muted">To</td><td><strong><?= e($transfer['to_name'] ?? $transfer['to_applicant_name'] ?? '') ?></strong><?php if ($transfer['to_applicant_national_id']): ?><br><small>NID: <?= e($transfer['to_applicant_national_id']) ?></small><?php endif; ?></td></tr>
                    <tr><td class="text-muted">Type</td><td><?= e($transfer['membership_type_name'] ?? '') ?></td></tr>
                    <tr><td class="text-muted">Fees</td><td><?= money($transfer['total_fees']) ?></td></tr>
                    <tr><td class="text-muted">Requested</td><td><?= format_date($transfer['request_date']) ?></td></tr>
                    <?php if ($transfer['transfer_date']): ?>
                    <tr><td class="text-muted">Completed</td><td><?= format_date($transfer['transfer_date']) ?></td></tr>
                    <?php endif; ?>
                </table>

                <?php if (auth_can('transfers.manage') && !in_array($transfer['status'], ['completed','cancelled'])): ?>
                <hr>
                <form method="POST" action="/transfers/<?= $transfer['id'] ?>/status" data-ajax="true" class="mb-2">
                    <?= csrf_field() ?>
                    <label class="form-label small">Change Status</label>
                    <select name="status" class="form-select form-select-sm mb-2">
                        <?php foreach (['requested','documents_pending','under_review','board_pending','cancelled'] as $s): ?>
                        <option value="<?= $s ?>" <?= selected($s, $transfer['status']) ?>><?= ucfirst(str_replace('_', ' ', $s)) ?></option>
                        <?php endforeach; ?>
                    </select>
                    <input type="text" name="reason" class="form-control form-control-sm mb-2" placeholder="Reason (optional)">
                    <button type="submit" class="btn btn-sm btn-primary w-100">Update Status</button>
                </form>
                <?php endif; ?>

                <?php if (auth_can('transfers.approve') && $transfer['status'] === 'board_pending'): ?>
                <form method="POST" action="/transfers/<?= $transfer['id'] ?>/approve" data-ajax="true" class="mb-2">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-success w-100"><i class="fas fa-check me-1"></i>Approve Transfer</button>
                </form>
                <?php endif; ?>

                <?php if (auth_can('transfers.approve') && $transfer['status'] === 'approved'): ?>
                <form method="POST" action="/transfers/<?= $transfer['id'] ?>/complete" data-ajax="true">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-success w-100" onclick="return confirm('Complete transfer? This will terminate the original member.')"><i class="fas fa-flag-checkered me-1"></i>Complete Transfer</button>
                </form>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div class="col-md-8">
        <ul class="nav nav-tabs mb-3">
            <li class="nav-item"><a class="nav-link active" data-bs-toggle="tab" href="#tab-docs">Documents</a></li>
            <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-heirs">Heirs</a></li>
            <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-history">History</a></li>
        </ul>
        <div class="tab-content">
            <div class="tab-pane fade show active" id="tab-docs">
                <?php if (auth_can('transfers.manage') && !in_array($transfer['status'], ['completed','cancelled'])): ?>
                <div class="card mb-3">
                    <div class="card-body">
                        <form method="POST" action="/transfers/<?= $transfer['id'] ?>/document" data-ajax="true" enctype="multipart/form-data">
                            <?= csrf_field() ?>
                            <div class="row g-2">
                                <div class="col-md-3"><input type="text" name="document_type" class="form-control" placeholder="Type (e.g. death_cert)" required></div>
                                <div class="col-md-4"><input type="text" name="document_name" class="form-control" placeholder="Document name" required></div>
                                <div class="col-md-3"><input type="file" name="document" class="form-control" required></div>
                                <div class="col-md-2"><button type="submit" class="btn btn-primary w-100"><i class="fas fa-upload"></i></button></div>
                            </div>
                        </form>
                    </div>
                </div>
                <?php endif; ?>
                <div class="card">
                    <div class="card-body p-0">
                        <table class="table table-sm mb-0">
                            <thead><tr><th>Type</th><th>Name</th><th>Size</th><th>Uploaded</th><th>Verified</th></tr></thead>
                            <tbody>
                            <?php foreach ($documents as $doc): ?>
                            <tr>
                                <td><span class="badge bg-secondary"><?= e($doc['document_type']) ?></span></td>
                                <td><?= e($doc['document_name']) ?></td>
                                <td><?= $doc['file_size_bytes'] ? round($doc['file_size_bytes'] / 1024, 1) . ' KB' : '-' ?></td>
                                <td><?= format_datetime($doc['uploaded_at']) ?></td>
                                <td><?= $doc['is_verified'] ? '<i class="fas fa-check text-success"></i>' : '<i class="fas fa-clock text-warning"></i>' ?></td>
                            </tr>
                            <?php endforeach; ?>
                            <?php if (empty($documents)): ?><tr><td colspan="5" class="text-center text-muted py-3">No documents uploaded</td></tr><?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="tab-pane fade" id="tab-heirs">
                <?php if (auth_can('transfers.manage') && $transfer['reason'] === 'inheritance'): ?>
                <div class="card mb-3">
                    <div class="card-body">
                        <form method="POST" action="/transfers/<?= $transfer['id'] ?>/heir" data-ajax="true">
                            <?= csrf_field() ?>
                            <div class="row g-2">
                                <div class="col-md-3"><input type="text" name="heir_name" class="form-control" placeholder="Heir name" required></div>
                                <div class="col-md-2"><input type="text" name="heir_national_id" class="form-control" placeholder="National ID"></div>
                                <div class="col-md-2"><input type="text" name="heir_phone" class="form-control" placeholder="Phone"></div>
                                <div class="col-md-2"><input type="text" name="relationship" class="form-control" placeholder="Relationship" required></div>
                                <div class="col-md-1"><input type="number" name="share_percentage" class="form-control" placeholder="%" value="100" step="0.01" required></div>
                                <div class="col-md-2"><button type="submit" class="btn btn-primary w-100"><i class="fas fa-plus me-1"></i>Add</button></div>
                            </div>
                        </form>
                    </div>
                </div>
                <?php endif; ?>
                <div class="card">
                    <div class="card-body p-0">
                        <table class="table table-sm mb-0">
                            <thead><tr><th>Name</th><th>Relationship</th><th>Share %</th><th>Phone</th><th>Primary</th><th>Consent</th><?php if (auth_can('transfers.manage')): ?><th></th><?php endif; ?></tr></thead>
                            <tbody>
                            <?php foreach ($heirs as $h): ?>
                            <tr>
                                <td><?= e($h['heir_name']) ?></td>
                                <td><?= e($h['relationship']) ?></td>
                                <td><?= format_number($h['share_percentage']) ?>%</td>
                                <td><?= e($h['heir_phone'] ?? '-') ?></td>
                                <td><?= $h['is_primary_heir'] ? '<i class="fas fa-star text-warning"></i>' : '' ?></td>
                                <td><?= $h['consent_given'] ? '<i class="fas fa-check text-success"></i>' : '<i class="fas fa-times text-danger"></i>' ?></td>
                                <?php if (auth_can('transfers.manage')): ?>
                                <td>
                                    <form method="POST" action="/transfers/<?= $transfer['id'] ?>/heir-delete" data-ajax="true" class="d-inline">
                                        <?= csrf_field() ?>
                                        <input type="hidden" name="heir_id" value="<?= $h['id'] ?>">
                                        <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash"></i></button>
                                    </form>
                                </td>
                                <?php endif; ?>
                            </tr>
                            <?php endforeach; ?>
                            <?php if (empty($heirs)): ?><tr><td colspan="7" class="text-center text-muted py-3">No heirs recorded</td></tr><?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="tab-pane fade" id="tab-history">
                <div class="card">
                    <div class="card-body p-0">
                        <table class="table table-sm mb-0">
                            <thead><tr><th>Date</th><th>From</th><th>To</th><th>By</th><th>Notes</th></tr></thead>
                            <tbody>
                            <?php foreach ($history as $h): ?>
                            <tr>
                                <td><?= format_datetime($h['changed_at']) ?></td>
                                <td><?= $h['from_status'] ? status_badge($h['from_status']) : '-' ?></td>
                                <td><?= status_badge($h['to_status']) ?></td>
                                <td><?= e($h['changed_by_name'] ?? '-') ?></td>
                                <td><?= e($h['notes'] ?? '') ?></td>
                            </tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>