@extends('layouts.admin.app')
@section('title', __('trans.exam.edit_exam') . ' of lecture: ' . $lecture->title)

@section('content')
    <div class="card">
        <h5 class="card-header">
            <a href="{{ url('admin/lectures') }}" class="btn rounded-pill btn-label-primary">
                <i class="ti me-2 ti-arrow-forward-up"></i> {{ __('trans.lecture.title') }}
            </a>
            @yield('title')
        </h5>

        <div class="d-flex flex-row-reverse m-2">
            <form action="{{ url('/admin/exams/' . $lecture->exam->id) }}/" method="post">
                @csrf
                @method('DELETE')
                <button class="btn btn-danger btn-delete">DELETE EXAM</button>
            </form>
        </div>

        <div class="card-body">
            <form action="{{ url('admin/exams/' . $lecture->exam->id . '/update') }}" method="POST">
                @csrf
                @method('PUT')

                <input type="hidden" name="lecture_id" value="{{ $lecture->id }}">

                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">{{ __('trans.exam.name') }} <b class="text-danger">*</b></label>
                        <input type="text" class="form-control" name="name" required
                            value="{{ old('name', $lecture->exam->name) }}" placeholder="{{ __('trans.exam.name') }}" />
                    </div>

                    <div class="col-md-3 mb-3">
                        <label class="form-label">{{ __('trans.exam.status') }}</label>
                        <select name="status" class="form-select" required>
                            <option value="Show" @selected($lecture->exam->status == 'Show')>Show</option>
                            <option value="Hide" @selected($lecture->exam->status == 'Hide')>Hide</option>
                        </select>
                    </div>

                    <div class="col-md-3 mb-3">
                        <label class="form-label">{{ __('trans.exam.retake') }}</label>
                        <select name="retake" class="form-select" required>
                            <option value="1" @selected(old('retake', $lecture->exam->retake) == '1')>Yes</option>
                            <option value="0" @selected(old('retake', $lecture->exam->retake) == '0')>No</option>
                        </select>
                    </div>
                </div>

                <div class="rows">
                    @foreach ($lecture->exam->questions as $index1 => $question)
                        <div class="question-block row" data-question-index="{{ $index1 }}">
                            <div class="divider divider-success">
                                <div class="divider-text text-success">Question {{ $index1 + 1 }}</div>
                            </div>

                            <div class="col-md-6 mb-3">
                                <label>Question {{ $index1 + 1 }}</label>
                                <input type="hidden" name="questions[{{ $index1 }}][id]"
                                    value="{{ $question->id }}">
                                <input type="text" name="questions[{{ $index1 }}][title]" class="form-control"
                                    value="{{ $question->question }}" />
                            </div>

                            <div class="col-md-3 mb-2">
                                <label>&nbsp;</label><br>
                                <span class="btn btn-danger delete-ques" data-id="{{ $index1 }}">Delete Question
                                    {{ $index1 + 1 }}</span>
                            </div>

                            <div class="options" data-question-index="{{ $index1 }}">
                                @foreach ($question->options as $index2 => $option)
                                    <div class="row mb-2 option-row" data-option-index="{{ $index2 }}">
                                        <div class="col-md-4 mb-2">
                                            <label>Option {{ $index2 + 1 }}</label>
                                            <input type="hidden"
                                                name="questions[{{ $index1 }}][options][{{ $index2 }}][id]"
                                                value="{{ $option->id }}">
                                            <input type="text"
                                                name="questions[{{ $index1 }}][options][{{ $index2 }}][text]"
                                                class="form-control" value="{{ $option->option }}" />
                                        </div>

                                        <div class="col-md-2">
                                            <label>Is True</label>
                                            <select
                                                name="questions[{{ $index1 }}][options][{{ $index2 }}][is_true]"
                                                class="form-select">
                                                <option value="0" @selected($option->is_correct == 0)>false</option>
                                                <option value="1" @selected($option->is_correct == 1)>true</option>
                                            </select>
                                        </div>

                                        @if ($index2 >= 2)
                                            <div class="col-md-2 mt-4">
                                                <button type="button"
                                                    class="btn btn-danger btn-sm delete-option">Delete</button>
                                            </div>
                                        @endif
                                    </div>
                                @endforeach
                            </div>

                            <div class="col-md-4 mb-3">
                                <button type="button" class="btn btn-info btn-sm add-new-option"
                                    data-question-index="{{ $index1 }}">
                                    Add New Option
                                </button>
                            </div>
                        </div>
                    @endforeach
                </div>

                <div class="row">
                    <div class="col-md-3">
                        <span class="btn btn-info add-question">Add New Question</span>
                    </div>
                    <div class="col-md-12 mt-5">
                        <button class="btn btn-primary btn-create w-100">{{ __('trans.save') }}</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
