{% extends "layout.html" %}
{% block css %}
<style>
    #donorsOverrideForm {
        display: flex;
        flex-wrap: wrap;
    }

    #donorsOverrideForm div {
        background-color: #0000000d;
        flex-grow: 1;
        margin-top: 12px;
    }

    #donorsOverrideForm label {
        color: white;
        background-color: #343a40;
        font-weight: bold;
        width: 100%;
        margin: 0;
        padding: 12px;
    }

    #donorsOverrideForm input {
        margin: 6px;
        width: calc(100% - 12px);
    }

    #donorsOverrideForm .btn_container {
        flex-grow: unset;
    }
    #donorsOverrideForm .btn_container label {
        padding: 12px 0;
    }
    #donorsOverrideForm .btn_container button {
        margin: 6px 0;
    }
</style>
{% endblock %}
{% block content %}

<h1>Detail dárce</h1>

<div class="row my-2">
    <div class="col-xs ml-3">
        <h4 class="text-muted border-bottom">Aktuální informace</h4>
        <div class="container">
            <div class="row">
                Rodné číslo: {{ overview.rodne_cislo }}
            </div>
            <div class="row">
                Jméno: {{ overview.first_name|capitalize }}
            </div>
            <div class="row">
                Příjmení: {{ overview.last_name|capitalize }}
            </div>
            <div class="row">
                Adresa: {{ overview.address|capitalize }}
            </div>
            <div class="row">
                Město: {{ overview.city|capitalize }}
            </div>
            <div class="row">
                PSČ: {{ overview.postal_code|postal_code }}
            </div>
            <div class="row">
                Pojišťovna: {{ overview.kod_pojistovny }}
            </div>
            {% if emails %}
                <div class="row">
                    E-mail:
                    {% for email in emails %}
                        <a href="mailto:{{ email }}">{{ email }}</a>
                    {% endfor %}
                </div>
            {% endif %}
        </div>
    </div>

    <div class="col-xs ml-3">
        <h4 class="text-muted border-bottom">Počty darování</h4>

        <div class="container">
            {% for donation_center in donation_centers %}
                <div class="row">
                    {{ donation_center.title }}:&nbsp;{{ overview["donation_count_" + donation_center.slug] }}
                </div>
            {% endfor %}
            <div class="row border-bottom">
                Jinde: {{ overview["donation_count_manual"] }}
            </div>
            <div class="row">
                <b>Celkem: {{ overview.donation_count_total }}</b>
            </div>
            <div class="row">
                <a href="{{ url_for('donor.render_confirmation_document',
                rc=overview.rodne_cislo) }}"
                class="btn btn-primary"
                role="button"
                target="_blank">
                Potvrzení o odběrech
                </a>
            </div>
        </div>
    </div>

    <div class="col-xs ml-3">
        <h4 class="text-muted border-bottom">Získaná ocenění</h4>

        <div class="container">
            {% for medal in all_medals %}
            <div class="row">
                {% if medal.id in awarded_medals.keys() %}
                <div class="col-2">
                    {% with form=remove_medal_form %}
                        <form id="removeMedalForm_{{ medal.id }}"
                            action="{{ url_for('donor.remove_medal') }}"
                            method="POST"
                            class="form-inline"
                            role="form">

                            {{ form.csrf_token }}
                            {{ form.rodne_cislo(value=overview.rodne_cislo, id="rodne_cislo_" ~ overview.rodne_cislo) }}
                            {{ form.medal_id(value=medal.id, id="medal_id_" ~ medal.id) }}
                            <input type="submit" class="btn btn-sm btn-outline-danger" value="🗑" title="Odebrat medaili">

                        </form>
                    {% endwith %}
                </div>
                {% else %}
                <div class="col-2">
                    {% with form=award_medal_form %}
                        <form id="awardMedalForm_{{ medal.id }}"
                            action="{{ url_for('donor.award_medal') }}"
                            method="POST"
                            class="form-inline"
                            role="form">

                            {{ form.csrf_token }}
                            {{ form["rodne_cislo_" + overview.rodne_cislo](value=overview.rodne_cislo) }}
                            {{ form.medal_id(value=medal.id, id="medal_id_" ~ medal.id) }}

                            {% if medal.minimum_donations > overview.donation_count_total %}
                                <input type="submit" class="btn btn-sm btn-outline-warning" value="✔" title="Udělit medaili (Nemá nárok)">
                            {% else %}
                                <input type="submit" class="btn btn-sm btn-outline-success" value="✔" title="Udělit medaili">
                            {% endif %}
                        </form>
                    {% endwith %}
                </div>
                {% endif %}

                <div class="col-5">
                    {{ medal.title }}
                </div>

                <div class="col-1">
                    {% if medal.id in awarded_medals.keys() %}
                    <span data-toggle="modal"
                        data-target="#titleModal"
                        class="btn btn-sm"
                        role="button"
                        title="
                        {%- if awarded_medals[medal.id].awarded_at is not none -%}
                        Medaile udělena {{ awarded_medals[medal.id].awarded_at | format_time }}
                        {%- else -%}
                        Medaile udělena ve starém systému
                        {%- endif -%}
                        ">
                            📅
                        </span>
                        {% endif %}
                </div>
                <div class="col-1">
                    <a href="{{ url_for('donor.render_award_document',
                                        rc=overview.rodne_cislo,
                                        medal_slug=medal.slug) }}"
                        class="btn btn-sm"
                        role="button"
                        aria-pressed="true"
                        target="_blank">
                        🖨️
                    </a>
                </div>
                <div class="col-1">
                    <a href="{{ url_for('donor.render_award_document',
                                        rc=overview.rodne_cislo,
                                        medal_slug=medal.slug, today=1) }}"
                        class="btn btn-sm"
                        role="button"
                        aria-pressed="true"
                        target="_blank">
                        📠
                    </a>
                </div>
                <div class="col-1">
                    {% if emails %}
                    <a href="{{ url_for('donor.email_award_document',
                                        rc=overview.rodne_cislo,
                                        medal_slug=medal.slug) }}"
                        class="btn btn-sm"
                        role="button"
                        aria-pressed="true">
                        📧
                    </a>
                    {% endif %}
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
</div>

