<div class="d-flex justify-content-between align-items-center mb-4">
    <h4 class="mb-0"><i class="fas fa-tags me-2"></i><?= lang('cards.carnet_types') ?></h4>
    <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#ctModal" onclick="resetCtForm()"><i class="fas fa-plus me-1"></i>Add Type</button>
</div>

<div class="card"><div class="card-body"><div class="table-responsive">
    <table class="table table-striped">
        <thead><tr><th>Code</th><th>Name (EN)</th><th>Name (AR)</th><th>Entries</th><th>Validity</th><th>Price</th><th>Active</th><th>Actions</th></tr></thead>
        <tbody>
        <?php foreach ($types as $t): ?>
        <tr>
            <td><code><?= e($t['type_code']) ?></code></td>
            <td><?= e($t['name_en']) ?></td>
            <td><?= e($t['name_ar']) ?></td>
            <td><?= (int)$t['max_entries'] ?></td>
            <td><?= $t['validity_days'] ? $t['validity_days'].' days' : 'N/A' ?></td>
            <td><?= number_format($t['price'], 2) ?></td>
            <td><?= $t['is_active'] ? '<span class="badge bg-success">Yes</span>' : '<span class="badge bg-secondary">No</span>' ?></td>
            <td><button class="btn btn-sm btn-outline-warning btn-edit-ct" data-ct='<?= e(json_encode($t)) ?>'><i class="fas fa-edit"></i></button></td>
        </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div></div></div>

<div class="modal fade" id="ctModal" tabindex="-1"><div class="modal-dialog"><div class="modal-content">
    <form id="ctForm"><?= csrf_field() ?><input type="hidden" name="_ct_id" id="ctId">
        <div class="modal-header"><h5 class="modal-title" id="ctModalTitle">Add Carnet Type</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
        <div class="modal-body">
            <div class="row g-3">
                <div class="col-12"><label class="form-label">Code *</label><input type="text" name="type_code" class="form-control" id="ctCode" required></div>
                <div class="col-md-6"><label class="form-label">Name (EN) *</label><input type="text" name="name_en" class="form-control" required></div>
                <div class="col-md-6"><label class="form-label">Name (AR) *</label><input type="text" name="name_ar" class="form-control" required></div>
                <div class="col-md-4"><label class="form-label">Max Entries *</label><input type="number" name="max_entries" class="form-control" min="1" required></div>
                <div class="col-md-4"><label class="form-label">Validity (days)</label><input type="number" name="validity_days" class="form-control" min="1"></div>
                <div class="col-md-4"><label class="form-label">Price *</label><input type="number" name="price" class="form-control" step="0.01" min="0" required></div>
                <div class="col-md-4"><label class="form-label">Active</label><select name="is_active" class="form-select"><option value="1">Yes</option><option value="0">No</option></select></div>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?= lang('common.cancel') ?></button>
            <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i><?= lang('common.save') ?></button>
        </div>
    </form>
</div></div></div>

<?php $__view->push('scripts') ?>
<script>
function resetCtForm(){ $('#ctForm')[0].reset(); $('#ctId').val(''); $('#ctCode').prop('disabled',false); $('#ctModalTitle').text('Add Carnet Type'); }
$(document).on('click', '.btn-edit-ct', function(){
    const t = $(this).data('ct'); $('#ctId').val(t.id); $('#ctCode').val(t.type_code).prop('disabled',true);
    Object.keys(t).forEach(k => { const el=$(`#ctForm [name="${k}"]`); if(el.length) el.val(t[k]); });
    $('#ctModalTitle').text('Edit Carnet Type'); new bootstrap.Modal('#ctModal').show();
});
$('#ctForm').on('submit', function(e){
    e.preventDefault(); const id = $('#ctId').val();
    $.post(id ? '/carnets/types/'+id : '/carnets/types', $(this).serialize(), function(res){
        if(res.success) location.reload(); else Swal.fire({icon:'error',text:res.message||JSON.stringify(res.errors)});
    });
});
</script>
<?php $__view->endPush() ?>