@extends('layout.layout')

@php
    $title = 'Edit Pengguna';
    $subTitle = 'Manajemen Pengguna';
@endphp

@section('content')

    <div class="card basic-data-table">
        <div class="card-body">
            <div
                class="d-flex flex-column flex-md-row justify-content-between align-items-start align-items-md-center mb-3 gap-3">
                <div>
                    <h5 class="mb-0">Edit Pengguna: {{ $user->name }}</h5>
                </div>
                <div class="d-flex gap-2">
                    <a href="{{ route('admin.users.index') }}"
                        class="btn btn-secondary btn-sm d-flex align-items-center gap-2">
                        <iconify-icon icon="iconoir:arrow-left"></iconify-icon>
                        <span>Kembali</span>
                    </a>
                </div>
            </div>

            @if ($errors->any())
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <form method="POST" action="{{ route('admin.users.update', $user) }}">
                @csrf
                @method('PUT')
                <div class="row g-3">
                    <div class="col-12 col-md-6">
                        <label class="form-label">Nama Lengkap</label>
                        <input type="text" name="name" class="form-control" required
                            value="{{ old('name', $user->name) }}" placeholder="Masukkan nama lengkap">
                    </div>
                    <div class="col-12 col-md-6">
                        <label class="form-label">Email</label>
                        <input type="email" name="email" class="form-control" required
                            value="{{ old('email', $user->email) }}" placeholder="Masukkan email">
                    </div>
                    <div class="col-12 col-md-6">
                        <label class="form-label">Username</label>
                        <input type="text" name="username" class="form-control" required
                            value="{{ old('username', $user->username) }}" placeholder="Masukkan username">
                    </div>
                    <div class="col-12 col-md-6">
                        <label class="form-label">Password Baru</label>
                        <input type="password" name="password" class="form-control"
                            placeholder="Kosongkan jika tidak ingin mengubah">
                        <div class="form-text">Kosongkan jika tidak ingin mengubah password. Password harus minimal 8
                            karakter, mengandung huruf besar, huruf kecil, angka, dan simbol khusus.</div>
                    </div>
                    <div class="col-12 col-md-6">
                        <label class="form-label">Konfirmasi Password Baru</label>
                        <input type="password" name="password_confirmation" class="form-control"
                            placeholder="Konfirmasi password baru">
                    </div>
                </div>

                <div class="mt-4">
                    <label class="form-label">Roles</label>
                    <div class="row g-2">
                        @foreach ($roles as $role)
                            <div class="col-12 col-md-4">
                                <div class="form-check style-check d-flex align-items-center">
                                    <input class="form-check-input border border-neutral-300 me-8" type="checkbox"
                                        name="roles[]" value="{{ $role->name }}" id="role_{{ $role->id }}"
                                        {{ in_array($role->name, old('roles', $userRoles)) ? 'checked' : '' }}>
                                    <label class="form-check-label"
                                        for="role_{{ $role->id }}">{{ $role->name }}</label>
                                </div>
                            </div>
                        @endforeach
                    </div>
                </div>

                <!-- User Info -->
                <div class="mt-4">
                    <div class="alert alert-info">
                        <h6>Informasi Pengguna:</h6>
                        <ul class="mb-0">
                            <li>Dibuat: {{ $user->created_at->format('d/m/Y H:i:s') }}</li>
                            <li>Terakhir diperbarui: {{ $user->updated_at->format('d/m/Y H:i:s') }}</li>
                            @if ($user->email_verified_at)
                                <li>Email diverifikasi: {{ $user->email_verified_at->format('d/m/Y H:i:s') }}</li>
                            @else
                                <li class="text-warning">Email belum diverifikasi</li>
                            @endif
                        </ul>
                    </div>
                </div>

                <div class="mt-4 d-flex gap-2">
                    <button class="btn btn-primary d-flex align-items-center gap-2">
                        <iconify-icon icon="material-symbols:save"></iconify-icon>
                        <span>Perbarui</span>
                    </button>
                    <a href="{{ route('admin.users.show', $user) }}" class="btn btn-info d-flex align-items-center gap-2">
                        <iconify-icon icon="lucide:eye"></iconify-icon>
                        <span>Lihat Detail</span>
                    </a>
                    <a href="{{ route('admin.users.index') }}" class="btn btn-light">Batal</a>
                </div>
            </form>
        </div>
    </div>

@endsection
