@extends('admin.layouts.app')

@section('title', 'Edit Statistic')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="h3">Edit Statistic: {{ $statistic->title }}</h1>
                <div class="btn-group">
                    <a href="{{ route('admin.statistics.show', $statistic) }}" class="btn btn-outline-primary">
                        <i class="bi bi-eye"></i> View
                    </a>
                    <a href="{{ route('admin.statistics.index') }}" class="btn btn-secondary">
                        <i class="bi bi-arrow-left"></i> Back to List
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <div class="card">
                <div class="card-body">
                    <form action="{{ route('admin.statistics.update', $statistic) }}" method="POST">
                        @csrf
                        @method('PUT')
                        
                        <div class="mb-3">
                            <label for="title" class="form-label">Title <span class="text-danger">*</span></label>
                            <input type="text" class="form-control @error('title') is-invalid @enderror" 
                                   id="title" name="title" 
                                   value="{{ old('title', $statistic->title) }}" 
                                   placeholder="e.g., Students Served, Courses Offered" required>
                            <div class="form-text">The label that will appear below the statistic value</div>
                            @error('title')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="row">
                            <div class="col-md-8">
                                <div class="mb-3">
                                    <label for="value" class="form-label">Value <span class="text-danger">*</span></label>
                                    <input type="number" class="form-control @error('value') is-invalid @enderror" 
                                           id="value" name="value" 
                                           value="{{ old('value', $statistic->value) }}" 
                                           min="0" step="1" placeholder="1000" required>
                                    <div class="form-text">The numeric value to display</div>
                                    @error('value')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="mb-3">
                                    <label for="suffix" class="form-label">Suffix</label>
                                    <input type="text" class="form-control @error('suffix') is-invalid @enderror" 
                                           id="suffix" name="suffix" 
                                           value="{{ old('suffix', $statistic->suffix) }}" 
                                           placeholder="+, %, K, M" maxlength="10">
                                    <div class="form-text">Optional suffix (e.g., +, %, K)</div>
                                    @error('suffix')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="icon" class="form-label">Icon Class</label>
                            <input type="text" class="form-control @error('icon') is-invalid @enderror" 
                                   id="icon" name="icon" 
                                   value="{{ old('icon', $statistic->icon) }}" 
                                   placeholder="bi bi-people, bi bi-book, bi bi-graph-up">
                            <div class="form-text">Bootstrap Icons class (e.g., bi bi-people, bi bi-book)</div>
                            @error('icon')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="sort_order" class="form-label">Sort Order</label>
                                    <input type="number" class="form-control @error('sort_order') is-invalid @enderror" 
                                           id="sort_order" name="sort_order" 
                                           value="{{ old('sort_order', $statistic->sort_order) }}" 
                                           min="0" placeholder="0">
                                    <div class="form-text">Lower numbers appear first</div>
                                    @error('sort_order')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label class="form-label">Status</label>
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" 
                                               id="is_active" name="is_active" value="1" 
                                               {{ old('is_active', $statistic->is_active) ? 'checked' : '' }}>
                                        <label class="form-check-label" for="is_active">
                                            Active (visible on website)
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="d-flex gap-2">
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-check"></i> Update Statistic
                            </button>
                            <a href="{{ route('admin.statistics.show', $statistic) }}" class="btn btn-outline-secondary">
                                <i class="bi bi-eye"></i> View
                            </a>
                            <a href="{{ route('admin.statistics.index') }}" class="btn btn-secondary">
                                Cancel
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-lg-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Current Preview</h5>
                </div>
                <div class="card-body text-center">
                    <div class="border rounded p-4">
                        @if($statistic->icon)
                            <i class="{{ $statistic->icon }} fs-1 text-primary mb-2"></i>
                        @else
                            <i class="bi bi-graph-up fs-1 text-muted mb-2"></i>
                        @endif
                        <h3 class="text-primary mb-1">{{ number_format($statistic->value) }}{{ $statistic->suffix }}</h3>
                        <p class="text-muted mb-0">{{ $statistic->title }}</p>
                    </div>
                    <small class="text-muted mt-2 d-block">
                        <i class="bi bi-info-circle"></i> Current appearance
                    </small>
                </div>
            </div>

            <div class="card mt-3">
                <div class="card-header">
                    <h5 class="mb-0">Live Preview</h5>
                </div>
                <div class="card-body text-center">
                    <div id="statistic-preview" class="border rounded p-4">
                        <i id="preview-icon" class="{{ $statistic->icon ?: 'bi bi-graph-up' }} fs-1 text-primary mb-2"></i>
                        <h3 id="preview-value" class="text-primary mb-1">{{ number_format($statistic->value) }}{{ $statistic->suffix }}</h3>
                        <p id="preview-title" class="text-muted mb-0">{{ $statistic->title }}</p>
                    </div>
                    <small class="text-muted mt-2 d-block">
                        <i class="bi bi-info-circle"></i> Preview with your changes
                    </small>
                </div>
            </div>

            <div class="card mt-3">
                <div class="card-header">
                    <h5 class="mb-0">Statistic Info</h5>
                </div>
                <div class="card-body">
                    <dl class="row small">
                        <dt class="col-sm-5">Created:</dt>
                        <dd class="col-sm-7">{{ $statistic->created_at->format('M d, Y H:i') }}</dd>
                        
                        <dt class="col-sm-5">Last Updated:</dt>
                        <dd class="col-sm-7">{{ $statistic->updated_at->format('M d, Y H:i') }}</dd>
                        
                        <dt class="col-sm-5">Status:</dt>
                        <dd class="col-sm-7">
                            @if($statistic->is_active)
                                <span class="badge bg-success">Active</span>
                            @else
                                <span class="badge bg-secondary">Inactive</span>
                            @endif
                        </dd>
                    </dl>
                </div>
            </div>

            <div class="card mt-3">
                <div class="card-header">
                    <h5 class="mb-0">Icon Examples</h5>
                </div>
                <div class="card-body">
                    <div class="row text-center">
                        <div class="col-4 mb-3">
                            <i class="bi bi-people fs-2 text-primary"></i>
                            <br><small>bi bi-people</small>
                        </div>
                        <div class="col-4 mb-3">
                            <i class="bi bi-book fs-2 text-success"></i>
                            <br><small>bi bi-book</small>
                        </div>
                        <div class="col-4 mb-3">
                            <i class="bi bi-graph-up fs-2 text-info"></i>
                            <br><small>bi bi-graph-up</small>
                        </div>
                        <div class="col-4 mb-3">
                            <i class="bi bi-award fs-2 text-warning"></i>
                            <br><small>bi bi-award</small>
                        </div>
                        <div class="col-4 mb-3">
                            <i class="bi bi-heart fs-2 text-danger"></i>
                            <br><small>bi bi-heart</small>
                        </div>
                        <div class="col-4 mb-3">
                            <i class="bi bi-star fs-2 text-primary"></i>
                            <br><small>bi bi-star</small>
                        </div>
                    </div>
                    <small class="text-muted">
                        <a href="https://icons.getbootstrap.com/" target="_blank" class="text-decoration-none">
                            <i class="bi bi-box-arrow-up-right"></i> Browse more icons
                        </a>
                    </small>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
// Live preview functionality
document.addEventListener('DOMContentLoaded', function() {
    const titleInput = document.getElementById('title');
    const valueInput = document.getElementById('value');
    const suffixInput = document.getElementById('suffix');
    const iconInput = document.getElementById('icon');
    
    const previewTitle = document.getElementById('preview-title');
    const previewValue = document.getElementById('preview-value');
    const previewIcon = document.getElementById('preview-icon');
    
    function updatePreview() {
        const title = titleInput.value || 'Statistic Title';
        const value = valueInput.value || '0';
        const suffix = suffixInput.value || '';
        const icon = iconInput.value || 'bi bi-graph-up';
        
        previewTitle.textContent = title;
        previewValue.textContent = parseInt(value).toLocaleString() + suffix;
        previewIcon.className = icon + ' fs-1 text-primary mb-2';
    }
    
    titleInput.addEventListener('input', updatePreview);
    valueInput.addEventListener('input', updatePreview);
    suffixInput.addEventListener('input', updatePreview);
    iconInput.addEventListener('input', updatePreview);
});
</script>
@endsection