@extends('layouts.app')

@section('title', 'Daftar Pengguna - Kei\'s Store')

@section('content')
<div class="container-fluid px-4 py-3">

    {{-- HEADER SECTION --}}
    <div class="hero-section mb-5">
        <div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3">
            <div>
                <h1 class="display-6 fw-bold mb-2">
                    👥 Kelola Pengguna
                </h1>
                <p class="text-muted mb-0">Atur dan kelola pengguna yang memiliki akses ke sistem Kei's Store</p>
            </div>
            
            <div class="d-flex gap-3">
                {{-- SEARCH MODERN --}}
                <form action="{{ route('users.index') }}" method="GET" class="search-wrapper">
                    <div class="search-box">
                        <i class="bi bi-search search-icon"></i>
                        <input type="text" name="search" value="{{ request('search') }}"
                               class="search-input" placeholder="Cari pengguna...">
                        @if(request('search'))
                            <a href="{{ route('users.index') }}" class="clear-search">
                                <i class="bi bi-x-circle"></i>
                            </a>
                        @endif
                    </div>
                </form>

                {{-- TAMBAH PENGGUNA BUTTON --}}
                <a href="{{ route('users.create') }}" class="btn-primary-modern">
                    <i class="bi bi-plus-lg"></i>
                    <span>Tambah Pengguna</span>
                </a>
            </div>
        </div>
    </div>

    {{-- ALERT SUCCESS --}}
    @if(session('success'))
        <div class="alert-modern alert-success-modern mb-4">
            <i class="bi bi-check-circle-fill"></i>
            <span>{{ session('success') }}</span>
            <button class="close-alert"><i class="bi bi-x-lg"></i></button>
        </div>
    @endif

    {{-- TABLE PENGGUNA --}}
    <div class="table-container">
        <div class="table-responsive">
            <table class="modern-table">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>ID Pengguna</th>
                        <th>Username</th>
                        <th>Email</th>
                        <th>Role</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($users as $index => $u)
                        <tr>
                            <td data-label="No">
                                <span class="number-badge">{{ $loop->iteration }}</span>
                            </td>
                            <td data-label="ID Pengguna">
                                <span class="id-badge">#{{ $u->user_id }}</span>
                            </td>
                            <td data-label="Username">
                                <div class="user-info">
                                    <span class="user-name">{{ $u->username }}</span>
                                </div>
                            </td>
                            <td data-label="Email">
                                <span class="user-email">
                                    <i class="bi bi-envelope"></i>
                                    {{ $u->email }}
                                </span>
                            </td>
                            <td data-label="Role">
                                @if($u->role == 'admin')
                                    <span class="role-badge role-admin">
                                        <i class="bi bi-shield-lock-fill"></i> Admin
                                    </span>
                                @elseif($u->role == 'pengguna')
                                    <span class="role-badge role-user">
                                        <i class="bi bi-person-fill"></i> Pengguna
                                    </span>
                                @else
                                    <span class="role-badge role-other">
                                        {{ ucfirst($u->role) }}
                                    </span>
                                @endif
                            </td>
                            <td data-label="Aksi">
                                <div class="action-buttons">
                                    <a href="{{ route('users.edit', $u->user_id) }}" 
                                       class="action-btn edit-btn" 
                                       title="Edit Pengguna">
                                        <i class="bi bi-pencil"></i>
                                        <span>Edit</span>
                                    </a>
                                    
                                    <form action="{{ route('users.destroy', $u->user_id) }}" 
                                          method="POST" 
                                          class="delete-form"
                                          onsubmit="return confirmDelete(event, this, '{{ $u->username }}')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="action-btn delete-btn" title="Hapus Pengguna">
                                            <i class="bi bi-trash"></i>
                                            <span>Hapus</span>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="empty-row">
                                <div class="empty-state-table">
                                    <i class="bi bi-people"></i>
                                    <h4>Belum Ada Pengguna</h4>
                                    <p>Yuk tambahkan pengguna pertama untuk mengelola sistem!</p>
                                    <a href="{{ route('users.create') }}" class="btn-primary-modern">
                                        <i class="bi bi-plus-lg"></i> Tambah Pengguna Sekarang
                                    </a>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    {{-- PAGINATION (jika ada) --}}
    @if(method_exists($users, 'links') && $users->hasPages())
        <div class="pagination-wrapper mt-5">
            {{ $users->appends(request()->query())->links() }}
        </div>
    @endif
</div>

