{% extends "base.html" %}
{% load static %}
{% load humanize %}
{% load naturalduration %}
{% load form_tags %}

{% block head_extra  %}
<style>
 .container { margin-bottom:100px; }
 h2 { padding-top:20px; }
 .image-wrapper {
    contain: content;
 }
 .image-wrapper :hover {
     background:rgba(0,0,0,0.3);
 }
 .caption {
     position: fixed;
     top: 5px;
     left: 5px;
     padding: 3px;
     font-size: 90%;
     color:white;
     background:rgba(0,0,0,0.4);
 }

 .caption-medium {
     position: fixed;
     top: 5px;
     left: 5px;
     padding: 3px;
     font-size: 75%;
     color:white;
     background:rgba(0,0,0,0.4);

 }
 .caption-small {
     position: fixed;
     top: 5px;
     left: 5px;
     padding: 3px;
     font-size: 60%;
     color:white;
     background:rgba(0,0,0,0.4);

 }
 .in-progress {
     background: #CCFFFF;
 }
 .in-progress a { color: black }
</style>
{% endblock %}
{% block content %}
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
    <div
        class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">{% if date %}{{date|naturaltime}}{% else %}{{title}}{% endif %}</h1>
        <div class="btn-toolbar mb-2 mb-md-0">
            {% if user.is_authenticated %}
            <form method="get" action="{% url 'scrobbles:search' %}" class="d-flex me-2">
                <input type="text"
                       name="q"
                       class="form-control form-control-sm"
                       placeholder="Search..."
                       style="width: 150px;">
            </form>
            <div class="btn-group me-2">
              <a href="{% url 'scrobbles:calendar' %}" class="btn btn-sm btn-outline-secondary">Calendar</a>
            </div>
            <div class="btn-group me-2">
              <a href="{% url 'admin:index' %}" class="btn btn-sm btn-outline-secondary">Admin</a>
            </div>
            <div class="btn-group me-2">
              <a href="{% url 'charts:charts-home' %}" class="btn btn-sm btn-outline-secondary">Charts</a>
            </div>
            <div class="btn-group me-2">
              <a href="{% url 'scrobbles:scrobble-list' %}" class="btn btn-sm btn-outline-secondary">Live</a>
            </div>
            {% if not user.profile.trends_disabled %}
            <div class="btn-group me-2">
              <a href="{% url 'trends:trends-home' %}" class="btn btn-sm btn-outline-secondary">Trends</a>
            </div>
            {% endif %}
            <div class="btn-group me-2">
                {% if user.profile.lastfm_username and not user.profile.lastfm_auto_import %}
                <form action="{% url 'scrobbles:lastfm-import' %}" method="get">
                    <button type="submit" class="btn btn-sm btn-outline-secondary">Last.fm Sync</button>
                </form>
                {% endif %}

            </div>
            <div class="btn-group me-2">
                {% if user.profile.lastfm_username and not user.profile.lastfm_auto_import %}

                <form action="{% url 'scrobbles:lastfm-import' %}" method="get">
                    <button type="submit" class="btn btn-sm btn-outline-secondary">Last.fm Sync</button>
                </form>
                {% endif %}
                <button type="button" class="btn btn-sm btn-outline-secondary" data-bs-toggle="modal"
                    data-bs-target="#importModal">Import</button>
                <button type="button" class="btn btn-sm btn-outline-secondary" data-bs-toggle="modal"
                    data-bs-target="#exportModal">Export</button>
            </div>
            {% endif %}
        </div>
    </div>

    {% if not user.is_authenticated %}
    <p>
        <a href="/charts/?date={% current_date "%Y-%m-%d" %}">Today</a> <b>{{counts.today}}</b> |
        <a href="/charts/?date={% current_date "%Y" %}-W{% current_date "%W" %}">This Week</a> <b>{{counts.week}}</b> |
        <a href="/charts/?date={% current_date "%Y-%m" %}">This Month</a> <b>{{counts.month}}</b> |
        <a href="/charts/?date={% current_date "%Y" %}">This Year</a> <b>{{counts.year}}</b> |
        <a href="/charts/">All Time</a> <b>{{counts.alltime}}</b>
    </p>
    <canvas class="my-4 w-100" id="myChart" width="900" height="300"></canvas>
    {% else %}
    <div class="container">
        <div class="row">
            {% include "scrobbles/_last_scrobbles.html" %}
        </div>
    </div>
    {% endif %}

