@extends('layout.layout')

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

@section('content')
    <div class="card basic-data-table">
        <div class="card-body">
            <!-- Header Section -->
            <div class="d-flex flex-column flex-md-row justify-content-between align-items-start align-items-md-center mb-4 gap-3">
                <div>
                    <h5 class="mb-0">Edit Pengguna</h5>
                </div>
                <div>
                    <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>

            <!-- Error Messages -->
            @if ($errors->any())
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <div class="d-flex align-items-center mb-2">
                        <iconify-icon icon="material-symbols:error" class="me-2"></iconify-icon>
                        <strong>Terjadi kesalahan:</strong>
                    </div>
                    <ul class="mb-0 ps-3">
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif

            <!-- Form -->
            <form method="POST" action="{{ route('admin.users.update', $user) }}" class="needs-validation" novalidate>
                @csrf
                @method('PUT')

                <!-- Personal Information Section -->
                <div class="mb-4">
                    <h6 class="mb-3 text-primary d-flex align-items-center">
                        <iconify-icon icon="mdi:account-circle" class="me-2"></iconify-icon>
                        Informasi Personal
                    </h6>
                    <div class="row g-3">
                        <div class="col-12 col-md-6">
                            <label class="form-label fw-medium">
                                Nama Lengkap <span class="text-danger">*</span>
                            </label>
                            <input type="text"
                                   name="name"
                                   class="form-control @error('name') is-invalid @enderror"
                                   required
                                   value="{{ old('name', $user->name) }}"
                                   placeholder="Masukkan nama lengkap">
                            @error('name')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-12 col-md-6">
                            <label class="form-label fw-medium">
                                Email <span class="text-danger">*</span>
                            </label>
                            <input type="email"
                                   name="email"
                                   class="form-control @error('email') is-invalid @enderror"
                                   required
                                   value="{{ old('email', $user->email) }}"
                                   placeholder="Masukkan email">
                            @error('email')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-12 col-md-6">
                            <label class="form-label fw-medium">
                                Username <span class="text-danger">*</span>
                            </label>
                            <input type="text"
                                   name="username"
                                   class="form-control @error('username') is-invalid @enderror"
                                   required
                                   value="{{ old('username', $user->username) }}"
                                   placeholder="Masukkan username">
                            @error('username')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>
                </div>

                <!-- Divider -->
                <hr class="my-4">

                <!-- Password Section -->
                <div class="mb-4">
                    <h6 class="mb-3 text-primary d-flex align-items-center">
                        <iconify-icon icon="mdi:lock" class="me-2"></iconify-icon>
                        Keamanan
                    </h6>
                    <div class="row g-3">
                        <div class="col-12 col-md-6">
                            <label class="form-label fw-medium">Password Baru</label>
                            <input type="password"
                                   name="password"
                                   class="form-control @error('password') is-invalid @enderror"
                                   placeholder="Kosongkan jika tidak ingin mengubah">
                            <div class="form-text d-flex align-items-center">
                                <iconify-icon icon="mdi:information" class="me-1"></iconify-icon>
                                Kosongkan jika tidak ingin mengubah password. Password minimal 8 karakter dengan huruf besar, kecil, angka, dan simbol.
                            </div>
                            @error('password')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-12 col-md-6">
                            <label class="form-label fw-medium">Konfirmasi Password Baru</label>
                            <input type="password"
                                   name="password_confirmation"
                                   class="form-control"
                                   placeholder="Konfirmasi password baru">
                        </div>
                    </div>
                </div>

                <!-- Roles Section -->
                <div class="mb-4">
                    <h6 class="mb-3 text-primary d-flex align-items-center">
                        <iconify-icon icon="mdi:shield-account" class="me-2"></iconify-icon>
                        Roles & Permissions
                    </h6>
                    <div class="p-3 bg-light rounded-3">
                        @if ($roles->count() > 0)
                            <div class="row g-2">
                                @foreach ($roles as $role)
                                    <div class="col-12 col-sm-6 col-md-4">
                                        <div class="form-check style-check d-flex align-items-center gap-2">
                                            <input class="form-check-input border border-neutral-300"
                                                   type="checkbox"
                                                   name="roles[]"
                                                   value="{{ $role->name }}"
                                                   id="role_{{ $role->id }}"
                                                   {{ in_array($role->name, old('roles', $userRoles)) ? 'checked' : '' }}>
                                            <label class="form-check-label fw-medium" for="role_{{ $role->id }}">
                                                {{ ucfirst($role->name) }}
                                            </label>
                                        </div>
                                    </div>
                                @endforeach
                            </div>
                        @else
                            <div class="text-center text-muted py-3">
                                <iconify-icon icon="mdi:shield-off" class="fs-2 mb-2"></iconify-icon>
                                <p class="mb-0">Tidak ada roles yang tersedia</p>
                            </div>
                        @endif
                    </div>
                </div>

                <!-- Action Buttons -->
                <div class="d-flex align-items-center gap-2 pt-3 border-top">
                    <button type="submit" class="btn btn-primary d-flex align-items-center gap-2">
                        <iconify-icon icon="material-symbols:save"></iconify-icon>
                        <span>Perbarui Pengguna</span>
                    </button>
                    <a href="{{ route('admin.users.index') }}" class="btn btn-light d-flex align-items-center gap-2">
                        <iconify-icon icon="material-symbols:cancel"></iconify-icon>
                        <span>Batal</span>
                    </a>
                </div>
            </form>
        </div>
    </div>
@endsection
