@php
    $role = session('user')['role'] ?? 'user';
    $layout = in_array($role, ['admin', 'superadmin']) ? 'admin.layouts.app' : 'user.layouts.app';
    
    if ($role === 'superadmin') {
        $dashboardRoute = route('admin.superadmin.dashboard');
    } elseif ($role === 'admin') {
        $dashboardRoute = route('admin.dashboard');
    } else {
        $dashboardRoute = route('user.dashboard');
    }
@endphp
@extends($layout)

@section('content')

<div class="max-w-4xl mx-auto mt-10 px-4 pb-20">
    
    {{-- HEADER --}}
    <div class="mb-8">
        <a href="{{ $dashboardRoute }}" class="inline-flex items-center gap-2 text-sm text-slate-500 hover:text-[#0D215C] font-semibold mb-4 transition">
            <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24"><path d="M19 12H5M12 5l-7 7 7 7"/></svg>
            Kembali ke Dashboard
        </a>
        <h2 class="text-2xl font-bold text-[#0D215C]">
            Pengaturan Profil
        </h2>
        <p class="text-sm text-slate-400">Kelola informasi akun dan kata sandi Anda.</p>
    </div>

    @if(session('success'))
        <div class="mb-6 p-4 bg-green-100 border border-green-200 text-green-700 rounded-xl text-sm font-medium">
            {{ session('success') }}
        </div>
    @endif

    @if(session('error'))
        <div class="mb-6 p-4 bg-red-100 border border-red-200 text-red-700 rounded-xl text-sm font-medium">
            {{ session('error') }}
        </div>
    @endif

    @if($errors->any())
        <div class="mb-6 p-4 bg-red-100 border border-red-200 text-red-700 rounded-xl text-sm font-medium">
            <ul class="list-disc list-inside">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <div class="grid grid-cols-1 gap-8">
        
        {{-- FORM INFORMASI PROFIL --}}
        <div class="bg-white p-8 rounded-2xl shadow-sm border border-slate-100">
            <h4 class="text-lg font-bold text-[#0D215C] mb-6 flex items-center gap-2">
                <i data-lucide="user" class="w-5 h-5"></i> Informasi Pribadi
            </h4>
            
            <form action="{{ route('settings.update') }}" method="POST" class="space-y-5">
                @csrf
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="text-sm font-semibold text-slate-700 block mb-2">Nama Lengkap</label>
                        <input type="text" name="name" value="{{ session('user')['name'] ?? '' }}" 
                            class="w-full p-3 rounded-xl bg-slate-50 border border-slate-200 focus:border-blue-500 focus:ring-2 focus:ring-blue-100 outline-none transition" readonly>
                    </div>
                    <div>
                        <label class="text-sm font-semibold text-slate-700 block mb-2">Username <span class="text-red-500">*</span></label>
                        <input type="text" name="username" value="{{ session('user')['username'] ?? '' }}" 
                            class="w-full p-3 rounded-xl bg-slate-50 border border-slate-200 focus:border-blue-500 focus:ring-2 focus:ring-blue-100 outline-none transition">
                    </div>
                </div>



                <div class="pt-4">
                    <button type="submit" class="bg-[#0D215C] hover:bg-[#0a1a47] text-white px-8 py-3 rounded-xl font-bold transition shadow-lg shadow-blue-900/10">
                        Simpan Perubahan
                    </button>
                </div>
            </form>
        </div>

        @if($role !== 'user')
        {{-- FORM GANTI PASSWORD --}}
        <div class="bg-white p-8 rounded-2xl shadow-sm border border-slate-100">
            <h4 class="text-lg font-bold text-[#0D215C] mb-6 flex items-center gap-2">
                <i data-lucide="lock" class="w-5 h-5"></i> Keamanan Akun
            </h4>

            <form action="{{ route('settings.update') }}" method="POST" class="space-y-5">
                @csrf
                <input type="hidden" name="username" value="{{ session('user')['username'] ?? '' }}">
                <div>
                    <label class="text-sm font-semibold text-slate-700 block mb-2">Kata Sandi Saat Ini</label>
                    <div class="relative">
                        <input type="password" name="current_password" id="current_password" autocomplete="off"
                            class="w-full p-3 pr-10 rounded-xl bg-slate-50 border border-slate-200 focus:border-blue-500 focus:ring-2 focus:ring-blue-100 outline-none transition">
                        <button type="button" onclick="togglePassword('current_password', this)" class="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-blue-500 focus:outline-none">
                            <i data-lucide="eye" class="w-5 h-5 eye-icon"></i>
                            <i data-lucide="eye-off" class="w-5 h-5 eye-off-icon hidden"></i>
                        </button>
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="text-sm font-semibold text-slate-700 block mb-2">Kata Sandi Baru</label>
                        <div class="relative">
                            <input type="password" name="password" id="password"
                                class="w-full p-3 pr-10 rounded-xl bg-slate-50 border border-slate-200 focus:border-blue-500 focus:ring-2 focus:ring-blue-100 outline-none transition">
                            <button type="button" onclick="togglePassword('password', this)" class="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-blue-500 focus:outline-none">
                                <i data-lucide="eye" class="w-5 h-5 eye-icon"></i>
                                <i data-lucide="eye-off" class="w-5 h-5 eye-off-icon hidden"></i>
                            </button>
                        </div>
                    </div>
                    <div>
                        <label class="text-sm font-semibold text-slate-700 block mb-2">Konfirmasi Kata Sandi</label>
                        <div class="relative">
                            <input type="password" name="password_confirmation" id="password_confirmation"
                                class="w-full p-3 pr-10 rounded-xl bg-slate-50 border border-slate-200 focus:border-blue-500 focus:ring-2 focus:ring-blue-100 outline-none transition">
                            <button type="button" onclick="togglePassword('password_confirmation', this)" class="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-blue-500 focus:outline-none">
                                <i data-lucide="eye" class="w-5 h-5 eye-icon"></i>
                                <i data-lucide="eye-off" class="w-5 h-5 eye-off-icon hidden"></i>
                            </button>
                        </div>
                    </div>
                </div>

                <div class="pt-4">
                    <button type="submit" class="bg-white border border-slate-200 hover:bg-slate-50 text-[#0D215C] px-8 py-3 rounded-xl font-bold transition">
                        Perbarui Kata Sandi
                    </button>
                </div>
            </form>
        </div>
        @endif

    </div>
</div>

<script>
    document.addEventListener("DOMContentLoaded", function() {
        if (typeof lucide !== 'undefined') {
            lucide.createIcons();
        }
    });

    function togglePassword(inputId, btn) {
        const input = document.getElementById(inputId);
        const eye = btn.querySelector('.eye-icon');
        const eyeOff = btn.querySelector('.eye-off-icon');
        
        if (input.type === 'password') {
            input.type = 'text';
            eye.classList.add('hidden');
            eyeOff.classList.remove('hidden');
            btn.classList.add('text-blue-500');
            btn.classList.remove('text-slate-400');
        } else {
            input.type = 'password';
            eye.classList.remove('hidden');
            eyeOff.classList.add('hidden');
            btn.classList.remove('text-blue-500');
            btn.classList.add('text-slate-400');
        }
    }
</script>

@endsection