<template>
    <section v-if="customerSearchable"
        :class="['customer-search', darkMode ? 'section-darkmode' : 'section-lightmode']">
        <div :class="['customerLabel', darkMode ? 'label-darkmode' : 'label-lightmode']">Name</div>
        <input v-model="customerSearchFilter" @change="searchCustomer()"
            :class="['dataInput', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
    </section>
    <section :class="['data', darkMode ? 'section-darkmode' : 'section-lightmode']">
        <table class="data-table" id="customer-table">
            <tbody>
                <tr :class="['table-row', darkMode ? 'tr-head-darkmode' : 'tr-head-lightmode']" id="table-head">
                    <th
                        :class="['ID', darkMode ? 'th-darkmode' : 'th-lightmode', darkMode ? 'ID-darkmode' : 'ID-lightmode']">
                        ID
                    </th>
                    <th
                        :class="['Name', darkMode ? 'th-darkmode' : 'th-lightmode', darkMode ? 'Name-darkmode' : 'Name-lightmode']">
                        Name</th>
                </tr>
                <tr v-for="cust in customerList" :key="cust.customerID"
                    :class="['table-row', darkMode ? 'tr-darkmode' : 'tr-lightmode']" id="row-1">
                    <td
                        :class="['ID', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'ID-darkmode' : 'ID-lightmode']">
                        <nuxt-link to="/customers" id="nuxt-link" class="button"
                            :class="[darkMode ? 'button-darkmode' : 'button-lightmode', darkMode ? 'nuxt-link-darkmode' : 'nuxt-link-lightmode']"
                            @click="goToChosenCustomer(cust.customerID)">
                            {{ cust.customerID }}
                        </nuxt-link>
                    </td>
                    <td
                        :class="['Name', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'Name-darkmode' : 'Name-lightmode']">
                        <nuxt-link to="/customers" id="nuxt-link" class="button"
                            :class="[darkMode ? 'button-darkmode' : 'button-lightmode', darkMode ? 'nuxt-link-darkmode' : 'nuxt-link-lightmode']"
                            @click="goToChosenCustomer(cust.customerID)">
                            {{ cust.customername }}
                        </nuxt-link>
                    </td>
                </tr>
            </tbody>
        </table>
    </section>
</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';

const store = useStore();
const modeChanged = computed(() => store.state.updateDarkMode);

const goToChosenCustomer = (id) => {
    store.commit('setChosenCustomer', id);
    store.commit('changeToCustomer');
};

const darkMode = ref('')
const customerSearchable = computed(() => store.state.searchable);
const customerSearchFilter = ref('');
const customerList = ref([]);

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

//get all customers
const getCustomers = async () => {
    try {
        const response = await Axios.get(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/getAllCustomers`);
        customerList.value = response.data;
    } catch (err) {
        console.log(err.response.statusText);
    }
}

//get all customers based on the searched customers name
const searchCustomer = async () => {
    if (customerSearchFilter.value === '') {
        await getCustomers();
    } else {
        try {
            const response = await Axios.get(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/getSelectedCustomersByName/${customerSearchFilter.value}`);
            customerList.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(modeChanged, getSession)
watch(customerSearchable, updateSearchTerm);

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

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


<style scoped>
.data {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    align-self: stretch;
    width: 100%;
    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: #ffffff;
}

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

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

.input {
    border: none;
}

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

.label-darkmode {
    color: #FFFFFF;
}

.label-lightmode {
    color: #000;
}

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

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

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

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

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

.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: #000000;
}

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

.ID {
    width: 40%;
}

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

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

.Name {
    width: 60%;
}
</style>