@extends('layouts.admin')

@section('title', 'Add Achievement')

@section('content')
<div class="dashboard-header" style="margin-bottom: 24px; display: flex; justify-content: space-between; align-items: center;">
    <div>
        <nav style="font-size:0.8rem; color:var(--text-muted); margin-bottom:4px;">
            <a href="{{ route('admin.achievements.index') }}" style="color:var(--text-muted); text-decoration:none;">Achievements</a>
            <span style="margin:0 6px;">/</span>
            <span style="color:var(--primary); font-weight:600;">Add</span>
        </nav>
        <h1 style="font-family:'Oswald',sans-serif; font-size:2rem; color:var(--primary); margin:0;">Add Achievement</h1>
    </div>
    <a href="{{ route('admin.achievements.index') }}" class="btn btn-outline">
        <i class="fas fa-arrow-left"></i> Back
    </a>
</div>

<form method="POST" action="{{ route('admin.achievements.store') }}" enctype="multipart/form-data">
    @csrf
    <div style="display:grid; grid-template-columns:1fr 300px; gap:24px; align-items:start;">

        <!-- Left: Main Details -->
        <div class="admin-card" style="margin-bottom:0;">
            <div class="admin-card-header">
                <span class="admin-card-title"><i class="fas fa-trophy" style="color:#d4a017;margin-right:8px;"></i> Achievement Details</span>
            </div>
            <div class="admin-card-body">
                <div style="display:grid; grid-template-columns:1fr 1fr; gap:20px;">
                    <div class="form-group" style="margin-bottom:0;">
                        <label class="form-label">Title <span style="color:var(--secondary);">*</span></label>
                        <input type="text" name="title" class="form-control" value="{{ old('title') }}" required placeholder="Achievement title">
                    </div>
                    <div class="form-group" style="margin-bottom:0;">
                        <label class="form-label">Player Name <span style="color:var(--secondary);">*</span></label>
                        <input type="text" name="player_name" class="form-control" value="{{ old('player_name') }}" required placeholder="Athlete's name">
                    </div>
                    <div class="form-group" style="margin-bottom:0;">
                        <label class="form-label">Achievement Date <span style="color:var(--secondary);">*</span></label>
                        <input type="date" name="achievement_date" class="form-control" value="{{ old('achievement_date') }}" required>
                    </div>
                    <div class="form-group" style="margin-bottom:0;">
                        <label class="form-label">Type <span style="color:var(--secondary);">*</span></label>
                        <select name="type" class="form-control" required>
                            <option value="national" {{ old('type') == 'national' ? 'selected' : '' }}>National</option>
                            <option value="international" {{ old('type') == 'international' ? 'selected' : '' }}>International</option>
                        </select>
                    </div>
                    <div class="form-group" style="grid-column:1 / -1; margin-bottom:0;">
                        <label class="form-label">Description</label>
                        <textarea name="description" class="form-control" rows="5" placeholder="Describe the achievement...">{{ old('description') }}</textarea>
                    </div>
                </div>
            </div>
        </div>

        <!-- Right: Medal & Photo -->
        <div style="display:flex; flex-direction:column; gap:24px;">

            <div class="admin-card" style="margin-bottom:0;">
                <div class="admin-card-header">
                    <span class="admin-card-title"><i class="fas fa-medal" style="color:#d4a017;margin-right:8px;"></i> Medal & Photo</span>
                </div>
                <div class="admin-card-body">
                    <div class="form-group">
                        <label class="form-label">Medal Type</label>
                        <select name="medal_type" class="form-control">
                            <option value="none" {{ old('medal_type') == 'none' ? 'selected' : '' }}>None</option>
                            <option value="gold" {{ old('medal_type') == 'gold' ? 'selected' : '' }}>🥇 Gold</option>
                            <option value="silver" {{ old('medal_type') == 'silver' ? 'selected' : '' }}>🥈 Silver</option>
                            <option value="bronze" {{ old('medal_type') == 'bronze' ? 'selected' : '' }}>🥉 Bronze</option>
                        </select>
                    </div>

                    <div class="form-group" style="margin-bottom:0;">
                        <label class="form-label">Photo (optional)</label>
                        <div id="imgDropZone"
                             style="border:2px dashed var(--border-color);border-radius:10px;padding:24px 16px;text-align:center;cursor:pointer;transition:var(--transition);background-color:var(--bg-color);"
                             onclick="document.getElementById('imgInput').click()"
                             onmouseenter="this.style.borderColor='var(--primary)'"
                             onmouseleave="this.style.borderColor='var(--border-color)'">
                            <div id="imgPreviewWrap">
                                <i class="fas fa-cloud-upload-alt" style="font-size:1.8rem;color:var(--text-muted);opacity:0.5;margin-bottom:6px;display:block;"></i>
                                <p style="font-size:0.85rem;color:var(--text-main);margin:0;font-weight:500;">Click to upload</p>
                                <p style="font-size:0.72rem;color:var(--text-muted);margin-top:3px;">JPG, PNG, WEBP · Max 4MB</p>
                            </div>
                            <img id="imgPreviewImg" src="" alt="Preview" style="display:none;width:100%;height:140px;object-fit:cover;border-radius:8px;">
                        </div>
                        <input type="file" id="imgInput" name="photo" accept="image/*" style="display:none;" onchange="previewImg(this)">
                    </div>
                </div>
            </div>

            <!-- Actions -->
            <div class="admin-card" style="margin-bottom:0;">
                <div class="admin-card-body">
                    <button type="submit" class="btn btn-primary" style="width:100%; justify-content:center; padding:12px; margin-bottom:12px;">
                        <i class="fas fa-trophy"></i> Add Achievement
                    </button>
                    <a href="{{ route('admin.achievements.index') }}" class="btn btn-outline" style="width:100%; justify-content:center; padding:12px;">
                        <i class="fas fa-times"></i> Cancel
                    </a>
                </div>
            </div>
        </div>
    </div>
</form>
@endsection

@section('scripts')
<script>
function previewImg(input) {
    const img = document.getElementById('imgPreviewImg');
    const wrap = document.getElementById('imgPreviewWrap');
    if (input.files && input.files[0]) {
        const reader = new FileReader();
        reader.onload = e => {
            img.src = e.target.result;
            img.style.display = 'block';
            wrap.style.display = 'none';
        };
        reader.readAsDataURL(input.files[0]);
    }
}
</script>
@endsection
