<template>
  <section id="content">
    <div id="content-header">
      <router-link to="/customers" class="button" id="customers-button" @click="defaultCustomerPage()">
        <h1 :class="[loggedInUserDarkModeBoolean ? 'h1-darkmode' : 'h1-lightmode']" id="page-name">Customers</h1>
      </router-link>
    </div>
    <div id="content-body">
      <CustomerTable v-if="onCustomerlist" />
      <Customer v-if="onCustomer" />
      <!-- <CustomerQuickAccess v-if="onCustomer" /> -->
      <ClientSearch v-if="onCustomerEmployeelist || onDepartmentlist" />
      <CustomerEmployeeList v-if="onCustomerEmployeelist" />
      <CustomerEmployee v-if="onCustomerEmployee" />
      <CustomerDepartmentList v-if="onDepartmentlist" />
      <CustomerDepartment v-if="onDepartment" />
      <CustomerDepartmentEmployeeList v-if="onDepartment && !addBool" />
    </div>
  </section>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue';

import CustomerTable from "../components/server/CustomerTable.vue";
import Customer from "../components/server/Customer.vue";
import CustomerQuickAccess from "../components/server/CustomerQuickAccess.vue";
import ClientSearch from "../components/ClientSearch.vue";
import CustomerEmployeeList from "../components/server/CustomerEmployeeList.vue";
import CustomerEmployee from "../components/server/CustomerEmployee.vue";
import CustomerDepartmentList from '~/components/server/CustomerDepartmentList.vue';
import CustomerDepartment from '~/components/server/CustomerDepartment.vue';
import CustomerDepartmentEmployeeList from '~/components/server/CustomerDepartmentEmployeeList.vue';
import { useStore } from 'vuex';
import { computed } from 'vue';

const store = useStore();
const addBool = computed(() => store.state.new);

const onCustomerlist = computed(() => store.state.onCustomerlist);
const onCustomer = computed(() => store.state.onCustomer);
const onCustomerEmployeelist = computed(() => store.state.onCustomerEmployeelist);
const onCustomerEmployee = computed(() => store.state.onCustomerEmployee);
const onDepartmentlist = computed(() => store.state.onDepartmentlist);
const onDepartment = computed(() => store.state.onDepartment);
const modeChanged = computed(() => store.state.updateDarkMode);

definePageMeta({
  layout: 'default',
  title: 'Customers'
})

const defaultCustomerPage = () => {
  store.commit('resetStore');
  store.commit('changeToCustomerlist')
}

const loggedInUserDarkModeBoolean = ref('');

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

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

watch(modeChanged, getSession)

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

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


<style scoped>
* {
  box-sizing: border-box;
}


#content {
  display: flex;
  flex-direction: column;
  float: left;
  justify-content: stretch;
  align-items: stretch;
  width: 100%;
  flex-grow: 1;
  gap: 0.625rem;
  padding: 0 1.25rem;
}

#content-header {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 3.125rem;
}

.button {
  text-decoration: none;
}

#page-name {
  letter-spacing: 5%;
  font: 400 1.5rem/125% Overpass, -apple-system, Roboto, Helvetica, sans-serif;
  text-decoration: underline;
}

.h1-darkmode {
  color: #fff;
}

.h1-lightmode {
  color: #000;
}

#content-body {
  display: flex;
  flex-direction: column;
  width: 100%;
  gap: 1.25rem;
  margin-bottom: 0.625rem;
  border-radius: 0.625rem;
}
</style>