@extends('layouts.app')

@section('title', 'Edit 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">
                    ✨ Edit Merk
                </h1>
                <p class="text-muted mb-0">Perbarui informasi Merk di Kei's Store</p>
            </div>
            
            <div>
                <a href="{{ route('kategori.index') }}" class="btn-outline-modern">
                    <i class="bi bi-arrow-left"></i>
                    <span>Kembali ke Daftar Merk</span>
                </a>
            </div>
        </div>
    </div>

    {{-- ERROR ALERT --}}
    @if($errors->any())
        <div class="alert-modern alert-error-modern mb-4">
            <i class="bi bi-exclamation-triangle-fill"></i>
            <div class="alert-content">
                <strong>Terjadi Kesalahan!</strong>
                <ul class="mb-0 mt-1 ps-3">
                    @foreach($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
            <button class="close-alert"><i class="bi bi-x-lg"></i></button>
        </div>
    @endif

    {{-- FORM CARD --}}
    <div class="form-card">
        <div class="form-card-header">
            <div class="header-icon">
                <i class="bi bi-plus-square-fill"></i>
            </div>
            <div>
                <h3 class="header-title">Form Edit Merk</h3>
                <p class="header-subtitle">Edit data Merk di bawah ini</p>
            </div>
        </div>

        <div class="form-card-body">
            <form action="{{ route('kategori.update', $kategori->kategori_id) }}" method="POST" class="modern-form" id="categoryForm">
                @csrf
                @method('PUT')

        
                {{-- INPUT NAMA KATEGORI --}}
                <div class="form-group-modern">
                    <label for="nama_kategori" class="form-label-modern">
                        <i class="bi bi-tag-fill"></i>
                        Nama Merk
                        <span class="required-badge">Wajib</span>
                    </label>
                    <div class="input-wrapper">
                        <span class="input-icon">
                            <i class="bi bi-tags"></i>
                        </span>
                        <input type="text" 
                               name="nama_kategori" 
                               id="nama_kategori"
                               value="{{ old('nama_kategori', $kategori->nama_kategori) }}" 
                               class="form-input-modern" 
                               placeholder="Contoh: Elektronik, Fashion, Makanan..."
                               required
                               maxlength="100"
                               autocomplete="off">
                        <span class="input-character-count" id="charCount">0/100</span>
                    </div>
                    <div class="form-hint">
                        <i class="bi bi-info-circle"></i>
                        Nama Merk 
                    </div>
                    @error('nama_kategori')
                        <div class="form-hint text-danger">
                            <i class="bi bi-exclamation-circle"></i>
                            {{ $message }}
                        </div>
                    @enderror
                </div>

                {{-- ACTION BUTTONS --}}
                <div class="form-actions">
                    <button type="submit" class="btn-submit-modern" id="submitBtn">
                        <i class="bi bi-check-lg"></i>
                        <span>Update Merk</span>
                        <div class="btn-spinner d-none">
                            <div class="spinner-border spinner-border-sm" role="status"></div>
                        </div>
                    </button>
                    <a href="{{ route('kategori.index') }}" class="btn-cancel-modern">
                        <i class="bi bi-x-lg"></i>
                        <span>Batal</span>
                    </a>
                </div>
            </form>
        </div>
    </div>

    {{-- INFO TIPS --}}
    <div class="info-tips mt-4">
        <div class="tips-content">
            <i class="bi bi-lightbulb-fill"></i>
            <div>
                <strong>Tips:</strong>
                Gunakan nama Merk yang singkat, jelas, dan mudah diingat. Hindari duplikasi nama Merk.
            </div>
        </div>
    </div>
</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-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;
}

/* BUTTON OUTLINE MODERN */
.btn-outline-modern {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    background: transparent;
    color: var(--pink-primary);
    border: 2px solid var(--pink-primary);
    border-radius: 60px;
    font-weight: 600;
    font-size: 0.875rem;
    text-decoration: none;
    transition: all 0.3s ease;
}

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

/* ALERT MODERN */
.alert-modern {
    display: flex;
    align-items: flex-start;
    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-error-modern {
    background: linear-gradient(135deg, #fef2f2 0%, #fee2e2 100%);
    border: 1px solid #fecaca;
    color: #991b1b;
}

.alert-content {
    flex: 1;
}

.alert-content strong {
    display: block;
    margin-bottom: 4px;
}

.alert-content ul {
    margin-top: 8px;
    padding-left: 20px;
}

.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;
}

/* FORM CARD */
.form-card {
    background: white;
    border-radius: 24px;
    overflow: hidden;
    box-shadow: var(--shadow-lg);
    border: 1px solid rgba(236, 72, 153, 0.1);
}

.form-card-header {
    background: linear-gradient(135deg, #fdf2f8 0%, #fce7f3 100%);
    padding: 24px 32px;
    display: flex;
    align-items: center;
    gap: 16px;
    border-bottom: 1px solid rgba(236, 72, 153, 0.1);
}

.header-icon {
    width: 56px;
    height: 56px;
    background: var(--pink-gradient);
    border-radius: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.header-icon i {
    font-size: 28px;
    color: white;
}

.header-title {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--gray-800);
    margin: 0 0 4px 0;
}

.header-subtitle {
    font-size: 0.875rem;
    color: var(--gray-600);
    margin: 0;
}

.form-card-body {
    padding: 32px;
}

/* ICON PREVIEW SECTION */
.icon-preview-section {
    background: linear-gradient(135deg, #faf9fe 0%, #f5f3ff 100%);
    border-radius: 20px;
    padding: 20px;
    margin-bottom: 32px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 16px;
}

.icon-preview-label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--gray-600);
}

.icon-preview-label i {
    font-size: 1.125rem;
    color: var(--pink-primary);
}

.icon-preview-wrapper {
    text-align: center;
}

.icon-preview {
    width: 80px;
    height: 80px;
    background: var(--pink-gradient);
    border-radius: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 8px;
    box-shadow: var(--shadow-pink);
    transition: transform 0.3s ease;
}

.icon-preview:hover {
    transform: scale(1.05);
}

.preview-emoji {
    font-size: 3rem;
}

.icon-hint {
    font-size: 0.7rem;
    color: var(--gray-600);
    margin: 0;
}

/* FORM GROUP MODERN */
.form-group-modern {
    margin-bottom: 28px;
}

.form-label-modern {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    color: var(--gray-800);
    margin-bottom: 12px;
    font-size: 0.9rem;
}

.form-label-modern i {
    color: var(--pink-primary);
    font-size: 1rem;
}

.required-badge {
    background: var(--pink-soft);
    color: var(--pink-primary);
    padding: 2px 8px;
    border-radius: 20px;
    font-size: 0.7rem;
    font-weight: 500;
    margin-left: 8px;
}

.input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.input-icon {
    position: absolute;
    left: 16px;
    color: #9ca3af;
    font-size: 1rem;
    pointer-events: none;
    z-index: 1;
}

.form-input-modern {
    width: 100%;
    padding: 14px 16px 14px 44px;
    font-size: 1rem;
    border: 2px solid #e5e7eb;
    border-radius: 16px;
    transition: all 0.3s ease;
    background: white;
}

.form-input-modern:focus {
    outline: none;
    border-color: var(--pink-primary);
    box-shadow: 0 0 0 4px rgba(236, 72, 153, 0.1);
}

.form-input-modern.is-valid {
    border-color: #10b981;
}

.form-input-modern.is-invalid {
    border-color: #ef4444;
}

.input-character-count {
    position: absolute;
    right: 16px;
    font-size: 0.7rem;
    color: var(--gray-600);
    pointer-events: none;
}

.form-hint {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 8px;
    font-size: 0.75rem;
    color: var(--gray-600);
}

.form-hint i {
    font-size: 0.7rem;
}

.form-hint.text-danger {
    color: #ef4444;
}

/* SUGGESTED SECTION */
.suggested-section {
    margin-bottom: 32px;
}

.suggested-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 12px;
}

.suggest-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    background: var(--gray-100);
    border-radius: 40px;
    font-size: 0.8rem;
    color: var(--gray-800);
    cursor: pointer;
    transition: all 0.2s ease;
    border: 1px solid transparent;
}