</main>

<div class="modal fade" id="importModal" tabindex="-1" role="dialog" aria-labelledby="importModalLabel"
    aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="importModalLabel">Import scrobbles</h5>
                <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                    <div aria-hidden="true">&times;</div>
                </button>
            </div>
            <form action="{% url 'scrobbles:audioscrobbler-file-upload' %}" method="post" enctype="multipart/form-data">
                <div class="modal-body">
                    {% csrf_token %}
                    <div class="form-group">
                        <label for="tsv_file" class="col-form-label">Audioscrobbler TSV file:</label>
                        <input type="file" name="tsv_file" class="form-control" id="id_tsv_file">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Import</button>
                </div>
            </form>
            <form action="{% url 'scrobbles:koreader-file-upload' %}" method="post" enctype="multipart/form-data">
                <div class="modal-body">
                    {% csrf_token %}
                    <div class="form-group">
                        <label for="tsv_file" class="col-form-label">KOReader sqlite file:</label>
                        <input type="file" name="sqlite_file" class="form-control" id="id_sqlite_file">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Import</button>
                </div>
            </form>
            <form action="{% url 'birds:csv-upload' %}" method="post" enctype="multipart/form-data">
                <div class="modal-body">
                    {% csrf_token %}
                    <div class="form-group">
                        <label for="id_csv_file" class="col-form-label">Birding CSV file:</label>
                        <input type="file" name="csv_file" class="form-control" id="id_csv_file" accept=".csv" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Import</button>
                </div>
            </form>
            <form action="{% url 'scrobbles:scale-csv-upload' %}" method="post" enctype="multipart/form-data" style="margin-top: 10px;">
                <div class="modal-body">
                    {% csrf_token %}
                    <div class="form-group">
                        <label for="id_csv_file" class="col-form-label">openScale CSV file:</label>
                        <input type="file" name="csv_file" class="form-control" id="id_csv_file" accept=".csv" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Import</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="exportModal" tabindex="-1" role="dialog" aria-labelledby="exportModalLabel"
    aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exportModalLabel">Export scrobbles</h5>
                <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                    <div aria-hidden="true">&times;</div>
                </button>
            </div>
            <form action="{% url 'scrobbles:export' %}" method="get">
                <div class="modal-body">
                    {% csrf_token %}
                    <div class="form-group">
                        {{export_form.as_div}}
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Export</button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}

<script src="https://cdn.jsdelivr.net/npm/feather-icons@4.28.0/dist/feather.min.js" integrity="sha384-uO3SXW5IuS1ZpFPKugNNWqTZRRglnUJK6UAZ/gxOX80nxEkN9NcGZTftn6RzhGWE" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4/dist/Chart.min.js" integrity="sha384-zNy6FEbO50N+Cg5wap8IKA4M/ZnLJgzc6w2NqACZaK0u0FXfOWRRJOnQtpZun8ha" crossorigin="anonymous"></script>
<script>
    /* globals Chart:false, feather:false */
    (function () {
    'use strict'

    feather.replace({ 'aria-hidden': 'true' })

    // Graphs
    var ctx = document.getElementById('myChart')
    // eslint-disable-next-line no-unused-vars
    var myChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: [
                {% for day in weekly_data.keys %}
                "{{day}}"{% if not forloop.last %},{% endif %}
                {% endfor %}
            ],
            datasets: [{
                data: [
                {% for count in weekly_data.values %}
                {{count}}{% if not forloop.last %},{% endif %}
                {% endfor %}
                ],
                lineTension: 0,
                backgroundColor: 'transparent',
                borderColor: '#007bf0',
                borderWidth: 4,
                pointBackgroundColor: '#007bff'
            }]
        },
        options: {
            scales: {
                yAxes: [{
                ticks: {
                    beginAtZero: true
                }
                }]
            },
            legend: {
                display: false
            }
        }
    })
    })()

</script>
{% endblock %}
