<template>
    <div>
        <p class='text-center m-3 h3'> Лабораторная работа 4</p>
    </div>
    <div>
        <p class='h4 text-center' v-if="isAuthorized() == true"> 
            <router-link to="/profiles" class="text-decoration-none m-3" v-if="isAdminFlag == true">Профили</router-link> 
            <router-link to="/" class="text-decoration-none m-3">Лента</router-link>
            <a class="text-blue text-decoration-none" @click="logout">Выход</a>
        </p>
    </div>
    <router-view></router-view>
</template>

<script>
import axios from 'axios'
export default {
    data() {
        return {
            isAdminFlag: false
        }
    },
    methods: {
        logout() {
            localStorage.removeItem('user')
            localStorage.removeItem('token')
            location.reload()
        },
        isAuthorized() {
            if (localStorage.getItem('token') != null) return true
            return false
        },
        async isAdmin() {
            var a = await axios.get('http://localhost:8080/api/1.0/profile/role/' + localStorage.token, 
                {
                    headers: {
                        'Authorization': 'Bearer ' + localStorage.token
                    }
                }
            )
            console.log('role')
            console.log(a.data)
            if (a.data == 'ADMIN') return true
            
            return false
        },

        async created() {
            var a = await axios.get('http://localhost:8080/api/1.0/profile/role/' + localStorage.token, 
                {
                    headers: {
                        'Authorization': 'Bearer ' + localStorage.token
                    }
                }
            )
            console.log('role')
            console.log(a.data)
            if (a.data == 'ADMIN'){
                this.isAdminFlag = true
            }
        },
    }

}
</script>

<style lang="">
    
</style>