<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h4 class="mb-1"><?= e($offer['offer_code']) ?> — <?= e($offer['name_en']) ?> <?= status_badge($offer['status']) ?></h4>
        <p class="text-muted mb-0"><?= format_date($offer['start_date']) ?> → <?= format_date($offer['end_date']) ?> | Uses: <?= $offer['current_uses'] ?><?= $offer['max_uses'] ? '/' . $offer['max_uses'] : '' ?></p>
    </div>
    <div class="d-flex gap-2">
        <?php if (auth_can('offers.manage')): ?>
        <a href="/offers/<?= $offer['id'] ?>/edit" class="btn btn-warning btn-sm"><i class="fas fa-edit me-1"></i>Edit</a>
        <?php endif; ?>
        <a href="/offers" class="btn btn-secondary btn-sm"><i class="fas fa-arrow-left me-1"></i>Back</a>
    </div>
</div>

<div class="row g-3">
    <div class="col-md-4">
        <div class="card">
            <div class="card-header"><h6 class="mb-0">Offer Details</h6></div>
            <div class="card-body">
                <table class="table table-sm table-borderless mb-0">
                    <tr><td class="text-muted">Type</td><td><?= ucfirst(str_replace('_', ' ', $offer['offer_type'])) ?></td></tr>
                    <?php if ($offer['discount_method']): ?>
                    <tr><td class="text-muted">Discount</td><td><?= $offer['discount_method'] === 'percentage' ? format_number($offer['discount_value']) . '%' : money($offer['discount_value']) ?></td></tr>
                    <?php endif; ?>
                    <tr><td class="text-muted">Approval</td><td><?= $offer['requires_approval'] ? 'Required' : 'Not required' ?></td></tr>
                </table>
                <?php if (!empty($membershipTypes)): ?>
                <hr>
                <h6 class="small text-muted">Target Types</h6>
                <?php foreach ($membershipTypes as $mt): ?>
                <span class="badge bg-info me-1"><?= e($mt['name_en']) ?></span>
                <?php endforeach; ?>
                <?php endif; ?>

                <?php if (auth_can('offers.manage') && $offer['status'] === 'active'): ?>
                <hr>
                <h6 class="small text-muted">Apply to Member</h6>
                <form method="POST" action="/offers/<?= $offer['id'] ?>/apply" data-ajax="true">
                    <?= csrf_field() ?>
                    <input type="number" name="member_id" class="form-control form-control-sm mb-2" placeholder="Member ID" required>
                    <input type="number" name="discount_amount" class="form-control form-control-sm mb-2" placeholder="Discount Amount" step="0.01" required>
                    <input type="number" name="invoice_id" class="form-control form-control-sm mb-2" placeholder="Invoice ID (optional)">
                    <input type="text" name="notes" class="form-control form-control-sm mb-2" placeholder="Notes">
                    <button type="submit" class="btn btn-success btn-sm w-100"><i class="fas fa-check me-1"></i>Apply Offer</button>
                </form>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <div class="col-md-8">
        <div class="card">
            <div class="card-header"><h6 class="mb-0">Applied To (<?= count($applications) ?>)</h6></div>
            <div class="card-body p-0">
                <table class="table table-sm mb-0">
                    <thead><tr><th>Member</th><th>Date</th><th>Discount</th><th>Invoice</th></tr></thead>
                    <tbody>
                    <?php foreach ($applications as $app): ?>
                    <tr>
                        <td><?= e($app['full_name_en']) ?> <small class="text-muted">(<?= e($app['membership_number']) ?>)</small></td>
                        <td><?= format_date($app['applied_date']) ?></td>
                        <td><?= money($app['discount_amount']) ?></td>
                        <td><?= $app['invoice_id'] ?: '-' ?></td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if (empty($applications)): ?><tr><td colspan="4" class="text-center text-muted py-3">No applications yet</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>