<div x-data="publicForm()" class="pf-container">
    <?php if(session('success')): ?>
    <div class="pf-success">
        <div style="width:72px; height:72px; margin:0 auto 8px; border-radius:50%; background:linear-gradient(135deg, rgba(34,197,94,0.12), rgba(22,163,74,0.12)); display:flex; align-items:center; justify-content:center;">
            <svg width="36" height="36" viewBox="0 0 24 24" fill="none" stroke="#22c55e" stroke-width="2.5"><circle cx="12" cy="12" r="10"/><polyline points="16 8 10 16 7 13"/></svg>
        </div>
        <h2><?php echo e(session('success')); ?></h2>
        <?php if(session('ticket_id')): ?>
        <p class="pf-ticket">Ticket ID: <strong><?php echo e(session('ticket_id')); ?></strong></p>
        <?php endif; ?>
        <a href="<?php echo e(route('forms.public', $form->slug)); ?>" class="btn btn-primary" style="margin-top:20px; background:linear-gradient(135deg, var(--accent-primary), #a78bfa); border:none; padding:10px 28px; border-radius:10px;">Submit Another</a>
    </div>
    <?php else: ?>

    <div class="pf-card">
        <div class="pf-header" style="border-top: none;">
            <h1 class="pf-title"><?php echo e($form->title); ?></h1>
            <?php if($form->description): ?>
            <p class="pf-desc"><?php echo e($form->description); ?></p>
            <?php endif; ?>
        </div>

        <?php if($preview ?? false): ?>
        <div class="pf-preview-banner">
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>
            Preview Mode — submissions are disabled
        </div>
        <?php endif; ?>

        <form action="<?php echo e(route('forms.submit', $form->slug)); ?>" method="POST" enctype="multipart/form-data" class="pf-form" @submit="submitting = true">
            <?php echo csrf_field(); ?>

            <?php if($errors->any()): ?>
            <div class="alert alert-danger" style="margin-bottom:20px; border-radius:12px; border:1px solid rgba(239,68,68,0.15);">
                <ul style="margin:0; padding-left:16px;">
                    <?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 $__currentLoopData = $form->fields; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $fi => $field): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <div class="pf-field-wrap"
                 x-show="isFieldVisible('<?php echo e($field->field_key); ?>')"
                 style="<?php echo e($field->width === 'half' ? 'width:calc(50% - 8px); display:inline-block; vertical-align:top;' : ''); ?> animation-delay: <?php echo e($fi * 0.05); ?>s;"
            >
                <?php if($field->type === 'section'): ?>
                    <div class="pf-section">
                        <div class="pf-section-line"></div>
                        <span><?php echo e($field->label); ?></span>
                    </div>
                <?php elseif($field->type === 'heading'): ?>
                    <h3 class="pf-heading"><?php echo e($field->label); ?></h3>
                <?php else: ?>
                    <label class="pf-label">
                        <?php echo e($field->label); ?>

                        <?php if($field->is_required): ?><span class="pf-required">*</span><?php endif; ?>
                    </label>

                    <?php switch($field->type):
                        case ('text'): ?>
                            <input type="text" name="field_<?php echo e($field->field_key); ?>" class="form-input"
                                   placeholder="<?php echo e($field->placeholder); ?>"
                                   <?php echo e($field->is_required ? 'required' : ''); ?>

                                   value="<?php echo e(old('field_' . $field->field_key)); ?>"
                                   x-model="formData.<?php echo e($field->field_key); ?>">
                            <?php break; ?>
                        <?php case ('textarea'): ?>
                            <textarea name="field_<?php echo e($field->field_key); ?>" class="form-input" rows="4"
                                      placeholder="<?php echo e($field->placeholder); ?>"
                                      <?php echo e($field->is_required ? 'required' : ''); ?>

                                      x-model="formData.<?php echo e($field->field_key); ?>"><?php echo e(old('field_' . $field->field_key)); ?></textarea>
                            <?php break; ?>
                        <?php case ('number'): ?>
                            <input type="number" name="field_<?php echo e($field->field_key); ?>" class="form-input"
                                   placeholder="<?php echo e($field->placeholder); ?>"
                                   <?php echo e($field->is_required ? 'required' : ''); ?>

                                   value="<?php echo e(old('field_' . $field->field_key)); ?>"
                                   x-model="formData.<?php echo e($field->field_key); ?>">
                            <?php break; ?>
                        <?php case ('date'): ?>
                            <input type="date" name="field_<?php echo e($field->field_key); ?>" class="form-input"
                                   <?php echo e($field->is_required ? 'required' : ''); ?>

                                   value="<?php echo e(old('field_' . $field->field_key)); ?>"
                                   x-model="formData.<?php echo e($field->field_key); ?>">
                            <?php break; ?>
                        <?php case ('select'): ?>
                            <select name="field_<?php echo e($field->field_key); ?>" class="form-input"
                                    <?php echo e($field->is_required ? 'required' : ''); ?>

                                    x-model="formData.<?php echo e($field->field_key); ?>">
                                <option value=""><?php echo e($field->placeholder ?: 'Select...'); ?></option>
                                <?php $__currentLoopData = $field->options ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $opt): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($opt['value']); ?>"><?php echo e($opt['label']); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <?php break; ?>
                        <?php case ('multi_select'): ?>
                            <div class="pf-multi-group">
                                <?php $__currentLoopData = $field->options ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $opt): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <label class="pf-checkbox">
                                    <input type="checkbox" name="field_<?php echo e($field->field_key); ?>[]" value="<?php echo e($opt['value']); ?>">
                                    <span><?php echo e($opt['label']); ?></span>
                                </label>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </div>
                            <?php break; ?>
                        <?php case ('file'): ?>
                            <div class="pf-file-drop" @dragover.prevent="$el.classList.add('dragover')" @dragleave="$el.classList.remove('dragover')" @drop.prevent="$el.classList.remove('dragover'); $refs.fileInput_<?php echo e($field->field_key); ?>.files = $event.dataTransfer.files; $el.querySelector('.pf-file-name').textContent = $event.dataTransfer.files[0]?.name || '';">
                                <input type="file" name="field_<?php echo e($field->field_key); ?>" x-ref="fileInput_<?php echo e($field->field_key); ?>" class="pf-file-input" @change="$el.closest('.pf-file-drop').querySelector('.pf-file-name').textContent = $el.files[0]?.name || ''">
                                <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" style="opacity:.5;"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>
                                <span>Drop file or <strong style="color:var(--accent-primary);">browse</strong></span>
                                <span class="pf-file-name"></span>
                            </div>
                            <?php break; ?>
                    <?php endswitch; ?>

                    <?php if($field->help_text): ?>
                    <p class="pf-help"><?php echo e($field->help_text); ?></p>
                    <?php endif; ?>

                    <?php $__errorArgs = ['field_' . $field->field_key];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                    <p class="pf-error">
                        <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>
                        <?php echo e($message); ?>

                    </p>
                    <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                <?php endif; ?>
            </div>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

            <?php if (! ($preview ?? false)): ?>
            <button type="submit" class="pf-submit" :disabled="submitting" style="background: linear-gradient(135deg, <?php echo e($form->settings['accent_color'] ?? '#6366f1'); ?>, #a78bfa);">
                <span x-show="!submitting">Submit</span>
                <span x-show="submitting" style="display:flex; align-items:center; justify-content:center; gap:8px;">
                    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="animation: spin 1s linear infinite;"><circle cx="12" cy="12" r="10" stroke-dasharray="60" stroke-dashoffset="20"/></svg>
                    Submitting...
                </span>
            </button>
            <?php endif; ?>
        </form>
    </div>
    <?php endif; ?>
