@extends('layout.layout')

@php
    $title = 'Daftar Pengguna';
    $subTitle = 'Manajemen Pengguna';
    $script = '
    <script>
        let table = new DataTable("#dataTable");

        // Handle delete modal
        document.addEventListener("DOMContentLoaded", function() {
            const deleteModal = document.getElementById("deleteModal");
            const deleteForm = document.getElementById("deleteForm");
            const userNameText = document.getElementById("userNameText");

            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">Daftar Pengguna</h5>
                </div>
                <div
                    class="d-flex flex-column flex-sm-row align-items-stretch align-items-sm-center justify-content-end gap-2 w-md-auto">
                    <div class="flex-fill flex-sm-fill-0">
                        <a href="{{ route('admin.users.create') }}"
                            class="btn btn-primary btn-sm d-flex align-items-center justify-content-center gap-2 w-100">
                            <iconify-icon icon="material-symbols:add" class="text-lg"></iconify-icon>
                            <span>Tambah Pengguna</span>
                        </a>
                    </div>
                </div>
            </div>

            @if (session('success'))
                <div class="alert alert-success">{{ session('success') }}</div>
            @endif

            @if (session('error'))
                <div class="alert alert-danger">{{ session('error') }}</div>
            @endif

            <div class="table-responsive">
                <table class="table bordered-table mb-0" id="dataTable" data-page-length='10'>
                    <thead>
                        <tr>
                            <th scope="col" class="text-center">No</th>
                            <th scope="col" class="text-center">Nama</th>
                            <th scope="col" class="text-center">Email</th>
                            <th scope="col" class="text-center">Username</th>
                            <th scope="col" class="text-center">Role</th>
                            <th scope="col" class="text-center">Dibuat</th>
                            <th scope="col" class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($users as $user)
                            <tr>
                                <td class="text-center">
                                    {{ isset($users->firstItem) ? $users->firstItem() + $loop->index : $loop->iteration }}
                                </td>
                                <td class="text-center">{{ $user->name }}</td>
                                <td class="text-center">{{ $user->email }}</td>
                                <td class="text-center">{{ $user->username }}</td>
                                <td class="text-center">
                                    @if ($user->roles->count() > 0)
                                        @foreach ($user->roles as $role)
                                            <span class="badge bg-primary me-1">{{ $role->name }}</span>
                                        @endforeach
                                    @else
                                        <span class="badge bg-secondary">Tidak ada role</span>
                                    @endif
                                </td>
                                <td class="text-center">{{ $user->created_at->format('d/m/Y H:i') }}</td>
                                <td class="text-center">
                                    <div class="d-flex align-items-center gap-10 justify-content-center">
                                        <a href="{{ route('admin.users.show', $user) }}"
                                            class="bg-info-focus text-info-600 bg-hover-info-200 fw-medium w-40-px h-40-px d-flex justify-content-center align-items-center rounded-circle text-decoration-none"
                                            title="Lihat Detail">
                                            <iconify-icon icon="lucide:eye" class="menu-icon"></iconify-icon>
                                        </a>
                                        <a href="{{ route('admin.users.edit', $user) }}"
                                            class="bg-success-focus text-success-600 bg-hover-success-200 fw-medium w-40-px h-40-px d-flex justify-content-center align-items-center rounded-circle text-decoration-none"
                                            title="Edit Pengguna">
                                            <iconify-icon icon="lucide:edit" class="menu-icon"></iconify-icon>
                                        </a>
                                        @if ($user->id !== auth()->id())
                                            <button type="button"
                                                class="remove-item-btn bg-danger-focus bg-hover-danger-200 text-danger-600 fw-medium w-40-px h-40-px d-flex justify-content-center align-items-center rounded-circle"
                                                title="Hapus Pengguna" 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"
                                                    class="menu-icon"></iconify-icon>
                                            </button>
                                        @endif
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </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">
                    <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"
                                style="font-size: 2.5rem;"></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></br>? Tindakan ini tidak dapat dibatalkan.</p>
                    </div>
                    <div class="d-flex gap-2 justify-content-center pb-8">
                        <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
