@extends('layout.layout')

@php
    $title = 'Daftar Roles';
    $subTitle = 'Manajemen Roles Akses';
    $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 roleNameText = document.getElementById("roleNameText");

            deleteModal.addEventListener("show.bs.modal", function(event) {
                const button = event.relatedTarget;
                const roleName = button.getAttribute("data-role-name");
                const roleId = button.getAttribute("data-role-id");

                roleNameText.textContent = roleName;
                deleteForm.action = "' . route('admin.roles.index') . '/" + roleId;
            });
        });
    </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 Roles</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.roles.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 Role</span>
                        </a>
                    </div>
                </div>
            </div>

            @if(session('success'))
                <div class="alert alert-success">{{ session('success') }}</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 Role</th>
                            <th scope="col" class="text-center">Jumlah Permissions</th>
                            <th scope="col" class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($roles as $role)
                        <tr>
                            <td class="text-center">{{ isset($roles->firstItem) ? ($roles->firstItem() + $loop->index) : $loop->iteration }}</td>
                            <td class="text-center">{{ $role->name }}</td>
                            <td class="text-center">{{ $role->permissions_count }}</td>
                            <td class="text-center">
                                <div class="d-flex align-items-center gap-10 justify-content-center">
                                    <a href="{{ route('admin.roles.edit', $role) }}" 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 Role">
                                        <iconify-icon icon="lucide:edit" class="menu-icon"></iconify-icon>
                                    </a>
                                    <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 Role" data-bs-toggle="modal" data-bs-target="#deleteModal" data-role-name="{{ $role->name }}" data-role-id="{{ $role->id }}">
                                        <iconify-icon icon="fluent:delete-24-regular" class="menu-icon"></iconify-icon>
                                    </button>
                                </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 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 Role</h4>
                        <p class="text-secondary mb-0">Apakah Anda yakin ingin menghapus role <strong id="roleNameText"></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
