@extends('layouts.student')

@section('title', 'Customize Your Keychain - Lindsay\'s Leather')

@section('head')
<style>
            body.with-order-header {
                font-family: 'Montserrat', sans-serif;
                background-color: #f8f9fa;
                margin: 0;
                padding: 0;
                min-height: 100vh;
                display: flex;
                align-items: center;
                justify-content: center;
            }
            
            .landing-container {
                max-width: 700px;
                width: 100%;
                padding: 2rem;
                margin: 0 auto; /* Center the container */
            }
            
            .lindsay-card {
                background: white;
                border-radius: 16px;
                box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
                padding: 2rem;
                border: 1px solid rgba(181, 112, 47, 0.1);
            }
            
            .flex {
                display: flex;
            }
            
            .justify-between {
                justify-content: space-between;
            }
            
            .items-center {
                align-items: center;
            }
            
            .mb-6 {
                margin-bottom: 1.5rem;
            }
            
            
            .body-text {
                font-family: 'Montserrat', sans-serif;
                font-weight: 400;
            }
            
            .progress-bar {
                height: 4px;
                background: #e5e7eb;
                border-radius: 2px;
                overflow: hidden;
                margin-bottom: 2rem;
            }
            
            .progress-fill {
                height: 100%;
                background: #B5702F;
                border-radius: 2px;
                transition: width 0.5s ease;
            }
            
            .landing-title {
                font-family: 'Lato', sans-serif;
                font-weight: 700;
                font-size: 2.5rem;
                color: #371C16;
                margin-bottom: 1rem;
                line-height: 1.2;
                text-align: center;
            }
            
            .letter-inputs-grid {
                display: flex;
                flex-direction: row;
                gap: 0.5rem;
                margin-bottom: 2rem;
                justify-content: center;
                flex-wrap: nowrap;
            }
            
            .letter-input {
                width: 50px;
                height: 60px;
                border: 2px solid #e5e7eb;
                border-radius: 8px;
                text-align: center;
                font-family: 'Lato', sans-serif;
                font-weight: 700;
                font-size: 28px;
                text-transform: uppercase;
                transition: all 0.2s ease;
                background: white;
            }
            
            .letter-input:focus {
                outline: none;
                border-color: #B5702F;
                box-shadow: 0 0 0 3px rgba(181, 112, 47, 0.1);
            }
            
            .letter-input.filled {
                border-color: #B5702F;
                background: rgba(181, 112, 47, 0.05);
            }
            
            .stamps-section {
                margin-top: 2rem;
            }
            
            .stamps-grid {
                display: grid;
                grid-template-columns: repeat(4, 1fr);
                gap: 1rem;
                margin-top: 1rem;
            }
            
            .stamp-item {
                aspect-ratio: 1;
                border: 2px solid #e5e7eb;
                border-radius: 12px;
                cursor: pointer;
                transition: all 0.2s ease;
                display: flex;
                align-items: center;
                justify-content: center;
                background: white;
                padding: 8px;
            }
            
            .stamp-item:hover {
                border-color: #B5702F;
                transform: scale(1.05);
            }
            
            .stamp-item.selected {
                border-color: #B5702F;
                background: rgba(181, 112, 47, 0.1);
                box-shadow: 0 0 0 3px rgba(181, 112, 47, 0.2);
            }
            
            .stamp-item img {
                max-width: 100%;
                max-height: 100%;
                object-fit: contain;
            }
            
            .lindsay-btn {
                font-family: 'Lato', sans-serif;
                font-weight: 600;
                border-radius: 12px;
                padding: 16px 32px;
                min-height: 52px;
                transition: all 0.3s ease;
                border: none;
                cursor: pointer;
                text-decoration: none;
                display: inline-flex;
                align-items: center;
                justify-content: center;
                font-size: 16px;
                background: #B5702F;
                color: white;
                width: 100%;
                margin-top: 2rem;
            }
            
            .lindsay-btn:hover:not(:disabled) {
                background: #371C16;
                transform: translateY(-1px);
                box-shadow: 0 6px 20px rgba(181, 112, 47, 0.3);
            }
            
            .lindsay-btn:disabled {
                opacity: 0.5;
                cursor: not-allowed;
                transform: none;
                box-shadow: none;
            }
            
            @media (max-width: 768px) {
                .landing-title {
                    font-size: 1.8rem;
                }
                
                .letter-inputs-grid {
                    gap: 0.3rem;
                    overflow-x: auto;
                    padding: 0.5rem 0;
                }
                
                .letter-input {
                    width: 40px;
                    height: 50px;
                    font-size: 18px;
                    flex-shrink: 0;
                }
                
                .stamps-grid {
                    grid-template-columns: repeat(3, 1fr);
                }
            }
        </style>
