<?php
/**
 * @var array $services
 * @var array $categories
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-box me-2 text-gold"></i>Add New Package</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-box card-icon"></i>
            <h6 class="card-title">Package Information</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('packages/store') ?>">
            <?= csrf_field() ?>

            <div class="row g-3">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Package Name <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control bg-transparent <?= errorClass('name') ?>"
                               value="<?= e(old('name')) ?>" required maxlength="255">
                        <?= errorMsg('name') ?>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="mb-3">
                        <label class="form-label">Price <span class="text-danger">*</span></label>
                        <div class="input-group">
                            <span class="input-group-text bg-transparent border-gold"><?= APP_CURRENCY_SYMBOL ?></span>
                            <input type="number" step="0.01" name="price"
                                   class="form-control bg-transparent <?= errorClass('price') ?>"
                                   value="<?= e(old('price', 0)) ?>" required min="0">
                        </div>
                        <?= errorMsg('price') ?>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="mb-3">
                        <label class="form-label">Tax (%)</label>
                        <input type="number" step="0.01" name="tax_percent"
                               class="form-control bg-transparent <?= errorClass('tax_percent') ?>"
                               value="<?= e(old('tax_percent', 0)) ?>" min="0" max="100">
                        <?= errorMsg('tax_percent') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Description</label>
                        <textarea name="description" class="form-control bg-transparent <?= errorClass('description') ?>"
                                  rows="3" maxlength="2000"><?= e(old('description')) ?></textarea>
                        <?= errorMsg('description') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Validity (days) <span class="text-danger">*</span></label>
                        <input type="number" name="validity_days" class="form-control bg-transparent <?= errorClass('validity_days') ?>"
                               value="<?= e(old('validity_days', 30)) ?>" min="1">
                        <?= errorMsg('validity_days') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Max Visits <span class="text-danger">*</span></label>
                        <input type="number" name="max_visits" class="form-control bg-transparent <?= errorClass('max_visits') ?>"
                               value="<?= e(old('max_visits', 1)) ?>" min="1">
                        <?= errorMsg('max_visits') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <div class="form-check form-switch mt-4 pt-2">
                            <input type="hidden" name="is_active" value="0">
                            <input type="checkbox" name="is_active" id="is_active" class="form-check-input" value="1"
                                <?= old('is_active', '1') == '1' ? 'checked' : '' ?>>
                            <label class="form-check-label" for="is_active">Active</label>
                        </div>
                    </div>
                </div>
            </div>

            <hr class="border-secondary">
            <h6 class="text-gold mb-3"><i class="fas fa-list me-2"></i>Select Services</h6>

            <div class="table-responsive">
                <table class="table table-dark table-hover mb-0" id="servicesTable">
                    <thead>
                        <tr>
                            <th width="50"><input type="checkbox" id="selectAll"></th>
                            <th>Service</th>
                            <th>Category</th>
                            <th class="text-center">Duration</th>
                            <th class="text-end">Price</th>
                            <th class="text-center" width="120">Quantity</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($services)): ?>
                            <tr>
                                <td colspan="6" class="text-center text-muted py-3">
                                    No active services found. <a href="<?= url('services/create') ?>" class="text-gold">Create a service</a> first.
                                </td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($services as $svc): ?>
                                <tr>
                                    <td>
                                        <input type="checkbox" name="service_ids[]" value="<?= $svc->id ?>"
                                               class="service-checkbox">
                                    </td>
                                    <td>
                                        <div class="d-flex align-items-center gap-2">
                                            <span class="rounded-circle d-inline-block"
                                                  style="width:10px;height:10px;background:<?= e($svc->color ?? '#C8A44E') ?>;"></span>
                                            <strong><?= e($svc->name) ?></strong>
                                        </div>
                                    </td>
                                    <td><?= e($svc->category_name ?? '-') ?></td>
                                    <td class="text-center"><?= (int)$svc->duration_minutes ?> min</td>
                                    <td class="text-end"><?= currency($svc->price) ?></td>
                                    <td class="text-center">
                                        <input type="number" name="quantities[<?= $svc->id ?>]"
                                               class="form-control form-control-sm bg-transparent text-center"
                                               value="1" min="1" max="999" style="width:80px;" disabled>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            <?= errorMsg('service_ids') ?>

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

<?php $inlineScripts = <<<'EOS'
$('#selectAll').change(function() {
    const checked = $(this).prop('checked');
    $('.service-checkbox').each(function() {
        $(this).prop('checked', checked);
        const qtyInput = $(this).closest('tr').find('input[type="number"]');
        qtyInput.prop('disabled', !checked);
    });
});

$('.service-checkbox').change(function() {
    const qtyInput = $(this).closest('tr').find('input[type="number"]');
    qtyInput.prop('disabled', !$(this).prop('checked'));
});
EOS;
?>
