{% extends "base.html" %}

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

{% block header %}
{{ emit_tep('all_publishers_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">
    <publishers v-bind:publishers='publishers'></publishers>
</div>

{% endblock %}

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

{% block button_container %}
{{ emit_tep('all_publishers_button_container') }}
{% endblock %}

{% block script %}

Vue.component('publishers', {
    props: ['publishers'],
    template: `
    <div>
        <div class="mb-3 px-5">
            <input type="text" v-model="search" class="form-control" placeholder="Search Publishers..." />
        </div>
        <ul class="d-flex flex-wrap w-100 m-0 p-0 py-2 justify-content-center">
            <publisher
                v-for="publisher in filteredList"
                v-bind:publisher="publisher"
                v-bind:key="publisher.publisher_id"
            ></publisher>
        </ul>
    </div>
    `,
    data() {
        return {
            search: '',
        }
    },
    computed: {
        filteredList() {
            return this.publishers.filter(publisher => {
                return publisher.publisher_name.toLowerCase().includes(this.search.toLowerCase())
            })
        }
    },
    delimiters: ["[[","]]"],
})

Vue.component('publisher',{
    props: ['publisher'],
    template: `
    <li class='stashr-cover_size m-2'
        @mouseover="hover = true"
        @mouseleave="hover = false"
    >
        <div class='stashr-poster_wrapper rounded'>
            <div class="stashr-poster_container border rounded">
                <div class="stashr-overlay_bottom w-100 center" v-if="hover">
                    <a href="#">[[ publisher.publisher_name ]]</a>
                </div>
                <img class="stashr-poster_background w-100" loading="eager" src="/static/assets/cover.svg" />
                <a class="stashr-poster_link align-middle" :href="'{{ url_for('single_publisher_page', publisher_id='PUBLISHERID') }}'.replace('PUBLISHERID', publisher.publisher_id)">
                    <img class="w-100" loading="lazy" v-bind:src=publisher.publisher_image />
                </a>
            </div>
        </div>
    </li>
    `,
    data() {
        return {
            hover: false,
        }
    },
    delimiters: ["[[","]]"],
})

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

{{ emit_tep('all_publishers_page_script') }}

{% endblock %}