@endsection

@section('js')
    <script>
        let index = {{ count($lecture->exam->questions) }};

        $('.add-question').on('click', function() {
            console.log($('.question-block').length);
            if ($('.question-block').length >= 99) {
                alert('Max questions per exam is 100');
                return;
            }
            let html = `
        <div class="question-block row" data-question-index="${index}">
            <div class="divider divider-success">
                <div class="divider-text text-success">Question ${index + 1}</div>
            </div>

            <div class="col-md-6 mb-3">
                <label>Question ${index + 1}</label>
                <input type="text" name="questions[${index}][title]" class="form-control" />
            </div>

            <div class="col-md-3 mb-2">
                <label>&nbsp;</label><br>
                <span class="btn btn-danger delete-ques" data-id="${index}">Delete Question ${index + 1}</span>
            </div>

            <div class="options" data-question-index="${index}">
                <div class="row mb-2 option-row" data-option-index="0">
                    <div class="col-md-4">
                        <input type="text" name="questions[${index}][options][0][text]" class="form-control" placeholder="Option 1" />
                    </div>
                    <div class="col-md-2">
                        <select name="questions[${index}][options][0][is_true]" class="form-select">
                            <option value="0">false</option>
                            <option value="1">true</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-2 option-row" data-option-index="1">
                    <div class="col-md-4">
                        <input type="text" name="questions[${index}][options][1][text]" class="form-control" placeholder="Option 2" />
                    </div>
                    <div class="col-md-2">
                        <select name="questions[${index}][options][1][is_true]" class="form-select">
                            <option value="0">false</option>
                            <option value="1">true</option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="col-md-4 mb-3">
                <button type="button" class="btn btn-info btn-sm add-new-option" data-question-index="${index}">
                    Add New Option
                </button>
            </div>
        </div>`;
            $('.rows').append(html);
            index++;
        });

        $(document).on('click', '.add-new-option', function() {
            const questionIndex = $(this).data('question-index');
            const questionBlock = $(this).closest('.question-block');
            const optionsWrapper = questionBlock.find('.options');
            const currentCount = optionsWrapper.find('.option-row').length;
            if (currentCount > 5) {
                alert('Max options per question is 6');
                return;
            }

            let newOptionHTML = `
            <div class="row mb-2 option-row" data-option-index="${currentCount}">
                <div class="col-md-4">
                    <input type="text" name="questions[${questionIndex}][options][${currentCount}][text]" class="form-control" placeholder="Option ${currentCount + 1}" />
                </div>
                <div class="col-md-2">
                    <select name="questions[${questionIndex}][options][${currentCount}][is_true]" class="form-select">
                        <option value="0">false</option>
                        <option value="1">true</option>
                    </select>
                </div>
                <div class="col-md-2 mt-4">
                    <button type="button" class="btn btn-danger btn-sm delete-option">Delete</button>
                </div>
            </div>
        `;
            optionsWrapper.append(newOptionHTML);
        });

        $(document).on('click', '.delete-option', function() {
            const optionRow = $(this).closest('.option-row');
            const optionIndex = parseInt(optionRow.data('option-index'));

            if (optionIndex < 2) {
                alert("You can't delete the first 2 default options.");
                return;
            }

            optionRow.remove();
        });

        $(document).on('click', '.delete-ques', function() {
            console.log($('.question-block').length);

            if ($('.question-block').length <= 1) {
                alert('Exam must have at least one question.');
                return;
            }
            if (confirm(`Are you sure you want to delete this question?`)) {
                $(this).closest('.question-block').remove();
            }
        });
    </script>
@endsection
