@extends('layout.layout')

@php
    $title = 'View/Edit Profile';
    $subTitle = 'View/Edit Profile';
    // Script diletakkan di @push('scripts') di bawah, jadi variabel $script ini dikosongkan saja
    $script = ''; 
@endphp

@section('content')

    <div class="grid grid-cols-1 lg:grid-cols-12 gap-6">
        {{-- ============================================== --}}
        {{-- ===== KOLOM KIRI (INFO PROFIL)          ===== --}}
        {{-- ============================================== --}}
        <div class="col-span-12 lg:col-span-4">
            <div class="user-grid-card relative border border-neutral-200 dark:border-neutral-600 rounded-2xl overflow-hidden bg-white dark:bg-neutral-700 h-full">
                
                <div class="pb-6 ms-6 mb-6 me-6 -mt-[100px]">
                    <div class="text-center border-b border-neutral-200 dark:border-neutral-600 pb-4">
                        
                        <h6 class="mb-0 mt-4 text-lg font-semibold">{{ $user->name }}</h6>
                        <span class="text-secondary-light mb-4 block">{{ $user->email }}</span>
                    </div>

                    <div class="mt-6">
                        <h6 class="text-xl mb-4 font-semibold text-neutral-800 dark:text-white">Personal Info</h6>
                        <ul>
                            <li class="flex items-center gap-1 mb-3">
                                <span class="w-[35%] text-base font-semibold text-neutral-600 dark:text-neutral-200">Full Name</span>
                                <span class="w-[65%] text-secondary-light font-medium">: {{ $user->name }}</span>
                            </li>
                            <li class="flex items-center gap-1 mb-3">
                                <span class="w-[35%] text-base font-semibold text-neutral-600 dark:text-neutral-200">Email</span>
                                <span class="w-[65%] text-secondary-light font-medium">: {{ $user->email }}</span>
                            </li>
                            <li class="flex items-center gap-1 mb-3">
                                <span class="w-[35%] text-base font-semibold text-neutral-600 dark:text-neutral-200">Phone</span>
                                <span class="w-[65%] text-secondary-light font-medium">: {{ $user->profile->phone_number ?? '-' }}</span>
                            </li>
                            <li class="flex items-center gap-1 mb-3">
                                <span class="w-[35%] text-base font-semibold text-neutral-600 dark:text-neutral-200">Address</span>
                                <span class="w-[65%] text-secondary-light font-medium">: {{ $user->profile->address ?? '-' }}</span>
                            </li>
                            <li class="flex items-center gap-1 mb-3">
                                <span class="w-[35%] text-base font-semibold text-neutral-600 dark:text-neutral-200">Status</span>
                                <span class="w-[65%] text-secondary-light font-medium">: 
                                    <span class="{{ $user->status == 'aktif' ? 'text-success-600' : 'text-danger-600' }}">
                                        {{ ucfirst($user->status) }}
                                    </span>
                                </span>
                            </li>
                             <li class="flex items-center gap-1">
                                <span class="w-[35%] text-base font-semibold text-neutral-600 dark:text-neutral-200">Joined Date</span>
                                <span class="w-[65%] text-secondary-light font-medium">: {{ $user->created_at->translatedFormat('d M Y') }}</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        {{-- ============================================== --}}
        {{-- ===== KOLOM KANAN (FORM EDIT)           ===== --}}
        {{-- ============================================== --}}
        <div class="col-span-12 lg:col-span-8">
            <div class="card h-full border-0 shadow-sm">
                <div class="card-body p-6">

                    {{-- Navigasi Tab --}}
                    <ul class="tab-style-gradient flex flex-wrap text-sm font-medium text-center mb-5" id="default-tab" data-tabs-toggle="#default-tab-content" role="tablist">
                         <li class="me-2" role="presentation">
                            <button class="py-2.5 px-4 border-b-2 font-semibold text-base inline-flex items-center gap-3" id="edit-profile-tab" data-tabs-target="#edit-profile" type="button" role="tab">
                                Edit Profile
                            </button>
                        </li>
                    </ul>

                    {{-- Konten Tab --}}
                    <div id="default-tab-content">
                        <div id="edit-profile" role="tabpanel">
                            
                            {{-- FORM EDIT --}}
                            <form action="{{ route('DataPetugas.update', $user->id) }}" method="POST" enctype="multipart/form-data">
                                @csrf
                                @method('PUT')

                                <div class="grid grid-cols-1 sm:grid-cols-12 gap-x-6">
                                    <div class="col-span-12 sm:col-span-6 mb-5">
                                        <label class="inline-block font-semibold text-neutral-600 dark:text-neutral-200 text-sm mb-2">Full Name <span class="text-danger-600">*</span></label>
                                        <input type="text" name="name" class="form-control rounded-lg" value="{{ old('name', $user->name) }}" required>
                                    </div>

                                    <div class="col-span-12 sm:col-span-6 mb-5">
                                        <label class="inline-block font-semibold text-neutral-600 dark:text-neutral-200 text-sm mb-2">Phone</label>
                                        <input type="text" name="phone_number" class="form-control rounded-lg" value="{{ old('phone_number', $user->profile->phone_number ?? '') }}">
                                    </div>


                                    <div class="col-span-12 mb-5">
                                        <label class="inline-block font-semibold text-neutral-600 dark:text-neutral-200 text-sm mb-2">Address</label>
                                        <textarea name="address" class="form-control rounded-lg" rows="3">{{ old('address', $user->profile->address ?? '') }}</textarea>
                                    </div>

                                    <div class="col-span-12 sm:col-span-6 mb-5">
                                        <label class="inline-block font-semibold text-neutral-600 dark:text-neutral-200 text-sm mb-2">Status <span class="text-danger-600">*</span></label>
                                        <select name="status" class="form-control rounded-lg form-select" required>
                                            <option value="aktif" {{ old('status', $user->status) == 'aktif' ? 'selected' : '' }}>Aktif</option>
                                            <option value="nonaktif" {{ old('status', $user->status) == 'nonaktif' ? 'selected' : '' }}>Nonaktif</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="flex items-center justify-end gap-3 mt-6">
                                    <a href="{{ route('DataPetugas') }}" class="px-8 py-2.5 rounded-lg border border-neutral-300 text-neutral-600 hover:bg-neutral-50">Cancel</a>
                                    <button type="submit" class="px-8 py-2.5 rounded-lg bg-primary-600 text-white hover:bg-primary-700 transition">Simpan</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

@endsection

@push('scripts')
    <script>
        // Preview Gambar saat Upload
        $(document).ready(function() {
            function readURL(input) {
                if (input.files && input.files[0]) {
                    var reader = new FileReader();
                    reader.onload = function(e) {
                        $('#imagePreview').css('background-image', 'url(' + e.target.result + ')');
                        $('#imagePreview').hide().fadeIn(650);
                    }
                    reader.readAsDataURL(input.files[0]);
                }
            }
            $("#imageUpload").change(function() {
                readURL(this);
            });
        });
    </script>
@endpush