{% extends "base.html" %}

{% block header_script_files %}
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
{% endblock %}

{% block header %}
{{ emit_tep('all_volumes_page_header') }}
{% endblock %}

{% block content %}

<div class="hidden">
	<script type="text/javascript">
		<!--//--><![CDATA[//><!--
			var images = new Array()
			function preload() {
				for (i = 0; i < preload.arguments.length; i++) {
					images[i] = new Image()
					images[i].src = preload.arguments[i]
				}
			}
			preload(
				"{{ url_for('static', filename='/assets/cover.svg') }}"
			)
		//--><!]]>
	</script>
</div>

<div id="app">
    <volumes v-bind:volumes='volumesList'></volumes>
</div>

{% endblock %}

{% block modals %}
{{ emit_tep('all_volumes_modals') }}
{% endblock %}

{% block button_container %}
{% if current_user.role == 'admin' %}
<button type="button" class="btn btn-outline-info btn-circle btn-md" data-bs-toggle="tooltip" data-bs-placement="top" title="Add Volume from Comicvine" onclick="location.href='{{ url_for('search_page') }}';">
    <i class="text-white fas fa-2x fa-plus"></i>
</button>
{% endif %}
{{ emit_tep('all_volumes_button_container') }}
{% endblock %}

{% block script %}

Vue.component('volume-item', {
    props: ['volume'],
    template: `
    <li class='stashr-cover_size m-2'
        @mouseover="hover = true"
        @mouseleave="hover = false"
    >
        <div class='stashr-poster_wrapper rounded'>
            <div class="stashr-badge_tl badge rounded-pill bg-info border">[[ volume.age_rating[0].rating_short ]]</div>
            <div class="stashr-badge_tr badge rounded-pill bg-primary border">[[ volume.volume_have ]]/[[ volume.volume_total ]]</div>
            <div class="stashr-badge_br badge rounded-pill border" :class="statusClass">[[ statusWord ]]</div>
            <div class="stashr-poster_container border rounded">
                <div class="stashr-overlay_bottom w-100 center" v-if="hover">
                    <a :href="'/volumes/'+volume.volume_slug">[[ volume.volume_name ]]</a>
                </div>
                <img class="stashr-poster_background w-100" loading="eager" src="/static/assets/cover.svg" />
                <a class="stashr-poster_link" :href="'/volumes/'+volume.volume_slug">
                    <img class="w-100" loading="lazy" v-bind:src="'/images/volumes/'+volume.volume_id+'.jpg'" @error="$event.target.src=volume.volume_image_med"/>
                </a>
            </div>
        </div>
    </li>
    `,
    computed: {
        statusClass() {
            let classname = 'bg-danger';
            if(this.volume.volume_status) {
                classname = 'bg-success';
            };
            return classname;
        },
        statusWord() {
            let status = 'ENDED';
            if(this.volume.volume_status) {
                status = 'ONGOING';
            };
            return status;
        }
    },
    data() { return { hover: false } },
    delimiters: ["[[","]]"],
})

Vue.component('volumes', {
    props: ['volumes'],
    template: `
    <div>
        <div class="mb-3 px-5">
            <input type="text" v-model="search" class="form-control" placeholder="Search Volumes..." />
        </div>
        <ul class="d-flex flex-wrap w-100 m-0 p-0 py-2 justify-content-center">
            <volume-item
                v-for="volume in filteredList"
                v-bind:volume="volume"
                v-bind:key="volume.volume_id"
            ></volume-item>
        </ul>
        <!--
        <i class="text-primary fas fa-spinner fa-spin fa-3x" v-if="loading"></i>
        -->
    </div>
    `,
    data() { return { loading: true, search: '', } },
    computed: {
        filteredList() {
            return this.volumes.filter(volume => {
                return volume.volume_name.toLowerCase().includes(this.search.toLowerCase())
            })
        },
    },
    delimiters: ["[[","]]"],
})

var app = new Vue({
    el: '#app',
    data: {
        volumesList: [],
    },
    created() {
        this.getVolumes()
    },
    methods:{
        getVolumes() {
            axios.get('{{ url_for('api.api_get_all_volumes') }}', {
                params: {
                    offset: this.volumesList.length
                }
            })
                .then(res => {
                    if(res.data.number_of_page_results > 0) {
                        res.data.results.forEach(result => {
                            this.volumesList.push(result)
                        })
                        if(this.volumesList.length < res.data.number_of_total_results) {
                            this.getVolumes()
                        }
                    }
                })
        },
    },
    delimiters: ["[[","]]"]
});

{{ emit_tep('all_volumes_page_script') }}

{% endblock %}