<template>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-12">
                <div class="col-md-4" v-for="(workOrder, index) in workOrders" :key="workOrder.woid">
                    <div class="card">
                        <div class="card-header">
                            <router-link :to="{ name: 'workorders', params: { id: workOrder.woid } }" class="h4 card-title">{{workOrder.woid}}</router-link>
                        </div>
                        <div class="card-body card-body-dark">
                            <div>
                                {{ workOrder.probdesc }}
                            </div>
                            <div>
                                {{ workOrder.suggested }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    data () {
        return {
            workOrders: []
        }
    },
    mounted () {
        let token = localStorage.getItem('jwt')
        let user = localStorage.getItem('user')

        axios.defaults.headers.common['Content-Type'] = 'application/json'
        axios.defaults.headers.common['Authorization'] = 'Bearer ' + token

        axios.get('api/users/'+user+'/workorders').then(response => {
            this.workOrders = response.data
        }).catch(error => {
            console.log(error)
        })
    },
    beforeRouteEnter (to, from, next) {
        if ( ! localStorage.getItem('jwt')) {
            return next('login')
        }

        next()
    }
}
</script>