<template>
    <div>
        <Loader v-if="loading" />
        <div v-else>
            <div v-if="modal" class="modal-container" @click="modal = ! modal"></div>
            <div v-if="modal" class="modal">
                <p class="m-2 text-center">Are you sure you want to delete this record ?</p>
                <div class="flex justify-center mx-2 my-4">
                    <button class="btn-secondary mr-2" @click="modal = ! modal">Cancel</button>
                    <button class="btn-alert" @click="destroy"> Delete</button>
                </div>
            </div>
            <div class="relative">
                <img
                    v-if="!loading"
                    class="cover"
                    :src="memo.attributes.cover_image.data.attributes.path"
                />
                <div class="absolute flex flex-col justify-between w-full top-0 bottom-0">
                    <div class="flex items-center justify-between p-4">
                        <router-link to="/memos/" class="btn-secondary">Back</router-link>
                        <div class="flex-middle">
                            <router-link :to="'/memos/' + memo.memo_id + '/edit'" class="btn-secondary mr-1" >Edit</router-link>
                            <a href="#" class="btn-alert" @click="modal = ! modal">Delete</a>
                        </div>
                    </div>
                    <h1 class="flex justify-center text-white text-3xl px-4 font-bold text-shadow">{{ memo.name }}</h1>
                </div>
            </div>
    <!--           <TagBox :memo="memo" />-->
            <div class="p-4">

                <div class="memo-style p-4 pb-3 -mb-1 bg-white" v-html="memoMarkdown"></div>
                <div class="bg-orange-400 px-2 py-1 flex justify-end">@last update {{ memo.last_updated }}</div>
            </div>
        </div>
    </div>
</template>

<script>
// import TagBox from "../Tag/TagBox";
import Loader from '../../components/Loader'
let MarkdownIt = require('markdown-it'),
    md = new MarkdownIt()
        .use(require('markdown-it-checkbox'))

export default {
    name: 'MemoShow',
    components: {
        Loader
        // TagBox
    },
    data: function () {
        return {
            loading: true,
            modal: false,
            memo: null,
        }
    },
    computed: {
        memoMarkdown: function () {
            return md.render(this.memo.memo)
        }
    },
    mounted() {
        // eslint-disable-next-line no-undef
        axios.get('/api/memos/' + this.$route.params.id)
            .then(response => {
                this.memo = response.data.data
                this.loading = false
            })
            .catch(errorRes => {
                this.loading = false
                if (errorRes.response.status === 404) {
                    this.$router.push('/memos')
                }
            })
    },
    methods: {
        destroy: function () {
            // eslint-disable-next-line no-undef
            axios.delete('/api/memos/' + this.$route.params.id)
                .then(() => {
                    this.$router.push('/memos')
                })
                .catch(errorRes => {
                    console.log('Internal Error, Unable to delete contact.' + errorRes)
                })
        }
    }
}
</script>
