@extends('layouts.app')

@section('title', 'Tambah 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-plus-circle me-2"></i> Tambah Pertanyaan
            </h2>
        </div>
    </div>

    <div class="card card-gov shadow-sm">
        <div class="card-header">
            <h5 class="mb-0">Form Tambah Pertanyaan</h5>
        </div>
        <div class="card-body">
            <form method="POST" action="{{ route('admin.questions.store') }}" id="questionForm">
                @csrf
                <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') }}" required maxlength="10">
                        @error('code')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                        <small class="text-muted">Contoh: U1, U2, U3, dll.</small>
                    </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') == $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', 0) }}" 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', true) ? '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') }}</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">
                    <div class="answer-option-item card mb-3 p-3" data-index="0">
                        <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[0][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[0][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[0][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[0][order]" value="0" 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>
                </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> Simpan
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

@push('scripts')
<script>
let answerOptionIndex = 1;

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(/\[0\]/, `[${answerOptionIndex}]`));
            input.value = '';
        }
    });
    
    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

