<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <title>Medikation – <%= patient.firstname %> <%= patient.lastname %></title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/css/bootstrap.min.css">
</head>
<body class="bg-light">

<%
/* =========================
   HILFSFUNKTION
========================== */
function formatDate(d) {
    return d ? new Date(d).toLocaleDateString("de-DE") : "-";
}
%>

<nav class="navbar navbar-dark bg-dark px-3">
    <span class="navbar-brand">
        💊 Medikation – <%= patient.firstname %> <%= patient.lastname %>
    </span>

    <a href="<%= returnTo === 'overview'
                ? `/patients/${patient.id}/overview`
                : '/patients' %>"
       class="btn btn-outline-light btn-sm">
        Zurück
    </a>
</nav>

<div class="container mt-4">
    <%- include("partials/flash") %>
    <!-- =========================
         FORMULAR (NUR ADMIN)
    ========================== -->
    <% if (user && user.role === 'arzt') { %>

        <div class="card shadow mb-4">
            <div class="card-body">

                <h5 class="mb-3">➕ Medikament hinzufügen</h5>

                <form method="POST"
                      action="/patients/<%= patient.id %>/medications?returnTo=<%= returnTo || '' %>">

                    <div class="mb-3">
                        <label class="form-label">Medikament</label>
                        <select name="medication_variant_id"
                                class="form-select"
                                required>
                            <option value="">Bitte wählen</option>
                            <% meds.forEach(m => { %>
                                <option value="<%= m.id %>">
                                    <%= m.medication %> –
                                    <%= m.form %> –
                                    <%= m.dosage %> –
                                    <%= m.package %>
                                </option>
                            <% }) %>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Dosieranweisung</label>
                        <input type="text"
                               name="dosage_instruction"
                               class="form-control"
                               placeholder="z. B. 1–0–1 nach dem Essen">
                    </div>

                    <div class="row mb-3">
                        <div class="col">
                            <label class="form-label">Startdatum</label>
                            <input type="date"
                                   name="start_date"
                                   class="form-control">
                        </div>
                        <div class="col">
                            <label class="form-label">Enddatum</label>
                            <input type="date"
                                   name="end_date"
                                   class="form-control">
                        </div>
                    </div>

                    <button class="btn btn-primary">
                        💾 Medikament hinzufügen
                    </button>

                </form>
            </div>
        </div>

    <% } else { %>

        <div class="alert alert-info">
            ℹ️ Nur Administratoren dürfen Medikamente eintragen.
        </div>

    <% } %>

    <!-- =========================
         AKTUELLE MEDIKATION
    ========================== -->

    <h4>Aktuelle Medikation</h4>

    <table class="table table-bordered table-sm mt-3">
        <thead class="table-light">
            <tr>
                <th>Medikament</th>
                <th>Dosierung</th>
                <th>Packung</th>
                <th>Anweisung</th>
                <th>Zeitraum</th>
                <% if (user && user.role === 'arzt') { %>
                    <th>Aktionen</th>
                <% } %>
            </tr>
        </thead>

        <tbody>

        <% if (!currentMeds || currentMeds.length === 0) { %>

            <tr>
                <td colspan="6" class="text-center text-muted">
                    Keine Medikation vorhanden
                </td>
            </tr>

        <% } else { %>

            <% currentMeds.forEach(m => { %>
                <tr>
                    <td><%= m.medication %> (<%= m.form %>)</td>
                    <td><%= m.dosage %></td>
                    <td><%= m.package %></td>
                    <td><%= m.dosage_instruction || "-" %></td>
                    <td>
                        <%= formatDate(m.start_date) %> –
                        <%= m.end_date ? formatDate(m.end_date) : "laufend" %>
                    </td>

                    <% if (user && user.role === 'arzt') { %>
                        <td class="d-flex gap-1">

                            <form method="POST"
                                  action="/patient-medications/end/<%= m.id %>?returnTo=<%= returnTo || '' %>">
                                <button class="btn btn-sm btn-warning">
                                    ⏹ Beenden
                                </button>
                            </form>

                            <form method="POST"
                                  action="/patient-medications/delete/<%= m.id %>?returnTo=<%= returnTo || '' %>"
                                  onsubmit="return confirm('Medikation wirklich löschen?')">
                                <button class="btn btn-sm btn-danger">
                                    🗑️ Löschen
                                </button>
                            </form>

                        </td>
                    <% } %>
                </tr>
            <% }) %>

        <% } %>

        </tbody>
    </table>

</div>

</body>
</html>
