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

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-striped">
                <thead><tr><th>Rule Name</th><th>Access Point</th><th>Membership Type</th><th>Status Req.</th><th>Card Req.</th><th>Priority</th><th>Active</th><th>Actions</th></tr></thead>
                <tbody>
                <?php foreach ($rules as $r): ?>
                <tr>
                    <td><?= e($r['rule_name']) ?></td>
                    <td><?= e($r['point_name_en']) ?></td>
                    <td><?= e($r['memtype_name_en'] ?? 'All') ?></td>
                    <td><?= e($r['member_status_required']) ?></td>
                    <td><?= $r['card_required'] ? '<i class="fas fa-check text-success"></i>' : '<i class="fas fa-times text-muted"></i>' ?></td>
                    <td><?= (int)$r['priority'] ?></td>
                    <td><?= $r['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 me-1 btn-edit-rule" data-rule='<?= e(json_encode($r)) ?>'><i class="fas fa-edit"></i></button>
                        <form method="POST" action="/access/rules/<?= $r['id'] ?>/delete" class="d-inline" onsubmit="return confirm('Delete this rule?')">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash"></i></button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal fade" id="ruleModal" tabindex="-1">
    <div class="modal-dialog modal-lg"><div class="modal-content">
        <form id="ruleForm"><?= csrf_field() ?>
            <input type="hidden" name="_rule_id" id="ruleId">
            <div class="modal-header"><h5 class="modal-title" id="ruleModalTitle">Add Access Rule</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-6"><label class="form-label">Access Point *</label><select name="access_point_id" class="form-select" required><option value="">-- Select --</option><?php foreach ($points as $p): ?><option value="<?= $p['id'] ?>"><?= e($p['name_en']) ?></option><?php endforeach; ?></select></div>
                    <div class="col-md-6"><label class="form-label">Rule Name *</label><input type="text" name="rule_name" class="form-control" required></div>
                    <div class="col-md-4"><label class="form-label">Membership Type</label><select name="membership_type_id" class="form-select"><option value="">All Types</option><?php foreach ($membershipTypes as $mt): ?><option value="<?= $mt['id'] ?>"><?= e($mt['name_en']) ?></option><?php endforeach; ?></select></div>
                    <div class="col-md-4"><label class="form-label">Member Status Required</label><input type="text" name="member_status_required" class="form-control" value="active"></div>
                    <div class="col-md-4"><label class="form-label">Priority</label><input type="number" name="priority" class="form-control" value="0" min="0"></div>
                    <div class="col-md-3"><label class="form-label">Subscription Req.</label><select name="subscription_required" class="form-select"><option value="1">Yes</option><option value="0">No</option></select></div>
                    <div class="col-md-3"><label class="form-label">Card Req.</label><select name="card_required" class="form-select"><option value="1">Yes</option><option value="0">No</option></select></div>
                    <div class="col-md-3"><label class="form-label">Allow Dependents</label><select name="allow_dependents" class="form-select"><option value="1">Yes</option><option value="0">No</option></select></div>
                    <div class="col-md-3"><label class="form-label">Allow Guests</label><select name="allow_guests" class="form-select"><option value="0">No</option><option value="1">Yes</option></select></div>
                    <div class="col-md-4"><label class="form-label">Max Daily Entries</label><input type="number" name="max_daily_entries" class="form-control" min="1" placeholder="Unlimited"></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 resetRuleForm(){ $('#ruleForm')[0].reset(); $('#ruleId').val(''); $('#ruleModalTitle').text('Add Access Rule'); }
$(document).on('click', '.btn-edit-rule', function(){
    const r = $(this).data('rule'); $('#ruleId').val(r.id);
    Object.keys(r).forEach(k => { const el = $(`#ruleForm [name="${k}"]`); if(el.length) el.val(r[k]); });
    $('#ruleModalTitle').text('Edit Access Rule'); new bootstrap.Modal('#ruleModal').show();
});
$('#ruleForm').on('submit', function(e){
    e.preventDefault();
    const id = $('#ruleId').val();
    $.post(id ? '/access/rules/'+id : '/access/rules', $(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() ?>