<?php
function as_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$conference = $conference ?? [];
$workflow = $workflow ?? [];
$submissionOpen = $submissionOpen ?? true;
$fileCategories = $fileCategories ?? [];
?>

<div class="container-fluid py-3">

    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#1d4ed8);color:#fff;">
        <div class="card-body p-4">
            <h3 class="fw-bold mb-1">تقديم بحث جديد</h3>
            <div class="opacity-75"><?= as_h(($conference['title_ar'] ?? '') ?: ($conference['title_en'] ?? '')) ?></div>
        </div>
    </div>

    <?php if (!empty($workflow)): ?>
        <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
            <div class="card-body">
                <h5 class="fw-bold">سير العمل</h5>
                <?php foreach ($workflow as $w): ?>
                    <span class="badge <?= !empty($w['is_enabled']) ? 'bg-primary' : 'bg-secondary' ?> p-2 m-1">
                        <?= as_h($w['stage']) ?>
                    </span>
                <?php endforeach; ?>
            </div>
        </div>
    <?php endif; ?>

    <?php if (!$submissionOpen): ?>
        <div class="alert alert-warning">التقديم مغلق حالياً لهذا المؤتمر.</div>
    <?php else: ?>

    <form method="post" action="/easychair/author/submission/store" enctype="multipart/form-data">
        <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
            <div class="card-body">
                <h5 class="fw-bold mb-3"><?php echo ec_h(ec_t('author.submit.paper_info','بيانات البحث')); ?></h5>

                <div class="row g-3">
                    <div class="col-md-6">
                        <label class="form-label fw-bold">Title</label>
                        <input name="title" class="form-control">
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-bold">العنوان بالعربية</label>
                        <input name="title_ar" class="form-control">
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-bold">Abstract</label>
                        <textarea name="abstract" class="form-control" rows="5"></textarea>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-bold">الملخص بالعربية</label>
                        <textarea name="abstract_ar" class="form-control" rows="5"></textarea>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-bold">Keywords</label>
                        <input name="keywords" class="form-control">
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-bold">الكلمات المفتاحية</label>
                        <input name="keywords_ar" class="form-control">
                    </div>
                </div>
            </div>
        </div>

        <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
            <div class="card-body">
                <h5 class="fw-bold mb-3"><?php echo ec_h(ec_t('author.submit.coauthors','المؤلفون المشاركون')); ?></h5>

                <div id="coauthors">
                    <div class="row g-2 mb-2 coauthor-row">
                        <div class="col-md-3"><input name="coauthor_name[]" class="form-control" placeholder="Full name"></div>
                        <div class="col-md-3"><input name="coauthor_email[]" class="form-control" placeholder="Email"></div>
                        <div class="col-md-4"><input name="coauthor_affiliation[]" class="form-control" placeholder="Affiliation"></div>
                        <div class="col-md-2"><input name="coauthor_country[]" class="form-control" placeholder="Country"></div>
                    </div>
                </div>

                <button type="button" class="btn btn-outline-primary btn-sm" onclick="addCoauthor()">+ <?php echo ec_h(ec_t('author.submit.add_coauthor','إضافة مؤلف مشارك')); ?></button>
            </div>
        </div>

        <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
            <div class="card-body">
                <h5 class="fw-bold mb-3">ملفات البحث حسب التصنيف</h5>

                <div id="files">
                    <div class="row g-2 mb-2 file-row">
                        <div class="col-md-4">
                            <select name="file_category[]" class="form-select">
                                <?php foreach ($fileCategories as $key=>$label): ?>
                                    <option value="<?= as_h($key) ?>" <?= $key === 'manuscript' ? 'selected' : '' ?>>
                                        <?= as_h($label) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-8">
                            <input type="file" name="submission_files[]" class="form-control" required>
                        </div>
                    </div>
                </div>

                <button type="button" class="btn btn-outline-primary btn-sm" onclick="addFileRow()">+ إضافة ملف آخر</button>
            </div>
        </div>

        <div class="card border-0 shadow-sm" style="border-radius:22px;">
            <div class="card-body">
                <button class="btn btn-success btn-lg">
                    إرسال البحث
                </button>
            </div>
        </div>
<div class="row g-3">

<div class="col-md-6">
<label class="form-label">Theme</label>
<select name="theme_id" class="form-control" required>
<option value="">Select Theme</option>
<option value="<?= (int)$t['id'] ?>">
<?= htmlspecialchars($t['title_ar'] ?? $t['title_en']) ?>
</option>
<?php endforeach; endif; ?>
</select>
</div>

<div class="col-md-6">
<label class="form-label">Submission Language</label>
<select name="submission_language" class="form-control">
<option value="ar">Arabic</option>
<option value="en">English</option>
</select>
</div>

</div>
    </form>

    <script>
    function addCoauthor(){
        const box = document.getElementById('coauthors');
        const row = document.querySelector('.coauthor-row').cloneNode(true);
        row.querySelectorAll('input').forEach(i => i.value = '');
        box.appendChild(row);
    }

    function addFileRow(){
        const box = document.getElementById('files');
        const row = document.querySelector('.file-row').cloneNode(true);
        row.querySelector('input[type=file]').value = '';
        box.appendChild(row);
    }
    </script>

    <?php endif; ?>

</div>
