<h4 class="mb-4"><i class="fas fa-list me-2"></i><?= lang('admin.lookups') ?></h4>
<div class="row g-4">
    <div class="col-md-4"><div class="card"><div class="card-header">Categories</div><div class="card-body">
        <div class="list-group list-group-flush">
            <?php foreach ($categories as $c): ?>
            <a href="#" class="list-group-item list-group-item-action category-item" data-id="<?= $c['id'] ?>" data-name="<?= e($c['category_name_en']) ?>">
                <div class="d-flex justify-content-between align-items-center">
                    <div><strong><?= e($c['category_name_en']) ?></strong><br><small class="text-muted"><?= e($c['category_code']) ?></small></div>
                    <?php if ($c['is_system']): ?><span class="badge bg-info">System</span><?php endif; ?>
                </div>
            </a>
            <?php endforeach; ?>
        </div>
    </div></div></div>
    <div class="col-md-8"><div class="card"><div class="card-header d-flex justify-content-between"><span id="values-title">Select a category</span><button class="btn btn-sm btn-primary d-none" id="btn-add-value"><i class="fas fa-plus me-1"></i>Add Value</button></div><div class="card-body">
        <table id="values-table" class="table table-sm table-striped w-100" data-url="/admin/lookups/values/datatable"
               data-columns='[{"data":"code"},{"data":"value_en"},{"data":"value_ar"},{"data":"sort"},{"data":"active"},{"data":"actions","orderable":false}]'>
            <thead><tr><th>Code</th><th>English</th><th>Arabic</th><th>Sort</th><th>Status</th><th>Actions</th></tr></thead>
        </table>
    </div></div></div>
</div>
<div class="modal fade" id="valueModal" tabindex="-1"><div class="modal-dialog"><div class="modal-content">
    <div class="modal-header"><h5 class="modal-title">Lookup Value</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
    <div class="modal-body">
        <form id="value-form"><input type="hidden" name="_token" value="<?= csrf_token() ?>"><input type="hidden" name="category_id" id="vf_cat_id"><input type="hidden" name="value_id" id="vf_id">
            <div class="mb-3"><label class="form-label">Code</label><input type="text" name="lookup_code" id="vf_code" class="form-control" required></div>
            <div class="mb-3"><label class="form-label">English</label><input type="text" name="value_en" id="vf_en" class="form-control" required></div>
            <div class="mb-3"><label class="form-label">Arabic</label><input type="text" name="value_ar" id="vf_ar" class="form-control" required></div>
            <div class="mb-3"><label class="form-label">Sort Order</label><input type="number" name="sort_order" id="vf_sort" class="form-control" value="0"></div>
            <div class="form-check"><input class="form-check-input" type="checkbox" name="is_active" value="1" id="vf_active" checked><label class="form-check-label">Active</label></div>
        </form>
    </div>
    <div class="modal-footer"><button class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button><button class="btn btn-primary" id="btn-save-value">Save</button></div>
</div></div></div>
<?php $__view->push('scripts') ?>
<script>
let currentCatId=null;
const vt=DataTableHelper.init('values-table',{paging:false,ajax:{data:function(d){d.category_id=currentCatId||0;}}});
$('.category-item').click(function(e){e.preventDefault();$('.category-item').removeClass('active');$(this).addClass('active');
    currentCatId=$(this).data('id');$('#values-title').text($(this).data('name'));$('#btn-add-value').removeClass('d-none');DataTableHelper.reload('values-table');});
$('#btn-add-value').click(function(){$('#vf_id').val('');$('#vf_cat_id').val(currentCatId);$('#vf_code').val('').prop('readonly',false);$('#vf_en').val('');$('#vf_ar').val('');$('#vf_sort').val(0);$('#vf_active').prop('checked',true);new bootstrap.Modal('#valueModal').show();});
$(document).on('click','.btn-edit-value',function(){$('#vf_id').val($(this).data('id'));$('#vf_cat_id').val(currentCatId);$('#vf_code').val($(this).data('code')).prop('readonly',true);$('#vf_en').val($(this).data('en'));$('#vf_ar').val($(this).data('ar'));$('#vf_sort').val($(this).data('sort'));$('#vf_active').prop('checked',$(this).data('active')=='1');new bootstrap.Modal('#valueModal').show();});
$('#btn-save-value').click(function(){const id=$('#vf_id').val();const url=id?'/admin/lookups/values/'+id+'/update':'/admin/lookups/values/store';
    $.post(url,$('#value-form').serialize(),function(r){if(r.success){bootstrap.Modal.getInstance(document.getElementById('valueModal')).hide();FormHelper.showToast('success',r.message);DataTableHelper.reload('values-table');}else FormHelper.showToast('error',r.message);},'json');});
$(document).on('click','.btn-delete-value',function(){if(!confirm('Delete?'))return;$.post('/admin/lookups/values/'+$(this).data('id')+'/delete',{_token:'<?= csrf_token() ?>'},function(r){if(r.success){FormHelper.showToast('success',r.message);DataTableHelper.reload('values-table');}else FormHelper.showToast('error',r.message);},'json');});
</script>
<?php $__view->endPush() ?>