{% extends 'base.html' %}

{% block content %}

<a hx-get="{{ url_for('get_person', person_id=person['PersonId']) }}"
    hx-include="[name='exercise_id'],[name='min_date'],[name='max_date']" hx-target="#container"
    hx-trigger="refreshView" id="refreshViewElement"></a>

<div class="flex justify-center">
    <div class="bg-white shadow rounded-lg p-4 sm:w-full xl:p-8 md:w-full lg:w-11/12">

        <div class="mb-4 flex items-center justify-between px-3">
            <div>
                <h3 class="text-xl font-bold text-gray-900 mb-2">{{ person['PersonName'] }}</h3>
                <span class="text-base font-normal text-gray-500">List of workouts</span>
            </div>
            <div>
                <div>
                    <select name="view"
                        hx-get="{{ url_for('get_calendar', person_id=person['PersonId']) }}" hx-target="#container"
                        hx-push-url="true"
                        _="init js(me) tail.select(me, {}) end">
                        <option value="month">Month</option>
                        <option value="year">Year</option>
                        <option value="all" selected>All</option>
                    </select>
                </div>
            </div>
        </div>


        <div class="flex">
            {% set exercise_list = person['FilteredExercises'] %}

        </div>

        <div class="flex flex-wrap mb-1">
            <div class="w-full md:w-1/4 px-3 mb-6 md:mb-0">
                <div class="mb-1 w-full">
                    <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-city">
                        Exercises
                    </label>
                    <select data-te-select-filter="true"
                        data-te-select-size="lg" name="exercise_id" class="bg-gray-50 border border-gray-300 " multiple
                        hx-get="{{ url_for('get_person', person_id=person['PersonId']) }}"
                        hx-include="[name='exercise_id'],[name='min_date'],[name='max_date'],[name='graph_axis']"
                        hx-target="#container" hx-push-url="true"
                        _="init js(me)
                            tail.select(me, {
                                multiple: true,
                                search: true,
                                placeholder: 'Filter exercises',
                            })
                        end">
                        {% for e in person['Exercises'] %}
                        <option value="{{ e['ExerciseId'] }}" {% if e['ExerciseId'] in selected_exercise_ids %}selected{% endif %}>{{
                            e['ExerciseName']
                            }}</option>
                        {% endfor %}
                    </select>
                </div>
            </div>
            <div class="w-full md:w-1/4 px-3 mb-6 md:mb-0">
                <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-city">
                    Min date
                </label>
                <div class="relative">
                    <div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
                        <svg aria-hidden="true" class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor"
                            viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd"
                                d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z"
                                clip-rule="evenodd"></path>
                        </svg>
                    </div>
                    <input type="date"
                        class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500 w-full"
                        name="min_date" value="{{ min_date }}"
                        hx-get="{{ url_for('get_person', person_id=person['PersonId']) }}"
                        hx-include="[name='exercise_id'],[name='min_date'],[name='max_date'],[name='graph_axis']"
                        hx-target="#container" hx-push-url="true" hx-trigger="change">
                </div>
            </div>
            <div class="w-full md:w-1/4 px-3 mb-6 md:mb-0">
                <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-zip">
                    Max date
                </label>
                <div class="relative">
                    <div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
                        <svg aria-hidden="true" class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor"
                            viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd"
                                d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z"
                                clip-rule="evenodd"></path>
                        </svg>
                    </div>
                    <input type="date"
                        class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500 w-full"
                        name="max_date" value="{{ max_date }}"
                        hx-get="{{ url_for('get_person', person_id=person['PersonId']) }}"
                        hx-include="[name='exercise_id'],[name='min_date'],[name='max_date'],[name='graph_axis']"
                        hx-target="#container" hx-push-url="true" hx-trigger="change">
                </div>
            </div>
            <div class="w-full md:w-1/4 px-3 mb-6 md:mb-0">
                <div class="mb-1 w-full">
                    <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-city">
                        Graph Axis
                    </label>
                    <select name="graph_axis" class="bg-gray-50 border border-gray-300 " multiple
                        hx-get="{{ url_for('get_person', person_id=person['PersonId']) }}"
                        hx-include="[name='exercise_id'],[name='min_date'],[name='max_date'],[name='graph_axis']"
                        hx-target="#container" hx-push-url="true"
                        _="init js(me) tail.select(me, { multiple: true, placeholder: 'Select graphs' }) end">
                        <option value="repetitions"  {% if "repetitions" in graph_axis %}selected{% endif %}>Repetitions</option>
                        <option value="weight"  {% if "weight" in graph_axis %}selected{% endif %}>Weigh</option>
                        <option value="estimated1rm"  {% if "estimated1rm" in graph_axis %}selected{% endif %}>Estimated 1RM</option>
                    </select>
                </div>
            </div>
        </div>

        {% with person_id=person['PersonId'], tags=tags %}
        {% include 'partials/tags.html' %}
        {% endwith %}

        <div class="flex flex-col mt-3 w-screen sm:w-full overflow-auto">
            <div class="overflow-x-auto rounded-lg">
                <div class="flex justify-center">
                    <div class="shadow sm:rounded-lg w-screen sm:w-screen">

                        {% if person['Workouts']|length > 0 %}
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th scope="col"
                                        class="p-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Date
                                    </th>
                                    {% for e in exercise_list %}
                                    <th scope="col"
                                        class="p-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        {{ e['ExerciseName'] }}
                                    </th>
                                    {% endfor %}
                                </tr>
                            </thead>
                            <tbody class="bg-white">

                                {% for w in person['Workouts'] %}
                                <tr hx-get="{{ url_for('get_workout_modal', person_id=person['PersonId'], workout_id=w['WorkoutId']) }}"
                                    hx-target='body' hx-swap='beforeend' class="cursor-pointer">
                                    <td class="p-4 whitespace-nowrap text-sm font-normal text-gray-500">
                                        {{ w['StartDate'] | strftime("%b %d %Y") }}
                                    </td>

                                    {% for e in exercise_list %}
                                    <td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
                                        {% set topset_exercise = w['TopSets'] |
                                        get_first_element_from_list_with_matching_attribute('ExerciseId',
                                        e['ExerciseId']) %}
                                        {% if topset_exercise %}
                                        {{ topset_exercise['Repetitions'] }} x {{ topset_exercise['Weight'] }}kg
                                        {% endif %}
                                    </td>
                                    {% endfor %}
                                </tr>
                                {% endfor %}

                            </tbody>
                        </table>
                        {% endif %}

                        {% if person['Workouts']|length == 0 %}
                        <div class="bg-purple-100 rounded-lg py-5 px-6 mb-4 text-base text-purple-700 mb-3"
                            role="alert">
                            No workouts found.
                        </div>
                        {% endif %}

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

    </div>