.suggest-badge:hover {
    background: var(--pink-soft);
    border-color: var(--pink-primary);
    transform: translateY(-2px);
}

/* FORM ACTIONS */
.form-actions {
    display: flex;
    gap: 16px;
    padding-top: 24px;
    border-top: 1px solid #e5e7eb;
}

.btn-submit-modern {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 12px 28px;
    background: var(--pink-gradient);
    color: white;
    border: none;
    border-radius: 60px;
    font-weight: 600;
    font-size: 0.875rem;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: var(--shadow-pink);
    min-width: 160px;
}

.btn-submit-modern:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: var(--shadow-pink-lg);
}

.btn-submit-modern:disabled {
    opacity: 0.7;
    cursor: not-allowed;
}

.btn-cancel-modern {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 28px;
    background: transparent;
    color: var(--gray-600);
    border: 2px solid #e5e7eb;
    border-radius: 60px;
    font-weight: 600;
    font-size: 0.875rem;
    text-decoration: none;
    transition: all 0.3s ease;
}

.btn-cancel-modern:hover {
    border-color: var(--pink-primary);
    color: var(--pink-primary);
    transform: translateY(-1px);
}

/* INFO TIPS */
.info-tips {
    background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
    border-radius: 16px;
    padding: 12px 20px;
    border: 1px solid #fcd34d;
}

