<div class="d-flex justify-content-between align-items-center mb-4">
    <h4 class="mb-0"><i class="fas fa-map-marker-alt me-2"></i><?= lang('cards.access_points') ?></h4>
    <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#pointModal" onclick="resetPointForm()"><i class="fas fa-plus me-1"></i>Add Point</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>Type</th><th>Hardware</th><th>Direction</th><th>Facility</th><th>Active</th><th>Actions</th></tr></thead>
                <tbody>
                <?php foreach ($points as $p): ?>
                <tr>
                    <td><code><?= e($p['point_code']) ?></code></td>
                    <td><?= e($p['name_en']) ?></td>
                    <td><?= e($p['name_ar']) ?></td>
                    <td><span class="badge bg-info"><?= e($p['point_type']) ?></span></td>
                    <td><?= e($p['hardware_type']) ?></td>
                    <td><?= e($p['direction']) ?></td>
                    <td><?= e($p['facility_name_en'] ?? '-') ?></td>
                    <td><?= $p['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-point" data-point='<?= e(json_encode($p)) ?>'><i class="fas fa-edit"></i></button></td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal fade" id="pointModal" tabindex="-1">
    <div class="modal-dialog modal-lg"><div class="modal-content">
        <form id="pointForm"><?= csrf_field() ?>
            <input type="hidden" name="_point_id" id="pointId">
            <div class="modal-header"><h5 class="modal-title" id="pointModalTitle">Add Access Point</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-md-4"><label class="form-label">Code *</label><input type="text" name="point_code" class="form-control" id="pointCode" required></div>
                    <div class="col-md-4"><label class="form-label">Name (EN) *</label><input type="text" name="name_en" class="form-control" required></div>
                    <div class="col-md-4"><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">Type *</label><select name="point_type" class="form-select" required><option value="main_gate">Main Gate</option><option value="facility_entrance">Facility Entrance</option><option value="parking">Parking</option><option value="pool">Pool</option><option value="gym">Gym</option><option value="court">Court</option><option value="other">Other</option></select></div>
                    <div class="col-md-4"><label class="form-label">Hardware *</label><select name="hardware_type" class="form-select" required><option value="manual">Manual</option><option value="turnstile">Turnstile</option><option value="gate">Gate</option><option value="door">Door</option><option value="barrier">Barrier</option><option value="scanner">Scanner</option></select></div>
                    <div class="col-md-4"><label class="form-label">Direction *</label><select name="direction" class="form-select" required><option value="both">Both</option><option value="entry">Entry Only</option><option value="exit">Exit Only</option></select></div>
                    <div class="col-md-6"><label class="form-label">Facility</label><select name="facility_id" class="form-select"><option value="">None</option><?php foreach ($facilities as $f): ?><option value="<?= $f['id'] ?>"><?= e($f['name_en']) ?></option><?php endforeach; ?></select></div>
                    <div class="col-md-6"><label class="form-label">Location</label><input type="text" name="location" class="form-control"></div>
                    <div class="col-md-3"><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 resetPointForm(){ $('#pointForm')[0].reset(); $('#pointId').val(''); $('#pointCode').prop('disabled',false); $('#pointModalTitle').text('Add Access Point'); }
$(document).on('click', '.btn-edit-point', function(){
    const p = $(this).data('point');
    $('#pointId').val(p.id); $('#pointCode').val(p.point_code).prop('disabled',true);
    Object.keys(p).forEach(k => { const el = $(`#pointForm [name="${k}"]`); if(el.length) el.val(p[k]); });
    $('#pointModalTitle').text('Edit Access Point');
    new bootstrap.Modal('#pointModal').show();
});
$('#pointForm').on('submit', function(e){
    e.preventDefault();
    const id = $('#pointId').val();
    $.post(id ? '/access/points/'+id : '/access/points', $(this).serialize(), function(res){
        if(res.success) location.reload(); else Swal.fire({icon:'error',title:'Error',text:res.message||JSON.stringify(res.errors)});
    });
});
</script>
<?php $__view->endPush() ?>