<?php
function iss_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$issues = $issues ?? [];
$currentIssue = $currentIssue ?? null;
$articles = $articles ?? [];
$eligibleSubmissions = $eligibleSubmissions ?? [];
?>

<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"><i class="fa fa-book-open"></i> Proceedings / Issue</h3>
            <div class="opacity-75">One proceedings record per conference with article ordering, pages, DOI, ISBN/ISSN, and publishing.</div>
        </div>
    </div>

    <div class="row g-4 mb-4">
        <div class="col-lg-5">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-3">Proceedings Metadata</h5>

                    <form method="post" action="/easychair/admin/issues/store" class="row g-2">
                        <div class="col-md-12">
                            <input name="title_ar" class="form-control" placeholder="العنوان العربي" value="<?= iss_h($currentIssue['title_ar'] ?? '') ?>">
                        </div>

                        <div class="col-md-12">
                            <input name="title_en" class="form-control" placeholder="English title" value="<?= iss_h($currentIssue['title_en'] ?? '') ?>">
                        </div>

                        <div class="col-md-4">
                            <input name="volume_no" class="form-control" placeholder="Volume" value="<?= iss_h($currentIssue['volume_no'] ?? '') ?>">
                        </div>

                        <div class="col-md-4">
                            <input name="issue_no" class="form-control" placeholder="Issue" value="<?= iss_h($currentIssue['issue_no'] ?? '') ?>">
                        </div>

                        <div class="col-md-4">
                            <input name="publication_year" class="form-control" placeholder="Year" value="<?= iss_h($currentIssue['publication_year'] ?? date('Y')) ?>">
                        </div>

                        <div class="col-md-6">
                            <input type="date" name="publication_date" class="form-control" value="<?= iss_h($currentIssue['publication_date'] ?? '') ?>">
                        </div>

                        <div class="col-md-6">
                            <input name="doi_prefix" class="form-control" placeholder="DOI Prefix" value="<?= iss_h($currentIssue['doi_prefix'] ?? '') ?>">
                        </div>

                        <div class="col-md-4">
                            <input name="isbn" class="form-control" placeholder="ISBN" value="<?= iss_h($currentIssue['isbn'] ?? '') ?>">
                        </div>

                        <div class="col-md-4">
                            <input name="issn" class="form-control" placeholder="ISSN" value="<?= iss_h($currentIssue['issn'] ?? '') ?>">
                        </div>

                        <div class="col-md-4">
                            <input name="eissn" class="form-control" placeholder="EISSN" value="<?= iss_h($currentIssue['eissn'] ?? '') ?>">
                        </div>

                        <div class="col-md-6">
                            <textarea name="intro_ar" rows="4" class="form-control" placeholder="مقدمة عربية"><?= iss_h($currentIssue['intro_ar'] ?? '') ?></textarea>
                        </div>

                        <div class="col-md-6">
                            <textarea name="intro_en" rows="4" class="form-control" placeholder="English intro"><?= iss_h($currentIssue['intro_en'] ?? '') ?></textarea>
                        </div>

                        <div class="col-12">
                            <button class="btn btn-success w-100 rounded-pill">
                                Save Proceedings Metadata
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-lg-7">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-3">Add Paper to Proceedings</h5>

                    <?php if (!$currentIssue): ?>
                        <div class="alert alert-warning">Please save proceedings metadata first.</div>
                    <?php else: ?>
                        <form method="post" action="/easychair/admin/issues/add-article" class="row g-2">
                            <input type="hidden" name="issue_id" value="<?= (int)$currentIssue['id'] ?>">

                            <div class="col-md-12">
                                <?php if (empty($eligibleSubmissions)): ?>
                                    <div class="alert alert-warning mb-2">
                                        No accepted/production papers are available for this proceedings issue.
                                    </div>
                                <?php else: ?>
                                    <select name="submission_id" class="form-select ec-issue-submission-select" required style="color:#111827;background:#ffffff;">
                                        <option value="" style="color:#111827;background:#ffffff;">Select accepted/production paper</option>
                                        <?php foreach ($eligibleSubmissions as $s): ?>
                                            <?php
                                                $paperLabel =
                                                    trim((string)($s['submission_no'] ?? 'Submission #' . ($s['id'] ?? '')))
                                                    . ' - '
                                                    . trim((string)($s['display_title'] ?? 'Untitled'))
                                                    . ' ['
                                                    . trim((string)($s['galley_status'] ?? 'no approved galley'))
                                                    . ']';
                                            ?>
                                            <option value="<?= (int)$s['id'] ?>" style="color:#111827;background:#ffffff;">
                                                <?= iss_h($paperLabel) ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>

                                    <div class="small text-muted mt-1">
                                        Available papers: <?= count($eligibleSubmissions) ?>
                                    </div>
                                <?php endif; ?>
                            </div>

                            <div class="col-md-3"><input name="page_from" class="form-control" placeholder="Page from"></div>
                            <div class="col-md-3"><input name="page_to" class="form-control" placeholder="Page to"></div>
                            <div class="col-md-2"><input name="article_order" class="form-control" value="1" placeholder="Order"></div>
                            <div class="col-md-4"><input name="doi" class="form-control" placeholder="Article DOI"></div>

                            <div class="col-12">
                                <button class="btn btn-primary w-100 rounded-pill"><?php echo ec_h(ec_t('admin.issues.add_article','Add Article')); ?></button>
                            </div>
                        </form>
                    <?php endif; ?>

                    <?php if ($currentIssue): ?>
                        <hr>
                        <div>
                            <strong>Status:</strong>
                            <span class="badge <?= ($currentIssue['status'] ?? '') === 'published' ? 'bg-success' : 'bg-warning text-dark' ?>">
                                <?= iss_h($currentIssue['status']) ?>
                            </span>

                            <a class="btn btn-sm btn-success ms-2" onclick="return confirm('Publish proceedings and all assigned articles?')" href="/easychair/admin/issues/publish?id=<?= (int)$currentIssue['id'] ?>">
                                Publish Proceedings
                            </a>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold mb-3">Proceedings Articles</h5>

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th><?php echo ec_h(ec_t('common.order','Order')); ?></th>
                        <th>Article</th>
                        <th>Pages</th>
                        <th>DOI</th>
                        <th><?php echo ec_h(ec_t('admin.production.galley','Galley')); ?></th>
                        <th>Action</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($articles as $a): ?>
                        <tr>
                            <td><?= (int)$a['article_order'] ?></td>
                            <td>
                                <strong><?= iss_h($a['submission_no']) ?></strong>
                                <div class="text-muted small"><?= iss_h($a['submission_title']) ?></div>
                            </td>
                            <td><?= iss_h(($a['page_from'] ?? '') . ' - ' . ($a['page_to'] ?? '')) ?></td>
                            <td><?= iss_h($a['doi']) ?></td>
                            <td>
                                <?php if (!empty($a['galley_file'])): ?>
                                    <a target="_blank" href="/easychair/storage/<?= iss_h($a['galley_file']) ?>">
                                        <?= iss_h(($a['galley_label_ar'] ?: $a['galley_label_en']) ?: ($a['galley_name'] ?: 'Download')) ?>
                                    </a>
                                    <span class="badge bg-success"><?= iss_h($a['galley_status']) ?></span>
                                    <?php if ((int)$a['is_public']): ?>
                                        <span class="badge bg-primary">Public</span>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <span class="badge bg-danger">No approved galley</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <a class="btn btn-sm btn-danger" onclick="return confirm('Remove article?')" href="/easychair/admin/issues/article/delete?id=<?= (int)$a['id'] ?>">Remove</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$articles): ?>
                        <tr><td colspan="6" class="text-center text-muted py-4">No proceedings articles yet.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>

        </div>
    </div>
</div>

<style>
.ec-issue-submission-select,
.ec-issue-submission-select option {
    color: #111827 !important;
    background: #ffffff !important;
    opacity: 1 !important;
    visibility: visible !important;
}
</style>