@endsection

@section('content')
<div class="landing-container">
            <div class="lindsay-card">
                <!-- Back Button and Step Indicator -->
                <div class="flex justify-between items-center mb-6">
                    <x-back-button route="student.keychain-version" />
                    <span class="body-text" style="color: #6b7280;">Step 3 of 3</span>
                </div>

                <!-- Progress Bar -->
                <div class="progress-bar">
                    <div class="progress-fill" style="width: 100%;"></div>
                </div>

                <!-- Page Title -->
                <h1 class="landing-title">Circular Keychain</h1>
                <p class="body-text" style="color: #6b7280; text-align: center; margin-bottom: 2rem;">
                    6 letters in circle + 1 small symbol in center
                </p>

                @if ($errors->any())
                    <div style="background: #fee2e2; border: 1px solid #ef4444; border-radius: 8px; padding: 1rem; margin-bottom: 1rem;">
                        <ul style="color: #dc2626; margin-left: 1.5rem;">
                            @foreach ($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                @endif

                <form id="customizeForm" method="POST" action="{{ route('student.submit-keychain') }}">
                    @csrf
                    <input type="hidden" name="product_id" value="{{ $product->id }}">
                    <input type="hidden" name="version" value="2">
                    <input type="hidden" name="selected_stamp" id="selectedStampInput">

                    <!-- 6 Letter Inputs -->
                    <div style="margin-bottom: 2rem;">
                        <p class="body-text" style="color: #6b7280; text-align: center; margin-bottom: 1rem;">
                            Enter 6 letters (will arrange in circle)
                        </p>
                        <div class="letter-inputs-grid">
                            @for($i = 0; $i < 6; $i++)
                                <input type="text" 
                                       name="letters[]" 
                                       class="letter-input" 
                                       maxlength="1" 
                                       pattern="[A-Z0-9]"
                                       data-index="{{ $i }}"
                                       required>
                            @endfor
                        </div>
                    </div>

                    <!-- Small Stamps Section -->
                    <div class="stamps-section">
                        <h3 class="body-text" style="font-weight: 600; margin-bottom: 1rem;">Select 1 Small Symbol</h3>
                        <div class="stamps-grid" id="stampsGrid">
                            @foreach($stamps as $stamp)
                                <div class="stamp-item" data-stamp="{{ $stamp->image_path }}" onclick="selectStamp('{{ $stamp->image_path }}', this)">
                                    <img src="/images/stamps/small/{{ $stamp->image_path }}" alt="{{ pathinfo($stamp->image_path, PATHINFO_FILENAME) }}">
                                </div>
                            @endforeach
                        </div>
                    </div>

                    <!-- Continue Button -->
                    <button type="submit" 
                            id="continueBtn" 
                            class="lindsay-btn" 
                            disabled>
                        Continue to Preview →
                    </button>
                </form>
            </div>
        </div>
@endsection

@section('scripts')
<script>
        document.addEventListener('DOMContentLoaded', function() {
            const letterInputs = document.querySelectorAll('.letter-input');
            const continueBtn = document.getElementById('continueBtn');
            let selectedStamp = null;

            // Letter input handling
            letterInputs.forEach((input, index) => {
                input.addEventListener('input', function(e) {
                    let value = e.target.value.toUpperCase().replace(/[^A-Z0-9]/g, '');
                    e.target.value = value;
                    
                    if (value) {
                        e.target.classList.add('filled');
                        if (index < letterInputs.length - 1) {
                            letterInputs[index + 1].focus();
                        }
                    } else {
                        e.target.classList.remove('filled');
                    }
                    
                    updateContinueButton();
                });

                input.addEventListener('keydown', function(e) {
                    if (e.key === 'Backspace' && !e.target.value && index > 0) {
                        letterInputs[index - 1].focus();
                    }
                });
            });

            window.selectStamp = function(stampPath, element) {
                // Deselect all
                document.querySelectorAll('.stamp-item').forEach(item => {
                    item.classList.remove('selected');
                });
                
                // Select clicked stamp
                element.classList.add('selected');
                selectedStamp = stampPath;
                document.getElementById('selectedStampInput').value = stampPath;
                
                updateContinueButton();
            };

            function updateContinueButton() {
                const allLettersFilled = Array.from(letterInputs).every(input => input.value);
                const stampSelected = selectedStamp !== null;
                
                if (allLettersFilled && stampSelected) {
                    continueBtn.disabled = false;
                    continueBtn.style.opacity = '1';
                } else {
                    continueBtn.disabled = true;
                    continueBtn.style.opacity = '0.5';
                }
            }
        });
        </script>
@endsection



