<div class="row justify-content-center">
    <div class="col-lg-6">
        <div class="card">
            <div class="card-header"><h5 class="mb-0"><i class="fas fa-ticket-alt me-2"></i><?= lang('cards.issue_carnet') ?></h5></div>
            <div class="card-body">
                <form id="carnetForm" data-ajax="true">
                    <?= csrf_field() ?>
                    <div class="mb-3">
                        <label class="form-label">Member <span class="text-danger">*</span></label>
                        <select name="member_id" class="form-select select2" id="carnetMemberSearch" required></select>
                        <div class="invalid-feedback" data-field="member_id"></div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Carnet Type <span class="text-danger">*</span></label>
                        <select name="carnet_type_id" class="form-select" required>
                            <option value="">-- Select --</option>
                            <?php foreach ($carnetTypes as $ct): ?>
                            <option value="<?= $ct['id'] ?>"><?= e($ct['name_en']) ?> (<?= e($ct['name_ar']) ?>) — <?= (int)$ct['max_entries'] ?> entries — <?= number_format($ct['price'], 2) ?></option>
                            <?php endforeach; ?>
                        </select>
                        <div class="invalid-feedback" data-field="carnet_type_id"></div>
                    </div>
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i><?= lang('cards.issue_carnet') ?></button>
                        <a href="/carnets" class="btn btn-secondary"><?= lang('common.cancel') ?></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php $__view->push('scripts') ?>
<script>
$(function(){
    $('#carnetMemberSearch').select2({
        theme:'bootstrap-5', width:'100%',
        ajax:{ url:'/api/members/search', dataType:'json', delay:300, data:function(p){return{q:p.term};}, processResults:function(d){return{results:d.map(m=>({id:m.id,text:m.membership_number+' - '+m.text}))};} },
        minimumInputLength:2, placeholder:'Search member...'
    });
    $('#carnetForm').on('submit', function(e){
        e.preventDefault();
        const form = $(this), btn = form.find('[type="submit"]');
        btn.prop('disabled',true);
        $.ajax({
            url: '/carnets', method: 'POST', data: form.serialize(),
            success: function(res){
                if(res.success){ Swal.fire({icon:'success',title:res.message,timer:1500,showConfirmButton:false}).then(()=>{window.location.href=res.redirect||'/carnets';}); }
                else { if(res.message) Swal.fire({icon:'error',title:'Error',text:res.message}); }
            },
            complete: function(){ btn.prop('disabled',false); }
        });
    });
});
</script>
<?php $__view->endPush() ?>