<?php
/**
 * @var array $customers
 * @var array $tiers
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-user-plus me-2 text-gold"></i>Assign Membership</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-id-card card-icon"></i>
            <h6 class="card-title">Assign New Membership</h6>
        </div>
        <div>
            <span class="text-muted small"><span class="text-danger">*</span> Required fields</span>
        </div>
    </div>
    <div class="card-body">
        <form method="POST" action="<?= url('membership/store') ?>">
            <?= csrf_field() ?>

            <div class="row g-3">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Customer <span class="text-danger">*</span></label>
                        <select name="customer_id" class="form-select bg-transparent <?= errorClass('customer_id') ?>" required>
                            <option value="">Select Customer</option>
                            <?php foreach ($customers as $c): ?>
                                <option value="<?= $c->id ?>" <?= old('customer_id') == $c->id ? 'selected' : '' ?>>
                                    <?= e($c->first_name . ' ' . $c->last_name) ?> (<?= e($c->phone) ?>)
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <?= errorMsg('customer_id') ?>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Tier <span class="text-danger">*</span></label>
                        <select name="tier_id" class="form-select bg-transparent <?= errorClass('tier_id') ?>" required>
                            <option value="">Select Tier</option>
                            <?php foreach ($tiers as $t): ?>
                                <option value="<?= $t->id ?>" <?= old('tier_id') == $t->id ? 'selected' : '' ?>>
                                    <?= e($t->name) ?> (Level <?= (int)$t->priority ?>)
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <?= errorMsg('tier_id') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Start Date <span class="text-danger">*</span></label>
                        <input type="date" name="start_date" class="form-control bg-transparent <?= errorClass('start_date') ?>"
                               value="<?= e(old('start_date', date('Y-m-d'))) ?>" required>
                        <?= errorMsg('start_date') ?>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">End Date</label>
                        <input type="date" name="end_date" class="form-control bg-transparent <?= errorClass('end_date') ?>"
                               value="<?= e(old('end_date')) ?>">
                        <small class="text-muted">Leave empty for unlimited</small>
                        <?= errorMsg('end_date') ?>
                    </div>
                </div>
            </div>

            <div class="mt-4 d-flex gap-2">
                <button type="submit" class="btn btn-gold">
                    <i class="fas fa-save me-1"></i>Assign Membership
                </button>
                <a href="<?= url('membership') ?>" class="btn btn-outline-secondary">
                    <i class="fas fa-times me-1"></i>Cancel
                </a>
            </div>
        </form>
    </div>
</div>
