<?php
defined('BASE_PATH') || exit;
/** @var array $poll */
/** @var array $options */
/** @var array $responses */
/** @var array $stats */
/** @var array|null $mine */
/** @var string $organizer */
/** @var string $prefillName */
/** @var string $prefillEmail */
/** @var bool $emailLocked */
/** @var string $inviteToken */
/** @var bool $closed */
/** @var bool $isOwner */
/** @var string|null $flashOk */
/** @var string|null $flashErr */
$appName     = Helper::getSetting('app_name', 'Plenvo');
$finalized   = !empty($poll['final_option_id']);
$expired     = !$finalized && !empty($poll['deadline']) && (int)$poll['deadline'] < time();
$allowMaybe  = (int)$poll['allow_maybe'] === 1;
$hideResults = (int)$poll['hide_results'] === 1 && !$isOwner;
$counts      = $stats['counts'];
$bestIds     = $stats['best_ids'];
$finalOption = null;
foreach ($options as $o) { if ((int)$o['id'] === (int)$poll['final_option_id']) { $finalOption = $o; break; } }
$myVotes = $mine['votes'] ?? [];
?>
<!DOCTYPE html>
<html lang="<?= Lang::current() ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= Helper::e($poll['title']) ?> – <?= Helper::e($appName) ?></title>
<link rel="stylesheet" href="<?= url('/assets/css/app.css') ?>?v=<?= APP_VERSION ?>">
<link rel="icon" href="<?= url('/assets/img/favicon.svg') ?>" type="image/svg+xml">
<meta name="robots" content="noindex,nofollow">
<style>
body{margin:0;background:var(--bg-base,#f1f5f9);color:var(--text-primary,#0f172a);font-family:var(--font,system-ui,-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif)}
.mpx-header{background:var(--bg-card,#fff);border-bottom:1px solid var(--border-color,#e2e8f0);padding:.75rem 1.25rem;display:flex;align-items:center;gap:.6rem}
.mpx-header .mpx-logo{display:flex;align-items:center;gap:.5rem;font-weight:700;color:var(--text-primary,#0f172a);text-decoration:none}
.mpx-header .mpx-spacer{flex:1}
.mpx-header a.mpx-app-link{font-size:.85rem;color:var(--color-primary,#4f46e5);text-decoration:none}
.mpx-wrap{max-width:920px;margin:1.5rem auto;padding:0 1rem}
.mpx-card{background:var(--bg-card,#fff);border:1px solid var(--border-color,#e2e8f0);border-radius:var(--border-radius-lg,12px);padding:1.3rem 1.5rem;margin-bottom:1.25rem;box-shadow:var(--shadow-sm,0 1px 2px rgba(0,0,0,.05))}
.mpx-title{margin:0 0 .35rem;font-size:1.4rem;color:var(--text-primary,#0f172a)}
.mpx-meta{color:var(--text-secondary,#475569);font-size:.88rem;margin:.15rem 0}
.mpx-desc{white-space:pre-wrap;margin:.7rem 0 0;color:var(--text-secondary,#475569)}
.mpx-banner{border-radius:var(--border-radius,8px);padding:.85rem 1.1rem;margin-bottom:1.25rem;display:flex;gap:.7rem;align-items:flex-start;font-size:.92rem}
.mpx-banner.ok{background:rgba(5,150,105,.12);color:#047857;border:1px solid rgba(5,150,105,.35)}
.mpx-banner.muted{background:var(--bg-table-head,#f8fafc);color:var(--text-secondary,#475569);border:1px solid var(--border-color,#e2e8f0)}
.mpx-banner.err{background:rgba(220,38,38,.1);color:#b91c1c;border:1px solid rgba(220,38,38,.35)}
.mpx-section-h{font-size:1.05rem;margin:0 0 .9rem;color:var(--text-primary,#0f172a)}
.mpx-scroll{overflow-x:auto;border:1px solid var(--border-color,#e2e8f0);border-radius:var(--border-radius,8px)}
table.mpx-grid{border-collapse:collapse;width:100%;font-size:.9rem}
table.mpx-grid th,table.mpx-grid td{padding:.6rem .75rem;text-align:center;border-bottom:1px solid var(--border-color,#e2e8f0);border-right:1px solid var(--border-color,#e2e8f0)}
table.mpx-grid th:last-child,table.mpx-grid td:last-child{border-right:0}
table.mpx-grid tfoot td{border-bottom:0}
table.mpx-grid thead th{background:var(--bg-table-head,#f8fafc);font-weight:600;color:var(--text-secondary,#475569);font-size:.78rem;position:relative}
table.mpx-grid tbody tr:hover td{background:var(--bg-hover,#f8fafc)}
table.mpx-grid .mpx-name{text-align:left;min-width:140px;background:var(--bg-table-head,#f8fafc);font-weight:600;color:var(--text-primary,#0f172a)}
table.mpx-grid tbody tr:hover .mpx-name{background:var(--bg-hover,#f8fafc)}
table.mpx-grid .mpx-cmt{font-weight:400;font-size:.72rem;color:var(--text-muted,#94a3b8);font-style:italic;margin-top:2px}
table.mpx-grid .c-yes{color:#16a34a;font-weight:700;font-size:1.1rem;line-height:1}
table.mpx-grid .c-maybe{color:#d97706;font-weight:700;font-size:1.1rem;line-height:1}
table.mpx-grid .c-no{color:#cbd5e1;font-weight:500;font-size:1.1rem;line-height:1}
table.mpx-grid thead th.mpx-best{background:linear-gradient(180deg,rgba(20,184,166,.10) 0%,rgba(20,184,166,.20) 100%) !important;color:#0f766e}
table.mpx-grid thead th.mpx-best .od{color:#0f766e}
table.mpx-grid thead th.mpx-best::after{content:'★';position:absolute;top:5px;right:8px;font-size:.7rem;color:#14b8a6}
table.mpx-grid tbody td.mpx-best{background:linear-gradient(180deg,rgba(20,184,166,.04) 0%,rgba(20,184,166,.10) 100%)}
table.mpx-grid tbody tr:hover td.mpx-best{background:linear-gradient(180deg,rgba(20,184,166,.10) 0%,rgba(20,184,166,.16) 100%)}
table.mpx-grid tfoot td{background:var(--bg-table-head,#f8fafc);font-weight:600;color:var(--text-secondary,#475569)}
table.mpx-grid tfoot td.mpx-best{background:linear-gradient(180deg,rgba(20,184,166,.10) 0%,rgba(20,184,166,.18) 100%);color:#0f766e}
table.mpx-grid .od{font-size:.83rem;font-weight:700;color:var(--text-primary,#0f172a)}
table.mpx-grid .ot{font-size:.72rem;color:var(--text-muted,#94a3b8);font-weight:400;margin-top:1px}
table.mpx-grid .of{display:inline-block;margin-top:5px;font-size:.6rem;font-weight:700;text-transform:uppercase;letter-spacing:.05em;color:#fff;background:linear-gradient(135deg,#14b8a6,#0ea5e9);padding:2px 7px;border-radius:999px}
[data-theme-mode="dark"] table.mpx-grid .c-yes{color:#34d399}
[data-theme-mode="dark"] table.mpx-grid .c-maybe{color:#fbbf24}
[data-theme-mode="dark"] table.mpx-grid .c-no{color:#475569}
[data-theme-mode="dark"] table.mpx-grid thead th.mpx-best,
[data-theme-mode="dark"] table.mpx-grid tfoot td.mpx-best{color:#5eead4}
[data-theme-mode="dark"] table.mpx-grid thead th.mpx-best .od{color:#5eead4}
.mpx-vote-opt{display:flex;align-items:center;gap:.7rem;flex-wrap:wrap;padding:.6rem .15rem;border-bottom:1px solid var(--border-color,#e2e8f0)}
.mpx-vote-opt:last-child{border-bottom:0}
.mpx-vote-opt .lbl{flex:1;min-width:170px}
.mpx-vote-opt .lbl .od{font-weight:600;color:var(--text-primary,#0f172a)}
.mpx-vote-opt .lbl .ot{font-size:.78rem;color:var(--text-muted,#94a3b8)}
.mpx-choices{display:inline-flex;border:1px solid var(--border-color,#cbd5e1);border-radius:var(--border-radius,8px);overflow:hidden}
.mpx-choices label{position:relative;cursor:pointer}
.mpx-choices label input{position:absolute;opacity:0;width:0;height:0}
.mpx-choices label span{display:block;padding:.42rem .85rem;font-size:.85rem;background:var(--bg-card,#fff);color:var(--text-secondary,#475569);border-right:1px solid var(--border-color,#e2e8f0);transition:background .12s}
.mpx-choices label:last-child span{border-right:0}
.mpx-choices label span:hover{background:var(--bg-hover,#f1f5f9)}
.mpx-choices label.yes input:checked ~ span{background:var(--color-success,#059669);color:#fff}
.mpx-choices label.maybe input:checked ~ span{background:var(--color-warning,#d97706);color:#fff}
.mpx-choices label.no input:checked ~ span{background:#94a3b8;color:#fff}
.mpx-choices label input:focus-visible ~ span{outline:2px solid var(--color-primary,#4f46e5);outline-offset:-2px}
.mpx-input{width:100%;box-sizing:border-box;padding:.5rem .65rem;border:1px solid var(--border-color,#cbd5e1);border-radius:var(--border-radius-sm,6px);font:inherit;color:var(--text-primary,#0f172a);background:var(--bg-card,#fff)}
.mpx-input:focus{outline:2px solid var(--color-primary,#4f46e5);outline-offset:-1px;border-color:var(--color-primary,#4f46e5)}
.mpx-input[readonly]{background:var(--bg-table-head,#f8fafc);color:var(--text-secondary,#475569)}
.mpx-row2{display:flex;gap:1rem;flex-wrap:wrap}
.mpx-row2 > div{flex:1;min-width:220px}
.mpx-label{display:block;font-size:.82rem;color:var(--text-secondary,#475569);margin:.25rem 0;font-weight:600}
.mpx-btn{background:var(--color-primary,#4f46e5);color:#fff;border:0;border-radius:var(--border-radius,8px);padding:.62rem 1.25rem;font:inherit;font-weight:600;cursor:pointer;transition:background .12s}
.mpx-btn:hover{background:var(--color-primary-dark,#4338ca)}
.mpx-foot{text-align:center;color:var(--text-muted,#94a3b8);font-size:.78rem;margin:2rem 0 1rem}
.mpx-foot a{color:var(--text-muted,#94a3b8)}
</style>
</head>
<body>
<header class="mpx-header">
    <a href="<?= url('/') ?>" class="mpx-logo">
        <svg viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg" width="26" height="26"><rect width="40" height="40" rx="9" fill="#4f46e5"/><path d="M10 14h20M10 20h14M10 26h18" stroke="white" stroke-width="2.5" stroke-linecap="round"/></svg>
        <span><?= Helper::e($appName) ?></span>
    </a>
    <div class="mpx-spacer"></div>
    <?php if (Auth::check()): ?>
        <?php if ($isOwner): ?><a class="mpx-app-link" href="<?= url('/polls/' . (int)$poll['id']) ?>"><?= t('mp_manage_poll') ?></a><?php else: ?><a class="mpx-app-link" href="<?= url('/dashboard') ?>"><?= t('dashboard') ?></a><?php endif; ?>
    <?php endif; ?>
</header>

<div class="mpx-wrap">

    <?php if ($flashOk): ?><div class="mpx-banner ok">✓ <?= Helper::e($flashOk) ?></div><?php endif; ?>
    <?php if ($flashErr): ?><div class="mpx-banner err">⚠ <?= Helper::e($flashErr) ?></div><?php endif; ?>

    <div class="mpx-card">
        <h1 class="mpx-title"><?= Helper::e($poll['title']) ?></h1>
        <?php if ($organizer !== ''): ?><p class="mpx-meta"><?= t('mp_organized_by', ['name' => $organizer]) ?></p><?php endif; ?>
        <?php if (!empty($poll['location'])): ?><p class="mpx-meta"><strong><?= t('mp_location') ?>:</strong> <?= Helper::e($poll['location']) ?></p><?php endif; ?>
        <?php if (!empty($poll['deadline']) && !$expired): ?><p class="mpx-meta"><?= t('mp_meta_deadline', ['date' => MeetingPollController::fmtTs($poll['deadline'])]) ?></p><?php endif; ?>
        <?php if (!empty($poll['description'])): ?><div class="mpx-desc"><?= Helper::e($poll['description']) ?></div><?php endif; ?>
    </div>

    <?php if ($finalized && $finalOption): ?>
    <div class="mpx-banner ok">
        <span style="font-size:1.1rem">✓</span>
        <div><strong><?= t('mp_finalized_to') ?>:</strong> <?= Helper::e(MeetingPollController::optionLabel($finalOption)) ?><?php if (!empty($poll['location'])): ?> · <?= Helper::e($poll['location']) ?><?php endif; ?></div>
    </div>
    <?php elseif ($expired): ?>
    <div class="mpx-banner muted"><span>⏱</span><div><?= t('mp_voting_closed') ?></div></div>
    <?php endif; ?>

    <?php if (empty($options)): ?>
    <div class="mpx-card"><p style="margin:0;color:#6b7280"><?= t('mp_no_options_yet') ?></p></div>
    <?php else: ?>

    <?php if (!$hideResults): ?>
    <div class="mpx-card">
        <h2 class="mpx-section-h"><?= t('mp_results') ?> <span style="color:#6b7280;font-weight:400;font-size:.85rem">(<?= t('mp_meta_responses', ['count' => (string)(int)$stats['total']]) ?>)</span></h2>
        <div class="mpx-scroll">
        <table class="mpx-grid">
            <thead>
                <tr>
                    <th class="mpx-name"><?= t('mp_participant') ?></th>
                    <?php foreach ($options as $o): $oid=(int)$o['id']; ?>
                    <th class="<?= in_array($oid,$bestIds,true)?'mpx-best':'' ?>">
                        <div class="od"><?= Helper::e(MeetingPollController::optionDate($o)) ?></div>
                        <div class="ot"><?= ($tt=MeetingPollController::optionTime($o))!==null?Helper::e($tt):t('mp_all_day') ?></div>
                        <?php if ($finalized && $oid===(int)$poll['final_option_id']): ?><div class="of">✓ <?= t('mp_status_finalized') ?></div><?php endif; ?>
                    </th>
                    <?php endforeach; ?>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($responses)): ?>
                <tr><td colspan="<?= count($options)+1 ?>" style="padding:1.1rem;color:#6b7280"><?= t('mp_no_responses') ?></td></tr>
                <?php endif; ?>
                <?php foreach ($responses as $r): ?>
                <tr>
                    <td class="mpx-name"><?= Helper::e($r['name']) ?><?php if (!empty($r['comment'])): ?><div class="mpx-cmt">„<?= Helper::e($r['comment']) ?>"</div><?php endif; ?></td>
                    <?php foreach ($options as $o): $oid=(int)$o['id']; $v=$r['votes'][$oid]??'no'; ?>
                    <td class="c-<?= Helper::e($v) ?> <?= in_array($oid,$bestIds,true)?'mpx-best':'' ?>"><?= $v==='yes'?'●':($v==='maybe'?'?':'○') ?></td>
                    <?php endforeach; ?>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr>
                    <td class="mpx-name"><?= t('mp_total') ?></td>
                    <?php foreach ($options as $o): $oid=(int)$o['id']; $c=$counts[$oid]; ?>
                    <td class="<?= in_array($oid,$bestIds,true)?'mpx-best':'' ?>"><span style="color:#16a34a;font-weight:800">● <?= (int)$c['yes'] ?></span><?php if ($allowMaybe): ?> <span style="color:#d97706;font-weight:700;margin-left:.5rem">? <?= (int)$c['maybe'] ?></span><?php endif; ?></td>
                    <?php endforeach; ?>
                </tr>
            </tfoot>
        </table>
        </div>
    </div>
    <?php endif; ?>

    <?php if (!$closed): ?>
    <div class="mpx-card">
        <h2 class="mpx-section-h"><?= $mine ? t('mp_edit_your_response') : t('mp_your_response') ?></h2>
        <form method="POST" action="<?= url('/poll/' . Helper::e($poll['token']) . '/vote') ?>">
            <?= Helper::csrf() ?>
            <?php if ($inviteToken !== ''): ?><input type="hidden" name="invite_token" value="<?= Helper::e($inviteToken) ?>"><?php endif; ?>
            <?php if (Auth::check()): ?>
                <p class="mpx-meta" style="margin-bottom:.6rem"><?= t('mp_voting_as', ['name' => (string)(Auth::user()['username'] ?? '')]) ?></p>
            <?php else: ?>
                <div class="mpx-row2" style="margin-bottom:.6rem">
                    <div>
                        <label class="mpx-label" for="mpx-name"><?= t('mp_your_name') ?> <span style="color:#dc2626">*</span></label>
                        <input class="mpx-input" type="text" id="mpx-name" name="name" maxlength="80" required value="<?= Helper::e($prefillName) ?>">
                    </div>
                    <div>
                        <label class="mpx-label" for="mpx-email"><?= t('mp_your_email') ?><?php if ($emailLocked): ?> <span style="color:#dc2626">*</span><?php endif; ?></label>
                        <input class="mpx-input" type="email" id="mpx-email" name="email" maxlength="200" value="<?= Helper::e($prefillEmail) ?>"
                               <?= $emailLocked ? 'required readonly' : ('placeholder="' . t('mp_email_ph') . '"') ?>>
                        <?php if ($emailLocked): ?><small style="color:#6b7280;font-size:.75rem"><?= t('mp_email_from_invite') ?></small><?php endif; ?>
                    </div>
                </div>
            <?php endif; ?>

            <div style="margin:.4rem 0 .8rem">
                <?php foreach ($options as $o): $oid=(int)$o['id']; $cur=$myVotes[$oid] ?? 'no'; if ($cur==='maybe' && !$allowMaybe) $cur='no'; ?>
                <div class="mpx-vote-opt">
                    <div class="lbl"><span class="od"><?= Helper::e(MeetingPollController::optionDate($o)) ?></span> <span class="ot"><?= ($tt=MeetingPollController::optionTime($o))!==null?Helper::e($tt):t('mp_all_day') ?></span></div>
                    <div class="mpx-choices">
                        <label class="yes"><input type="radio" name="vote_<?= $oid ?>" value="yes" <?= $cur==='yes'?'checked':'' ?>><span>✓ <?= t('mp_yes') ?></span></label>
                        <?php if ($allowMaybe): ?><label class="maybe"><input type="radio" name="vote_<?= $oid ?>" value="maybe" <?= $cur==='maybe'?'checked':'' ?>><span>? <?= t('mp_maybe') ?></span></label><?php endif; ?>
                        <label class="no"><input type="radio" name="vote_<?= $oid ?>" value="no" <?= $cur==='no'?'checked':'' ?>><span>✗ <?= t('mp_no') ?></span></label>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>

            <div style="margin-bottom:.9rem">
                <label class="mpx-label" for="mpx-comment"><?= t('mp_comment') ?></label>
                <textarea class="mpx-input" id="mpx-comment" name="comment" rows="2" maxlength="500" placeholder="<?= t('mp_comment_ph') ?>"><?= Helper::e($mine['comment'] ?? '') ?></textarea>
            </div>

            <button type="submit" class="mpx-btn"><?= $mine ? t('mp_update_response') : t('mp_submit_response') ?></button>
        </form>
    </div>
    <?php endif; ?>

    <?php endif; /* options exist */ ?>

    <div class="mpx-foot">
        <?php if (PluginManager::filter('show_powered_by', true)): ?>
        <?= t('powered_by_plenvo') ?>
        <?php else: ?>
        <?= Helper::e($appName) ?>
        <?php endif; ?>
    </div>
</div>
</body>
</html>