<div class="row">
    <div class="col-12 ml-3">
        <h4 class="text-muted">Poznámky</h4>
        {% with form=note_form %}
        <form id="noteForm" action="{{ url_for('donor.save_note') }}"
        method="POST"
        role="form">
            {{ form.csrf_token }}
            {{ form.rodne_cislo(value=overview.rodne_cislo) }}
            {{ form.note(rows=3, class_="form-control") }}
            <div class="d-grid gap-2 d-md-flex justify-content-end">
                <button class="btn btn-success" type="submit">Uložit</button>
            </div>
        </form>
        {% endwith %}
    </div>
</div>

<h2>Přepsání údajů</h2>
{% with form=donors_override_form %}
<form id="donorsOverrideForm" action="/override/" method="POST">
    {{ form.csrf_token }}
    <div style="flex-grow: 8;">
        <label for="rodne_cislo">Rodné číslo</label>
        {{ form.rodne_cislo(readonly=True, size=8, class_="form-control") }}
    </div>
    <div style="flex-grow: 8;">
        <label for="first_name">Jméno</label>
        {{ form.first_name(size=8, class_="form-control") }}
    </div>
    <div style="flex-grow: 8;">
        <label for="last_name">Příjmení</label>
        {{ form.last_name(size=8, class_="form-control") }}
    </div>
    <div style="flex-grow: 10;">
        <label for="address">Adresa</label>
        {{ form.address(size=10, class_="form-control") }}
    </div>
    <div style="flex-grow: 8;">
        <label for="city">Město</label>
        {{ form.city(size=8, class_="form-control") }}
    </div>
    <div style="flex-grow: 3;">
        <label for="postal_code">PSČ</label>
        {{ form.postal_code(size=3, class_="form-control") }}
    </div>
    <div style="flex-grow: 3;">
        <label for="kod_pojistovny">Pojišťovna</label>
        {{ form.kod_pojistovny(size=3, class_="form-control") }}
    </div>

    <div class="btn_container">
        <label>&nbsp;</label>
        <button class="btn btn-success" type="submit" name="save_btn" title="Uložit výjimku">✔</button>
    </div>

    <div class="btn_container">
        <label>&nbsp;</label>
        <button class="btn btn-danger" type="submit" name="delete_btn" title="Smazat výjimku">🗑</button>
    </div>
</form>
{% endwith %}

<h2>Manuální import</h2>

<div class="form-row">
    <div class="form-group col-md-3">
        <form action="{{ url_for('batch.import_data', rodne_cislo=overview.rodne_cislo) }}" method="get" id="manualImportForm">
            <label for="donation_center">Individuální import z:</label>
            <select name="donation_center" id="donation_center" class="form-control">
                {% for donation_center in donation_centers %}
                <option value="{{ donation_center.id }}">{{ donation_center.title }}</option>
                {% endfor %}
                <option value="-1">Manuální import nebo data odjinud</option>
            </select>
            <input type="submit" class="btn btn-primary" value="Připravit">
        </form>
    </div>
</div>

<h2>Historie importů</h2>

<table id="records" class="table table-striped table-hovered table-hover">
    <thead class="thead-dark">
        <th>Dávka</th>
        <th>Importováno</th>
        <th>Centrum</th>
        <th>Rodné číslo</th>
        <th>Jméno</th>
        <th>Příjmení</th>
        <th>Adresa</th>
        <th>Město</th>
        <th>PSČ</th>
        <th>Pojišťovna</th>
        <th>Počet darování</th>
    </thead>
    <tbody>
        {% for record in records %}
        <tr>
            <td><a href="{{ url_for('batch.batch_detail', id=record.batch.id) }}">{{ record.batch_id }}</a></td>
            <td data-sort="{{ record.batch.imported_at }}">{{ record.batch.imported_at|format_time }}</td>
            <td>{{ record.batch.donation_center.title or "Jinde" }}</td>
            <td>{{ record.rodne_cislo }}</td>
            <td>{{ record.first_name }}</td>
            <td>{{ record.last_name }}</td>
            <td>{{ record.address }}</td>
            <td>{{ record.city }}</td>
            <td>{{ record.postal_code|postal_code }}</td>
            <td>{{ record.kod_pojistovny }}</td>
            <td>{{ record.donation_count }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

{% endblock %}

{% block js %}
<script type="text/javascript">
    $(document).ready( function () {
        $.fn.dataTable.ext.order.intl('cs-CZ');

        $('#records').DataTable({
            stateSave: true,
            stateDuration: -1, // -1 means session storage in the current browser window
            language: {
                url: '//cdn.datatables.net/plug-ins/1.10.21/i18n/Czech.json'
            },
            lengthMenu: [
                [10, 25, 50, 100, -1],
                [10, 25, 50, 100, 'Všechny']
            ],
        });
    });
</script>
{% include "title_popup.html" %}
{% endblock %}
