<template>
    <section v-if="searchable" :class="['solution-search', darkMode ? 'section-darkmode' : 'section-lightmode']">
        <div :class="['customerLabel', darkMode ? 'label-darkmode' : 'label-lightmode']">Solution</div>
        <input v-model="solutionSearchFilter" @change="searchSolution()"
            :class="['dataInput', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
    </section>
    <div :class="['data', darkMode ? 'div-darkmode' : 'div-lightmode']">
        <div :class="['label', darkMode ? 'label-darkmode' : 'label-lightmode']">Last viewed:</div>
        <table class="data-table" id="solution-table">
            <tbody>
                <tr :class="['table-row', darkMode ? 'tr-head-darkmode' : 'tr-head-lightmode']" id="table-head">
                    <th
                        :class="['Customer', darkMode ? 'th-darkmode' : 'th-lightmode', darkMode ? 'Customer-darkmode' : 'Customer-lightmode']">
                        Customer</th>
                    <th
                        :class="['Name', darkMode ? 'th-darkmode' : 'th-lightmode', darkMode ? 'Name-darkmode' : 'Name-lightmode']">
                        Name</th>
                    <th
                        :class="['Asset', darkMode ? 'th-darkmode' : 'th-lightmode', darkMode ? 'Asset-darkmode' : 'Asset-lightmode']">
                        Asset</th>
                    <th
                        :class="['Type', darkMode ? 'th-darkmode' : 'th-lightmode', darkMode ? 'Type-darkmode' : 'Type-lightmode']">
                        Type</th>
                    <th :class="['User', darkMode ? 'th-darkmode' : 'th-lightmode']">User</th>
                </tr>
                <tr v-for="sol in solutions" :key="sol.primaryID"
                    :class="['table-row', darkMode ? 'tr-darkmode' : 'tr-lightmode']" id="row-1">
                    <td
                        :class="['Customer', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'Customer-darkmode' : 'Customer-lightmode']">
                        {{ sol.customer }}</td>
                    <td
                        :class="['Name', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'Name-darkmode' : 'Name-lightmode']">
                        <nuxt-link to="/solutions" id="nuxt-link" class="button"
                            :class="[darkMode ? 'button-darkmode' : 'button-lightmode', darkMode ? 'nuxt-link-darkmode' : 'nuxt-link-lightmode']"
                            @click="goToChosenSolution(sol.primaryID)">
                            {{ sol.solutionName }}
                        </nuxt-link>
                    </td>
                    <td
                        :class="['Asset', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'Asset-darkmode' : 'Asset-lightmode']">
                        {{ sol.assetName }}</td>
                    <td
                        :class="['Type', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'Type-darkmode' : 'Type-lightmode']">
                        {{ sol.type }}</td>
                    <td :class="['User', darkMode ? 'td-darkmode' : 'td-lightmode']"> {{ sol.user }}</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 modeChanged = computed(() => store.state.updateDarkMode);

const goToChosenSolution = (id) => {
    store.commit('setChosenSolution', id);
    store.commit('changeToSolution');
};

const customerFilter = computed(() => store.state.filteredByCustomer);
const searchable = computed(() => store.state.searchable);

const solutionSearchFilter = ref('');
const darkMode = ref('');
const solutions = ref([]);

// update search term
const updateSearchTerm = async () => {
    solutionSearchFilter.value = '';
    await getSolutions();
}

//get all solutions
const getSolutions = async () => {
    if (!(customerFilter.value === '')) {
        await getFilteredSolutionsByCustomer();
    } else {
        try {
            const response = await Axios.get(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/getAllSolutions`);
            solutions.value = response.data;
        } catch (err) {
            console.log(err.response.statusText);
        }
    }
}

//get all solutions based on the searched customer
const getFilteredSolutionsByCustomer = async () => {
    try {
        const response = await Axios.get(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/getSelectedSolutionsByCustomer/${customerFilter.value}`);
        solutions.value = response.data;
    } catch (err) {
        console.log(err.response.statusText);
    }
}

//get all solutions based on the searched solution name
const searchSolution = async () => {
    if (solutionSearchFilter.value === '') {
        await getSolutions();
    } else {
        try {
            const response = await Axios.get(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/getSelectedSolutionsBySolution/${solutionSearchFilter.value}`);
            solutions.value = response.data;
        } catch (err) {
            console.log(err.response.statusText);
        }
    }
}

const getSession = async () => {
    const loggedInUserDarkModeBool = getItem('logged-in-user-darkMode');
    if (loggedInUserDarkModeBool == 1) {
        darkMode.value = true;
    } else {
        darkMode.value = false;
    }
}

function getItem(item) {
    if (process.client) {
        return localStorage.getItem(item)
    } else {
        return undefined
    }
}

watch(customerFilter, getSolutions);
watch(searchable, updateSearchTerm);
watch(modeChanged, getSession)

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

<script>
export default {
    name: "SolutionTable",
};
</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);
}

.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;
}

.input {
    border: none;
}

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

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

.solution-search {
    display: flex;
    align-items: center;
    padding: 0.625em 1.875em;
    gap: 1.25em;
    width: 30.125em;
    height: 3.125em;
    box-shadow: 0.25em 0.25em 0.25em rgba(0, 0, 0, 0.25);
    border-radius: 0.625em;
    box-sizing: border-box;
}

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

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

.dataInput {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    padding: 0 0.625rem;
    border-radius: 0.3125rem;
    box-shadow: 0.0625rem 0.0625rem 0.25rem 0rem rgba(0, 0, 0, 0.25) inset;
    letter-spacing: 5%;
    font: 400 0.75rem/250% Overpass, sans-serif;
}

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

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

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

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

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

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

#nuxt-link {
    text-decoration: none;
}

.nuxt-link-darkmode {
    color: white;
}

.nuxt-link-lightmode {
    color: #000;
}

.customerLabel {
    width: 5.5625em;
    height: 1.875em;
    font-family: "Overpass";
    font-style: normal;
    font-weight: 400;
    font-size: 0.875em;
    line-height: 1.875;
    letter-spacing: 0.05em;
}

.Customer {
    width: 33.3%;
}

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

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

.Name {
    width: 33.3%;
}

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

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

.Asset {
    width: 12.7%;
}

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

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

.Type {
    width: 12.7%;
}

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

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

.User {
    width: 8%;
}

.label {
    width: 6.0625em;
    height: 1.875em;
    font-family: 'Overpass';
    font-style: normal;
    font-weight: 400;
    font-size: 1em;
    line-height: 1.875em;
    letter-spacing: 0.05em;
}

.label-darkmode {
    color: #FFFFFF;
}

.label-lightmode {
    color: #000;
}
</style>