<?php

use Lab\Helpers;

require dirname(__DIR__) . '/partials/head.php';
require dirname(__DIR__) . '/partials/studio-nav.php';
$storeName = $stores[$order['store_id'] ?? '']['name'] ?? '—';
?>
<main class="wrap studio">
    <?php require dirname(__DIR__) . '/partials/flash.php'; ?>
    <div class="order-head">
        <div>
            <p class="kicker"><?= Helpers::e(Helpers::channelLabel($order['channel'])) ?></p>
            <h1><?= Helpers::e($order['code']) ?></h1>
            <p class="lede">
                <?= Helpers::e($order['customer_name']) ?> · <?= Helpers::e($order['customer_phone']) ?>
                <?php if ($order['customer_email']): ?> · <?= Helpers::e($order['customer_email']) ?><?php endif; ?>
            </p>
        </div>
        <div class="order-actions">
            <span class="badge badge-<?= Helpers::e($order['status']) ?>"><?= Helpers::e(Helpers::statusLabel($order['status'])) ?></span>
            <strong class="price"><?= Helpers::e(Helpers::euro((int) $order['total_cents'])) ?></strong>
        </div>
    </div>

    <div class="meta-grid">
        <div><span>Παραλαβή</span><b><?= Helpers::e($storeName) ?></b></div>
        <div><span>Τρόπος</span><b><?= ($order['delivery'] ?? '') === 'acs' ? 'ACS' : 'Κατάστημα' ?></b></div>
        <div><span>Καταχώρηση</span><b><?= Helpers::e($order['created_at']) ?></b></div>
        <div><span>Χαρτί</span><b><?= Helpers::e(Helpers::paperLabel((string) ($order['paper'] ?? 'glossy'))) ?> · <?= Helpers::e(Helpers::borderLabel((string) ($order['border'] ?? 'none'))) ?></b></div>
        <div><span>Πληρωμή</span><b><?= Helpers::e(Helpers::paymentLabel((string) ($order['payment'] ?? 'counter'))) ?><?= ! empty($order['rush']) ? ' · Επείγον' : '' ?></b></div>
    </div>
    <?php if (! empty($order['address_line'])): ?>
        <p class="note">Διεύθυνση: <?= Helpers::e($order['address_line']) ?></p>
    <?php endif; ?>
    <?php if (! empty($printDir)): ?>
        <p class="note">Αρχεία εκτύπωσης: <code><?= Helpers::e((string) $printDir) ?></code></p>
    <?php endif; ?>

    <div class="toolbar">
        <form method="POST" action="<?= Helpers::url('/studio/orders/' . $order['id'] . '/export') ?>">
            <input type="hidden" name="csrf_token" value="<?= Helpers::csrfToken() ?>">
            <button class="btn" type="submit">Προετοιμασία εκτύπωσης</button>
        </form>
        <form method="POST" action="<?= Helpers::url('/studio/orders/' . $order['id'] . '/dnp') ?>" class="print-now-form" data-print-now data-printer-map="<?= Helpers::e((string) json_encode($printerMap ?? [], JSON_UNESCAPED_UNICODE)) ?>">
            <input type="hidden" name="csrf_token" value="<?= Helpers::csrfToken() ?>">
            <h3>Εκτύπωση τώρα</h3>
            <?php
            $name = 'printer';
            $selected = '';
            foreach ($printers ?? [] as $printer) {
                if (! empty($printer['default'])) {
                    $selected = (string) $printer['id'];
                    break;
                }
            }
            $networkName = '';
            $networkValue = '';
            require dirname(__DIR__) . '/partials/printer-picker.php';
            ?>
            <p class="muted">Διάσταση</p>
            <div class="size-pick">
                <?php foreach ($sizes ?? [] as $i => $size): ?>
                    <button type="button" class="size-chip<?= $i === 0 ? ' on' : '' ?>" data-size-id="<?= Helpers::e($size['id']) ?>"><?= Helpers::e($size['name']) ?><?= ! empty($size['qty']) ? ' · ' . (int) $size['qty'] : '' ?></button>
                <?php endforeach; ?>
            </div>
            <input type="hidden" name="sku" value="<?= Helpers::e($sizes[0]['id'] ?? 'all') ?>" data-print-sku>
            <button class="btn" type="submit">Εκτύπωση τώρα</button>
        </form>
        <form method="POST" action="<?= Helpers::url('/studio/orders/' . $order['id'] . '/save') ?>">
            <input type="hidden" name="csrf_token" value="<?= Helpers::csrfToken() ?>">
            <button class="btn" type="submit">Αποθήκευση αρχείων</button>
        </form>
        <a class="btn btn-ghost" href="<?= Helpers::url('/studio/orders/' . $order['id'] . '/ticket') ?>" target="_blank">Εκτύπωση φακέλου</a>
        <a class="btn btn-ghost" href="<?= Helpers::url('/studio/orders/' . $order['id'] . '/zip') ?>">Λήψη ZIP</a>
        <?php foreach (['new' => 'Νέα', 'printing' => 'Εκτύπωση', 'ready' => 'Έτοιμη', 'delivered' => 'Παραδόθηκε', 'cancelled' => 'Ακύρωση'] as $st => $label): ?>
            <form method="POST" action="<?= Helpers::url('/studio/orders/' . $order['id'] . '/status') ?>">
                <input type="hidden" name="csrf_token" value="<?= Helpers::csrfToken() ?>">
                <input type="hidden" name="status" value="<?= Helpers::e($st) ?>">
                <button class="btn btn-ghost" type="submit"><?= Helpers::e($label) ?></button>
            </form>
        <?php endforeach; ?>
    </div>

    <div class="job-list">
        <?php foreach ($items as $item): ?>
            <article class="job-card">
                <a class="job-thumb" href="<?= Helpers::url('/studio/orders/' . $order['id'] . '/photo/' . rawurlencode($item['stored_name'])) ?>" target="_blank">
                    <img src="<?= Helpers::url('/studio/orders/' . $order['id'] . '/photo/' . rawurlencode($item['stored_name'])) ?>" alt="">
                </a>
                <div>
                    <strong><?= Helpers::e($item['original_name']) ?></strong>
                    <p class="muted"><?= (int) $item['width'] ?>×<?= (int) $item['height'] ?> · <?= Helpers::e($item['fit'] ?? 'fill') ?> · <?= (int) ($item['rotate'] ?? 0) ?>°</p>
                    <ul>
                        <?php foreach ($item['lines'] as $line): ?>
                            <li><?= Helpers::e($line['name'] ?? $line['sku']) ?> ×<?= (int) $line['qty'] ?> — <?= Helpers::e(Helpers::euro((int) $line['line_cents'])) ?></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            </article>
        <?php endforeach; ?>
    </div>
</main>
<script src="<?= Helpers::url('/assets/studio.js') ?>?v=<?= (int) @filemtime(__DIR__ . '/../../public/assets/studio.js') ?>"></script>
</body>
</html>
