
<script setup lang="ts">

import { onMounted } from "vue"
import { useRoute, useRouter } from "vue-router";
import { useUsersStore } from "../stores/UsersStore.ts"
import * as authApi from "../api/AuthApi";

const store = useUsersStore()
const router = useRouter();

onMounted(() => {
  store.fetchUsers()
})

function logout() {
    authApi.logout();
    router.push("/login");
}

</script>

<template>
    <div>
        <h1>Users</h1>

        <router-link to="/user/new">Create User</router-link>

        <table>
            <tr v-for="user in store.users" :key="user.id">
                <td>{{ user.name }}</td>
                <td>

                    <router-link :to="`/user/${user.id}`" custom v-slot="{ navigate }">
                        <button @click="navigate" role="link">Edit</button>
                    </router-link>

                    <button @click="store.removeUser(user.id)">Delete</button>
                </td>
            </tr>
        </table>
        <button @click="logout()">Logout</button>
    </div>
</template>