<?php
function prod_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$rows = $rows ?? [];
$galleys = $galleys ?? [];
?>

<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-industry"></i> Production / Galleys</h3>
            <div class="opacity-75">Manage final files: PDF, HTML, XML, EPUB, DOCX, public visibility, and approval.</div>
        </div>
    </div>

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

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>Submission</th>
                        <th>Status</th>
                        <th>Latest <?php echo ec_h(ec_t('admin.production.galley','Galley')); ?></th>
                        <th><?php echo ec_h(ec_t('admin.production.upload_galley','Upload Galley')); ?></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($rows as $r): ?>
                        <tr>
                            <td>
                                <strong><?= prod_h($r['submission_no'] ?? ('#'.$r['id'])) ?></strong>
                                <div class="text-muted small"><?= prod_h($r['display_title']) ?></div>
                            </td>
                            <td>
                                <span class="badge bg-primary"><?= prod_h($r['status']) ?></span>
                                <span class="badge bg-secondary"><?= prod_h($r['stage']) ?></span>
                                <span class="badge bg-info"><?= prod_h($r['production_status']) ?></span>
                            </td>
                            <td>
                                <?php if (!empty($r['file_path'])): ?>
                                    <a target="_blank" href="/easychair/storage/<?= prod_h($r['file_path']) ?>">
                                        <?= prod_h($r['original_filename'] ?: $r['file_path']) ?>
                                    </a>
                                    <div class="small mt-1">
                                        <span class="badge bg-primary"><?= prod_h($r['galley_type']) ?></span>
                                        <span class="badge <?= in_array($r['galley_status'], ['approved','published'], true) ? 'bg-success' : 'bg-warning text-dark' ?>">
                                            <?= prod_h($r['galley_status']) ?>
                                        </span>
                                        <span class="badge <?= (int)$r['is_public'] ? 'bg-success' : 'bg-secondary' ?>">
                                            <?= (int)$r['is_public'] ? 'Public' : 'Private' ?>
                                        </span>
                                    </div>
                                <?php else: ?>
                                    <span class="text-muted">No galley</span>
                                <?php endif; ?>
                            </td>
                            <td style="min-width:520px;">
                                <form method="post" action="/easychair/admin/production/upload" enctype="multipart/form-data" class="row g-1">
                                    <input type="hidden" name="submission_id" value="<?= (int)$r['id'] ?>">

                                    <div class="col-md-2">
                                        <select name="galley_type" class="form-select form-select-sm">
                                            <option value="pdf">PDF</option>
                                            <option value="html">HTML</option>
                                            <option value="xml">XML</option>
                                            <option value="epub">EPUB</option>
                                            <option value="docx">DOCX</option>
                                            <option value="other">Other</option>
                                        </select>
                                    </div>

                                    <div class="col-md-3">
                                        <input name="galley_label_en" class="form-control form-control-sm" placeholder="Label EN">
                                    </div>

                                    <div class="col-md-3">
                                        <input name="galley_label_ar" class="form-control form-control-sm" placeholder="Label AR">
                                    </div>

                                    <div class="col-md-2">
                                        <input type="number" name="sort_order" class="form-control form-control-sm" value="0">
                                    </div>

                                    <div class="col-md-2">
                                        <label class="small">
                                            <input type="checkbox" name="is_public" value="1"> Public
                                        </label>
                                    </div>

                                    <div class="col-md-8">
                                        <input type="file" name="galley_file" class="form-control form-control-sm" required>
                                    </div>

                                    <div class="col-md-4">
                                        <button class="btn btn-sm btn-success w-100"><?php echo ec_h(ec_t('action.upload','Upload')); ?></button>
                                    </div>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$rows): ?>
                        <tr><td colspan="4" class="text-center text-muted py-4">No production-ready submissions found. Accept a paper first.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </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">All Galley Files</h5>

            <div class="table-responsive">

<div class="row mb-4">

<div class="col-md-2">

<div class="card border-0 shadow-sm">
<div class="card-body text-center">

<h2 class="text-secondary">
<?= count($awaitingCameraReady) ?>
</h2>

<div>
Awaiting Camera Ready
</div>

</div>
</div>

</div>

<div class="col-md-2">

<div class="card border-0 shadow-sm">
<div class="card-body text-center">

<h2 class="text-warning">
<?= count($cameraReadyUploaded) ?>
</h2>

<div>
Camera Ready Uploaded
</div>

</div>
</div>

</div>

<div class="col-md-2">

<div class="card border-0 shadow-sm">
<div class="card-body text-center">

<h2 class="text-danger">
<?= count($awaitingApproval) ?>
</h2>

<div>
Awaiting Approval
</div>

</div>
</div>

</div>

<div class="col-md-2">

<div class="card border-0 shadow-sm">
<div class="card-body text-center">

<h2 class="text-success">
<?= count($approvedGalleys) ?>
</h2>

<div>
Approved Galleys
</div>

</div>
</div>

</div>

<div class="col-md-2">

<div class="card border-0 shadow-sm">
<div class="card-body text-center">

<h2 class="text-primary">
<?= count($assignedToIssue) ?>
</h2>

<div>
Assigned To Issue
</div>

</div>
</div>

</div>

<div class="col-md-2">

<div class="card border-0 shadow-sm">
<div class="card-body text-center">

<h2 class="text-dark">
<?= count($publishedArticles) ?>
</h2>

<div>
Published
</div>

</div>
</div>

</div>

</div>
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>ID</th>
                        <th>Submission</th>
                        <th>Label</th>
                        <th>File</th>
                        <th>Status</th>
                        <th>Public</th>
                        <th><?php echo ec_h(ec_t('common.order','Order')); ?></th>
                        <th><?php echo ec_h(ec_t('common.actions','Actions')); ?></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($galleys as $g): ?>
                        <tr>
                            <td><?= (int)$g['id'] ?></td>
                            <td>
                                <strong><?= prod_h($g['submission_no']) ?></strong>
                                <div class="text-muted small"><?= prod_h($g['submission_title']) ?></div>
                            </td>
                            <td>
                                <?= prod_h(($g['galley_label_ar'] ?: $g['galley_label_en']) ?: strtoupper($g['galley_type'])) ?>
                            </td>
                            <td>
                                <a target="_blank" href="/easychair/storage/<?= prod_h($g['file_path']) ?>">
                                    <?= prod_h($g['original_filename']) ?>
                                </a>
                            </td>
                            <td>
                                <span class="badge <?= in_array($g['status'], ['approved','published'], true) ? 'bg-success' : 'bg-warning text-dark' ?>">
                                    <?= prod_h($g['status']) ?>
                                </span>
                            </td>
                            <td>
                                <span class="badge <?= (int)$g['is_public'] ? 'bg-success' : 'bg-secondary' ?>">
                                    <?= (int)$g['is_public'] ? 'Public' : 'Private' ?>
                                </span>
                            </td>
                            <td><?= (int)$g['sort_order'] ?></td>
                            <td>
                                <a class="btn btn-sm btn-success" href="/easychair/admin/production/approve?id=<?= (int)$g['id'] ?>">Approve</a>
                                <a class="btn btn-sm btn-outline-primary" href="/easychair/admin/production/toggle-public?id=<?= (int)$g['id'] ?>">Public</a>
                                <a class="btn btn-sm btn-danger" onclick="return confirm('Delete galley?')" href="/easychair/admin/production/delete?id=<?= (int)$g['id'] ?>"><?php echo ec_h(ec_t('action.delete','Delete')); ?></a>
                            </td>
                        </tr>
                    <?php endforeach; ?>

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

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