<?php
require __DIR__ . '/../_boot.php';
require __DIR__ . '/../partials/nav.php';

if (!$user || !\App\Core\Auth::hasPermission('polls.manage', $user)) {
    header('Location: dashboard.php');
    exit;
}
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Member Polls | Pyrk Admin</title>
  <link rel="stylesheet" href="../assets/css/app.css">
  <link rel="stylesheet" href="../assets/css/admin_polls.css?v=6">
</head>
<body>
<?php pyrk_render_nav('admin'); ?>
<main class="container polls-admin-page">
  <section class="page-hero admin-hero enterprise-hero">
    <div><p class="eyebrow">Member feedback</p><h1>Polls & opinions</h1><p class="muted">Ask members about walk timing, dates, locations, routes, and community decisions.</p></div>
    <div class="hero-actions"><button class="btn" type="button" id="newPollBtn">+ New poll</button><a class="btn btn-light" href="../polls.php" target="_blank">Member view</a></div>
  </section>
  <section class="grid four compact-stats">
    <div class="stat stat-pro"><span>Total polls</span><strong id="pollTotal">0</strong></div><div class="stat stat-pro"><span>Active</span><strong id="pollActive">0</strong></div><div class="stat stat-pro"><span>Responses</span><strong id="pollResponses">0</strong></div><div class="stat stat-pro"><span>Closed</span><strong id="pollClosed">0</strong></div>
  </section>
  <section class="panel poll-list-panel">
    <div class="section-head"><div><p class="eyebrow">Poll library</p><h2>Manage polls</h2><p class="muted small">Click New poll to open the full enterprise form.</p></div><div class="inline-controls"><input id="pollSearch" placeholder="Search poll"><select id="pollStatusFilter"><option value="">All status</option><option value="draft">Draft</option><option value="active">Active</option><option value="closed">Closed</option></select></div></div>
    <div id="pollCards" class="poll-card-grid"></div>
  </section>
</main>
<div class="poll-modal" id="pollModal" hidden aria-hidden="true">
  <div class="poll-modal-card" role="dialog" aria-modal="true" aria-labelledby="pollFormTitle">
    <div class="poll-modal-head"><div><p class="eyebrow">Create / edit</p><h2 id="pollFormTitle">Create poll</h2><p class="muted small">Design the poll, choose recipients, then activate it when ready.</p></div><button class="btn btn-light btn-small" type="button" id="closePollModalBtn">Close</button></div>
    <form id="pollForm" class="poll-enterprise-form">
      <input type="hidden" name="id" id="pollId"><input type="hidden" name="member_ids" id="pollMemberIds"><input type="hidden" name="group_ids" id="pollGroupIds">
      <section class="poll-form-section"><p class="eyebrow">Question</p><div class="form-grid two"><label>Poll title<input name="title" id="pollTitle" placeholder="Which timing do you prefer for Sunday walks?" required></label><label>Poll type<select name="poll_type" id="pollType"><option value="general">General</option><option value="timing">Timing</option><option value="date">Date</option><option value="location">Location</option><option value="route">Route / routing</option><option value="other">Other</option></select></label></div><label>Description<textarea name="description" id="pollDescription" placeholder="Explain why you are asking this question."></textarea></label></section>
      <section class="poll-form-section"><p class="eyebrow">Audience</p><div class="form-grid two"><label>Send to<select name="target_scope" id="pollTargetScope"><option value="all_members">All members</option><option value="selected_members">Selected members</option><option value="selected_groups">Selected teams / groups</option><option value="walk_registrants">Members registered for a walk</option></select></label><label>Related walk<select name="walk_id" id="pollWalkId"><option value="">No walk</option></select></label></div><div class="member-picker-panel" id="pollMemberPickerPanel"><div class="member-picker-head"><div><strong>Choose members</strong><span class="muted small">All members are selected by default. Deselect any member you do not want to receive this poll.</span></div><div class="inline-controls"><button class="btn btn-light btn-small" type="button" id="pollSelectAllMembers">Select all</button><button class="btn btn-light btn-small" type="button" id="pollClearMembers">Clear</button></div></div><input id="pollMemberSearch" placeholder="Search member by name, email, or mobile"><div class="selected-member-count" id="pollSelectedMemberCount">0 selected</div><div class="member-picker-list" id="pollMemberList"></div></div>
        <div class="member-picker-panel group-picker-panel" id="pollGroupPickerPanel" hidden>
          <div class="member-picker-head">
            <div>
              <strong>Choose teams / groups</strong>
              <span class="muted small">Members belonging to selected active groups will receive and see this poll.</span>
            </div>
            <div class="inline-controls">
              <button class="btn btn-light btn-small" type="button" id="pollSelectAllGroups">Select all</button>
              <button class="btn btn-light btn-small" type="button" id="pollClearGroups">Clear</button>
            </div>
          </div>
          <input id="pollGroupSearch" placeholder="Search teams / groups">
          <div class="selected-member-count" id="pollSelectedGroupCount">0 selected</div>
          <div class="member-picker-list" id="pollGroupList"></div>
        </div>
      </section>
      <section class="poll-form-section"><p class="eyebrow">Validity</p><div class="form-grid three"><label>Status<select name="status" id="pollStatus"><option value="draft">Draft</option><option value="active">Active / send to members</option><option value="closed">Closed</option></select></label><label>Starts at <small class="muted">dd/mm/yyyy</small><span class="poll-date-field"><input class="pyrk-poll-date-input" name="starts_at" id="pollStartsAt" type="text" inputmode="numeric" placeholder="dd/mm/yyyy" autocomplete="off"><button class="poll-date-trigger" type="button" data-poll-calendar-target="pollStartsAt" data-date-target="pollStartsAt" onclick="window.PyrkPollCalendarHardFix&&window.PyrkPollCalendarHardFix.open('pollStartsAt')" aria-label="Open starts at calendar">📅</button></span></label><label>Ends at <small class="muted">dd/mm/yyyy</small><span class="poll-date-field"><input class="pyrk-poll-date-input" name="ends_at" id="pollEndsAt" type="text" inputmode="numeric" placeholder="dd/mm/yyyy" autocomplete="off"><button class="poll-date-trigger" type="button" data-poll-calendar-target="pollEndsAt" data-date-target="pollEndsAt" onclick="window.PyrkPollCalendarHardFix&&window.PyrkPollCalendarHardFix.open('pollEndsAt')" aria-label="Open ends at calendar">📅</button></span></label></div></section>
      <section class="poll-form-section"><p class="eyebrow">Answer options</p><label>Options <small class="muted">One option per line</small><textarea name="options_text" id="pollOptionsText" required placeholder="7:00 AM&#10;8:00 AM&#10;9:00 AM"></textarea></label><div class="grid two"><label class="nice-check"><input type="checkbox" name="allow_multiple" id="pollAllowMultiple" value="1"> Allow members to choose more than one option</label><label class="nice-check"><input type="checkbox" name="is_anonymous" id="pollAnonymous" value="1" checked> Keep responses anonymous in comments/results</label></div></section>
      <div class="poll-link-box" id="pollLinkBox" hidden><span>Member link</span><input id="pollShareLink" readonly><button class="btn btn-light btn-small" type="button" id="copyPollLinkBtn">Copy</button></div>
      <div class="poll-modal-actions"><p class="notice" id="pollMsg"></p><div class="inline-controls"><button class="btn btn-light" type="button" id="resetPollForm">Clear</button><button class="btn" type="submit">Save poll</button></div></div>
    </form>
  </div>
