@extends('layouts.app')

@section('title', 'Edit Pertanyaan - SKM Survey')

@section('content')
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-12">
            <a href="{{ route('admin.questions.index') }}" class="btn btn-secondary mb-3">
                <i class="bi bi-arrow-left"></i> Kembali
            </a>
            <h2 style="color: #1e3a8a; font-weight: 700;">
                <i class="bi bi-pencil me-2"></i> Edit Pertanyaan
            </h2>
        </div>
    </div>

    <div class="card card-gov shadow-sm">
        <div class="card-header">
            <h5 class="mb-0">Form Edit Pertanyaan</h5>
        </div>
        <div class="card-body">
            <form method="POST" action="{{ route('admin.questions.update', $question->id) }}" id="questionForm">
                @csrf
                @method('PUT')
                <div class="row mb-4">
                    <div class="col-md-3 mb-3">
                        <label for="code" class="form-label fw-semibold">Kode Pertanyaan <span class="text-danger">*</span></label>
                        <input type="text" class="form-control @error('code') is-invalid @enderror" 
                               id="code" name="code" value="{{ old('code', $question->code) }}" required maxlength="10">
                        @error('code')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                    <div class="col-md-3 mb-3">
                        <label for="element" class="form-label fw-semibold">Elemen (Unsur) <span class="text-danger">*</span></label>
                        <select class="form-select @error('element') is-invalid @enderror" id="element" name="element" required>
                            <option value="">Pilih Elemen</option>
                            @foreach($elements as $element)
                                <option value="{{ $element }}" {{ old('element', $question->element) == $element ? 'selected' : '' }}>{{ $element }}</option>
                            @endforeach
                        </select>
                        @error('element')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                    <div class="col-md-3 mb-3">
                        <label for="order" class="form-label fw-semibold">Urutan</label>
                        <input type="number" class="form-control @error('order') is-invalid @enderror" 
                               id="order" name="order" value="{{ old('order', $question->order) }}" min="0">
                        @error('order')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                    <div class="col-md-3 mb-3">
                        <div class="form-check mt-4">
                            <input class="form-check-input" type="checkbox" id="is_active" name="is_active" value="1" {{ old('is_active', $question->is_active) ? 'checked' : '' }}>
                            <label class="form-check-label" for="is_active">
                                Aktif
                            </label>
                        </div>
                    </div>
                </div>
                <div class="mb-4">
                    <label for="question" class="form-label fw-semibold">Pertanyaan <span class="text-danger">*</span></label>
                    <textarea class="form-control @error('question') is-invalid @enderror" 
                              id="question" name="question" rows="3" required>{{ old('question', $question->question) }}</textarea>
                    @error('question')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>

                <hr class="my-4">

                <h5 class="mb-3">Opsi Jawaban</h5>
                <div id="answerOptionsContainer">
                    @foreach($question->answerOptions as $index => $option)
                        <div class="answer-option-item card mb-3 p-3" data-index="{{ $index }}">
                            <input type="hidden" name="answer_options[{{ $index }}][id]" value="{{ $option->id }}">
                            <div class="row g-3">
                                <div class="col-md-4">
                                    <label class="form-label fw-semibold">Label Jawaban <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" name="answer_options[{{ $index }}][label]" value="{{ old("answer_options.{$index}.label", $option->label) }}" required>
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label fw-semibold">Score <span class="text-danger">*</span></label>
                                    <input type="number" class="form-control" name="answer_options[{{ $index }}][score]" value="{{ old("answer_options.{$index}.score", $option->score) }}" min="1" required>
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label fw-semibold">Weight <span class="text-danger">*</span></label>
                                    <input type="number" step="0.01" class="form-control" name="answer_options[{{ $index }}][weight]" value="{{ old("answer_options.{$index}.weight", $option->weight) }}" min="0" required>
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label fw-semibold">Urutan</label>
                                    <input type="number" class="form-control" name="answer_options[{{ $index }}][order]" value="{{ old("answer_options.{$index}.order", $option->order) }}" min="0">
                                </div>
                                <div class="col-md-2 d-flex align-items-end">
                                    <button type="button" class="btn btn-danger w-100 remove-option">
                                        <i class="bi bi-trash"></i> Hapus
                                    </button>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
                <button type="button" class="btn btn-outline-primary" id="addAnswerOption">
                    <i class="bi bi-plus-circle me-2"></i> Tambah Opsi Jawaban
                </button>

                <div class="d-flex justify-content-end mt-4">
                    <a href="{{ route('admin.questions.index') }}" class="btn btn-secondary me-2">
                        <i class="bi bi-x-circle me-2"></i> Batal
                    </a>
                    <button type="submit" class="btn btn-gov-primary">
                        <i class="bi bi-check-circle me-2"></i> Update
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

@push('scripts')
<script>
let answerOptionIndex = {{ $question->answerOptions->count() }};

document.getElementById('addAnswerOption').addEventListener('click', function() {
    const container = document.getElementById('answerOptionsContainer');
    const newOption = document.querySelector('.answer-option-item').cloneNode(true);
    newOption.setAttribute('data-index', answerOptionIndex);
    
    // Update input names
    newOption.querySelectorAll('input').forEach(input => {
        const name = input.getAttribute('name');
        if (name) {
            input.setAttribute('name', name.replace(/\[\d+\]/, `[${answerOptionIndex}]`));
            if (!input.type || input.type !== 'hidden') {
                input.value = '';
            } else {
                input.remove(); // Remove hidden id field for new options
            }
        }
    });
    
    container.appendChild(newOption);
    answerOptionIndex++;
});

document.addEventListener('click', function(e) {
    if (e.target.classList.contains('remove-option') || e.target.closest('.remove-option')) {
        const item = e.target.closest('.answer-option-item');
        if (document.querySelectorAll('.answer-option-item').length > 1) {
            item.remove();
        } else {
            alert('Minimal harus ada 1 opsi jawaban');
        }
    }
});
</script>
@endpush
@endsection



