<template>
    <div>
        <TitleBar/>
        <CreditLists v-bind:users="users" @add:amount="addAmount"></CreditLists>
    </div>
</template>

<script>
    import TitleBar from "@/components/TitleBar";
    import CreditLists from "@/components/baruser/CreditLists";
    import httpClient from "../plugins/restService";
    import axios from "axios";
    export default {
        name: "BarView",
        components: {CreditLists, TitleBar},
        created() {
            this.getUser()
        },
        data () {
            return {
                users: []
            }
        },
        methods: {
            getUser() {
                // eslint-disable-next-line no-console
                httpClient.getUserBar(this.$store.getters.getToken)
                .then(response => {
                    // eslint-disable-next-line no-console
                    console.log(response.data)
                    for (let user in response.data) {
                        const lastId = this.users.length > 0 ? this.users[this.users.length - 1].id : 0
                        this.users.push({
                            id: lastId + 1,
                            username: response.data[user].username,
                            firstname: response.data[user].firstname,
                            lastname: response.data[user].lastname,
                            amount: response.data[user].amount
                        })
                    }
                })
                .catch(error => {
                    // eslint-disable-next-line no-console
                    console.log(error)
                    this.users = []
                })
            },
            addAmount(username, amount) {
                axios.post('http://192.168.5.118:5000/baradd', {userId: username, amount: amount}, {headers: {Token: this.$store.getters.getToken}})
                //httpClient.addAmountBar(this.$store.getters.getToken, {username: username, amount: amount})
                /*.then(response => {
                    // eslint-disable-next-line no-console
                    console.log("responsedata: ", response.data)
                })
                .catch(error => {
                    // eslint-disable-next-line no-console
                    console.log("error: ", error.response.data.error)
                })*/
            }
        }
    }
</script>

<style scoped>

</style>
