

<?php $__env->startSection('content'); ?>
    <div class="container">
        <div class="card">
            <div class="card-header"><?php echo e(__('الاستبيان')); ?></div>

            <div class="card-body">
                <?php if($errors->any()): ?>
                    <div class="alert alert-danger">
                        <ul class="mb-0">
                            <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <li><?php echo e($error); ?></li>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </ul>
                    </div>
                <?php endif; ?>

                <?php if(session('success')): ?>
                    <div class="alert alert-success"><?php echo e(session('success')); ?></div>
                <?php endif; ?>

                <?php if($needsUnitSetup): ?>
                    <div class="alert alert-info">
                        <?php echo e(__('حسابك غير مرتبط بوحدة تنظيمية. أنشئ وحدة جديدة أدناه، ثم ستُعرض لك أسئلة الاستبيان لتُجيب عنها بالنيابة عن هذه الوحدة.')); ?>

                    </div>

                    <h6 class="mb-3"><?php echo e(__('إضافة وحدة تنظيمية')); ?></h6>
                    <form method="POST" action="<?php echo e(route('questionnaire.unit.store')); ?>">
                        <?php echo csrf_field(); ?>
                        <div class="mb-3">
                            <label class="form-label"><?php echo e(__('اسم الوحدة')); ?></label>
                            <input type="text" name="name" class="form-control <?php $__errorArgs = ['name'];
$__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); ?>"
                                value="<?php echo e(old('name')); ?>" required>
                            <?php $__errorArgs = ['name'];
$__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="mb-3">
                            <label class="form-label"><?php echo e(__('نوع الوحدة')); ?></label>
                            <select name="unit_type_id" class="form-select <?php $__errorArgs = ['unit_type_id'];
$__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); ?>" required>
                                <option value=""><?php echo e(__('اختر نوع الوحدة')); ?></option>
                                <?php $__currentLoopData = $unitTypes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $unitType): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($unitType->id); ?>" <?php if(old('unit_type_id') == $unitType->id): echo 'selected'; endif; ?>>
                                        <?php echo e($unitType->type_name); ?>

                                        <?php if(isset($unitType->level)): ?>
                                            (<?php echo e(__('مستوى')); ?>: <?php echo e($unitType->level); ?>)
                                        <?php endif; ?>
                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <?php $__errorArgs = ['unit_type_id'];
$__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="mb-3">
                            <label class="form-label"><?php echo e(__('الوحدة الأب (اختياري)')); ?></label>
                            <select name="parent_id" class="form-select <?php $__errorArgs = ['parent_id'];
$__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); ?>">
                                <option value=""><?php echo e(__('بدون — وحدة رئيسية')); ?></option>
                                <?php $__currentLoopData = $parentUnits; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $parent): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($parent->id); ?>" <?php if(old('parent_id') == $parent->id): echo 'selected'; endif; ?>>
                                        <?php echo e($parent->name); ?>

                                        <?php if($parent->unitType): ?>
                                            (<?php echo e($parent->unitType->type_name); ?>)
                                        <?php endif; ?>
                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <?php $__errorArgs = ['parent_id'];
$__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>
                        <button type="submit" class="btn btn-primary"><?php echo e(__('إنشاء الوحدة ومتابعة الاستبيان')); ?></button>
                    </form>
                <?php elseif(empty($unitId)): ?>
                    <div class="alert alert-warning mb-0">
                        <?php echo e(__('تعذر تحديد الوحدة التنظيمية للاستبيان.')); ?>

                    </div>
                <?php else: ?>
                    <?php if(!empty($usesSessionUnit) && $targetUnit): ?>
                        <div class="alert alert-secondary d-flex justify-content-between align-items-center flex-wrap gap-2">
                            <div>
                                <?php echo e(__('أنت تجيب عن الاستبيان للوحدة:')); ?>

                                <strong><?php echo e($targetUnit->name); ?></strong>
                            </div>
                            <form method="POST" action="<?php echo e(route('questionnaire.unit.clear')); ?>" class="m-0">
                                <?php echo csrf_field(); ?>
                                <button type="submit" class="btn btn-sm btn-outline-danger">
                                    <?php echo e(__('إلغاء الوحدة واختيار وحدة أخرى')); ?>

                                </button>
                            </form>
                        </div>
                    <?php endif; ?>

                    <?php if($questions->isEmpty()): ?>
                        <div class="alert alert-info mb-0 mt-3">
                            <?php echo e(__('لا توجد أسئلة جديدة لهذه الوحدة التنظيمية.')); ?>

                        </div>
                    <?php else: ?>
                        <form method="POST" action="<?php echo e(route('questionnaire.submit')); ?>" class="mt-3">
                            <?php echo csrf_field(); ?>

                            <?php $__currentLoopData = $questions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i => $question): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <div class="mb-4">
                                    <div class="mb-2" style="font-weight:600;">
                                        <?php echo e($question->question_text); ?>

                                    </div>

                                    <input type="hidden" name="answers[<?php echo e($i); ?>][question_id]" value="<?php echo e($question->id); ?>">

                                    <div class="d-flex gap-4">
                                        <div class="form-check">
                                            <input class="form-check-input" type="radio"
                                                name="answers[<?php echo e($i); ?>][answer_value]" id="q<?php echo e($question->id); ?>_yes"
                                                value="1" <?php if(old("answers.$i.answer_value") === '1'): echo 'checked'; endif; ?>>
                                            <label class="form-check-label" for="q<?php echo e($question->id); ?>_yes"><?php echo e(__('نعم')); ?></label>
                                        </div>
                                        <div class="form-check">
                                            <input class="form-check-input" type="radio"
                                                name="answers[<?php echo e($i); ?>][answer_value]" id="q<?php echo e($question->id); ?>_no"
                                                value="0" <?php if(old("answers.$i.answer_value") === '0'): echo 'checked'; endif; ?>>
                                            <label class="form-check-label" for="q<?php echo e($question->id); ?>_no"><?php echo e(__('لا')); ?></label>
                                        </div>
                                    </div>

                                    <div class="mt-2">
                                        <label class="form-label mb-1"><?php echo e(__('ملاحظات / بيان (اختياري)')); ?></label>
                                        <textarea class="form-control" rows="2" name="answers[<?php echo e($i); ?>][notes]"><?php echo e(old("answers.$i.notes")); ?></textarea>
                                    </div>

                                    <?php $__errorArgs = ["answers.$i.answer_value"];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                        <div class="text-danger mt-1"><?php echo e($message); ?></div>
                                    <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                </div>
                                <hr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                            <button class="btn btn-primary" type="submit"><?php echo e(__('حفظ الإجابات')); ?></button>
                        </form>
                    <?php endif; ?>
                <?php endif; ?>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\xampp82\htdocs\GeneralProject-form\resources\views/questionnaire/form.blade.php ENDPATH**/ ?>