.tips-content {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 0.8rem;
    color: #92400e;
}

.tips-content i {
    font-size: 1.25rem;
}

/* RESPONSIVE */
@media (max-width: 768px) {
    .hero-section {
        padding: 1.5rem;
    }
    
    .display-6 {
        font-size: 1.25rem;
    }
    
    .form-card-header {
        padding: 20px;
    }
    
    .form-card-body {
        padding: 20px;
    }
    
    .icon-preview-section {
        flex-direction: column;
        text-align: center;
    }
    
    .form-actions {
        flex-direction: column;
    }
    
    .btn-submit-modern,
    .btn-cancel-modern {
        width: 100%;
        justify-content: center;
    }
    
    .suggested-badges {
        justify-content: center;
    }
}

@media (max-width: 480px) {
    .header-icon {
        width: 48px;
        height: 48px;
    }
    
    .header-icon i {
        font-size: 24px;
    }
    
    .header-title {
        font-size: 1rem;
    }
    
    .header-subtitle {
        font-size: 0.75rem;
    }
    
    .icon-preview {
        width: 60px;
        height: 60px;
    }
    
    .preview-emoji {
        font-size: 2rem;
    }
}

/* LOADING SPINNER */
.btn-spinner {
    display: inline-flex;
}

/* ANIMATION FOR FORM */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.form-card {
    animation: fadeInUp 0.5s ease;
}
</style>

<script>
// Character counter
const inputField = document.getElementById('nama_kategori');
const charCount = document.getElementById('charCount');

if (inputField) {
    const currentLength = inputField.value.length;
    charCount.textContent = `${currentLength}/100`;
    
    if (currentLength > 0 && currentLength <= 100) {
        inputField.classList.add('is-valid');
    }
    
    inputField.addEventListener('input', function() {
        const length = this.value.length;
        charCount.textContent = `${length}/100`;
        
        if (length > 0 && length <= 100) {
            this.classList.remove('is-invalid');
            this.classList.add('is-valid');
        } else if (length > 100) {
            this.classList.remove('is-valid');
            this.classList.add('is-invalid');
        } else {
            this.classList.remove('is-valid', 'is-invalid');
        }
    });
}

// Suggested categories click handler
document.querySelectorAll('.suggest-badge').forEach(badge => {
    badge.addEventListener('click', function() {
        const categoryName = this.getAttribute('data-name');
        inputField.value = categoryName;
        inputField.dispatchEvent(new Event('input'));
        inputField.focus();
        
        // Add visual feedback
        this.style.transform = 'scale(0.95)';
        setTimeout(() => {
            this.style.transform = '';
        }, 200);
    });
});

// Form submit with loading state
const form = document.getElementById('categoryForm');
const submitBtn = document.getElementById('submitBtn');
const btnText = submitBtn ? submitBtn.querySelector('span') : null;
const btnSpinner = submitBtn ? submitBtn.querySelector('.btn-spinner') : null;

if (form && submitBtn) {
    form.addEventListener('submit', function(e) {
        // Validate input
        if (!inputField.value.trim()) {
            e.preventDefault();
            inputField.classList.add('is-invalid');
            inputField.focus();
            alert('Nama kategori tidak boleh kosong!');
            return false;
        }
        
        if (inputField.value.length > 100) {
            e.preventDefault();
            inputField.classList.add('is-invalid');
            alert('Nama kategori maksimal 100 karakter!');
            return false;
        }
        
        // Show loading state
        if (btnText && btnSpinner) {
            btnText.classList.add('d-none');
            btnSpinner.classList.remove('d-none');
        }
        submitBtn.disabled = true;
    });
}

// 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);

// Focus on input
if (inputField) {
    inputField.focus();
    inputField.select();
}
</script>
@endsection