<style>
/* MODERN CSS RESET & VARIABLES */
:root {
    --pink-primary: #ec4899;
    --pink-dark: #db2777;
    --pink-light: #fdf2f8;
    --pink-soft: #fce7f3;
    --pink-gradient: linear-gradient(135deg, #ec4899 0%, #db2777 100%);
    --pink-gradient-soft: linear-gradient(135deg, #fbcfe8 0%, #fce7f3 100%);
    --gray-50: #faf9fe;
    --gray-100: #f5f3ff;
    --gray-200: #ede9fe;
    --gray-600: #6b7280;
    --gray-700: #374151;
    --gray-800: #1f2937;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1);
    --shadow-pink: 0 4px 14px 0 rgba(236, 72, 153, 0.25);
    --shadow-pink-lg: 0 20px 25px -12px rgba(236, 72, 153, 0.3);
}

/* HERO SECTION */
.hero-section {
    background: linear-gradient(135deg, #fdf2f8 0%, #fce7f3 100%);
    padding: 2rem 2rem;
    border-radius: 28px;
    margin-bottom: 2rem;
}

.display-6 {
    font-size: 1.875rem;
    background: linear-gradient(135deg, #ec4899 0%, #db2777 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

/* SEARCH MODERN */
.search-wrapper {
    width: 300px;
}

.search-box {
    position: relative;
    width: 100%;
}

.search-icon {
    position: absolute;
    left: 16px;
    top: 50%;
    transform: translateY(-50%);
    color: #9ca3af;
    font-size: 1.125rem;
    pointer-events: none;
}

.search-input {
    width: 100%;
    padding: 10px 40px 10px 45px;
    border: 2px solid transparent;
    border-radius: 60px;
    font-size: 0.875rem;
    background: white;
    transition: all 0.3s ease;
    box-shadow: var(--shadow-sm);
}

.search-input:focus {
    outline: none;
    border-color: var(--pink-primary);
    box-shadow: var(--shadow-pink);
}

.clear-search {
    position: absolute;
    right: 16px;
    top: 50%;
    transform: translateY(-50%);
    color: #9ca3af;
    text-decoration: none;
    transition: color 0.2s;
}

.clear-search:hover {
    color: var(--pink-primary);
}

/* BUTTON MODERN */
.btn-primary-modern {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 24px;
    background: var(--pink-gradient);
    color: white;
    border: none;
    border-radius: 60px;
    font-weight: 600;
    font-size: 0.875rem;
    text-decoration: none;
    transition: all 0.3s ease;
    box-shadow: var(--shadow-sm);
}

.btn-primary-modern:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-pink-lg);
    color: white;
}

/* ALERT MODERN */
.alert-modern {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 20px;
    border-radius: 16px;
    font-size: 0.875rem;
    position: relative;
    animation: slideDown 0.3s ease;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.alert-success-modern {
    background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%);
    border: 1px solid #bbf7d0;
    color: #166534;
}

.close-alert {
    margin-left: auto;
    background: none;
    border: none;
    cursor: pointer;
    color: inherit;
    opacity: 0.6;
    transition: opacity 0.2s;
}

.close-alert:hover {
    opacity: 1;
}

/* TABLE CONTAINER */
.table-container {
    background: white;
    border-radius: 20px;
    overflow: hidden;
    box-shadow: var(--shadow-lg);
    border: 1px solid rgba(236, 72, 153, 0.1);
}

/* MODERN TABLE */
.modern-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}

.modern-table thead {
    background: linear-gradient(135deg, #fdf2f8 0%, #fce7f3 100%);
}

.modern-table th {
    padding: 16px 20px;
    text-align: left;
    font-weight: 600;
    color: var(--gray-700);
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 2px solid rgba(236, 72, 153, 0.2);
}

.modern-table td {
    padding: 16px 20px;
    border-bottom: 1px solid #f0f0f0;
    color: var(--gray-700);
    transition: all 0.2s ease;
}

.modern-table tbody tr {
    transition: all 0.2s ease;
}

.modern-table tbody tr:hover {
    background: linear-gradient(135deg, #fef2f8 0%, #fdf2f8 100%);
}

.modern-table tbody tr:last-child td {
    border-bottom: none;
}

/* NUMBER BADGE */
.number-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    background: linear-gradient(135deg, #fce7f3 0%, #fbcfe8 100%);
    border-radius: 12px;
    font-weight: 600;
    color: var(--pink-primary);
    font-size: 0.8rem;
}

/* ID BADGE */
.id-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    background: var(--gray-100);
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--gray-600);
    font-family: monospace;
}

/* USER INFO */
.user-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.user-avatar {
    width: 36px;
    height: 36px;
    background: linear-gradient(135deg, #fce7f3 0%, #fbcfe8 100%);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    transition: all 0.2s ease;
}

.modern-table tbody tr:hover .user-avatar {
    background: var(--pink-gradient);
    transform: scale(1.05);
}

.user-name {
    font-weight: 600;
    color: var(--gray-800);
    font-size: 0.9rem;
}

/* USER EMAIL */
.user-email {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 0.85rem;
    color: var(--gray-600);
}

.user-email i {
    font-size: 0.8rem;
    color: var(--pink-primary);
}

/* ROLE BADGE */
.role-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 12px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 500;
}

.role-admin {
    background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
    color: #92400e;
    border: 1px solid #fcd34d;
}

.role-user {
    background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 100%);
    color: #1e40af;
    border: 1px solid #93c5fd;
}

.role-other {
    background: var(--gray-100);
    color: var(--gray-600);
    border: 1px solid #e5e7eb;
}

/* ACTION BUTTONS */
.action-buttons {
    display: flex;
    gap: 8px;
}

.action-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 6px 14px;
    border-radius: 40px;
    font-size: 0.7rem;
    font-weight: 600;
    text-decoration: none;
    transition: all 0.2s ease;
    cursor: pointer;
    border: none;
    background: transparent;
}

