<?php
$srow = $submission ?? null;
$authorsRows = $authors ?? [];
if (empty($authorsRows)) {
    $u = auth_user();
    $authorsRows = [[
        'full_name' => trim((string) (($u['first_name'] ?? '') . ' ' . ($u['last_name'] ?? ''))),
        'email' => (string) ($u['email'] ?? ''),
        'affiliation' => '',
        'country' => '',
        'is_corresponding' => 1,
    ]];
}
function sub_val(?array $row, string $key, string $default = ''): string {
    if (isset($row[$key])) return (string) $row[$key];
    return old($key, $default);
}
?>

<div class="row g-3">
    <div class="col-md-4">
        <label class="form-label ec-required"><?= e(trans('select_conference')) ?></label>
        <select name="conference_id" id="conference_id" class="form-select rounded-3" required>
            <option value="0">--</option>
            <?php foreach ($conferences as $c): ?>
                <option value="<?= e((string) $c['id']) ?>" <?= (string) ((int) sub_val($srow, 'conference_id', old('conference_id', '0'))) === (string) $c['id'] ? 'selected' : '' ?>>
                    <?= e(current_lang() === 'ar' ? (string) $c['title_ar'] : (string) $c['title_en']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>

    <div class="col-md-4">
        <label class="form-label ec-required"><?= e(trans('select_track')) ?></label>
        <select name="track_id" id="track_id" class="form-select rounded-3" required>
            <option value="0">--</option>
            <?php foreach ($tracks as $t): ?>
                <option value="<?= e((string) $t['id']) ?>" <?= (string) ((int) sub_val($srow, 'track_id', old('track_id', '0'))) === (string) $t['id'] ? 'selected' : '' ?>>
                    <?= e(current_lang() === 'ar' ? (string) $t['name_ar'] : (string) $t['name_en']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>

    <div class="col-md-4">
        <label class="form-label ec-required"><?= e(trans('submission_type')) ?></label>
        <select name="submission_type_id" id="submission_type_id" class="form-select rounded-3">
            <option value="0">--</option>
            <?php foreach ($submissionTypes as $st): ?>
                <option value="<?= e((string) $st['id']) ?>" <?= (string) ((int) sub_val($srow, 'submission_type_id', old('submission_type_id', '0'))) === (string) $st['id'] ? 'selected' : '' ?>>
                    <?= e(current_lang() === 'ar' ? (string) $st['name_ar'] : (string) $st['name_en']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>

    <div class="col-md-6">
        <label class="form-label ec-required"><?= e(trans('title_en')) ?> *</label>
        <input type="text" name="title" class="form-control rounded-3" value="<?= e(sub_val($srow, 'title')) ?>" required>
    </div>

    <div class="col-md-6">
        <label class="form-label ec-required"><?= e(trans('title_ar')) ?> *</label>
        <input type="text" name="title_ar" class="form-control rounded-3" value="<?= e(sub_val($srow, 'title_ar')) ?>" required>
    </div>

    <div class="col-md-6">
        <label class="form-label ec-required"><?= e(trans('abstract_en')) ?> *</label>
        <textarea name="abstract" class="form-control rounded-3 richtext" rows="6" required><?= e(sub_val($srow, 'abstract')) ?></textarea>
    </div>

    <div class="col-md-6">
        <label class="form-label ec-required"><?= e(trans('abstract_ar')) ?> *</label>
        <textarea name="abstract_ar" class="form-control rounded-3 richtext" rows="6" required><?= e(sub_val($srow, 'abstract_ar')) ?></textarea>
    </div>

    <div class="col-md-6">
        <label class="form-label"><?= e(trans('keywords_en')) ?> *</label>
        <input type="hidden" name="keywords" id="keywords">
        <input type="text" id="keywords_input" class="form-control rounded-3" placeholder="<?= e(trans('keywords_tags_hint')) ?>">
        <div id="keywords_tags" class="mt-2 d-flex flex-wrap gap-2"></div>
    </div>

    <div class="col-md-6">
        <label class="form-label"><?= e(trans('keywords_ar')) ?> *</label>
        <input type="hidden" name="keywords_ar" id="keywords_ar">
        <input type="text" id="keywords_ar_input" class="form-control rounded-3" placeholder="<?= e(trans('keywords_tags_hint')) ?>">
        <div id="keywords_ar_tags" class="mt-2 d-flex flex-wrap gap-2"></div>
    </div>

    <div class="col-md-6">
        <label class="form-label"><?= e(trans('file_category')) ?></label>
        <select name="main_file_category" class="form-select rounded-3">
            <option value="abstract"><?= e(trans('abstract_file')) ?></option>
            <option value="full_article" selected><?= e(trans('full_article_file')) ?></option>
            <option value="revised_version"><?= e(trans('revised_version_file')) ?></option>
            <option value="receipt"><?= e(trans('receipt_file')) ?></option>
            <option value="galley"><?= e(trans('galley_file')) ?></option>
            <option value="supplementary"><?= e(trans('supplementary_file')) ?></option>
            <option value="camera_ready"><?= e(trans('camera_ready_file')) ?></option>
            <option value="other"><?= e(trans('other_file')) ?></option>
        </select>
    </div>

    <div class="col-md-6">
        <label class="form-label"><?= e(trans('main_file')) ?></label>
        <input type="file" name="main_file" class="form-control rounded-3" <?= $srow ? '' : 'required' ?>>
    </div>
</div>

<hr class="my-4">

<div class="d-flex justify-content-between align-items-center mb-3">
    <h3 class="h5 fw-bold mb-0"><?= e(trans('upload_files')) ?></h3>
</div>

<div id="extra-files-wrapper">
    <div class="border rounded-4 p-3 mb-3 extra-file-row">
        <div class="row g-3">
            <div class="col-md-4">
                <label class="form-label"><?= e(trans('file_category')) ?></label>
                <select name="extra_file_category[]" class="form-select rounded-3">
                    <option value="abstract"><?= e(trans('abstract_file')) ?></option>
                    <option value="full_article"><?= e(trans('full_article_file')) ?></option>
                    <option value="revised_version"><?= e(trans('revised_version_file')) ?></option>
                    <option value="receipt"><?= e(trans('receipt_file')) ?></option>
                    <option value="galley"><?= e(trans('galley_file')) ?></option>
                    <option value="supplementary"><?= e(trans('supplementary_file')) ?></option>
                    <option value="camera_ready"><?= e(trans('camera_ready_file')) ?></option>
                    <option value="other"><?= e(trans('other_file')) ?></option>
                </select>
            </div>
            <div class="col-md-8">
                <label class="form-label"><?= e(trans('submission_files')) ?></label>
                <input type="file" name="extra_file[]" class="form-control rounded-3">
            </div>
        </div>
    </div>
</div>

<button type="button" class="btn btn-outline-dark rounded-3 mb-4" onclick="addExtraFileRow()">
    <i class="fa-solid fa-plus me-1"></i><?= e(trans('add_file')) ?>
</button>

<hr class="my-4">

<div class="d-flex justify-content-between align-items-center mb-3">
    <h3 class="h5 fw-bold mb-0"><?= e(trans('authors')) ?></h3>
</div>

<div id="authors-wrapper">
    <?php foreach ($authorsRows as $i => $a): ?>
        <div class="border rounded-4 p-3 mb-3 author-row">
            <div class="row g-3">
                <div class="col-md-4">
                    <label class="form-label"><?= e(trans('author_name')) ?></label>
                    <input type="text" name="author_name[]" class="form-control rounded-3" value="<?= e((string) ($a['full_name'] ?? '')) ?>">
                </div>
                <div class="col-md-3">
                    <label class="form-label"><?= e(trans('author_email')) ?></label>
                    <input type="email" name="author_email[]" class="form-control rounded-3" value="<?= e((string) ($a['email'] ?? '')) ?>">
                </div>
                <div class="col-md-3">
                    <label class="form-label"><?= e(trans('affiliation')) ?></label>
                    <input type="text" name="author_affiliation[]" class="form-control rounded-3" value="<?= e((string) ($a['affiliation'] ?? '')) ?>">
                </div>
                <div class="col-md-2">
                    <label class="form-label"><?= e(trans('country')) ?></label>
                    <input type="text" name="author_country[]" class="form-control rounded-3" value="<?= e((string) ($a['country'] ?? '')) ?>">
                </div>
            </div>
        </div>
    <?php endforeach; ?>
</div>

<button type="button" class="btn btn-outline-primary rounded-3" onclick="addAuthorRow()"><i class="fa-solid fa-plus me-1"></i><?= e(trans('add_author')) ?></button>

<?php require __DIR__ . '/../custom_fields/render.php'; ?>

<script>
function addAuthorRow() {
    const wrapper = document.getElementById('authors-wrapper');
    const html = `
        <div class="border rounded-4 p-3 mb-3 author-row">
            <div class="row g-3">
                <div class="col-md-4"><label class="form-label"><?= e(trans('author_name')) ?></label><input type="text" name="author_name[]" class="form-control rounded-3"></div>
                <div class="col-md-3"><label class="form-label"><?= e(trans('author_email')) ?></label><input type="email" name="author_email[]" class="form-control rounded-3"></div>
                <div class="col-md-3"><label class="form-label"><?= e(trans('affiliation')) ?></label><input type="text" name="author_affiliation[]" class="form-control rounded-3"></div>
                <div class="col-md-2"><label class="form-label"><?= e(trans('country')) ?></label><input type="text" name="author_country[]" class="form-control rounded-3"></div>
            </div>
        </div>
    `;
    wrapper.insertAdjacentHTML('beforeend', html);
}

function initTagInput(textId, inputId, tagsId, initial) {
    const hidden = document.getElementById(textId);
    const input = document.getElementById(inputId);
    const tagsBox = document.getElementById(tagsId);
    let tags = initial ? initial.split(',').map(v => v.trim()).filter(Boolean) : [];

    function render() {
        tagsBox.innerHTML = '';
        tags.forEach((tag, idx) => {
            const span = document.createElement('span');
            span.className = 'badge bg-primary';
            span.style.cursor = 'pointer';
            span.textContent = tag + ' ×';
            span.onclick = () => {
                tags.splice(idx, 1);
                hidden.value = tags.join(', ');
                render();
            };
            tagsBox.appendChild(span);
        });
        hidden.value = tags.join(', ');
    }

    input.addEventListener('keydown', function(e) {
        if (e.key === 'Enter') {
            e.preventDefault();
            const val = input.value.trim();
            if (val && !tags.includes(val)) {
                tags.push(val);
                input.value = '';
                render();
            }
        }
    });

    render();
}

function addExtraFileRow() {
    const wrapper = document.getElementById('extra-files-wrapper');
    const html = `
        <div class="border rounded-4 p-3 mb-3 extra-file-row">
            <div class="row g-3">
                <div class="col-md-4">
                    <label class="form-label"><?= e(trans('file_category')) ?></label>
                    <select name="extra_file_category[]" class="form-select rounded-3">
                        <option value="abstract"><?= e(trans('abstract_file')) ?></option>
                        <option value="full_article"><?= e(trans('full_article_file')) ?></option>
                        <option value="revised_version"><?= e(trans('revised_version_file')) ?></option>
                        <option value="receipt"><?= e(trans('receipt_file')) ?></option>
                        <option value="galley"><?= e(trans('galley_file')) ?></option>
                        <option value="supplementary"><?= e(trans('supplementary_file')) ?></option>
                        <option value="camera_ready"><?= e(trans('camera_ready_file')) ?></option>
                        <option value="other"><?= e(trans('other_file')) ?></option>
                    </select>
                </div>
                <div class="col-md-8">
                    <label class="form-label"><?= e(trans('submission_files')) ?></label>
                    <input type="file" name="extra_file[]" class="form-control rounded-3">
                </div>
            </div>
        </div>
    `;
    wrapper.insertAdjacentHTML('beforeend', html);
}

document.addEventListener('DOMContentLoaded', function () {
    initTagInput('keywords', 'keywords_input', 'keywords_tags', <?= json_encode((string) sub_val($srow, 'keywords')) ?>);
    initTagInput('keywords_ar', 'keywords_ar_input', 'keywords_ar_tags', <?= json_encode((string) sub_val($srow, 'keywords_ar')) ?>);
});
</script>