</div>
<script>window.PYRK_API_BASE='../../api'; window.PYRK_CSRF='<?= htmlspecialchars($csrf) ?>';</script><script src="../assets/js/api.js"></script><script src="../assets/js/admin_polls.js?v=20260701-hide-deleted-walks-final"></script>
<script src="../assets/js/admin_poll_calendar_hard_fix.js?v=2"></script>

<div class="poll-email-modal" id="pollEmailModal" hidden aria-hidden="true">
  <div class="poll-email-card" role="dialog" aria-modal="true" aria-labelledby="pollEmailTitle">
    <div class="poll-email-head">
      <div>
        <p class="eyebrow">Email delivery</p>
        <h2 id="pollEmailTitle">Send poll by email</h2>
        <p class="muted small" id="pollEmailSubtitle">Prepare a professional email invitation for this poll.</p>
      </div>
      <button class="btn btn-light btn-small" type="button" id="closePollEmailModalBtn">Close</button>
    </div>
    <form id="pollEmailForm" class="poll-email-form">
      <input type="hidden" id="pollEmailPollId" name="poll_id">
      <div class="poll-email-summary" id="pollEmailSummary">Loading recipients...</div>
      <label>Subject<input id="pollEmailSubject" name="subject" required></label>
      <label>Message<textarea id="pollEmailMessage" name="message" placeholder="Please share your opinion. Your feedback helps us plan better walks."></textarea></label>
      <label class="nice-check"><input type="checkbox" id="pollEmailResend" name="resend" value="1"> Resend even if this poll was already emailed</label>
      <div class="poll-email-actions">
        <p class="notice" id="pollEmailMsg"></p>
        <div class="inline-controls">
          <button class="btn btn-light" type="button" id="previewPollLinkBtn">Open poll link</button>
          <button class="btn" type="submit" id="sendPollEmailBtn">Send email</button>
        </div>
      </div>
    </form>
  </div>
</div>

</body></html>