.action-btn span {
    display: inline-block;
}

.edit-btn {
    background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
    color: #92400e;
    border: 1px solid #fcd34d;
}

.edit-btn:hover {
    background: linear-gradient(135deg, #fde68a 0%, #fcd34d 100%);
    transform: translateY(-2px);
    color: #78350f;
}

.delete-btn {
    background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%);
    color: #dc2626;
    border: 1px solid #fca5a5;
}

.delete-btn:hover {
    background: linear-gradient(135deg, #fecaca 0%, #fca5a5 100%);
    transform: translateY(-2px);
    color: #b91c1c;
}

/* EMPTY STATE IN TABLE */
.empty-row td {
    padding: 60px 20px !important;
    text-align: center;
}

.empty-state-table {
    text-align: center;
    padding: 40px 20px;
}

.empty-state-table i {
    font-size: 4rem;
    color: var(--pink-primary);
    margin-bottom: 16px;
    display: inline-block;
    background: var(--pink-gradient-soft);
    padding: 20px;
    border-radius: 60px;
}

.empty-state-table h4 {
    font-size: 1.25rem;
    margin-bottom: 8px;
    color: var(--gray-800);
}

.empty-state-table p {
    color: var(--gray-600);
    margin-bottom: 24px;
}

/* PAGINATION WRAPPER */
.pagination-wrapper {
    display: flex;
    justify-content: center;
}

.pagination-wrapper nav {
    display: inline-block;
}

.pagination-wrapper .pagination {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: center;
}

.pagination-wrapper .page-item .page-link {
    padding: 8px 14px;
    border-radius: 12px;
    border: 1px solid var(--pink-soft);
    color: var(--pink-primary);
    background: white;
    transition: all 0.2s;
}

.pagination-wrapper .page-item.active .page-link {
    background: var(--pink-gradient);
    border-color: transparent;
    color: white;
}

.pagination-wrapper .page-item .page-link:hover {
    background: var(--pink-soft);
    border-color: var(--pink-primary);
    transform: translateY(-2px);
}

/* RESPONSIVE */
@media (max-width: 768px) {
    .hero-section {
        padding: 1.5rem;
    }
    
    .display-6 {
        font-size: 1.25rem;
    }
    
    .search-wrapper {
        width: 100%;
    }
    
    .action-btn span {
        display: none;
    }
    
    .action-btn {
        padding: 8px 12px;
    }
    
    .action-btn i {
        font-size: 1rem;
    }
}

@media (max-width: 640px) {
    .modern-table thead {
        display: none;
    }
    
    .modern-table tbody tr {
        display: block;
        margin-bottom: 16px;
        border: 1px solid var(--pink-soft);
        border-radius: 16px;
        padding: 16px;
        background: white;
    }
    
    .modern-table tbody tr:hover {
        transform: none;
    }
    
    .modern-table td {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 10px 0;
        border-bottom: 1px solid #f0f0f0;
    }
    
    .modern-table td:last-child {
        border-bottom: none;
    }
    
    .modern-table td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--gray-600);
        font-size: 0.75rem;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }
    
    .number-badge, .id-badge {
        margin-left: auto;
    }
    
    .user-info {
        flex: 1;
        justify-content: flex-end;
    }
    
    .user-email {
        margin-left: auto;
    }
    
    .role-badge {
        margin-left: auto;
    }
    
    .action-buttons {
        margin-left: auto;
    }
    
    .empty-state-table {
        padding: 20px;
    }
    
    .empty-state-table i {
        font-size: 3rem;
        padding: 15px;
    }
    
    .empty-state-table h4 {
        font-size: 1rem;
    }
}

/* ANIMATIONS */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.modern-table tbody tr {
    animation: fadeIn 0.3s ease forwards;
}
</style>

<script>
// Confirm delete function
function confirmDelete(event, form, userName) {
    event.preventDefault();
    
    if (confirm(`Apakah Anda yakin ingin menghapus pengguna "${userName}"?\n\nTindakan ini tidak dapat dibatalkan.`)) {
        form.submit();
    }
    return false;
}

// Close alert functionality
document.querySelectorAll('.close-alert').forEach(button => {
    button.addEventListener('click', function() {
        this.closest('.alert-modern').style.display = 'none';
    });
});

// Auto hide alerts after 5 seconds
setTimeout(function() {
    document.querySelectorAll('.alert-modern').forEach(alert => {
        alert.style.display = 'none';
    });
}, 5000);

// Add data-label for responsive table
document.querySelectorAll('.modern-table tbody tr').forEach(row => {
    const cells = row.querySelectorAll('td');
    const headers = document.querySelectorAll('.modern-table thead th');
    
    cells.forEach((cell, index) => {
        if (headers[index]) {
            const headerText = headers[index].innerText;
            cell.setAttribute('data-label', headerText);
        }
    });
});
</script>
@endsection