<?php
/**
 * @var array $rules
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-cog me-2 text-gold"></i>Loyalty Rules</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">Manage Rules</h6>
        </div>
        <div class="card-header-right">
            <button type="button" class="btn btn-sm btn-gold" data-bs-toggle="modal" data-bs-target="#ruleModal">
                <i class="fas fa-plus me-1"></i>Add Rule
            </button>
        </div>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Name</th>
                        <th>Type</th>
                        <th class="text-end">Points Per <?= APP_CURRENCY_SYMBOL ?></th>
                        <th class="text-end">Amount Required</th>
                        <th class="text-end">Max Points</th>
                        <th>Status</th>
                        <th width="80" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rules)): ?>
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">
                                <i class="fas fa-cog fa-2x mb-2"></i>
                                <p class="mb-0">No rules defined</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = 1; ?>
                        <?php foreach ($rules as $rule): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><strong><?= e($rule->name) ?></strong></td>
                                <td>
                                    <span class="badge bg-<?= $rule->type === 'earning' ? 'success' : 'warning' ?>">
                                        <?= e(ucfirst($rule->type)) ?>
                                    </span>
                                </td>
                                <td class="text-end"><?= (int)$rule->points_per_amount ?></td>
                                <td class="text-end"><?= currency($rule->amount_required) ?></td>
                                <td class="text-end"><?= $rule->max_points ? number_format((int)$rule->max_points) : 'Unlimited' ?></td>
                                <td>
                                    <span class="badge bg-<?= $rule->is_active ? 'success' : 'secondary' ?>">
                                        <?= $rule->is_active ? 'Active' : 'Inactive' ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <button type="button" class="btn btn-sm btn-outline-gold btn-edit-rule"
                                            data-id="<?= $rule->id ?>"
                                            data-name="<?= e($rule->name) ?>"
                                            data-type="<?= $rule->type ?>"
                                            data-points_per_amount="<?= (int)$rule->points_per_amount ?>"
                                            data-amount_required="<?= $rule->amount_required ?>"
                                            data-max_points="<?= (int)$rule->max_points ?>"
                                            data-is_active="<?= $rule->is_active ?>"
                                            title="Edit">
                                        <i class="fas fa-edit"></i>
                                    </button>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal fade" id="ruleModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content bg-dark">
            <div class="modal-header border-secondary">
                <h6 class="modal-title"><i class="fas fa-plus-circle text-gold me-2"></i><span id="ruleModalTitle">Add</span> Rule</h6>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="<?= url('loyalty/rules/store') ?>">
                <?= csrf_field() ?>
                <input type="hidden" name="id" id="ruleId">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Rule Name</label>
                        <input type="text" name="name" id="ruleName" class="form-control bg-transparent" required maxlength="100">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Type</label>
                        <select name="type" id="ruleType" class="form-select bg-transparent" required>
                            <option value="earning">Earning</option>
                            <option value="redeeming">Redeeming</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Points Per Amount</label>
                        <input type="number" step="0.01" name="points_per_amount" id="rulePointsPerAmount" class="form-control bg-transparent" required min="0">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Amount Required (<?= APP_CURRENCY_SYMBOL ?>)</label>
                        <input type="number" step="0.01" name="amount_required" id="ruleAmountRequired" class="form-control bg-transparent" required min="0">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Max Points (0 = Unlimited)</label>
                        <input type="number" name="max_points" id="ruleMaxPoints" class="form-control bg-transparent" min="0">
                    </div>
                    <div class="mb-3">
                        <div class="form-check form-switch">
                            <input type="hidden" name="is_active" value="0">
                            <input type="checkbox" name="is_active" id="ruleIsActive" class="form-check-input" value="1" checked>
                            <label class="form-check-label" for="ruleIsActive">Active</label>
                        </div>
                    </div>
                </div>
                <div class="modal-footer border-secondary">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-gold">Save Rule</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php $inlineScripts = <<<'EOS'
$('.btn-edit-rule').click(function() {
    $('#ruleModalTitle').text('Edit');
    $('#ruleId').val($(this).data('id'));
    $('#ruleName').val($(this).data('name'));
    $('#ruleType').val($(this).data('type'));
    $('#rulePointsPerAmount').val($(this).data('points_per_amount'));
    $('#ruleAmountRequired').val($(this).data('amount_required'));
    $('#ruleMaxPoints').val($(this).data('max_points'));
    $('#ruleIsActive').prop('checked', $(this).data('is_active') == 1);
    $('#ruleModal').modal('show');
});

$('#ruleModal').on('hidden.bs.modal', function() {
    $('#ruleModalTitle').text('Add');
    $('#ruleId').val('');
    $('#ruleName').val('');
    $('#ruleType').val('earning');
    $('#rulePointsPerAmount').val('');
    $('#ruleAmountRequired').val('');
    $('#ruleMaxPoints').val('');
    $('#ruleIsActive').prop('checked', true);
});
EOS;
?>
