<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?><?= e($schema->name_ar ?? 'نموذج') ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/forms/submissions" class="btn btn-outline">← العودة للنماذج</a>
<?php if (!empty($submission)): ?>
    <a href="/forms/print/<?= (int) $submission['id'] ?>" class="btn btn-outline" target="_blank">طباعة</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<?php if (!empty($submission)): ?>
<div class="card" style="margin-bottom:20px;padding:15px;display:flex;justify-content:space-between;align-items:center;">
    <div>
        <strong>رقم النموذج:</strong> <?= e($submission['form_number']) ?>
        &nbsp;&nbsp;|&nbsp;&nbsp;
        <strong>الحالة:</strong> <?= e($submission['status']) ?>
        &nbsp;&nbsp;|&nbsp;&nbsp;
        <strong>التاريخ:</strong> <?= e(substr($submission['created_at'], 0, 10)) ?>
        <?php if ($submission['employee_name']): ?>
            &nbsp;&nbsp;|&nbsp;&nbsp;
            <strong>بواسطة:</strong> <?= e($submission['employee_name']) ?>
        <?php endif; ?>
    </div>
    <?php if (empty($readOnly)): ?>
    <form method="POST" action="/forms/submissions/<?= (int) $submission['id'] ?>/status" style="display:flex;gap:8px;">
        <?= csrf_field() ?>
        <select name="status" class="form-select" style="width:auto;">
            <option value="submitted">مُقدّم</option>
            <option value="under_review">تحت المراجعة</option>
            <option value="approved">مقبول</option>
            <option value="rejected">مرفوض</option>
            <option value="expired">منتهي</option>
        </select>
        <button type="submit" class="btn btn-sm btn-outline">تحديث الحالة</button>
    </form>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php if (empty($readOnly) && empty($submission)): ?>
<form method="POST" action="/forms/submit/<?= e($schema->form_code) ?>">
    <?= csrf_field() ?>
    <?= $formHtml ?>
    <div style="margin-top:20px;display:flex;gap:10px;">
        <button type="submit" class="btn btn-primary">تقديم النموذج</button>
        <a href="/forms/submissions" class="btn btn-outline">إلغاء</a>
    </div>
</form>
<?php else: ?>
    <?= $formHtml ?>
<?php endif; ?>

<?php $__template->endSection(); ?>

<?php $__template->section('scripts'); ?>
<script src="<?= url('assets/js/forms-engine.js') ?>"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof FormsEngine !== 'undefined') {
        FormsEngine.init();
    }
});
</script>
<?php $__template->endSection(); ?>