<template>
    <div class="p-4">
        <div v-if="modal" class="modal-container" @click="modal = ! modal"></div>
        <div v-if="modal" class="modal px-2">
            <p class="m-2 text-center">Add a new to-do list ?</p>
            <InputField name="name" label="Title" placeholder="Your Title" @update:field="name = $event" :errors="errors" classes="border" />
            <InputField name="name" label="Url" placeholder="Website Url" required @update:field="url = $event" :errors="errors" classes="border" />
            <div class="flex justify-center mx-2 my-4">
                <button class="btn-primary mr-2" @click="create">Create</button>
                <button class="btn" @click="modal = ! modal">Cancel</button>
            </div>
        </div>
        <div class="flex justify-between mb-1">
            <router-link to="/" class="btn">Back</router-link>

            <a href="#" class="btn-primary" @click="modal = ! modal">Add Bookmark</a>
        </div>
        <Loader v-if="loading" />
        <div v-else class="flex flex-wrap -mx-2 mt-2">
            <div v-if="bookmarks.length < 1" class="font-bold p-2">No Bookmark Yet</div>
            <div v-else
                 v-for="(bookmark, index) in bookmarks"
                 :key="index"
                 class="w-full sm:w-1/2 md:w-1/3 lg:w-1/4" >
                <Bookmark :bookmark="bookmark" @update:destroy="deleteBookmark" />
            </div>
        </div>
    </div>
</template>

<script>
import Loader from '../../components/Loader'
import InputField from '../../components/InputField'
import Bookmark from './Bookmark'

export default {
    name: 'BookmarkIndex',
    components: {
        Loader, InputField, Bookmark
    },
    data: function () {
        return {
            loading: true,
            modal: false,
            bookmarks: null,
            name: '',
            url: '',
            errors: null,
        }
    },
    mounted() {
        // eslint-disable-next-line no-undef
        axios.get('/api/bookmarks')
            .then(res => {
                this.bookmarks = res.data.data
                this.loading = false
            })
            .catch(errorRes => {
                this.loading = false
                if (errorRes.response.status === 404) {
                    this.$router.push('/')
                }
            })
    },
    methods: {
        create: function () {
            // eslint-disable-next-line no-undef
            axios.post('/api/bookmarks', {name: this.name, url: this.url})
                .then(res => {
                    console.log(res)
                    this.modal = false
                    this.name = ''
                    this.url = ''
                    this.bookmarks.push(res.data)
                })
                .catch(errorRes => {
                    console.log('Internal Error, Unable to delete contact.' + errorRes)
                })
        },
        deleteBookmark: function(e) {
            this.bookmarks.forEach((bookmark, index) => {
                if(bookmark.data.bookmark_id === e) {
                    console.log(bookmark, bookmark.data.bookmark_id, index)
                    this.bookmarks.splice(index, 1)
                }
            })
        }
    }
}
</script>


