<div class="row g-4">
    <div class="col-md-4">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><?= e($carnet['carnet_number']) ?></h5>
                <?= status_badge($carnet['status']) ?>
            </div>
            <div class="card-body">
                <div class="text-center mb-3">
                    <div class="display-4 fw-bold <?= $carnet['remaining_entries'] <= 0 ? 'text-danger' : 'text-success' ?>"><?= (int)$carnet['remaining_entries'] ?></div>
                    <small class="text-muted">of <?= (int)$carnet['total_entries'] ?> remaining</small>
                    <div class="progress mt-2" style="height:8px;">
                        <?php $pct = $carnet['total_entries'] > 0 ? round(($carnet['used_entries'] / $carnet['total_entries']) * 100) : 0; ?>
                        <div class="progress-bar <?= $pct > 80 ? 'bg-danger' : ($pct > 50 ? 'bg-warning' : 'bg-success') ?>" style="width:<?= $pct ?>%"></div>
                    </div>
                </div>
                <table class="table table-sm">
                    <tr><th>Type</th><td><?= e($carnet['type_name_en']) ?></td></tr>
                    <tr><th>Member</th><td><a href="/members/show/<?= $carnet['member_id'] ?>"><?= e($carnet['member_name_en']) ?></a><br><small><?= e($carnet['membership_number']) ?></small></td></tr>
                    <tr><th>Purchase Date</th><td><?= e($carnet['purchase_date']) ?></td></tr>
                    <tr><th>Expiry</th><td><?= e($carnet['expiry_date'] ?? 'N/A') ?></td></tr>
                    <tr><th>Price Paid</th><td><?= money($carnet['price_paid']) ?></td></tr>
                </table>

                <?php if ($carnet['status'] === 'active' && $carnet['remaining_entries'] > 0): ?>
                <form id="entryForm" class="mt-3">
                    <?= csrf_field() ?>
                    <div class="mb-2"><label class="form-label">Facility (optional)</label>
                        <select name="facility_id" class="form-select form-select-sm">
                            <option value="">N/A</option>
                        </select>
                    </div>
                    <div class="mb-2"><label class="form-label">Notes</label><input type="text" name="notes" class="form-control form-control-sm"></div>
                    <button type="submit" class="btn btn-success btn-sm w-100"><i class="fas fa-stamp me-1"></i>Record Entry</button>
                </form>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <div class="col-md-8">
        <div class="card"><div class="card-header"><h6 class="mb-0"><i class="fas fa-list me-2"></i>Entry Log (<?= count($entries) ?>)</h6></div><div class="card-body">
            <?php if (empty($entries)): ?>
                <p class="text-muted text-center py-3">No entries recorded yet</p>
            <?php else: ?>
            <div class="table-responsive">
                <table class="table table-sm table-striped">
                    <thead><tr><th>#</th><th>Date</th><th>Time</th><th>Facility</th><th>Status</th><th>Actions</th></tr></thead>
                    <tbody>
                    <?php foreach ($entries as $e_entry): ?>
                    <tr class="<?= $e_entry['is_voided'] ? 'text-decoration-line-through text-muted' : '' ?>">
                        <td><?= (int)$e_entry['entry_number'] ?></td>
                        <td><?= e($e_entry['entry_date']) ?></td>
                        <td><?= e($e_entry['entry_time']) ?></td>
                        <td><?= e($e_entry['facility_name_en'] ?? '-') ?></td>
                        <td><?= $e_entry['is_voided'] ? '<span class="badge bg-danger">Voided</span>' : '<span class="badge bg-success">Valid</span>' ?></td>
                        <td>
                            <?php if (!$e_entry['is_voided']): ?>
                            <button class="btn btn-sm btn-outline-danger btn-void-entry" data-id="<?= $e_entry['id'] ?>"><i class="fas fa-undo"></i></button>
                            <?php else: ?>
                            <small><?= e($e_entry['void_reason'] ?? '') ?></small>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
        </div></div>
    </div>
</div>

<?php $__view->push('scripts') ?>
<script>
$(function(){
    $('#entryForm').on('submit', function(e){
        e.preventDefault();
        $.post('/carnets/<?= $carnet['id'] ?>/entry', $(this).serialize(), function(res){
            if(res.success){ Swal.fire({icon:'success',title:res.message,timer:1000,showConfirmButton:false}).then(()=>location.reload()); }
            else { Swal.fire({icon:'error',title:'Error',text:res.message}); }
        });
    });

    $(document).on('click', '.btn-void-entry', function(){
        const entryId = $(this).data('id');
        Swal.fire({title:'Void this entry?',input:'text',inputPlaceholder:'Reason for voiding',showCancelButton:true,confirmButtonText:'Void',confirmButtonColor:'#dc3545'}).then(r=>{
            if(r.isConfirmed){
                $.post('/carnets/<?= $carnet['id'] ?>/void-entry/'+entryId, {_token:$('meta[name="csrf-token"]').attr('content'),reason:r.value}, function(res){
                    if(res.success){ location.reload(); } else { Swal.fire({icon:'error',text:res.message}); }
                });
            }
        });
    });
});
</script>
<?php $__view->endPush() ?>