<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create Survey - </title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/dropzone@5.9.3/dist/dropzone.min.css">
  
    @hotelSettings
  <style>
        :root {
            --primary-green: #1a936f;
            --secondary-green: #88d498;
            --light-green: #c6dabf;
            --dark-green: #114b5f;
            --accent: #f3e9d2;
            --gradient-bg: linear-gradient(135deg, rgba(17, 75, 95, 0.05) 0%, rgba(26, 147, 111, 0.05) 100%);
        }
        
        body {
            background: var(--gradient-bg);
            font-family: 'Inter', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        .navbar {
            background: linear-gradient(135deg, var(--dark-green) 0%, var(--primary-green) 100%);
            box-shadow: 0 4px 20px rgba(17, 75, 95, 0.2);
            padding: 0.8rem 0;
        }
        
        .main-content {
            padding: 2rem;
            margin-top: 3.5rem;
            max-width: 1400px;
            margin-left: auto;
            margin-right: auto;
        }
        
        .card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
            overflow: hidden;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            margin-bottom: 1.5rem;
        }
        
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
        }
        
        .card-header {
            background: linear-gradient(135deg, var(--primary-green) 0%, var(--secondary-green) 100%);
            color: white;
            padding: 1.5rem;
            border-bottom: none;
        }
        
        .card-header h5 {
            font-weight: 600;
            margin: 0;
        }
        
        .btn-primary {
            background: linear-gradient(135deg, var(--primary-green) 0%, var(--secondary-green) 100%);
            border: none;
            padding: 0.75rem 2rem;
            border-radius: 10px;
            font-weight: 600;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(26, 147, 111, 0.3);
        }
        
        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(26, 147, 111, 0.4);
        }
        
        .btn-outline-primary {
            color: var(--primary-green);
            border-color: var(--primary-green);
            border-radius: 10px;
        }
        
        .btn-outline-primary:hover {
            background: linear-gradient(135deg, var(--primary-green) 0%, var(--secondary-green) 100%);
            border-color: transparent;
            transform: translateY(-2px);
        }
        
        .question-item {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            margin-bottom: 1.5rem;
            border-left: 4px solid var(--primary-green);
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
            transition: all 0.3s ease;
        }
        
        .question-item:hover {
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
            transform: translateY(-2px);
        }
        
        .question-item.dragging {
            opacity: 0.5;
            background: var(--light-green);
        }
        
        .remove-question {
            transition: all 0.3s ease;
        }
        
        .remove-question:hover {
            transform: rotate(90deg);
            color: #dc3545 !important;
        }
        
        .form-control, .form-select {
            border-radius: 10px;
            border: 2px solid #e9ecef;
            padding: 0.75rem 1rem;
            transition: all 0.3s ease;
        }
        
        .form-control:focus, .form-select:focus {
            border-color: var(--primary-green);
            box-shadow: 0 0 0 0.25rem rgba(26, 147, 111, 0.25);
        }
        
        .media-upload-section {
            background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
            border: 2px dashed var(--secondary-green);
            border-radius: 12px;
            padding: 2rem;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 1.5rem;
        }
        
        .media-upload-section:hover {
            background: linear-gradient(135deg, #f0f9f4 0%, #ffffff 100%);
            border-color: var(--primary-green);
            transform: translateY(-3px);
        }
        
        .media-upload-section i {
            font-size: 3rem;
            color: var(--secondary-green);
            margin-bottom: 1rem;
        }
        
        .media-preview {
            display: flex;
            gap: 1rem;
            flex-wrap: wrap;
            margin-top: 1rem;
        }
        
        .media-item {
            position: relative;
            width: 120px;
            height: 120px;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }
        
        .media-item img, .media-item video {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        
        .media-remove {
            position: absolute;
            top: 5px;
            right: 5px;
            background: rgba(220, 53, 69, 0.9);
            color: white;
            border: none;
            border-radius: 50%;
            width: 24px;
            height: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
        }
        
        .step-indicator {
            display: flex;
            align-items: center;
            margin-bottom: 2rem;
            gap: 1rem;
        }
        
        .step {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            color: #6c757d;
            font-weight: 500;
        }
        
        .step.active {
            color: var(--primary-green);
        }
        
        .step-number {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            background: #e9ecef;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
        }
        
        .step.active .step-number {
            background: linear-gradient(135deg, var(--primary-green) 0%, var(--secondary-green) 100%);
            color: white;
        }
        
        .step-line {
            flex: 1;
            height: 2px;
            background: #e9ecef;
        }
        
        .step:last-child .step-line {
            display: none;
        }
        
        .floating-action-buttons {
            position: fixed;
            bottom: 2rem;
            right: 2rem;
            display: flex;
            gap: 1rem;
            z-index: 1000;
        }
        
        .floating-btn {
            width: 56px;
            height: 56px;
            border-radius: 50%;
            background: linear-gradient(135deg, var(--primary-green) 0%, var(--secondary-green) 100%);
            color: white;
            border: none;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.25rem;
            box-shadow: 0 6px 20px rgba(26, 147, 111, 0.4);
            transition: all 0.3s ease;
            cursor: pointer;
        }
        
        .floating-btn:hover {
            transform: scale(1.1) rotate(10deg);
            box-shadow: 0 8px 25px rgba(26, 147, 111, 0.5);
        }
        
        .preview-btn {
            position: fixed;
            bottom: 2rem;
            left: 2rem;
            z-index: 1000;
        }
        
        @media (max-width: 768px) {
            .main-content {
                padding: 1rem;
                margin-top: 3rem;
            }
            
            .floating-action-buttons {
                bottom: 1rem;
                right: 1rem;
            }
            
            .preview-btn {
                bottom: 1rem;
                left: 1rem;
            }
        }
        
        .question-type-icon {
            width: 24px;
            height: 24px;
            background: var(--light-green);
            border-radius: 6px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            margin-right: 0.5rem;
            color: var(--dark-green);
        }
    </style>
</head>
<body>
    <!-- Navbar -->
    <!-- Main Content -->
    <div class="main-content">
        <div class="container-fluid">
            <!-- Step Indicator -->
            <div class="step-indicator">
                <div class="step active">
                    <div class="step-number">1</div>
                    <span>Basic Info</span>
                </div>
                <div class="step-line"></div>
                <div class="step">
                    <div class="step-number">2</div>
                    <span>Questions</span>
                </div>
                <div class="step-line"></div>
                <div class="step">
                    <div class="step-number">3</div>
                    <span>Design</span>
                </div>
                <div class="step-line"></div>
                <div class="step">
                    <div class="step-number">4</div>
                    <span>Review</span>
                </div>
            </div>

            <div class="d-flex justify-content-between align-items-center mb-4">
                <div>
                    <h2 class="fw-bold mb-1">Create New Survey</h2>
                    <p class="text-muted mb-0">Design engaging surveys with multimedia content</p>
                </div>
                <a href="{{ route('surveys.index') }}" class="btn btn-outline-primary">
                    <i class="fas fa-arrow-left me-2"></i>Back to Surveys
                </a>
            </div>

            @if($errors->any())
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <h5 class="alert-heading"><i class="fas fa-exclamation-triangle me-2"></i>Validation Errors</h5>
                    <ul class="mb-0">
                        @foreach($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            @endif

            <form action="{{ route('surveys.store') }}" method="POST" enctype="multipart/form-data" id="surveyForm">
                @csrf
                
                <div class="row">
                    <!-- Left Column - Survey Content -->
                    <div class="col-lg-8">
                        <!-- Survey Details Card -->
                        <div class="card mb-4">
                            <div class="card-header">
                                <h5 class="mb-0"><i class="fas fa-info-circle me-2"></i>Survey Details</h5>
                            </div>
                            <div class="card-body">
                                <div class="mb-3">
                                    <label for="title" class="form-label fw-bold">Survey Title</label>
                                    <input type="text" class="form-control" id="title" name="title" 
                                           placeholder="Enter survey title (e.g., Guest Satisfaction Survey)" 
                                           value="{{ old('title') }}" required>
                                    <div class="form-text">Choose a clear, descriptive title for your survey</div>
                                </div>
                                
                                <div class="mb-4">
                                    <label for="description" class="form-label fw-bold">Description</label>
                                    <textarea class="form-control" id="description" name="description" 
                                              rows="3" placeholder="Describe the purpose of this survey...">{{ old('description') }}</textarea>
                                    <div class="form-text">Briefly explain what this survey is about</div>
                                </div>
                                
                                <!-- Media Upload Section -->
                                <div class="media-upload-section" onclick="document.getElementById('mediaFiles').click()">
                                    <i class="fas fa-cloud-upload-alt"></i>
                                    <h5>Add Images & Videos</h5>
                                    <p class="text-muted mb-0">Click to upload or drag & drop images/videos</p>
                                    <p class="small text-muted mt-2">Supports JPG, PNG, GIF, MP4, MOV (Max 10MB each)</p>
                                </div>
                                <input type="file" id="mediaFiles" name="media[]" multiple accept="image/*,video/*" style="display: none;" onchange="previewMedia(event)">
                                
                                <div class="media-preview" id="mediaPreview"></div>
                            </div>
                        </div>

                        <!-- Survey Questions Card -->
                        <div class="card">
                            <div class="card-header d-flex justify-content-between align-items-center">
                                <h5 class="mb-0"><i class="fas fa-question-circle me-2"></i>Survey Questions</h5>
                                <button type="button" class="btn btn-primary" id="add-question">
                                    <i class="fas fa-plus me-2"></i>Add Question
                                </button>
                            </div>
                            <div class="card-body">
                                <div id="questions-container" class="sortable-list">
                                    <!-- Questions will be added here dynamically -->
                                </div>
                                
                                <div class="text-center py-4" id="empty-state" style="display: none;">
                                    <i class="fas fa-question-circle fa-3x text-muted mb-3"></i>
                                    <h5 class="text-muted">No questions added yet</h5>
                                    <p class="text-muted">Click "Add Question" to start building your survey</p>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Right Column - Settings & Preview -->
                    <div class="col-lg-4">
                        <!-- Survey Settings Card -->
                        <div class="card mb-4">
                            <div class="card-header">
                                <h5 class="mb-0"><i class="fas fa-cog me-2"></i>Survey Settings</h5>
                            </div>
                            <div class="card-body">
                                <div class="mb-3">
                                    <label for="status" class="form-label fw-bold">Status</label>
                                    <select class="form-select" id="status" name="status">
                                        <option value="draft" {{ old('status', 'draft') == 'draft' ? 'selected' : '' }}>Draft</option>
                                        <option value="active" {{ old('status') == 'active' ? 'selected' : '' }}>Active</option>
                                    </select>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="expires_at" class="form-label fw-bold">Expiry Date (Optional)</label>
                                    <input type="date" class="form-control" id="expires_at" name="expires_at" value="{{ old('expires_at') }}">
                                    <div class="form-text">Survey will automatically close on this date</div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="theme" class="form-label fw-bold">Theme</label>
                                    <select class="form-select" id="theme" name="theme">
                                        <option value="default">Default Green</option>
                                        <option value="modern">Modern</option>
                                        <option value="elegant">Elegant</option>
                                        <option value="minimal">Minimal</option>
                                    </select>
                                </div>
                                
                                <div class="form-check mb-3">
                                    <input class="form-check-input" type="checkbox" id="allow_anonymous" name="allow_anonymous" value="1">
                                    <label class="form-check-label" for="allow_anonymous">
                                        Allow anonymous responses
                                    </label>
                                </div>
                                
                                <div class="form-check mb-4">
                                    <input class="form-check-input" type="checkbox" id="show_progress" name="show_progress" value="1" checked>
                                    <label class="form-check-label" for="show_progress">
                                        Show progress bar to respondents
                                    </label>
                                </div>
                                
                                <div class="d-grid">
                                    <button type="submit" class="btn btn-primary btn-lg">
                                        <i class="fas fa-save me-2"></i>Create Survey
                                    </button>
                                </div>
                            </div>
                        </div>
                        
                        <!-- Preview Card -->
                        <div class="card">
                            <div class="card-header">
                                <h5 class="mb-0"><i class="fas fa-eye me-2"></i>Quick Preview</h5>
                            </div>
                            <div class="card-body">
                                <div class="survey-preview">
                                    <div class="text-center mb-3">
                                        <div class="bg-light rounded p-3">
                                            <i class="fas fa-poll fa-2x text-primary mb-2"></i>
                                            <h6 class="mb-1" id="preview-title">Survey Title</h6>
                                            <p class="small text-muted mb-0" id="preview-desc">Description will appear here</p>
                                        </div>
                                    </div>
                                    <div class="question-preview mb-2">
                                        <small class="text-muted">Sample Question</small>
                                        <div class="border rounded p-2">
                                            <small>How would you rate your stay?</small>
                                        </div>
                                    </div>
                                    <div class="text-center">
                                        <small class="text-muted">Preview updates as you type</small>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Floating Action Buttons -->
    <div class="floating-action-buttons">
        <button type="button" class="floating-btn" id="add-question-float" title="Add Question">
            <i class="fas fa-plus"></i>
        </button>
        <button type="button" class="floating-btn" id="preview-survey" title="Preview Survey">
            <i class="fas fa-eye"></i>
        </button>
    </div>

    <!-- Question Template (Hidden) -->
    <template id="question-template">
        <div class="question-item sortable-item" draggable="true">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <div class="d-flex align-items-center">
                    <i class="fas fa-grip-vertical text-muted me-2 handle" style="cursor: move;"></i>
                    <h6 class="mb-0">Question <span class="question-number">1</span></h6>
                    <span class="badge bg-light text-dark ms-2 question-type-badge">Text</span>
                </div>
                <button type="button" class="btn btn-sm btn-outline-danger remove-question">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            
            <div class="mb-3">
                <label class="form-label fw-bold">Question Text</label>
                <input type="text" class="form-control question-text" name="questions[0][text]" required placeholder="Enter your question here...">
            </div>
            
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label fw-bold">Question Type</label>
                    <select class="form-select question-type" name="questions[0][type]">
                        <option value="text"><i class="fas fa-align-left me-2"></i>Text Answer</option>
                        <option value="rating"><i class="fas fa-star me-2"></i>Rating (1-5)</option>
                        <option value="multiple_choice"><i class="fas fa-list-ul me-2"></i>Multiple Choice</option>
                        <option value="yes_no"><i class="fas fa-check-circle me-2"></i>Yes/No</option>
                    </select>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label fw-bold">Required</label>
                    <select class="form-select" name="questions[0][required]">
                        <option value="1">Required</option>
                        <option value="0">Optional</option>
                    </select>
                </div>
            </div>
            
            <div class="question-options-container" style="display: none;">
                <label class="form-label fw-bold">Options (one per line)</label>
                <textarea class="form-control question-options" name="questions[0][options]" rows="3" placeholder="Option 1&#10;Option 2&#10;Option 3"></textarea>
                <small class="text-muted d-block mt-1">Enter each option on a separate line. Max 10 options.</small>
            </div>
        </div>
    </template>

    <!-- JavaScript Libraries -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sortablejs@1.14.0/Sortable.min.js"></script>

    <script>
        let questionCount = 0;
        
        document.addEventListener('DOMContentLoaded', function() {
            const addQuestionBtn = document.getElementById('add-question');
            const addQuestionFloatBtn = document.getElementById('add-question-float');
            const questionsContainer = document.getElementById('questions-container');
            const questionTemplate = document.getElementById('question-template');
            const emptyState = document.getElementById('empty-state');
            const previewTitle = document.getElementById('preview-title');
            const previewDesc = document.getElementById('preview-desc');
            const titleInput = document.getElementById('title');
            const descInput = document.getElementById('description');
            
            // Initialize Sortable
            const sortable = Sortable.create(questionsContainer, {
                animation: 150,
                ghostClass: 'dragging',
                handle: '.handle',
                onEnd: updateQuestionNumbers
            });
            
            // Add first question
            addQuestion();
            
            // Event Listeners
            addQuestionBtn.addEventListener('click', addQuestion);
            addQuestionFloatBtn.addEventListener('click', addQuestion);
            
            // Live preview updates
            titleInput.addEventListener('input', function() {
                previewTitle.textContent = this.value || 'Survey Title';
            });
            
            descInput.addEventListener('input', function() {
                previewDesc.textContent = this.value || 'Description will appear here';
            });
            
            // Media preview function
            window.previewMedia = function(event) {
                const preview = document.getElementById('mediaPreview');
                preview.innerHTML = '';
                
                Array.from(event.target.files).forEach((file, index) => {
                    const reader = new FileReader();
                    reader.onload = function(e) {
                        const mediaItem = document.createElement('div');
                        mediaItem.className = 'media-item';
                        
                        if (file.type.startsWith('image/')) {
                            mediaItem.innerHTML = `
                                <img src="${e.target.result}" alt="Preview">
                                <button type="button" class="media-remove" onclick="removeMedia(${index})">
                                    <i class="fas fa-times"></i>
                                </button>
                            `;
                        } else if (file.type.startsWith('video/')) {
                            mediaItem.innerHTML = `
                                <video controls>
                                    <source src="${e.target.result}" type="${file.type}">
                                </video>
                                <button type="button" class="media-remove" onclick="removeMedia(${index})">
                                    <i class="fas fa-times"></i>
                                </button>
                            `;
                        }
                        
                        preview.appendChild(mediaItem);
                    };
                    reader.readAsDataURL(file);
                });
            };
            
            // Remove media function
            window.removeMedia = function(index) {
                const dt = new DataTransfer();
                const input = document.getElementById('mediaFiles');
                const files = Array.from(input.files);
                
                files.splice(index, 1);
                files.forEach(file => dt.items.add(file));
                
                input.files = dt.files;
                previewMedia({ target: input });
            };
            
            function addQuestion() {
                questionCount++;
                const newQuestion = questionTemplate.content.cloneNode(true);
                const questionElement = newQuestion.querySelector('.question-item');
                
                // Update question number
                questionElement.querySelector('.question-number').textContent = questionCount;
                
                // Update all input names with new index
                const inputs = questionElement.querySelectorAll('[name]');
                inputs.forEach(input => {
                    const name = input.getAttribute('name');
                    const newName = name.replace('[0]', `[${questionCount}]`);
                    input.setAttribute('name', newName);
                });
                
                // Add event listeners
                const removeBtn = questionElement.querySelector('.remove-question');
                removeBtn.addEventListener('click', function() {
                    if (questionsContainer.children.length === 1) {
                        emptyState.style.display = 'block';
                    }
                    questionElement.remove();
                    updateQuestionNumbers();
                });
                
                const typeSelect = questionElement.querySelector('.question-type');
                const typeBadge = questionElement.querySelector('.question-type-badge');
                
                typeSelect.addEventListener('change', function() {
                    updateQuestionOptions(this);
                    typeBadge.textContent = this.options[this.selectedIndex].text;
                });
                
                // Initialize options visibility
                updateQuestionOptions(typeSelect);
                typeBadge.textContent = typeSelect.options[typeSelect.selectedIndex].text;
                
                // Hide empty state
                emptyState.style.display = 'none';
                
                questionsContainer.appendChild(questionElement);
                updateQuestionNumbers();
            }
            
            function updateQuestionNumbers() {
                const questions = questionsContainer.querySelectorAll('.question-item');
                questions.forEach((question, index) => {
                    question.querySelector('.question-number').textContent = index + 1;
                    
                    // Update all input names with correct index
                    const inputs = question.querySelectorAll('[name]');
                    inputs.forEach(input => {
                        const name = input.getAttribute('name');
                        const newName = name.replace(/questions\[\d+\]/, `questions[${index}]`);
                        input.setAttribute('name', newName);
                    });
                });
                questionCount = questions.length;
            }
            
            function updateQuestionOptions(selectElement) {
                const questionItem = selectElement.closest('.question-item');
                const optionsContainer = questionItem.querySelector('.question-options-container');
                const optionsTextarea = questionItem.querySelector('.question-options');
                
                if (selectElement.value === 'multiple_choice') {
                    optionsContainer.style.display = 'block';
                    optionsTextarea.setAttribute('required', 'required');
                } else {
                    optionsContainer.style.display = 'none';
                    optionsTextarea.removeAttribute('required');
                }
            }
            
            // Form validation
            document.getElementById('surveyForm').addEventListener('submit', function(e) {
                const title = document.getElementById('title').value.trim();
                if (!title) {
                    e.preventDefault();
                    alert('Please enter a survey title');
                    return false;
                }
                
                const questions = questionsContainer.querySelectorAll('.question-item');
                if (questions.length === 0) {
                    e.preventDefault();
                    alert('Please add at least one question to the survey');
                    return false;
                }
                
                // Validate each question
                let valid = true;
                questions.forEach((question, index) => {
                    const questionText = question.querySelector('.question-text').value.trim();
                    if (!questionText) {
                        valid = false;
                        question.querySelector('.question-text').focus();
                        question.querySelector('.question-text').style.borderColor = '#dc3545';
                    }
                    
                    if (question.querySelector('.question-type').value === 'multiple_choice') {
                        const options = question.querySelector('.question-options').value.trim();
                        if (!options) {
                            valid = false;
                            question.querySelector('.question-options').focus();
                            question.querySelector('.question-options').style.borderColor = '#dc3545';
                        }
                    }
                });
                
                if (!valid) {
                    e.preventDefault();
                    alert('Please fill in all required fields');
                    return false;
                }
            });
        });
    </script>
      <script src="{{ asset('js/theme-applier.js') }}"></script>
</body>
</html>

