<?php
/**
 * @var object $service
 * @var array $categories
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-edit me-2 text-gold"></i>Edit Service</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-concierge-bell card-icon"></i>
            <h6 class="card-title">Service 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('services/update/' . $service->id) ?>">
            <?= csrf_field() ?>

            <div class="row g-3">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Service Name <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control bg-transparent <?= errorClass('name') ?>"
                               value="<?= e(old('name', $service->name)) ?>" required maxlength="255">
                        <?= errorMsg('name') ?>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="mb-3">
                        <label class="form-label">Category</label>
                        <select name="category_id" class="form-select bg-transparent <?= errorClass('category_id') ?>">
                            <option value="">No Category</option>
                            <?php foreach ($categories as $cat): ?>
                                <option value="<?= $cat->id ?>" <?= (old('category_id', $service->category_id) == $cat->id) ? 'selected' : '' ?>>
                                    <?= e($cat->name) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <?= errorMsg('category_id') ?>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="mb-3">
                        <label class="form-label">Color</label>
                        <input type="color" name="color" class="form-control form-control-color bg-transparent <?= errorClass('color') ?>"
                               value="<?= e(old('color', $service->color ?? '#C8A44E')) ?>">
                        <?= errorMsg('color') ?>
                    </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', $service->description)) ?></textarea>
                        <?= errorMsg('description') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-3">
                    <div class="mb-3">
                        <label class="form-label">Duration (minutes) <span class="text-danger">*</span></label>
                        <input type="number" name="duration_minutes" class="form-control bg-transparent <?= errorClass('duration_minutes') ?>"
                               value="<?= e(old('duration_minutes', $service->duration_minutes)) ?>" required min="1" max="1440">
                        <?= errorMsg('duration_minutes') ?>
                    </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', $service->price)) ?>" required min="0">
                        </div>
                        <?= errorMsg('price') ?>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="mb-3">
                        <label class="form-label">Cost Price</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="cost_price"
                                   class="form-control bg-transparent <?= errorClass('cost_price') ?>"
                                   value="<?= e(old('cost_price', $service->cost_price)) ?>" min="0">
                        </div>
                        <?= errorMsg('cost_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', $service->tax_percent)) ?>" min="0" max="100">
                        <?= errorMsg('tax_percent') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Commission Type</label>
                        <select name="commission_type" class="form-select bg-transparent <?= errorClass('commission_type') ?>">
                            <option value="none" <?= (old('commission_type', $service->commission_type) === 'none') ? 'selected' : '' ?>>No Commission</option>
                            <option value="percentage" <?= (old('commission_type', $service->commission_type) === 'percentage') ? 'selected' : '' ?>>Percentage</option>
                            <option value="fixed" <?= (old('commission_type', $service->commission_type) === 'fixed') ? 'selected' : '' ?>>Fixed Amount</option>
                        </select>
                        <?= errorMsg('commission_type') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Commission Value</label>
                        <input type="number" step="0.01" name="commission_value"
                               class="form-control bg-transparent <?= errorClass('commission_value') ?>"
                               value="<?= e(old('commission_value', $service->commission_value)) ?>" min="0">
                        <?= errorMsg('commission_value') ?>
                    </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', $service->is_active) == 1) ? 'checked' : '' ?>>
                            <label class="form-check-label" for="is_active">Active</label>
                        </div>
                        <?= errorMsg('is_active') ?>
                    </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>Update Service
                </button>
                <a href="<?= url('services') ?>" class="btn btn-outline-secondary">
                    <i class="fas fa-times me-1"></i>Cancel
                </a>
            </div>
        </form>
    </div>
</div>
