<template>
    <div :class="['data', darkMode ? 'div-darkmode' : 'div-lightmode']">
        <table class="data-table" id="employeetable">
            <tbody>
                <tr :class="['table-row', darkMode ? 'tr-head-darkmode' : 'tr-head-lightmode']" id="table-head">
                    <th
                        :class="['Name', darkMode ? 'th-darkmode' : 'th-lightmode', darkMode ? 'Name-darkmode' : 'Name-lightmode']">
                        Name</th>
                    <th
                        :class="['Username', darkMode ? 'th-darkmode' : 'th-lightmode', darkMode ? 'Username-darkmode' : 'Username-lightmode']">
                        Username</th>
                    <th :class="['ID', darkMode ? 'th-darkmode' : 'th-lightmode']">ID</th>
                </tr>
                <tr v-for="u in userList" :key="u.id"
                    :class="['table-row', darkMode ? 'tr-darkmode' : 'tr-lightmode']">
                    <td
                        :class="['Name', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'Name-darkmode' : 'Name-lightmode']">
                        <nuxt-link to="/assets" id="nuxt-link" class="button"
                            :class="[darkMode ? 'button-darkmode' : 'button-lightmode']"
                            @click="goToChosenAsset(u.id)">
                            {{ u.fullName }}
                        </nuxt-link>
                    </td>
                    <td
                        :class="['Username', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'Username-darkmode' : 'Username-lightmode']">
                        <nuxt-link to="/assets" id="nuxt-link" class="button"
                            :class="[darkMode ? 'button-darkmode' : 'button-lightmode']"
                            @click="goToChosenAsset(u.id)">
                            {{ u.username }}
                        </nuxt-link>
                    </td>
                    <td :class="['ID', darkMode ? 'td-darkmode' : 'td-lightmode']"> {{ u.id }}</td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue';
import Axios from '../axios.config.js';
import clientsideConfig from '../../clientsideConfig.js';
import { useStore } from 'vuex';
import { computed } from 'vue';

// get accesss to the store
const store = useStore()
const goToChosenAsset = (id) => {
    store.commit('setChosenEmployee', id);
    store.commit('changeToEmployee');
};
const employeeFilter = computed(() => store.state.filteredByCustomer);

const darkMode = ref(true)
const userList = ref([]);

//get all users
const getEmployees = async () => {
    if (!(employeeFilter.value === '')) {
        await getFilteredUsersByUser();
    } else {
        try {
            const response = await Axios.get(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/getAllEmployees`);
            userList.value = response.data;
        } catch (err) {
            console.log(err.response.statusText);
        }
    }
}

//get all users based on the searched user
const getFilteredUsersByUser = async () => {
    try {
        const response = await Axios.get(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/getSelectedEmployeesByEmployee/${employeeFilter.value}`);
        userList.value = response.data;
    } catch (err) {
        console.log(err.response.statusText);
    }
}


watch(employeeFilter, getEmployees);

onMounted(async () => {
    await getEmployees();
});
</script>

<script>
export default {
    name: "AssetTable",
};
</script>


<style scoped>
.data {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    align-self: stretch;
    padding: 1.25rem 1.875rem;
    gap: 1.25rem;
    border-radius: 0.625rem;
    box-shadow: 0.25rem 0.25rem 0.25rem 0rem rgba(0, 0, 0, 0.25);
}

.section-darkmode {
    background-color: #2c2c2c;
}

.section-lightmode {
    background-color: #fff;
}

.input {
    border: none;
}

.data-darkmode {
    background-color: #212121;
    color: #fff;
}

.data-lightmode {
    background-color: #EBEBEB;
    color: #000;
}

.div-darkmode {
    background-color: #2c2c2c;
}

.div-lightmode {
    background-color: #fff;
}

.data-table {
    width: 100%;
    padding: 0 0.625rem;
    table-layout: fixed;
    border-collapse: collapse;
}

.table-row {
    display: flex;
    flex-direction: row;
    align-items: center;
    height: 3.125rem;
    padding: 0.625rem;
    gap: 0.625rem;
}

.tr-head-darkmode {
    border-top: none;
    border-bottom: 0.0625rem solid #000000;
}

.tr-head-lightmode {
    border-top: none;
    border-bottom: 0.0625rem solid #8e8e8e;
}

.tr-darkmode {
    border-top: 0.0625rem solid #000000;
}

.tr-lightmode {
    border-top: 0.0625rem solid #8e8e8e;
}

th,
td {
    height: 1.875rem;
    text-align: left;
    padding: 0;
    letter-spacing: 5%;
    font: 400 0.875rem/1.875rem Overpass, sans-serif;
}

.th-darkmode,
.td-darkmode {
    color: #ffffff;
}

.th-lightmode,
.td-lightmode {
    color: #000;
}

th {
    font: 700 0.875rem/1.875rem Overpass, sans-serif;
}

.Name {
    width: 40%;
}

.Name-darkmode {
    border-right: 0.0625rem solid #000000;
}

.Name-lightmode {
    border-right: 0.0625rem solid #8e8e8e;
}

.Username {
    width: 35%;
}

.Username-darkmode {
    border-right: 0.0625rem solid #000000;
}

.Username-lightmode {
    border-right: 0.0625rem solid #8e8e8e;
}

.ID {
    width: 25%;
}

#nuxt-link {
    text-decoration: none;
    color: white;
}
</style>