</div>

<style>@keyframes spin { to { transform: rotate(360deg); } }</style>

<script>
function publicForm() {
    return {
        formData: {},
        submitting: false,
        conditionalRules: <?php echo json_encode($form->fields->whereNotNull('conditional_rules')->mapWithKeys(fn($f) => [$f->field_key => $f->conditional_rules]), 15, 512) ?>,

        isFieldVisible(fieldKey) {
            const rules = this.conditionalRules[fieldKey];
            if (!rules || !rules.conditions || rules.conditions.length === 0) return true;

            const results = rules.conditions.map(cond => {
                const val = this.formData[cond.field_key] ?? '';
                switch(cond.operator) {
                    case 'equals': return val == cond.value;
                    case 'not_equals': return val != cond.value;
                    case 'contains': return String(val).includes(cond.value || '');
                    case 'is_empty': return !val || val === '';
                    case 'is_not_empty': return val && val !== '';
                    default: return true;
                }
            });

            const passes = rules.logic === 'or' ? results.some(r => r) : results.every(r => r);
            return rules.action === 'show' ? passes : !passes;
        }
    }
}
</script>
<?php /**PATH /Applications/XAMPP/xamppfiles/htdocs/asana/resources/views/forms/_public_body.blade.php ENDPATH**/ ?>