<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>

<div class="container-fluid mt-4">
    <h2 class="text-center mt-4 mb-3">Discounts</h2>
 <div class="d-flex gap-2 mb-3 justify-content-end">
        <div>
            <a href="/discount/create" class="btn btn-primary">Create New Voucher</a>
            <a href="/discount/apply" class="btn btn-success">Apply Voucher to Parents</a>
        </div>
    </div>
    <div class="table-responsive">
        <table id="discountsTable" class="table table-striped w-100" style="table-layout:auto;">
            <thead>
                <tr>
                    <th>Code</th>
                    <th>Type</th>
                    <th>Value</th>
                    <th>Max Uses</th>
                    <th>Times Used</th>
                    <th>Validity</th>
                    <th>Status</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $formatValidityDate = function ($value) {
                    $value = (string)($value ?? '');
                    if ($value === '') {
                        return 'N/A';
                    }
                    if (preg_match('/^(\\d{4})-(\\d{2})-(\\d{2})$/', $value, $m)) {
                        return $m[2] . '-' . $m[3] . '-' . $m[1];
                    }
                    try {
                        return local_date($value, 'm-d-Y');
                    } catch (Throwable $e) {
                        return $value;
                    }
                };
                ?>
                <?php foreach ($vouchers as $v): ?>
                    <tr>
                        <td><?= esc($v['code']) ?></td>
                        <td><?= esc(ucfirst($v['discount_type'])) ?></td>
                        <td>
                            <?= esc($v['discount_type'] === 'percent' ? $v['discount_value'] . '%' : '$' . number_format($v['discount_value'], 2)) ?>
                        </td>
                        <td><?= esc($v['max_uses'] ?? '∞') ?></td>
                        <td><?= esc($v['times_used']) ?></td>
                        <td>
                            <?= esc($formatValidityDate($v['valid_from'] ?? '')) ?> → <?= esc($formatValidityDate($v['valid_until'] ?? '')) ?>
                        </td>
                        <td>
                            <span class="badge <?= $v['is_active'] ? 'bg-success' : 'bg-secondary' ?>">
                                <?= $v['is_active'] ? 'Active' : 'Inactive' ?>
                            </span>
                        </td>
                        <td>
                            <a href="/discount/editVoucher/<?= $v['id'] ?>" class="btn btn-sm btn-warning">Edit</a>
                        </td>
                    </tr>
                <?php endforeach ?>
            </tbody>
        </table>
    </div>
   
</div>

<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
    $(document).ready(function() {
        $('#discountsTable').DataTable({
            pageLength: 100,
            order: [
                [0, 'asc']
            ],
            autoWidth: false,
            responsive: true,
            columns: [
                null, null, null, null, null, {
                    width: '20%'
                },
                null, {
                    orderable: false,
                    width: '120px'
                }
            ]
        });
    });
</script>
<?= $this->endSection() ?>