</div>

{% if graph_axis %}
<div class="mt-4 mb-4 w-full grid grid-cols-1 2xl:grid-cols-2 gap-4">
    {% for exercise_graph in person['ExerciseGraphs'] %}
    <div class="bg-white shadow rounded-lg p-4 sm:p-6 xl:p-8 ">
        <div class="flex flex-col items-center">
            <div class="flex-shrink-0">
                <span class="text-2xl sm:text-3xl leading-none font-bold text-gray-900">{{
                    exercise_graph['ExerciseName'] }}</span>
            </div>
            <div id="e-{{ exercise_graph['ExerciseId'] }}" class="w-full mt-2 aspect-video"></div>
            <script>
                Plotly.newPlot("e-{{ exercise_graph['ExerciseId'] }}", [
                    {% if 'repetitions' in graph_axis %}
                {
                    x: {{ exercise_graph['StartDates'] | replace('"', "'") | safe }},
                    y: {{ exercise_graph['Repetitions'] | replace('"', "'") | safe }},
                    name: 'Reps',
                        type: 'scatter'
                },
                {% endif %}
                {% if 'weight' in graph_axis %}
                {
                    x: {{ exercise_graph['StartDates'] | replace('"', "'") | safe }},
                    y: {{ exercise_graph['Weights'] | replace('"', "'") | safe }},
                    name: 'Weight',
                        yaxis: 'y2',
                            type: 'scatter'
                },
                {% endif %}
                {% if 'estimated1rm' in graph_axis %}
                {
                    x: {{ exercise_graph['StartDates'] | replace('"', "'") | safe }},
                    y: {{ exercise_graph['Estimated1RM'] | replace('"', "'") | safe }},
                    name: 'Estimated 1RM',
                        yaxis: 'y3',
                            type: 'scatter'
                },
                {% endif %}
            ],
                {
                    margin: {
                        t: 20,  // Increase top margin to move the graph downwards
                        l: 50,  // Increase left margin if the graph is too far to the right
                        r: 50,  // Increase right margin for the same reason
                        b: 40   // Bottom margin for better layout
                    },
                    xaxis: { type: 'date', showgrid: false },
                    yaxis: { title: 'Reps', showgrid: false },
                    yaxis2: {
                        title: 'Weight',
                            overlaying: 'y',
                                side: 'right',
                                    showgrid: false,
                                        position: 0.94
                    },
                    yaxis3: {
                        title: 'Estimated 1RM',
                            overlaying: 'y',
                                side: 'right',
                                    showgrid: false,
                                        position: 1.00
                    },
                    showlegend: false,
                    legend: {

                        font: {
                            size: 10  // Adjust the font size of the legend to make it smaller
                        },
                        "orientation": "h"
                    }
                }, {
                    responsive: true,
                    displayModeBar: false,
                    //staticPlot: true,
                    displaylogo: false,
                    modeBarButtonsToRemove: ['toImage', 'sendDataToCloud', 'zoom2d', 'pan2d', 'select2d', 'lasso2d',
                        'zoomIn2d', 'zoomOut2d', 'autoScale2d', 'resetScale2d', 'hoverClosestCartesian',
                        'hoverCompareCartesian', 'zoom3d', 'pan3d', 'orbitRotation', 'tableRotation',
                        'resetCameraDefault3d', 'resetCameraLastSave3d', 'hoverClosest3d', 'zoomInGeo',
                        'zoomOutGeo', 'resetGeo', 'hoverClosestGeo', 'hoverClosestGl2d', 'hoverClosestPie',
                        'toggleHover', 'resetViews', 'toggleSpikelines', 'resetViewMapbox'
                    ]
                });
            </script>
        </div>
    </div>
    {% endfor %}
</div>
{% endif %}

{{ render_partial('partials/stats.html', stats=person['Stats']) }}

<button
    class="fixed z-90 bottom-10 right-8 bg-blue-600 w-20 h-20 rounded-full drop-shadow-lg flex justify-center items-center text-white text-4xl hover:bg-blue-700 hover:drop-shadow-2xl hover:animate-bounce duration-300"
    hx-post="{{ url_for('create_workout', person_id=person['PersonId']) }}" hx-target='body' hx-swap='beforeend'>
    <svg viewBox="0 0 20 20" enable-background="new 0 0 20 20" class="w-6 h-6 inline-block">
        <path fill="#FFFFFF" d="M16,10c0,0.553-0.048,1-0.601,1H11v4.399C11,15.951,10.553,16,10,16c-0.553,0-1-0.049-1-0.601V11H4.601
                                C4.049,11,4,10.553,4,10c0-0.553,0.049-1,0.601-1H9V4.601C9,4.048,9.447,4,10,4c0.553,0,1,0.048,1,0.601V9h4.399
                                C15.952,9,16,9.447,16,10z" />
    </svg>
</button>

{% endblock %}