@extends('admin.layouts.app')

@section('title', 'Impact Numbers Management')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3 mb-0">Impact Numbers Management</h1>
    <a href="{{ route('admin.impact-numbers.edit') }}" class="btn btn-primary">
        <i class="bi bi-pencil-square me-2"></i>Edit Section
    </a>
</div>

@if(session('success'))
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        {{ session('success') }}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
@endif

<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0">Section Overview</h5>
            </div>
            <div class="card-body">
                @if($impactSection)
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <strong>Title:</strong>
                            <p class="mb-0">{{ $impactSection->title }}</p>
                        </div>
                        <div class="col-md-6">
                            <strong>Status:</strong>
                            <span class="badge {{ $impactSection->is_active ? 'bg-success' : 'bg-secondary' }}">
                                {{ $impactSection->is_active ? 'Active' : 'Inactive' }}
                            </span>
                        </div>
                    </div>
                    
                    @if($impactSection->content)
                        <div class="mb-3">
                            <strong>Description:</strong>
                            <p class="mb-0">{{ $impactSection->content }}</p>
                        </div>
                    @endif
                @else
                    <div class="alert alert-info">
                        <i class="bi bi-info-circle me-2"></i>
                        Impact Numbers section not found. Please create it first.
                    </div>
                @endif
            </div>
        </div>
    </div>
</div>

@if($impactSection && isset($impactSection->data['statistics']))
<div class="row mt-4">
    <div class="col-12">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="card-title mb-0">Statistics ({{ count($impactSection->data['statistics']) }})</h5>
                <button type="button" class="btn btn-success btn-sm" data-bs-toggle="modal" data-bs-target="#addStatisticModal">
                    <i class="bi bi-plus-circle me-1"></i>Add Statistic
                </button>
            </div>
            <div class="card-body">
                @if(count($impactSection->data['statistics']) > 0)
                    <div class="row">
                        @foreach($impactSection->data['statistics'] as $index => $statistic)
                            <div class="col-md-6 col-lg-4 mb-3">
                                <div class="card h-100 border-start border-4" style="border-left-color: {{ $statistic['color'] ?? 'var(--accent-gold)' }} !important;">
                                    <div class="card-body">
                                        <div class="d-flex align-items-center mb-2">
                                            <i class="{{ $statistic['icon'] ?? 'bi bi-building-check' }} me-2" style="color: {{ $statistic['color'] ?? 'var(--accent-gold)' }}; font-size: 1.5rem;"></i>
                                            <h3 class="mb-0 fw-bold">{{ $statistic['number'] }}</h3>
                                        </div>
                                        <h6 class="card-title">{{ $statistic['label'] }}</h6>
                                        @if(isset($statistic['description']))
                                            <p class="card-text small text-muted">{{ $statistic['description'] }}</p>
                                        @endif
                                        <div class="mt-2">
                                            <button type="button" class="btn btn-outline-danger btn-sm" onclick="removeStatistic({{ $index }})">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        @endforeach
                    </div>
                @else
                    <div class="text-center py-4">
                        <i class="bi bi-graph-up text-muted" style="font-size: 3rem;"></i>
                        <p class="text-muted mt-2">No statistics added yet. Click "Add Statistic" to get started.</p>
                    </div>
                @endif
            </div>
        </div>
    </div>
</div>
@endif

<!-- Add Statistic Modal -->
<div class="modal fade" id="addStatisticModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Add New Statistic</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form id="addStatisticForm">
                @csrf
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="number" class="form-label">Number/Value *</label>
                        <input type="text" class="form-control" id="number" name="number" required placeholder="e.g., 500+, 95%, $2M">
                        <div class="form-text">Enter the statistic value (numbers, percentages, etc.)</div>
                    </div>
                    
                    <div class="mb-3">
                        <label for="label" class="form-label">Label *</label>
                        <input type="text" class="form-control" id="label" name="label" required placeholder="e.g., Students Enrolled">
                        <div class="form-text">Short descriptive label for the statistic</div>
                    </div>
                    
                    <div class="mb-3">
                        <label for="description" class="form-label">Description</label>
                        <textarea class="form-control" id="description" name="description" rows="2" placeholder="Optional detailed description"></textarea>
                    </div>
                    
                    <div class="mb-3">
                        <label for="icon" class="form-label">Icon Class</label>
                        <input type="text" class="form-control" id="icon" name="icon" placeholder="bi bi-building-check" value="bi bi-building-check">
                        <div class="form-text">Bootstrap Icons class (e.g., bi bi-people, bi bi-award, bi bi-graph-up)</div>
                    </div>
                    
                    <div class="mb-3">
                        <label for="color" class="form-label">Color</label>
                        <input type="color" class="form-control form-control-color" id="color" name="color" value="#D4AF37">
                        <div class="form-text">Color for the icon and border</div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">Add Statistic</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
// Add Statistic Form Handler
document.getElementById('addStatisticForm').addEventListener('submit', function(e) {
    e.preventDefault();
    
    const formData = new FormData(this);
    
    fetch('{{ route("admin.impact-numbers.add-statistic") }}', {
        method: 'POST',
        body: formData,
        headers: {
            'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
        }
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            location.reload();
        } else {
            alert('Error: ' + (data.message || 'Failed to add statistic'));
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('An error occurred while adding the statistic.');
    });
});

// Remove Statistic Function
function removeStatistic(index) {
    if (confirm('Are you sure you want to remove this statistic?')) {
        fetch('{{ route("admin.impact-numbers.remove-statistic") }}', {
            method: 'DELETE',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
            },
            body: JSON.stringify({ index: index })
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                location.reload();
            } else {
                alert('Error: ' + (data.message || 'Failed to remove statistic'));
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('An error occurred while removing the statistic.');
        });
    }
}
</script>
@endsection