<?php $__env->startSection('title', 'Edit Pertanyaan - SKM Survey'); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-12">
            <a href="<?php echo e(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="<?php echo e(route('admin.questions.update', $question->id)); ?>" id="questionForm">
                <?php echo csrf_field(); ?>
                <?php echo method_field('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 <?php $__errorArgs = ['code'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" 
                               id="code" name="code" value="<?php echo e(old('code', $question->code)); ?>" required maxlength="10">
                        <?php $__errorArgs = ['code'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                            <div class="invalid-feedback"><?php echo e($message); ?></div>
                        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </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 <?php $__errorArgs = ['element'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" id="element" name="element" required>
                            <option value="">Pilih Elemen</option>
                            <?php $__currentLoopData = $elements; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $element): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($element); ?>" <?php echo e(old('element', $question->element) == $element ? 'selected' : ''); ?>><?php echo e($element); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                        <?php $__errorArgs = ['element'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                            <div class="invalid-feedback"><?php echo e($message); ?></div>
                        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>
                    <div class="col-md-3 mb-3">
                        <label for="order" class="form-label fw-semibold">Urutan</label>
                        <input type="number" class="form-control <?php $__errorArgs = ['order'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" 
                               id="order" name="order" value="<?php echo e(old('order', $question->order)); ?>" min="0">
                        <?php $__errorArgs = ['order'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                            <div class="invalid-feedback"><?php echo e($message); ?></div>
                        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </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" <?php echo e(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 <?php $__errorArgs = ['question'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" 
                              id="question" name="question" rows="3" required><?php echo e(old('question', $question->question)); ?></textarea>
                    <?php $__errorArgs = ['question'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                        <div class="invalid-feedback"><?php echo e($message); ?></div>
                    <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                </div>

                <hr class="my-4">

                <h5 class="mb-3">Opsi Jawaban</h5>
                <div id="answerOptionsContainer">
                    <?php $__currentLoopData = $question->answerOptions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $option): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <div class="answer-option-item card mb-3 p-3" data-index="<?php echo e($index); ?>">
                            <input type="hidden" name="answer_options[<?php echo e($index); ?>][id]" value="<?php echo e($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[<?php echo e($index); ?>][label]" value="<?php echo e(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[<?php echo e($index); ?>][score]" value="<?php echo e(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[<?php echo e($index); ?>][weight]" value="<?php echo e(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[<?php echo e($index); ?>][order]" value="<?php echo e(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>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </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="<?php echo e(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>

<?php $__env->startPush('scripts'); ?>
<script>
let answerOptionIndex = <?php echo e($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>
<?php $__env->stopPush(); ?>
<?php $__env->stopSection(); ?>


<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /Users/user/Documents/giriwidyan/project/inhouse/skm/resources/views/admin/questions/edit.blade.php ENDPATH**/ ?>