<div class="d-flex justify-content-between align-items-center mb-3">
    <h4 class="mb-0"><i class="fas fa-gavel me-2"></i><?= lang('subs.freeze_policies') ?></h4>
    <?php if (auth_can('freezes.approve')): ?>
    <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#policy-modal" id="btn-add-policy"><i class="fas fa-plus me-1"></i>Add Policy</button>
    <?php endif; ?>
</div>

<div class="card">
    <div class="card-body">
        <table id="policies-table" class="table table-striped w-100"
               data-url="/api/freezes/policies/datatable"
               data-columns='<?= json_encode([
                   ["data"=>"policy_code","title"=>"Code"],["data"=>"name_en","title"=>"Name"],["data"=>"name_ar","title"=>"Name (AR)"],
                   ["data"=>"type_name","title"=>"Type"],["data"=>"max_days","title"=>"Max Days"],["data"=>"max_times","title"=>"Max/Year"],
                   ["data"=>"fee_type","title"=>"Fee Type"],["data"=>"fee_amount","title"=>"Fee"],["data"=>"is_active","title"=>"Active"],
                   ["data"=>"actions","title"=>"","orderable"=>false]
               ]) ?>'>
            <thead><tr><th>Code</th><th>Name</th><th>Name (AR)</th><th>Type</th><th>Max Days</th><th>Max/Year</th><th>Fee Type</th><th>Fee</th><th>Active</th><th></th></tr></thead>
        </table>
    </div>
</div>

<!-- Policy Modal -->
<div class="modal fade" id="policy-modal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header"><h5 class="modal-title" id="policy-modal-title">Add Policy</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
            <form id="policy-form" method="POST" action="/freezes/policies" data-ajax="true">
                <?= csrf_field() ?>
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-md-4"><label class="form-label">Code *</label><input type="text" name="policy_code" class="form-control" 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 dir="rtl"></div>
                        <div class="col-md-6"><label class="form-label">Membership Type</label>
                            <select name="membership_type_id" class="form-select"><option value="">All Types</option>
                                <?php foreach ($types as $t): ?><option value="<?= $t['id'] ?>"><?= e($t['name_en']) ?></option><?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-3"><label class="form-label">Min Days *</label><input type="number" name="min_days" class="form-control" value="1" min="1" required></div>
                        <div class="col-md-3"><label class="form-label">Max Days</label><input type="number" name="max_days" class="form-control"></div>
                        <div class="col-md-3"><label class="form-label">Max Times/Year</label><input type="number" name="max_times_per_year" class="form-control"></div>
                        <div class="col-md-3"><label class="form-label">Max Total Days/Year</label><input type="number" name="max_total_days_per_year" class="form-control"></div>
                        <div class="col-md-3"><label class="form-label">Fee Type *</label>
                            <select name="fee_type" class="form-select" required>
                                <option value="none">None</option><option value="fixed">Fixed</option>
                                <option value="per_day">Per Day</option><option value="percentage_of_subscription">% of Sub</option>
                            </select>
                        </div>
                        <div class="col-md-3"><label class="form-label">Fee Amount</label><input type="number" step="0.01" name="fee_amount" class="form-control" value="0"></div>
                        <div class="col-md-3"><div class="form-check mt-4"><input type="checkbox" name="requires_approval" value="1" class="form-check-input" id="pol-approval"><label class="form-check-label" for="pol-approval">Requires Approval</label></div></div>
                        <div class="col-md-3"><div class="form-check mt-4"><input type="checkbox" name="extend_subscription" value="1" class="form-check-input" id="pol-extend" checked><label class="form-check-label" for="pol-extend">Extend Subscription</label></div></div>
                        <div class="col-md-3"><div class="form-check"><input type="checkbox" name="is_active" value="1" class="form-check-input" id="pol-active" checked><label class="form-check-label" for="pol-active">Active</label></div></div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i>Save</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php $__view->push('scripts') ?>
<script>
$(function(){
    DataTableHelper.init('policies-table');
    $('#btn-add-policy').on('click', function(){ $('#policy-modal-title').text('Add Policy'); $('#policy-form')[0].reset(); $('#policy-form').attr('action','/freezes/policies'); });
    $(document).on('click', '.btn-edit-policy', function(){
        var id=$(this).data('id');
        $.get('/api/freezes/policies/'+id, function(res){
            if(res.success){
                var d=res.data; $('#policy-modal-title').text('Edit Policy'); $('#policy-form').attr('action','/freezes/policies/'+id);
                Object.keys(d).forEach(function(k){ var el=$('#policy-form [name='+k+']'); if(el.length){ if(el.is(':checkbox')){el.prop('checked',d[k]==1);}else{el.val(d[k]);} }});
                new bootstrap.Modal('#policy-modal').show();
            }
        });
    });
    $(document).on('ajaxSuccess', function(e,xhr,s){ if(s.url&&s.url.includes('/freezes/policies')){ DataTableHelper.reload('policies-table'); bootstrap.Modal.getInstance(document.getElementById('policy-modal'))?.hide(); }});
});
</script>
<?php $__view->endPush() ?>