<?php require __DIR__ . '/../../layouts/header.php'; ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="page-title"><?= e(trans('production')) ?></h1>
        <div class="text-muted"><?= e((string)($submission['submission_no']??'')) ?> — <?= e((string)($submission['title']??'')) ?></div>
    </div>
    <a href="<?= e(base_path('admin/production')) ?>" class="btn ec-btn ec-btn btn-outline-secondary rounded-3"><?= e(trans('back')) ?></a>
</div>

<div class="row g-4">
    <div class="col-lg-6">
        <div class="ec-card  ">
            <div class="ec-card-bd p-4">
                <div class="section-title"><span class="dot"></span><h2 class="h5 mb-0"><?= e(trans('copyediting')) ?></h2></div>

                <form method="post" action="<?= e(base_path('admin/production/assign-copyeditor')) ?>">
                    <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
                    <input type="hidden" name="submission_id" value="<?= e((string)$submission['id']) ?>">
                    <div class="row g-3">
                        <div class="col-md-8">
                            <label class="form-label ec-required"><?= e(trans('copyeditor')) ?></label>
                            <select name="copyeditor_id" class="form-select rounded-3" required>
                                <option value="0">--</option>
                                <?php foreach ($copyeditors as $u): ?>
                                    <option value="<?= e((string)$u['id']) ?>" <?= ($copyedit && (int)$copyedit['copyeditor_id']===(int)$u['id'])?'selected':'' ?>>
                                        <?= e(trim((string)$u['first_name'].' '.(string)$u['last_name'])) ?> — <?= e((string)$u['email']) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label ec-required"><?= e(trans('due_date')) ?></label>
                            <input type="date" name="due_date" class="form-control rounded-3" value="<?= e((string)($copyedit['due_date']??'')) ?>">
                        </div>
                    </div>
                    <div class="mt-3">
                        <button class="btn ec-btn ec-btn btn-outline-primary rounded-3"><?= e(trans('assign_copyeditor')) ?></button>
                    </div>
                </form>

                <?php if ($copyedit): ?>
                <hr>
                <form method="post" action="<?= e(base_path('admin/production/update-copyediting')) ?>">
                    <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
                    <input type="hidden" name="submission_id" value="<?= e((string)$submission['id']) ?>">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label ec-required"><?= e(trans('status')) ?></label>
                            <select name="status" class="form-select rounded-3">
                                <?php foreach (['assigned','in_progress','returned','completed'] as $st): ?>
                                    <option value="<?= e($st) ?>" <?= (($copyedit['status']??'assigned')===$st)?'selected':'' ?>><?= e(trans($st)) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label ec-required"><?= e(trans('copyeditor')) ?></label>
                            <input class="form-control rounded-3" value="<?= e(trim((string)($copyedit['first_name']??'').' '.(string)($copyedit['last_name']??''))) ?>" disabled>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label ec-required">Notes EN</label>
                            <textarea name="notes_en" class="form-control rounded-3 richtext" rows="5"><?= e((string)($copyedit['notes_en']??'')) ?></textarea>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label ec-required">Notes AR</label>
                            <textarea name="notes_ar" class="form-control rounded-3 richtext" rows="5"><?= e((string)($copyedit['notes_ar']??'')) ?></textarea>
                        </div>
                    </div>
                    <div class="mt-3"><button class="btn ec-btn ec-btn btn-success rounded-3"><?= e(trans('save')) ?></button></div>
                </form>
                <?php endif; ?>
            </div>
        </div>

        <div class="ec-card   mt-4">
            <div class="ec-card-bd p-4">
                <div class="section-title"><span class="dot"></span><h2 class="h5 mb-0"><?= e(trans('doi_metadata')) ?></h2></div>

                <form method="post" action="<?= e(base_path('admin/production/doi')) ?>">
                    <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
                    <input type="hidden" name="submission_id" value="<?= e((string)$submission['id']) ?>">

                    <?php $meta = []; if (!empty($doi['meta_json'])) { $meta = json_decode((string)$doi['meta_json'], true) ?: []; } ?>

                    <div class="row g-3">
                        <div class="col-md-6"><label class="form-label ec-required">DOI</label><input name="doi" class="form-control rounded-3" value="<?= e((string)($doi['doi']??'')) ?>"></div>
                        <div class="col-md-6"><label class="form-label ec-required">Landing URL</label><input name="landing_url" class="form-control rounded-3" value="<?= e((string)($doi['landing_url']??'')) ?>"></div>
                        <div class="col-md-6"><label class="form-label ec-required">Publisher</label><input name="publisher_name" class="form-control rounded-3" value="<?= e((string)($doi['publisher_name']??'')) ?>"></div>
                        <div class="col-md-6"><label class="form-label ec-required">Year</label><input type="number" name="year" class="form-control rounded-3" value="<?= e((string)($doi['year']??'')) ?>"></div>

                        <div class="col-md-6"><label class="form-label ec-required">Meta Title EN</label><input name="meta_title_en" class="form-control rounded-3" value="<?= e((string)($meta['title_en']??'')) ?>"></div>
                        <div class="col-md-6"><label class="form-label ec-required">Meta Title AR</label><input name="meta_title_ar" class="form-control rounded-3" value="<?= e((string)($meta['title_ar']??'')) ?>"></div>
                        <div class="col-md-6"><label class="form-label ec-required">Authors</label><input name="meta_authors" class="form-control rounded-3" value="<?= e((string)($meta['authors']??'')) ?>"></div>
                        <div class="col-md-6"><label class="form-label ec-required">Keywords</label><input name="meta_keywords" class="form-control rounded-3" value="<?= e((string)($meta['keywords']??'')) ?>"></div>

                        <div class="col-md-12">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" name="is_ready" <?= !empty($doi['is_ready'])?'checked':'' ?>>
                                <label class="form-check-label"><?= e(trans('ready')) ?></label>
                            </div>
                        </div>
                    </div>
                    <div class="mt-3"><button class="btn ec-btn ec-btn btn-outline-success rounded-3"><?= e(trans('save')) ?></button></div>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-6">
        <div class="ec-card  ">
            <div class="ec-card-bd p-4">
                <div class="section-title"><span class="dot"></span><h2 class="h5 mb-0"><?= e(trans('galleys')) ?></h2></div>

                <form method="post" action="<?= e(base_path('admin/production/upload-galley')) ?>" enctype="multipart/form-data">
                    <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
                    <input type="hidden" name="submission_id" value="<?= e((string)$submission['id']) ?>">

                    <div class="row g-3">
                        <div class="col-md-4"><label class="form-label ec-required">Label</label><input name="label" class="form-control rounded-3" value="PDF Galley"></div>
                        <div class="col-md-4">
                            <label class="form-label ec-required">Lang</label>
                            <select name="lang_code" class="form-select rounded-3">
                                <option value="en">EN</option>
                                <option value="ar">AR</option>
                                <option value="both">Both</option>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label ec-required">Type</label>
                            <select name="file_type" class="form-select rounded-3">
                                <option value="pdf">pdf</option>
                                <option value="html">html</option>
                                <option value="zip">zip</option>
                                <option value="other">other</option>
                            </select>
                        </div>
                        <div class="col-md-12">
                            <label class="form-label ec-required"><?= e(trans('upload_galley')) ?></label>
                            <input type="file" name="galley_file" class="form-control rounded-3" required>
                        </div>
                    </div>

                    <div class="mt-3"><button class="btn ec-btn ec-btn btn-success rounded-3"><?= e(trans('save')) ?></button></div>
                </form>

                <hr>

                <div class="table-responsive">
                    <table class="table ec-table ec-table align-middle mb-0">
                        <thead>
                            <tr>
                                <th>Label</th>
                                <th>Lang</th>
                                <th>Type</th>
                                <th><?= e(trans('status')) ?></th>
                                <th><?= e(trans('actions')) ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($galleys as $g): ?>
                                <tr>
                                    <td><?= e((string)$g['label']) ?></td>
                                    <td><?= e((string)$g['lang_code']) ?></td>
                                    <td><?= e((string)$g['file_type']) ?></td>
                                    <td>
                                        <span class="badge ec-badge ec-badge <?= !empty($g['is_published'])?'bg-success':'bg-secondary' ?>">
                                            <?= e(!empty($g['is_published'])?trans('enabled'):trans('disabled')) ?>
                                        </span>
                                    </td>
                                    <td class="d-flex gap-2 flex-wrap">
                                        <form method="post" action="<?= e(base_path('admin/production/toggle-galley')) ?>">
                                            <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
                                            <input type="hidden" name="submission_id" value="<?= e((string)$submission['id']) ?>">
                                            <input type="hidden" name="galley_id" value="<?= e((string)$g['id']) ?>">
                                            <input type="hidden" name="publish" value="<?= e(!empty($g['is_published'])? '0':'1') ?>">
                                            <button class="btn ec-btn ec-btn btn-sm btn-outline-primary rounded-3">
                                                <?= e(!empty($g['is_published'])?trans('unpublish_galley'):trans('publish_galley')) ?>
                                            </button>
                                        </form>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (empty($galleys)): ?><tr><td colspan="5" class="text-center"><?= e(trans('no_data')) ?></td></tr><?php endif; ?>
                        </tbody>
                    </table>
                </div>

                <div class="mt-3 small text-muted">
                    Files are stored under <code>/storage/production/galleys</code> (relative path in DB).
                </div>
            </div>
        </div>
    </div>
</div>

<?php require __DIR__ . '/../../layouts/footer.php'; ?>
