@extends('siswa.layouts.app')

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

@push('styles')
<style>
.page-title { font-size: 24px; font-weight: 800; margin-bottom: 6px; }
.page-subtitle { font-size: 14px; color: #64748b; margin-bottom: 28px; }

.profile-card {
    background: white;
    border-radius: 20px;
    border: 2px solid #e5e5e5;
    padding: 32px;
    max-width: 560px;
}

/* Foto profil */
.foto-wrap {
    display: flex;
    align-items: center;
    gap: 24px;
    margin-bottom: 32px;
    padding-bottom: 28px;
    border-bottom: 1px solid #f1f5f9;
}

.foto-preview {
    width: 88px;
    height: 88px;
    border-radius: 50%;
    object-fit: cover;
    border: 3px solid #e6f0ff;
    flex-shrink: 0;
}

.foto-placeholder {
    width: 88px;
    height: 88px;
    border-radius: 50%;
    background: #e6f0ff;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    border: 3px solid #e6f0ff;
}

.foto-placeholder svg { width: 40px; height: 40px; color: #2b8ef3; }

.foto-info p { font-size: 13px; color: #64748b; margin: 0 0 10px; }

.btn-upload {
    display: inline-block;
    background: #e6f0ff;
    color: #2b8ef3;
    font-size: 13px;
    font-weight: 700;
    padding: 8px 18px;
    border-radius: 10px;
    cursor: pointer;
    border: none;
}

.field-group { margin-bottom: 18px; }

.field-label {
    display: block;
    font-size: 12px;
    font-weight: 700;
    color: #475569;
    margin-bottom: 6px;
}

.field-input {
    width: 100%;
    background: #f8fafc;
    border: 1.5px solid #e2e8f0;
    border-radius: 12px;
    padding: 11px 14px;
}

.divider {
    border: none;
    border-top: 1px solid #f1f5f9;
    margin: 24px 0;
}

.btn-save {
    background: #2b8ef3;
    color: white;
    border-radius: 12px;
    padding: 12px 28px;
    font-weight: 700;
    border: none;
}
</style>
@endpush

@section('content')

@php $siswa = Auth::guard('siswa')->user(); @endphp

<h3 class="page-title">Edit Profil</h3>
<p class="page-subtitle">Perbarui foto profil dan password akunmu.</p>

{{-- ganti form tag --}}
<form id="profile-form" enctype="multipart/form-data">
    @csrf

    <div class="profile-card">

        {{-- Foto Profil (tidak berubah) --}}
        <div class="foto-wrap">
            @if($siswa->foto_profil)
                <img src="/E31230356/storage/app/public/{{ $siswa->foto_profil }}" 
                     class="foto-preview" id="foto-preview">
            @else
                <div class="foto-placeholder" id="foto-placeholder">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
                        <circle cx="12" cy="8" r="4"/>
                        <path d="M4 20c0-4 3.6-7 8-7s8 3 8 7"/>
                    </svg>
                </div>
                <img src="" id="foto-preview" style="display:none">
            @endif
            <div class="foto-info">
                <p>Format: JPG, PNG, WEBP</p>
                <label for="foto_profil" class="btn-upload">Pilih Foto</label>
                <input type="file" name="foto_profil" id="foto_profil" hidden>
            </div>
        </div>

        {{-- Info --}}
        <div class="field-group">
            <label class="field-label">Nama</label>
            <input type="text" class="field-input" value="{{ $siswa->nama }}" disabled>
        </div>

        <hr class="divider">

        {{-- Password --}}
        <div class="field-group">
            <label class="field-label">Password Baru <span style="font-weight:400;color:#94a3b8">(kosongkan jika tidak ingin mengubah)</span></label>
            <input type="password" name="password" id="password" class="field-input" placeholder="Password baru">
        </div>

        <div class="field-group">
            <label class="field-label">Konfirmasi Password Baru</label>
            <input type="password" name="password_confirmation" id="password_confirmation" class="field-input" placeholder="Ulangi password baru">
            <div id="confirm-error" style="font-size:12px;color:#ef4444;margin-top:5px;display:none"></div>
        </div>

        {{-- Alert --}}
        <div id="alert-success" style="display:none;background:#f0fdf4;border:1.5px solid #86efac;color:#166534;border-radius:12px;padding:12px 16px;font-size:14px;font-weight:600;margin-bottom:16px"></div>
        <div id="alert-error" style="display:none;background:#fef2f2;border:1.5px solid #fca5a5;color:#991b1b;border-radius:12px;padding:12px 16px;font-size:13px;margin-bottom:16px"></div>

        <button type="submit" class="btn-save">Simpan</button>
    </div>
</form>

@endsection
@push('scripts')
<script>
// Preview foto
document.getElementById('foto_profil').addEventListener('change', function () {
    const file = this.files[0];
    if (!file) return;
    const preview     = document.getElementById('foto-preview');
    const placeholder = document.getElementById('foto-placeholder');
    preview.src = URL.createObjectURL(file);
    preview.style.display = 'block';
    if (placeholder) placeholder.style.display = 'none';
});

// Submit AJAX
document.getElementById('profile-form').addEventListener('submit', function (e) {
    e.preventDefault();

    const password     = document.getElementById('password').value;
    const confirmation = document.getElementById('password_confirmation').value;
    const confirmError = document.getElementById('confirm-error');

    // Validasi password client-side
    if (password !== '' && password !== confirmation) {
        confirmError.textContent = 'Konfirmasi password tidak cocok.';
        confirmError.style.display = 'block';
        return;
    }
    confirmError.style.display = 'none';

    const formData = new FormData(this);
    formData.append('_method', 'PUT');

    fetch('{{ route('siswa.profile.updateAjax') }}', {
        method: 'POST',
        headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
        body: formData,
    })
    .then(r => r.json())
    .then(data => {
        if (data.success) {
            document.getElementById('alert-success').textContent = '✓ ' + data.message;
            document.getElementById('alert-success').style.display = 'block';
            document.getElementById('alert-error').style.display = 'none';
            if (data.foto_url) {
                document.getElementById('foto-preview').src = data.foto_url + '?t=' + Date.now();
                document.getElementById('foto-preview').style.display = 'block';
                const ph = document.getElementById('foto-placeholder');
                if (ph) ph.style.display = 'none';
            }
            // Reset password fields
            document.getElementById('password').value = '';
            document.getElementById('password_confirmation').value = '';
        } else {
            const msgs = Object.values(data.errors || {}).flat().join('\n');
            document.getElementById('alert-error').textContent = msgs;
            document.getElementById('alert-error').style.display = 'block';
            document.getElementById('alert-success').style.display = 'none';
        }
    })
    .catch(() => {
        document.getElementById('alert-error').textContent = 'Terjadi kesalahan, coba lagi.';
        document.getElementById('alert-error').style.display = 'block';
    });
});
</script>
@endpush