import { h } from "./jsx";
import { Sidebar } from "./components/Sidebar";
import { PaliadinWidget } from "./components/PaliadinWidget";
import { BottomNav } from "./components/BottomNav";
import { Footer } from "./components/Footer";
import { PWAHead } from "./components/PWAHead";

// Interactive instance page. Loads template + instance JSON, renders
// checkboxes, PATCHes /api/checklist-instances/{id} on every toggle.
// Reset button POSTs to .../reset.
export function renderChecklistsInstance(): string {
  return "<!DOCTYPE html>" + (
    <html lang="de">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
        <meta name="theme-color" content="#BFF355" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="default" />
        <PWAHead />
        <title data-i18n="checklisten.instance.title">Checklisten-Instanz &mdash; Paliad</title>
        <link rel="stylesheet" href="/assets/global.css" />
      </head>
      <body className="has-sidebar">
        <Sidebar currentPath="/checklists" />
        <BottomNav currentPath="/checklists" />

        <main>
          <section className="tool-page">
            <div className="container">
              <a href="#" id="instance-back" className="checklist-back">
                <span className="checklist-back-arrow">&larr;</span>
                <span data-i18n="checklisten.instance.back">Zur&uuml;ck zur Vorlage</span>
              </a>

              <div id="instance-loading" className="entity-loading">
                <p data-i18n="checklisten.instance.loading">L&auml;dt&hellip;</p>
              </div>

              <div id="instance-notfound" className="entity-empty" style="display:none">
                <p data-i18n="checklisten.instance.notfound">Instanz nicht gefunden oder keine Berechtigung.</p>
              </div>

              <div id="instance-body" style="display:none">
                <div className="tool-header checklist-detail-header">
                  <div className="checklist-detail-head-row">
                    <div className="checklist-instance-titles">
                      <div className="checklist-instance-name-row">
                        <h1 id="instance-name-display" />
                        <input type="text" id="instance-name-edit" className="entity-title-input" maxLength={200} style="display:none" />
                        <button id="instance-rename-btn" className="btn-icon" type="button" aria-label="Umbenennen" data-i18n-title="checklisten.instance.rename" title="Umbenennen">
                          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                            <path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7" />
                            <path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z" />
                          </svg>
                        </button>
                        <button id="instance-name-save" className="btn-primary btn-cta-lime btn-small" type="button" style="display:none" data-i18n="checklisten.instance.rename.save">Speichern</button>
                      </div>
                      <p className="tool-subtitle" id="instance-template-title">&nbsp;</p>
                      <dl className="checklist-meta" id="instance-meta" />
                    </div>
                    <div className="checklist-actions">
                      <button type="button" id="btn-print" className="btn-ghost" data-i18n="checklisten.print">Drucken</button>
                      <button type="button" id="btn-reset" className="btn-ghost" data-i18n="checklisten.reset">Zur&uuml;cksetzen</button>
                      <button type="button" id="btn-feedback" className="btn-suggest">
                        <span data-i18n="checklisten.feedback.btn">Feedback</span>
                      </button>
                    </div>
                  </div>

                  <div className="checklist-progress">
                    <div className="checklist-progress-bar">
                      <div className="checklist-progress-fill" id="progress-fill" />
                    </div>
                    <span className="checklist-progress-label" id="progress-label">0 / 0</span>
                  </div>
                </div>

                <div id="checklist-groups" className="checklist-groups" />

                <div className="checklist-print-footer">
                  <p className="checklist-disclaimer" data-i18n="checklisten.disclaimer">
                    Hinweis: Diese Checklisten dienen als Ged&auml;chtnisst&uuml;tze und ersetzen keine Pr&uuml;fung im Einzelfall. Ma&szlig;geblich sind die jeweils geltenden Verfahrensregeln.
                  </p>
                </div>
              </div>
            </div>
          </section>
        </main>

        {/* Feedback modal */}
        <div className="modal-overlay" id="feedback-modal" style="display:none">
          <div className="modal-card">
            <div className="modal-header">
              <h2 data-i18n="checklisten.feedback.title">Feedback zur Checkliste</h2>
              <button className="modal-close" id="modal-close" type="button">&times;</button>
            </div>
            <form id="feedback-form">
              <div className="form-field">
                <label htmlFor="feedback-type" data-i18n="checklisten.feedback.type">Art</label>
                <select id="feedback-type" required>
                  <option value="error" data-i18n="checklisten.feedback.error">Fehler gefunden</option>
                  <option value="missing" data-i18n="checklisten.feedback.missing">Fehlender Punkt</option>
                  <option value="suggestion" data-i18n="checklisten.feedback.suggestion">Verbesserungsvorschlag</option>
                  <option value="other" data-i18n="checklisten.feedback.other">Sonstiges</option>
                </select>
              </div>
              <div className="form-field">
                <label htmlFor="feedback-message" data-i18n="checklisten.feedback.message">Nachricht</label>
                <textarea id="feedback-message" rows={4} required />
              </div>
              <div className="form-actions">
                <button type="button" className="btn-cancel" id="modal-cancel" data-i18n="checklisten.feedback.cancel">Abbrechen</button>
                <button type="submit" className="btn-submit" data-i18n="checklisten.feedback.submit">Absenden</button>
              </div>
              <p className="form-msg" id="feedback-msg" />
            </form>
          </div>
        </div>

        <Footer />
        <PaliadinWidget />
        <script src="/assets/checklists-instance.js"></script>
      </body>
    </html>
  );
}
