@extends('layout.layout')

@php
    $title = 'Detail Pengguna';
    $subTitle = 'Manajemen Pengguna';
    $script = '
    <script>
        // Handle delete modal
        document.addEventListener("DOMContentLoaded", function() {
            const deleteModal = document.getElementById("deleteModal");
            const deleteForm = document.getElementById("deleteForm");
            const userNameText = document.getElementById("userNameText");

            if (deleteModal) {
                deleteModal.addEventListener("show.bs.modal", function(event) {
                    const button = event.relatedTarget;
                    const userName = button.getAttribute("data-user-name");
                    const userId = button.getAttribute("data-user-id");

                    userNameText.textContent = userName;
                    deleteForm.action = "' . route('admin.users.index') . '/" + userId;
                });
            }
        });
    </script>';
@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">Detail Pengguna:</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>

            <div class="row g-4">
                <div class="col-md-6">
                    <div class="card h-100">
                        <div class="card-header">
                            <h6 class="mb-0">Informasi Dasar</h6>
                        </div>
                        <div class="card-body">
                            <table class="table table-borderless mb-0">
                                <tr>
                                    <td><strong>Nama:</strong></td>
                                    <td>{{ $user->name }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Email:</strong></td>
                                    <td>
                                        {{ $user->email }}
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>Username:</strong></td>
                                    <td>{{ $user->username }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Dibuat:</strong></td>
                                    <td>{{ $user->created_at->format('d/m/Y H:i:s') }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Diperbarui:</strong></td>
                                    <td>{{ $user->updated_at->format('d/m/Y H:i:s') }}</td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>

                <div class="col-md-6">
                    <div class="card h-100">
                        <div class="card-header">
                            <h6 class="mb-0">Role & Permissions</h6>
                        </div>
                        <div class="card-body">
                            <div class="mb-3">
                                <strong>Role:</strong><br>
                                @if ($user->roles->count() > 0)
                                    @foreach ($user->roles as $role)
                                        <span class="badge bg-primary me-1 mb-1">{{ $role->name }}</span>
                                    @endforeach
                                @else
                                    <span class="badge bg-secondary">Tidak ada role</span>
                                @endif
                            </div>

                            <div class="mb-3">
                                <strong>Permissions:</strong><br>
                                @if ($user->getAllPermissions()->count() > 0)
                                    <div class="row">
                                        @foreach ($user->getAllPermissions()->chunk(ceil($user->getAllPermissions()->count() / 2)) as $permissionChunk)
                                            <div class="col-12">
                                                @foreach ($permissionChunk as $permission)
                                                    <span class="badge bg-info me-1 mb-1">{{ $permission->name }}</span>
                                                @endforeach
                                            </div>
                                        @endforeach
                                    </div>
                                @else
                                    <span class="badge bg-secondary">Tidak ada permission</span>
                                @endif
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Activity Summary -->
            <div class="row g-4 mt-2">
                <div class="col-12">
                    <div class="card">
                        <div class="card-header">
                            <h6 class="mb-0">Ringkasan Aktivitas</h6>
                        </div>
                        <div class="card-body">
                            <div class="row g-4">
                                <div class="col-md-4">
                                    <div class="card bg-light text-center">
                                        <div class="card-body">
                                            <h6 class="card-title">Total Role</h6>
                                            <h3 class="text-primary mb-0">{{ $user->roles->count() }}</h3>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="card bg-light text-center">
                                        <div class="card-body">
                                            <h6 class="card-title">Total Permission</h6>
                                            <h3 class="text-info mb-0">{{ $user->getAllPermissions()->count() }}</h3>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="card bg-light text-center">
                                        <div class="card-body">
                                            <h6 class="card-title">Akun Aktif</h6>
                                            <h3 class="text-success mb-0">{{ max(1, $user->created_at->diffInDays(now())) }} hari
                                            </h3>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="mt-4 d-flex gap-2">
                <a href="{{ route('admin.users.edit', $user) }}" class="btn btn-primary d-flex align-items-center gap-2">
                    <iconify-icon icon="lucide:edit"></iconify-icon>
                    <span>Edit</span>
                </a>
                @if ($user->id !== auth()->id())
                    <button type="button" class="btn btn-danger d-flex align-items-center gap-2" data-bs-toggle="modal"
                        data-bs-target="#deleteModal" data-user-name="{{ $user->name }}"
                        data-user-id="{{ $user->id }}">
                        <iconify-icon icon="fluent:delete-24-regular"></iconify-icon>
                        <span>Hapus</span>
                    </button>
                @endif
            </div>
        </div>
    </div>

    <!-- Delete Modal -->
    <div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header border-0 pb-0">
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body text-center px-4 pb-4">
                    <div class="mb-3">
                        <div
                            class="bg-danger-focus w-80-px h-80-px d-flex justify-content-center align-items-center rounded-circle mx-auto mb-3">
                            <iconify-icon icon="fluent:delete-24-regular" class="text-danger-600 text-4xl"></iconify-icon>
                        </div>
                        <h4 class="mb-2 text-dark">Hapus Pengguna</h4>
                        <p class="text-secondary mb-0">Apakah Anda yakin ingin menghapus pengguna <strong
                                id="userNameText"></strong>? Tindakan ini tidak dapat dibatalkan.</p>
                    </div>
                    <div class="d-flex gap-2 justify-content-center">
                        <button type="button" class="btn btn-light-secondary px-4" data-bs-dismiss="modal">Batal</button>
                        <form id="deleteForm" method="POST" class="d-inline">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-danger px-4">Ya, Hapus</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

@endsection
