@model QuestionnaireViewModel

@{
    ViewData["Title"] = "Create";
}

<style>
    /* Modern Design Enhancements */
    @@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

    :root {
        --primary-color: #6366f1;
        --primary-light: #8b5cf6;
        --primary-dark: #4338ca;
        --success-color: #10b981;
        --danger-color: #ef4444;
        --warning-color: #f59e0b;
        --info-color: #06b6d4;
        --gray-50: #f8fafc;
        --gray-100: #f1f5f9;
        --gray-200: #e2e8f0;
        --gray-300: #cbd5e1;
        --gray-400: #94a3b8;
        --gray-500: #64748b;
        --gray-600: #475569;
        --gray-700: #334155;
        --gray-800: #1e293b;
        --gray-900: #0f172a;
        --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
        --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
        --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
        --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
        --shadow-2xl: 0 25px 50px -12px rgb(0 0 0 / 0.25);
        --border-radius-sm: 8px;
        --border-radius-md: 12px;
        --border-radius-lg: 16px;
        --border-radius-xl: 20px;
    }

    * {
        font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    }

    body {
        background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
        min-height: 100vh;
    }

    .container {
        max-width: 1200px;
    }

    /* Enhanced Card Design */
    .card {
        background: white;
        border-radius: var(--border-radius-xl);
        box-shadow: var(--shadow-2xl);
        border: 1px solid var(--gray-200);
        overflow: hidden;
        backdrop-filter: blur(10px);
    }

    .card-title {
        font-size: 2rem;
        font-weight: 700;
        color: var(--gray-800);
        text-align: center;
        margin-bottom: 2rem;
        position: relative;
        padding-bottom: 1rem;
    }

        .card-title::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 60px;
            height: 4px;
            background: linear-gradient(135deg, var(--primary-color), var(--primary-light));
            border-radius: 2px;
        }

    .card-body {
        padding: 3rem;
        background: var(--gray-50);
    }

    .card-footer {
        background: white;
        padding: 2.5rem;
        border-top: 1px solid var(--gray-200);
        display: flex;
        gap: 1.5rem;
        align-items: center;
        flex-wrap: wrap;
    }

    /* Form Enhancements */
    .mb-3 {
        margin-bottom: 2rem !important;
    }

    .form-control {
        border: 2px solid var(--gray-200);
        border-radius: var(--border-radius-md);
        padding: 1rem 1.25rem;
        font-size: 0.95rem;
        font-weight: 500;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        background: white;
        box-shadow: var(--shadow-sm);
        width: 100%;
        min-height: 50px;
    }

        .form-control:focus {
            border-color: var(--primary-color);
            background: white;
            box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1), var(--shadow-md);
            outline: none;
            transform: translateY(-1px);
        }

    .control-label {
        font-weight: 600;
        color: var(--gray-700);
        margin-bottom: 0.75rem;
        font-size: 0.95rem;
        display: flex;
        align-items: center;
    }

    /* Enhanced Select Styling */
    select.form-control {
        appearance: none;
        -webkit-appearance: none;
        -moz-appearance: none;
        background-color: white;
        background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='m6 8 4 4 4-4'/%3e%3c/svg%3e");
        background-position: right 0.75rem center;
        background-repeat: no-repeat;
        background-size: 1.25em 1.25em;
        padding-right: 2.5rem;
        cursor: pointer;
    }

        select.form-control:focus {
            background-color: white;
            background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236366f1' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='m6 8 4 4 4-4'/%3e%3c/svg%3e");
            background-position: right 0.75rem center;
            background-repeat: no-repeat;
            background-size: 1.25em 1.25em;
        }

        select.form-control option {
            padding: 0.75rem;
            font-weight: 500;
            color: var(--gray-900);
            background: white;
        }

    /* Textarea specific styling */
    textarea.form-control {
        min-height: 120px;
        resize: vertical;
    }

    /* Button Enhancements */
    .btn {
        border-radius: var(--border-radius-md);
        font-weight: 600;
        padding: 0.75rem 1.5rem;
        font-size: 0.875rem;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        border: none;
        text-decoration: none;
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        box-shadow: var(--shadow-sm);
        position: relative;
        overflow: hidden;
    }

        .btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
            transition: left 0.5s;
        }

        .btn:hover::before {
            left: 100%;
        }

    .btn-primary {
        background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-light) 100%);
        color: white;
    }

        .btn-primary:hover {
            background: linear-gradient(135deg, var(--primary-dark) 0%, var(--primary-color) 100%);
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
            color: white;
        }

    .btn-success {
        background: linear-gradient(135deg, var(--success-color) 0%, #059669 100%);
        color: white;
    }

        .btn-success:hover {
            background: linear-gradient(135deg, #059669 0%, #047857 100%);
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
            color: white;
        }

    .btn-danger {
        background: linear-gradient(135deg, var(--danger-color) 0%, #dc2626 100%);
        color: white;
    }

        .btn-danger:hover {
            background: linear-gradient(135deg, #dc2626 0%, #b91c1c 100%);
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
            color: white;
        }

    .btn-info {
        background: linear-gradient(135deg, var(--info-color) 0%, #0891b2 100%);
        color: white;
    }

        .btn-info:hover {
            background: linear-gradient(135deg, #0891b2 0%, #0e7490 100%);
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
            color: white;
        }

    .btn-warning {
        background: linear-gradient(135deg, var(--warning-color) 0%, #d97706 100%);
        color: white;
    }

        .btn-warning:hover {
            background: linear-gradient(135deg, #d97706 0%, #b45309 100%);
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
            color: white;
        }

    /* Questions Container */
    #questions-container {
        background: white;
        border-radius: var(--border-radius-lg);
        padding: 3rem;
        margin: 2rem 0;
        box-shadow: var(--shadow-xl);
        border: 1px solid var(--gray-200);
        position: relative;
        overflow: hidden;
    }

        #questions-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, var(--primary-color), var(--primary-light), var(--success-color));
        }

        #questions-container h3 {
            font-size: 1.75rem;
            font-weight: 700;
            color: var(--gray-800);
            margin-bottom: 3rem;
            text-align: center;
        }

    /* Question Group Styling */
    .question-group {
        background: var(--gray-50);
        border-radius: var(--border-radius-lg);
        padding: 2.5rem;
        margin-bottom: 2.5rem;
        border: 2px solid var(--gray-200);
        transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        position: relative;
        overflow: hidden;
    }

        .question-group::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 4px;
            height: 100%;
            background: linear-gradient(135deg, var(--primary-color), var(--primary-light));
            transform: scaleY(0);
            transition: transform 0.3s ease;
            transform-origin: bottom;
        }

        .question-group:hover {
            border-color: var(--primary-color);
            box-shadow: var(--shadow-xl);
            transform: translateY(-2px);
            background: white;
        }

            .question-group:hover::before {
                transform: scaleY(1);
            }

        .question-group.collapsed {
            background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%);
            border-color: var(--success-color);
        }

        .question-group label {
            font-size: 1.1rem;
            font-weight: 600;
            color: var(--gray-800);
            margin-bottom: 1rem;
        }

    /* Answers Container */
    .answers-container {
        background: white;
        border-radius: var(--border-radius-md);
        padding: 2rem;
        margin: 1.5rem 0;
        border: 1px solid var(--gray-200);
        box-shadow: var(--shadow-sm);
    }

        .answers-container label {
            font-size: 1.25rem;
            font-weight: 700;
            color: var(--gray-800);
            margin-bottom: 1.5rem;
        }

    /* Answer Group Styling */
    .answer-group {
        background: var(--gray-50);
        border-radius: var(--border-radius-md);
        padding: 1.5rem;
        margin-bottom: 1.25rem;
        border: 1px solid var(--gray-200);
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

        .answer-group:hover {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
            background: white;
        }

    /* Other Option Styling */
    .other-option-group {
        background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
        border: 2px solid var(--warning-color);
        position: relative;
    }

        .other-option-group::before {
            content: "OTHER OPTION";
            position: absolute;
            top: -12px;
            left: 20px;
            background: var(--warning-color);
            color: white;
            padding: 0.375rem 1rem;
            border-radius: var(--border-radius-sm);
            font-size: 0.7rem;
            font-weight: 700;
            letter-spacing: 0.5px;
            box-shadow: var(--shadow-sm);
        }

    /* Enhanced Other Option Button - Conditional Styling */
    .add-other-option {
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

        .add-other-option.hidden {
            display: none !important;
        }

        .add-other-option.visible {
            display: inline-flex !important;
        }

    /* Question Type Info */
    .question-type-info {
        background: var(--gray-100);
        border-radius: var(--border-radius-sm);
        padding: 0.75rem 1rem;
        margin-top: 0.5rem;
        border-left: 4px solid var(--info-color);
        font-size: 0.875rem;
        color: var(--gray-700);
    }

        .question-type-info.supports-other {
            background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 100%);
            border-left-color: var(--info-color);
        }

        .question-type-info.no-other {
            background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
            border-left-color: var(--gray-400);
        }

    /* Add Question Button */
    #add-question-btn {
        background: linear-gradient(135deg, var(--success-color) 0%, #059669 100%);
        color: white;
        padding: 1.25rem 2.5rem;
        font-size: 1.1rem;
        font-weight: 700;
        border-radius: var(--border-radius-lg);
        margin: 2rem auto;
        box-shadow: var(--shadow-lg);
        border: none;
        display: block;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

        #add-question-btn:hover {
            transform: translateY(-3px);
            box-shadow: var(--shadow-2xl);
            background: linear-gradient(135deg, #059669 0%, #047857 100%);
        }

    /* Alert Enhancements */
    .alert {
        border-radius: var(--border-radius-md);
        padding: 1.25rem 1.5rem;
        margin: 1.5rem 0;
        border: none;
        font-weight: 500;
        box-shadow: var(--shadow-sm);
        animation: slideDown 0.3s ease;
    }

    .alert-success {
        background: linear-gradient(135deg, #d1fae5 0%, #a7f3d0 100%);
        color: #065f46;
        border-left: 4px solid var(--success-color);
    }

    .alert-info {
        background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 100%);
        color: #0c4a6e;
        border-left: 4px solid var(--info-color);
    }

    .alert-danger {
        background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%);
        color: #991b1b;
        border-left: 4px solid var(--danger-color);
    }

    /* Horizontal Rules */
    hr {
        border: none;
        height: 2px;
        background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-light) 100%);
        margin: 2rem 0;
        border-radius: 1px;
    }

        hr.border-4 {
            height: 4px;
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-light) 100%);
            margin: 3rem 0;
        }

    /* Container Enhancements */
    .container-ms {
        background: white;
        border-radius: var(--border-radius-md);
        box-shadow: var(--shadow-md);
        border: 1px solid var(--gray-200);
    }

    /* Responsive Design */
    @@media (max-width: 768px) {
        .card-body {
            padding: 2rem;
        }

        #questions-container {
            padding: 2rem;
        }

        .question-group {
            padding: 2rem;
        }

        .card-footer {
            flex-direction: column;
            align-items: stretch;
        }

        .card-title {
            font-size: 1.5rem;
        }
    }

    /* Animations */
    @@keyframes slideDown {
        from {
            opacity: 0;
            transform: translateY(-20px);
        }

        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    .fade-in {
        animation: fadeIn 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    }

    @@keyframes fadeIn {
        from {
            opacity: 0;
            transform: translateY(20px);
        }

        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    /* Enhanced Focus States */
    .btn:focus,
    .form-control:focus {
        outline: 2px solid var(--primary-color);
        outline-offset: 2px;
    }

    /* Typography Enhancements */
    h1, h2, h3, h4, h5, h6 {
        font-weight: 700;
        letter-spacing: -0.025em;
    }

    /* Icon styling */
    .bi {
        font-size: 1.1em;
    }

    /* Validation styling */
    .text-danger {
        color: var(--danger-color) !important;
        font-weight: 500;
        font-size: 0.875rem;
        margin-top: 0.5rem;
    }
</style>

<div class="container mt-4">
    <div class="card justify-content-center p-4 shadow rounded">
        <div class="card-body">
            <h5 class="card-title h5">Create questionnaire</h5>

            <div class="row">
                <form asp-action="Create" asp-controller="Questionnaire">
                    <div asp-validation-summary="All" class="text-danger"></div>

                    <div class="mb-3 col-12">
                        <label asp-for="Title" class="control-label"></label>
                        <input asp-for="Title" class="form-control" />
                        <span asp-validation-for="Title" class="text-danger"></span>
                    </div>
                    <div class="mb-3 col-12">
                        <label asp-for="Description" class="control-label"></label>
                        <textarea asp-for="Description" class="form-control"></textarea>
                        <span asp-validation-for="Description" class="text-danger"></span>
                    </div>
                    <div class="container p-5 shadow bg-body-tertiary rounded" id="questions-container">
                        <div class="mx-md-3 mx-lg-3 px-md-3 px-lg-3 mx-sm-0 px-sm-0">
                            <h3 class="text-primary font-weight-bold">Create Questions</h3>
                            <div class="form-group">
                                @for (int i = 0; i < Model.Questions?.Count; i++)
                                {
                                    <div class="question-group fade-in" data-question-index="@i">
                                        <label>Question @(i + 1)</label>
                                        <textarea name="Questions[@i].Text" class="form-control">@Model.Questions[i].Text</textarea>
                                        <select name="Questions[@i].Type" asp-items="ViewBag.QuestionTypes" class="form-control question-type">

                                            <!-- Include options for question types... -->
                                            <div class="container-sm"></div>
                                        </select>

                                        <!-- Question Type Information -->
                                        <div class="question-type-info">
                                            <i class="bi bi-info-circle"></i>
                                            <span class="type-info-text">Select a question type to see available options</span>
                                        </div>

                                        <div class="answers-container">
                                            <label>Answers:</label>
                                            @for (int j = 0; j < Model.Questions?[i].Answers?.Count; j++)
                                            {
                                                <div class="answer-group">
                                                    <input type="text" name="Questions[@i].Answers[@j].Text" class="form-control" value="@Model.Questions?[i]?.Answers?[j]?.Text" />
                                                    <input type="hidden" name="Questions[@i].Answers[@j].IsOtherOption" value="@Model.Questions?[i]?.Answers?[j]?.IsOtherOption" />
                                                    <button type="button" class="btn btn-sm btn-danger  shadow remove-answer">Remove Answer</button>
                                                </div>
                                            }
                                            <button type="button" class="btn btn-sm btn-success shadow add-answer"><i class="bi bi-plus-square"></i> Create Answer</button>
                                            <button type="button" class="btn btn-sm btn-info shadow add-other-option"><i class="bi bi-pencil-square"></i> Add "Other" Option</button>
                                            <input type="hidden" name="Questions[@i].Answers" />
                                        </div>
                                        <button type="button" class="btn btn-sm p-3 btn-danger  shadow remove-question">Remove Question <i class="bi bi-trash3-fill"></i></button> |
                                        <button type="button" class="btn btn-sm btn-primary  shadow m-3 save-question"><i class="bi bi-floppy"></i> Save Question</button>
                                        <button type="button" class="btn btn-sm btn-primary  shadow edit-question" style="display: none;"><i class="bi bi-pencil-square"></i> Edit Question</button>
                                    </div>
                                }
                            </div>
                        </div>
                        <button type="button" id="add-question-btn" class="btn btn-md btn-success shadow"><i class="bi bi-plus-square"></i> Create New Question</button>
                    </div>
                    <div class="mt-3 card-footer">

                        <button type="submit" class="btn btn-primary  shadow"> <i class="bi bi-floppy2-fill"></i> Save questionnaire</button>
                        | <a asp-action="Index" class="btn btn-info  shadow">Back to list</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<hr />


@section Scripts {
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.11.4/ckeditor.js"></script>
    <script>
        CKEDITOR.replace("Description");

    </script>



    @{
        <partial name="_ValidationScriptsPartial" />
    }

    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>

    <script>
        $(document).ready(function () {
            var questionIndex = @Model.Questions?.Count;

            // ===== QUESTION TYPES THAT SUPPORT OTHER OPTIONS =====
            const questionTypesWithOtherOption = [
                'CheckBox',
                'Multiple_choice',
                'Demographic'
            ];

            // ===== FUNCTION TO CHECK IF QUESTION TYPE SUPPORTS OTHER OPTIONS =====
            function supportsOtherOption(questionType) {
                return questionTypesWithOtherOption.includes(questionType);
            }

            // ===== FUNCTION TO UPDATE OTHER OPTION VISIBILITY =====
            function updateOtherOptionVisibility(questionGroup) {
                const questionTypeSelect = questionGroup.find('.question-type, [class*="question-type-"]');
                const selectedType = questionTypeSelect.val();
                const addOtherBtn = questionGroup.find('.add-other-option');
                const typeInfo = questionGroup.find('.question-type-info');
                const typeInfoText = typeInfo.find('.type-info-text');

                if (supportsOtherOption(selectedType)) {
                    // Show "Add Other Option" button
                    addOtherBtn.removeClass('hidden').addClass('visible').show();
                    typeInfo.removeClass('no-other').addClass('supports-other');
                    typeInfoText.html(`<strong>${selectedType}</strong> supports "Other" options - users can provide custom answers`);
                } else {
                    // Hide "Add Other Option" button
                    addOtherBtn.removeClass('visible').addClass('hidden').hide();
                    typeInfo.removeClass('supports-other').addClass('no-other');
                    typeInfoText.html(`<strong>${selectedType}</strong> does not support "Other" options`);

                    // Remove any existing other options for this question
                    questionGroup.find('.other-option-group').remove();
                }
            }

            // ===== UPDATE EXISTING QUESTIONS ON PAGE LOAD =====
            $('.question-group').each(function() {
                updateOtherOptionVisibility($(this));
            });

            // ===== HANDLE QUESTION TYPE CHANGES FOR EXISTING QUESTIONS =====
            $("#questions-container").on("change", ".question-type, [class*='question-type-']", function () {
                const questionGroup = $(this).closest('.question-group');
                updateOtherOptionVisibility(questionGroup);
            });

            $("#add-question-btn").click(function () {
                var newQuestionHtml = `
                    <div class="question-group fade-in" data-question-index="${questionIndex}">
                                <label>Question ${questionIndex + 1}</label>
                                <textarea name="Questions[${questionIndex}].Text" class="form-control" placeholder="new question"></textarea>
                        <br>
                        <label class=h5>Select question type</label>
                        <select name="Questions[${questionIndex}].Type" class="form-control question-type-${questionIndex}">`;

                var questionTypes = @Html.Raw(Json.Serialize(Enum.GetNames(typeof(QuestionType))));

                for (var i = 0; i < questionTypes.length; i++) {
                    newQuestionHtml += `<option value="${questionTypes[i]}">${questionTypes[i]}</option>`;
                }

                newQuestionHtml += `</select>`;

                // Add question type information
                newQuestionHtml += `
                    <div class="question-type-info">
                        <i class="bi bi-info-circle"></i>
                        <span class="type-info-text">Question type information will appear here</span>
                    </div>`;

                // Add answers input fields
                newQuestionHtml += `<div class="container-ms mx-5 py-2 px-5 ">`;
                newQuestionHtml += `<div class="answers-container" data-question-index="${questionIndex}"><br>`;
                newQuestionHtml += `<label class="h3">Create Answers:</label>`;
                newQuestionHtml += `<div class="answer-group" data-answer-index="0"><hr class="border border-primary border-1 opacity-35">`;
                newQuestionHtml += `<label>Answer 1</label>`;
                newQuestionHtml += `<input type="text" name="Questions[${questionIndex}].Answers[0].Text" class="form-control" placeholder="new answer"/><br>`;
                newQuestionHtml += `<input type="hidden" name="Questions[${questionIndex}].Answers[0].IsOtherOption" value="false"/>`;
                newQuestionHtml += `<button type="button" class="btn btn-sm btn-success add-answer shadow mt-2"><i class="bi bi-plus-lg"></i> Add Answer</button>`;
                newQuestionHtml += `<button type="button" class="btn btn-sm btn-info add-other-option shadow mt-2 ml-2 hidden"><i class="bi bi-pencil-square"></i> Add "Other" Option</button>`;
                newQuestionHtml += `<hr class="border m-2">`
                newQuestionHtml += `</div> `;
                newQuestionHtml += `</div>`;
                newQuestionHtml += `</div> `;

                // Add Remove Question button
                newQuestionHtml += `<button type="button" class="btn btn-sm btn-danger shadow m-4 remove-question"><i class="bi bi-trash3-fill"></i> Remove Question </button>`;
                newQuestionHtml += `<button type="button" class="btn btn-sm btn-primary shadow save-question"><i class="bi bi-floppy"></i> Save Question</button>`;
                newQuestionHtml += `<button type="button" class="btn btn-sm btn-info shadow edit-question" style="display: none;"><i class="bi bi-pencil-square"></i> Edit Question</button>`;
                newQuestionHtml += `<hr class="border border-primary border-4 opacity-75 mb-5">`
                newQuestionHtml += `</div>`;

                const newQuestionElement = $(newQuestionHtml);
                $("#questions-container .form-group").append(newQuestionElement);

                // Update other option visibility for the new question
                updateOtherOptionVisibility(newQuestionElement);

                questionIndex++;
            });

            // Add regular answer
            $("#questions-container").on("click", ".add-answer", function () {
                var questionIndex = $(this).closest('.answers-container').data('question-index');
                var answerIndex = $(this).closest('.answers-container').find('.answer-group').length;

                var answerGroupHtml = `
                    <div class="answer-group fade-in">
                        <br>
                        <label class="control-label">Answer ${answerIndex + 1}</label>
                        <input type="text" class="form-control" name="Questions[${questionIndex}].Answers[${answerIndex}].Text" placeholder="new answer"/>
                        <input type="hidden" name="Questions[${questionIndex}].Answers[${answerIndex}].IsOtherOption" value="false"/>
                        <button type="button" class="btn btn-danger btn-sm mt-1 remove-answer"><i class="bi bi-trash3-fill"></i></button>
                        <br>
                    </div>`;

                $(this).before(answerGroupHtml);
                $(this).prev('.answer-group').find('.remove-answer').show();
            });

            // Add "Other" option - Enhanced with validation
            $("#questions-container").on("click", ".add-other-option", function () {
                var questionIndex = $(this).closest('.answers-container').data('question-index');
                var answerIndex = $(this).closest('.answers-container').find('.answer-group').length;
                var questionGroup = $(this).closest('.question-group');
                var questionTypeSelect = questionGroup.find('.question-type, [class*="question-type-"]');
                var selectedType = questionTypeSelect.val();

                // Double-check if the question type supports other options
                if (!supportsOtherOption(selectedType)) {
                    alert(`"Other" options are not supported for ${selectedType} question type.`);
                    return;
                }

                // Check if "Other" option already exists
                var existingOther = $(this).closest('.answers-container').find('.other-option-group');
                if (existingOther.length > 0) {
                    alert('An "Other" option already exists for this question.');
                    return;
                }

                var otherOptionHtml = `
                    <div class="answer-group other-option-group fade-in">
                        <br>
                        <label class="control-label"><strong>Other Option ${answerIndex + 1}</strong></label>
                        <div class="alert alert-info" role="alert">
                            <small><i class="bi bi-info-circle"></i> This will allow users to write their own answer</small>
                        </div>
                        <input type="text" class="form-control" name="Questions[${questionIndex}].Answers[${answerIndex}].Text" placeholder="Other (please specify)" value="Other (please specify)"/>
                        <input type="hidden" name="Questions[${questionIndex}].Answers[${answerIndex}].IsOtherOption" value="true"/>
                        <button type="button" class="btn btn-warning btn-sm mt-1 remove-answer"><i class="bi bi-trash3-fill"></i> Remove Other Option</button>
                        <br>
                    </div>`;

                $(this).before(otherOptionHtml);
            });

            // Remove answer dynamically
            $("#questions-container").on("click", ".remove-answer", function () {
                $(this).closest('.answer-group').remove();
            });

            // Remove question dynamically
            $(document).on("click", ".remove-question", function () {
                $(this).closest('.question-group').remove();
            });

            // Save question
            $("#questions-container").on("click", ".save-question", function () {
                var questionGroup = $(this).closest('.question-group');
                questionGroup.find('.edit-question').show();
                questionGroup.find('.remove-question').hide();
                questionGroup.find('.save-question').hide();
                questionGroup.find('.form-control').attr('disabled', true);
                questionGroup.find('.question-type').attr('disabled', true);
                questionGroup.find('.answers-container').slideUp();
                questionGroup.addClass('collapsed');

                // Show success message
                var successMessage = $('<div class="alert alert-success" role="alert">Question created successfully!</div>');
                questionGroup.prepend(successMessage);
            });

            // Function to edit a question
            $("#questions-container").on("click", ".edit-question", function () {
                var questionGroup = $(this).closest('.question-group');
                questionGroup.find('.save-question').show();
                questionGroup.find('.remove-question').show();
                questionGroup.find('.edit-question').hide();
                questionGroup.find('.form-control').attr('disabled', false);
                questionGroup.find('.question-type').attr('disabled', false);
                questionGroup.find('.answers-container').slideDown();
                questionGroup.removeClass('collapsed');

                // Update other option visibility when editing
                updateOtherOptionVisibility(questionGroup);

                // Remove any existing success message
                questionGroup.find('.alert-success').remove();
            });

            // Enable form fields before form submission
            $("form").submit(function () {
                $(this).find('.form-control').prop('disabled', false);
            });

        });
    </script>

}