@extends('layouts.app')

@section('title', 'Daftar Merk - 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 Merk
                </h1>
                <p class="text-muted mb-0">Atur dan kelola Merk produk untuk memudahkan pencarian pelanggan</p>
            </div>
            
            <div class="d-flex gap-3">
                {{-- SEARCH MODERN --}}
                <form action="{{ route('kategori.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 Merk...">
                        @if(request('search'))
                            <a href="{{ route('kategori.index') }}" class="clear-search">
                                <i class="bi bi-x-circle"></i>
                            </a>
                        @endif
                    </div>
                </form>

                {{-- TAMBAH KATEGORI BUTTON --}}
                <a href="{{ route('kategori.create') }}" class="btn-primary-modern">
                    <i class="bi bi-plus-lg"></i>
                    <span>Tambah Merk</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 MERK --}}
    <div class="table-container">
        <div class="table-responsive">
            <table class="modern-table">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>ID Merk</th>
                        <th>Nama Merk</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($kategori as $index => $k)
                        <tr>
                            <td data-label="No">
                                <span class="number-badge">
                                    {{ $loop->iteration }}
                                    @if(method_exists($kategori, 'currentPage') && method_exists($kategori, 'perPage'))
                                        {{ ($kategori->currentPage() - 1) * $kategori->perPage() + $loop->iteration }}
                                    @else
                                        {{ $loop->iteration }}
                                    @endif
                                </span>
                            </td>
                            <td data-label="ID Kategori">
                                <span class="id-badge">#{{ $k->kategori_id }}</span>
                            </td>
                            <td data-label="Nama Kategori">
                                <div class="kategori-info">
                                   
                                    <span class="kategori-name">{{ $k->nama_kategori }}</span>
                                </div>
                            </td>
                            <td data-label="Aksi">
                                <div class="action-buttons">
                                    <a href="{{ route('kategori.edit', $k->kategori_id) }}" 
                                       class="action-btn edit-btn" 
                                       title="Edit Kategori">
                                        <i class="bi bi-pencil"></i>
                                        <span>Edit</span>
                                    </a>
                                    
                                    <form action="{{ route('kategori.destroy', $k->kategori_id) }}" 
                                          method="POST" 
                                          class="delete-form"
                                          onsubmit="return confirmDelete(event, this, '{{ $k->nama_kategori }}')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="action-btn delete-btn" title="Hapus Kategori">
                                            <i class="bi bi-trash"></i>
                                            <span>Hapus</span>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="empty-row">
                                <div class="empty-state-table">
                                    <i class="bi bi-tags"></i>
                                    <h4>Belum Ada Merk</h4>
                                    <p>Yuk tambahkan Merk pertama kamu untuk mengelompokkan produk!</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    {{-- PAGINATION (jika ada) --}}
    @if(method_exists($kategori, 'links') && $kategori->hasPages())
        <div class="pagination-wrapper mt-5">
            {{ $kategori->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;
}

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

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

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

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

/* PRODUCT COUNT */
.product-count {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 12px;
    background: var(--pink-soft);
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--pink-primary);
}

.product-count i {
    font-size: 0.7rem;
}

/* 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;
    }
    
    .kategori-info {
        flex: 1;
        justify-content: flex-end;
    }
    
    .product-count {
        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 with sweet alert style
function confirmDelete(event, form, categoryName) {
    event.preventDefault();
    
    if (confirm(`Apakah Anda yakin ingin menghapus kategori "${categoryName}"?\n\nProduk dalam kategori ini tidak akan terhapus, hanya akan menjadi "Uncategorized".`)) {
        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