<template>
  <section v-if="!addBool" :class="['information', darkMode ? 'section-darkmode' : 'section-lightmode']">
    <div>
      <h2 v-if="!editable" :class="['customer-name', darkMode ? 'h2-darkmode' : 'h2-lightmode']">{{ cust.customername }}
      </h2>
      <input v-if="editable" v-model="cust.customername" @change="updateCustomer()"
        :class="['data', 'customer-name-input', darkMode ? 'h2-darkmode' : 'h2-lightmode', darkMode ? 'h2-input-darkmode' : 'h2-input-lightmode', 'input']">
    </div>
    <div class="data-field" id="customer-id">
      <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">ID:</pre>
      <pre :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.customerID }}</pre>
    </div>
    <div class="customer-data">
      <div class="contact">
        <pre :class="['area-title', darkMode ? 'h3-darkmode' : 'h3-lightmode']">Contacts:</pre>
        <div class="data-field" id="contact-person">
          <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">Contact person:</pre>
          <pre v-if="!editable"
            :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.contactperson }}</pre>
          <input v-if="editable" v-model="cust.contactperson" @change="updateCustomer()"
            :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
        </div>
        <div class="data-field" id="first-e-mail">
          <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">1. E-Mail:</pre>
          <pre v-if="!editable" :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.EMail }}</pre>
          <input v-if="editable" v-model="cust.EMail" @change="updateCustomer()"
            :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
        </div>
        <div class="data-field" id="first-phone">
          <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">1. Phone number:</pre>
          <pre v-if="!editable"
            :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.phonenumber }}</pre>
          <input v-if="editable" v-model="cust.phonenumber" @change="updateCustomer()"
            :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
        </div>
        &nbsp;
        <div class="data-field" id="second-e-mail">
          <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">2. E-Mail:</pre>
          <pre v-if="!editable"
            :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.secondEMail }}</pre>
          <input v-if="editable" v-model="cust.secondEMail" @change="updateCustomer()"
            :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
        </div>
        <div class="data-field" id="second-phone">
          <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">2. Phone number:</pre>
          <pre v-if="!editable"
            :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.secondPhonenumber }}</pre>
          <input v-if="editable" v-model="cust.secondPhonenumber" @change="updateCustomer()"
            :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
        </div>
        &nbsp;
        <div class="data-field" id="third-e-mail">
          <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">3. E-Mail:</pre>
          <pre v-if="!editable"
            :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.thirdEMail }}</pre>
          <input v-if="editable" v-model="cust.thirdEMail" @change="updateCustomer()"
            :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
        </div>
        <div class="data-field" id="third-phone">
          <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">3. Phone number:</pre>
          <pre v-if="!editable"
            :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.thirdPhonenumber }}</pre>
          <input v-if="editable" v-model="cust.thirdPhonenumber" @change="updateCustomer()"
            :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
        </div>
      </div>
      <div class="address">
        <pre :class="['area-title', darkMode ? 'h3-darkmode' : 'h3-lightmode']">Addresses:</pre>
        <div class="street-address">
          <div class="data-field" id="street-name">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">1. Street:</pre>
            <pre v-if="!editable"
              :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.street }}</pre>
            <input v-if="editable" v-model="cust.street" @change="updateCustomer()"
              :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
          <div class="data-field" id="street-no">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">1. No.:</pre>
            <pre v-if="!editable" :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.no }}</pre>
            <input v-if="editable" v-model="cust.no" @change="updateCustomer()"
              :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
        </div>
        <div class="postcode-city">
          <div class="data-field" id="postal-code">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">1. Postcode:</pre>
            <pre v-if="!editable"
              :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.postcode }}</pre>
            <input v-if="editable" v-model="cust.postcode" @change="updateCustomer()"
              :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
          <div class="data-field" id="city">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">1. City:</pre>
            <pre v-if="!editable" :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.city }}</pre>
            <input v-if="editable" v-model="cust.city" @change="updateCustomer()"
              :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
        </div>
        &nbsp;
        <div class="street-address">
          <div class="data-field" id="street-name">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">2. Street:</pre>
            <pre v-if="!editable"
              :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.secondStreet }}</pre>
            <input v-if="editable" v-model="cust.secondStreet" @change="updateCustomer()"
              :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
          <div class="data-field" id="street-no">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">2. No.:</pre>
            <pre v-if="!editable"
              :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.secondNo }}</pre>
            <input v-if="editable" v-model="cust.secondNo" @change="updateCustomer()"
              :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
        </div>
        <div class="postcode-city">
          <div class="data-field" id="postal-code">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">2. Postcode:</pre>
            <pre v-if="!editable"
              :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.secondPostcode }}</pre>
            <input v-if="editable" v-model="cust.secondPostcode" @change="updateCustomer()"
              :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
          <div class="data-field" id="city">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">2. City:</pre>
            <pre v-if="!editable"
              :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.secondCity }}</pre>
            <input v-if="editable" v-model="cust.secondCity" @change="updateCustomer()"
              :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
        </div>
        &nbsp;
        <div class="street-address">
          <div class="data-field" id="street-name">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">3. Street:</pre>
            <pre v-if="!editable"
              :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.thirdStreet }}</pre>
            <input v-if="editable" v-model="cust.thirdStreet" @change="updateCustomer()"
              :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
          <div class="data-field" id="street-no">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">3. No.:</pre>
            <pre v-if="!editable"
              :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.thirdNo }}</pre>
            <input v-if="editable" v-model="cust.thirdNo" @change="updateCustomer()"
              :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
        </div>
        <div class="postcode-city">
          <div class="data-field" id="postal-code">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">3. Postcode:</pre>
            <pre v-if="!editable"
              :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.thirdPostcode }}</pre>
            <input v-if="editable" v-model="cust.thirdPostcode" @change="updateCustomer()"
              :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
          <div class="data-field" id="city">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">3. City:</pre>
            <pre v-if="!editable"
              :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ cust.thirdCity }}</pre>
            <input v-if="editable" v-model="cust.thirdCity" @change="updateCustomer()"
              :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
        </div>
      </div>
    </div>
    <div class="notes">
      <pre :class="['area-title', darkMode ? 'h3-darkmode' : 'h3-lightmode']">Notes:</pre>
      <input v-model="cust.notes" :readonly="!editable" @change="updateCustomer()"
        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']" id="notes">
    </div>
  </section>
  <section v-if="addBool" :class="['information', darkMode ? 'section-darkmode' : 'section-lightmode']">
    <div>
      <input v-model="newCustomername"
        :class="['data', 'customer-name-input', darkMode ? 'h2-darkmode' : 'h2-lightmode', darkMode ? 'h2-input-darkmode' : 'h2-input-lightmode', 'input']">
    </div>
    <div class="data-field" id="customer-id">
      <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">ID:</pre>
      <pre :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">...</pre>
    </div>
    <div class="customer-data">
      <div class="contact">
        <pre :class="['area-title', darkMode ? 'h3-darkmode' : 'h3-lightmode']">Contacts:</pre>
        <div class="data-field" id="contact-person">
          <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">Contact person:</pre>
          <input v-model="newContactperson" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
        </div>
        <div class="data-field" id="first-e-mail">
          <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">1. E-Mail:</pre>
          <input v-model="newEMail" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
        </div>
        <div class="data-field" id="first-phone">
          <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">1. Phone number:</pre>
          <input v-model="newPhonenumber" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
        </div>
        &nbsp;
        <div class="data-field" id="second-e-mail">
          <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">2. E-Mail:</pre>
          <input v-model="newSecondEMail" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
        </div>
        <div class="data-field" id="second-phone">
          <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">2. Phone number:</pre>
          <input v-model="newSecondPhonenumber"
            :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
        </div>
        &nbsp;
        <div class="data-field" id="third-e-mail">
          <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">3. E-Mail:</pre>
          <input v-model="newThirdEMail" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
        </div>
        <div class="data-field" id="third-phone">
          <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">3. Phone number:</pre>
          <input v-model="newThirdPhonenumber"
            :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
        </div>
      </div>
      <div class="address">
        <pre :class="['area-title', darkMode ? 'h3-darkmode' : 'h3-lightmode']">Addresses:</pre>
        <div class="street-address">
          <div class="data-field" id="street-name">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">1. Street:</pre>
            <input v-model="newStreet" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
          <div class="data-field" id="street-no">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">1. No.:</pre>
            <input v-model="newNo" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
        </div>
        <div class="postcode-city">
          <div class="data-field" id="postal-code">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">1. Postcode:</pre>
            <input v-model="newPostcode" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
          <div class="data-field" id="city">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">1. City:</pre>
            <input v-model="newCity" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
        </div>
        &nbsp;
        <div class="street-address">
          <div class="data-field" id="street-name">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">2. Street:</pre>
            <input v-model="newSecondStreet" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
          <div class="data-field" id="street-no">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">2. No.:</pre>
            <input v-model="newSecondNo" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
        </div>
        <div class="postcode-city">
          <div class="data-field" id="postal-code">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">2. Postcode:</pre>
            <input v-model="newSecondPostcode"
              :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
          <div class="data-field" id="city">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">2. City:</pre>
            <input v-model="newSecondCity" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
        </div>
        &nbsp;
        <div class="street-address">
          <div class="data-field" id="street-name">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">3. Street:</pre>
            <input v-model="newThirdStreet" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
          <div class="data-field" id="street-no">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">3. No.:</pre>
            <input v-model="newThirdNo" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
        </div>
        <div class="postcode-city">
          <div class="data-field" id="postal-code">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">3. Postcode:</pre>
            <input v-model="newThirdPostcode" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
          <div class="data-field" id="city">
            <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">3. City:</pre>
            <input v-model="newThirdCity" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
          </div>
        </div>
      </div>
    </div>
    <div class="notes">
      <pre :class="['area-title', darkMode ? 'h3-darkmode' : 'h3-lightmode']">Notes:</pre>
      <input v-model="newNotes" :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']" id="notes">
    </div>
  </section>
  <section v-if="addBool" id="saveNewItem">
    <button :class="[darkMode ? 'saveNewItem-darkmode' : 'saveNewItem-lightmode']" @click="addCustomer()">Save</button>
  </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 editable = computed(() => store.state.editable);
const chosenCustomerId = computed(() => store.state.chosenCustomerId);
const deleteBool = computed(() => store.state.deleteBool);
const addBool = computed(() => store.state.new);

const darkMode = ref('')
const cust = ref({});
const customers = ref([]);

const newCustomername = ref('');
const newCustomerID = ref('');
const newContactperson = ref('');
const newEMail = ref('');
const newSecondEMail = ref('');
const newThirdEMail = ref('');
const newPhonenumber = ref('');
const newSecondPhonenumber = ref('');
const newThirdPhonenumber = ref('');
const newStreet = ref('');
const newNo = ref('');
const newPostcode = ref('');
const newCity = ref('');
const newSecondStreet = ref('');
const newSecondNo = ref('');
const newSecondPostcode = ref('');
const newSecondCity = ref('');
const newThirdStreet = ref('');
const newThirdNo = ref('');
const newThirdPostcode = ref('');
const newThirdCity = ref('');
const newNotes = ref('');

// get customer from id
const getCustomerById = async () => {
  if (!addBool.value) {
    try {
      const response = await Axios.get(
        `https://${clientsideConfig.url}:${clientsideConfig.port}/api/getCustomerById/${chosenCustomerId.value}`
      );
      cust.value = response.data;
    } catch (err) {
      console.log(err.response.statusText);
    }
  }
}

//update data
const updateCustomer = async () => {
  if (cust.value.customername.trim() === "") {
    alert("Please add a customer name!");
    return;
  } else {
    var counter = 0;
    // check if customer name already exists
    customers.value.forEach(c => {
      if (c.customername === cust.value.customername) {
        counter += 1;
      }
    });
    if (counter == 2) {
      alert("This customer name already exists. Please choose an unique customer name or modify respectively delete the old one!");
      cust.value.customername = '';
      return;
    }
    if (!validateEMail(cust.value.EMail)) {
      alert("Please add a valid first email!");
      return;
    }
    if (!validatePhonenumber(cust.value.phonenumber)) {
      alert("Please add a valid first phonenumber!");
      return;
    }
    if (!validateEMail(cust.value.secondEMail)) {
      alert("Please add a valid second email!");
      return;
    }
    if (!validatePhonenumber(cust.value.secondPhonenumber)) {
      alert("Please add a valid second phonenumber!");
      return;
    }
    if (!validateEMail(cust.value.thirdEMail)) {
      alert("Please add a valid third email!");
      return;
    }
    if (!validatePhonenumber(cust.value.thirdPhonenumber)) {
      alert("Please add a valid third phonenumber!");
      return;
    }
    if (!validatePostcode(cust.value.postcode)) {
      alert("Please add a valid first postcode!");
      return;
    }
    if (!validatePostcode(cust.value.secondPostcode)) {
      alert("Please add a valid second postcode!");
      return;
    }
    if (!validatePostcode(cust.value.thirdPostcode)) {
      alert("Please add a valid third postcode!");
      return;
    }
    if (cust.value.contactperson.trim() === "") {
      alert("Please add a contactperson!");
      return;
    }
    if (cust.value.street.trim() === "") {
      alert("Please add a first street!");
      return;
    }
    if (cust.value.no.trim() === "") {
      alert("Please add a first number!");
      return;
    }
    if (cust.value.city.trim() === "") {
      alert("Please add a first city!");
      return;
    }
    try {
      await Axios.put(
        `https://${clientsideConfig.url}:${clientsideConfig.port}/api/updateCustomer`,
        {
          customerID: cust.value.customerID,
          customername: cust.value.customername,
          contactperson: cust.value.contactperson,
          EMail: cust.value.EMail,
          secondEMail: cust.value.secondEMail,
          thirdEMail: cust.value.thirdEMail,
          phonenumber: cust.value.phonenumber,
          secondPhonenumber: cust.value.secondPhonenumber,
          thirdPhonenumber: cust.value.thirdPhonenumber,
          street: cust.value.street,
          no: cust.value.no,
          postcode: cust.value.postcode,
          city: cust.value.city,
          secondStreet: cust.value.secondStreet,
          secondNo: cust.value.secondNo,
          secondPostcode: cust.value.secondPostcode,
          secondCity: cust.value.secondCity,
          thirdStreet: cust.value.thirdStreet,
          thirdNo: cust.value.thirdNo,
          thirdPostcode: cust.value.thirdPostcode,
          thirdCity: cust.value.thirdCity,
          notes: cust.value.notes,
        }
      )
      await getCustomerById();
    } catch (err) {
      console.log(err.response.statusText);
    }
  }
}

// add new customer
const addCustomer = async () => {
  // check if all input data is valid
  if (newCustomername.value.trim() === "") {
    alert("Please add a customer name!");
    return;
  } else {
    var counter = 0;
    // check if customer name already exists
    customers.value.forEach(c => {
      if (c.customername === newCustomername.value) {
        counter += 1;
      }
    });
    if (counter == 1) {
      alert("This customer name already exists. Please choose an unique customer name or modify respectively delete the old one!");
      return;
    }
  }
  if (!validateEMail(newEMail.value)) {
    alert("Please add a valid first email!");
    return;
  }
  if (!validatePhonenumber(newPhonenumber.value)) {
    alert("Please add a valid first phonenumber!");
    return;
  }
  if (!validateEMail(newSecondEMail.value)) {
    alert("Please add a valid second email!");
    return;
  }
  if (!validatePhonenumber(newSecondPhonenumber.value)) {
    alert("Please add a valid second phonenumber!");
    return;
  }
  if (!validateEMail(newThirdEMail.value)) {
    alert("Please add a valid third email!");
    return;
  }
  if (!validatePhonenumber(newThirdPhonenumber.value)) {
    alert("Please add a valid third phonenumber!");
    return;
  }
  if (!validatePostcode(newPostcode.value)) {
    alert("Please add a valid first postcode!");
    return;
  }
  if (!validatePostcode(newSecondPostcode.value)) {
    alert("Please add a valid second postcode!");
    return;
  }
  if (!validatePostcode(newThirdPostcode.value)) {
    alert("Please add a valid third postcode!");
    return;
  }
  if (newContactperson.value.trim() === "") {
    alert("Please add a contactperson!");
    return;
  }
  if (newStreet.value.trim() === "") {
    alert("Please add a first street!");
    return;
  }
  if (newNo.value.trim() === "") {
    alert("Please add a first number!");
    return;
  }
  if (newCity.value.trim() === "") {
    alert("Please add a first city!");
    return;
  }
  try {
    const response = await Axios.post(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/addCustomer`,
      {
        customername: newCustomername.value,
        contactperson: newContactperson.value,
        EMail: newEMail.value,
        secondEMail: newSecondEMail.value,
        thirdEMail: newThirdEMail.value,
        phonenumber: newPhonenumber.value,
        secondPhonenumber: newSecondPhonenumber.value,
        thirdPhonenumber: newThirdPhonenumber.value,
        street: newStreet.value,
        no: newNo.value,
        postcode: newPostcode.value,
        city: newCity.value,
        secondStreet: newSecondStreet.value,
        secondNo: newSecondNo.value,
        secondPostcode: newSecondPostcode.value,
        secondCity: newSecondCity.value,
        thirdStreet: newThirdStreet.value,
        thirdNo: newThirdNo.value,
        thirdPostcode: newThirdPostcode.value,
        thirdCity: newThirdCity.value,
        notes: newNotes.value,
      });
    store.commit('resetStore');
    store.commit('changeToCustomerlist');
  } catch (err) {
    console.log(err.response.statusText);
  }
}

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

const confirmDeleteCustomer = async () => {
  if (deleteBool.value === true) {
    if (confirm("Do you really want to delete this customer? It cannot be undone!")) {
      try {
        await Axios.delete(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/deleteCustomer/${chosenCustomerId.value}`);
        store.commit('undoDelete');
        store.commit('resetStore');
        store.commit('changeToCustomerlist');
      } catch (err) {
        console.log(err.response.statusText);
      }
    } else {
      store.commit('undoDelete');
    }
  }
}

// include delay to avoid 503 error
const triggerBackendCallsWithDelay = async (fetchDataFunc) => {
  setTimeout(() => {
    fetchDataFunc();
  }, 1500);
}

// to validate the email address
const validateEMail = (email) => {
  const emailRegex = new RegExp(/^[A-Za-z0-9_!#$%&'*+\/=?`{|}~^.-]+@[A-Za-z0-9.-]+$/, "gm");
  if (email === "") {
    return true
  }
  return emailRegex.test(email);
}

// to validate the phonenumber
const validatePhonenumber = (number) => {
  const mobilephonenumberRegex = new RegExp(/^(((((00|\+){1,2})49([ \-/]?|[ \()/]?))|0)[1]([0-2]|[4-7])([0-9]{1})([ \-/]?|[ \()/]?)([0-9]{8}))$/, "gm");
  const landlineRegex = new RegExp(/^0(30|40|69|89|20[12389]|21[24]|228|234|335|340|3[3-9][15]|906|[2-9][1-9]1|3(?:3(?:[0237][127]|0[3-6]|2[89]|3[458]|[46][1246]|7[589]|8[1256]|9[145])|4(?:[2347][135]|[34]7|[69][146]|48|62|76|93)|5(?:[04679][14]|[238][135]|[2469]2|[4789]6|[278]8|63|73|37|29)|6(?:[024689][13]|[03789]5|06|2[2489]|3[1246]|44|47|7[1279]|82|86)|7(?:[236][135]|2[2467]|37|4[145]|62|64|7[1-4])|8(?:[234678]1|3[468]|4[347]|6[035-9]|7[467]|83|86)|9(?:[236][135]|[04689]4|[04789]1|[03468]7|02|09|28|4[369]|6[2689]|73|76|96|98))|3[3-9][0-9]{3}|13[078]|180[235]|18[01]|19[0-9]{3}|[789]00|[2-9][0-9]{3})([0-9]{1,})$/, "gm");
  if (number === "") {
    return true
  }
  if (mobilephonenumberRegex.test(number)) {
    return true
  } else if (landlineRegex.test(number)) {
    return true;
  }
  return false;
}

// to validate the postcode
const validatePostcode = (postcode) => {
  const postcodeRegex = new RegExp(/^\d{5}$/);
  if (postcode === "") {
    return true
  }
  return postcodeRegex.test(postcode);
}

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(deleteBool, confirmDeleteCustomer);
watch(modeChanged, getSession)

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

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


<style scoped>
.information {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: center;
  padding: 1.25rem 1.875rem;
  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-name {
  align-self: stretch;
  padding: 1rem 0;
  margin: 1rem;
  letter-spacing: 5%;
  text-decoration-line: underline;
  font: italic 400 1rem/187.5% Overpass, -apple-system, Roboto, Helvetica,
    sans-serif;
}

.customer-name-input {
  padding: 1rem 0;
  margin: 1rem;
  font-size: initial;
  letter-spacing: 5%;
  text-decoration-line: underline;
  font: italic 400 1rem/187.5% Overpass, -apple-system, Roboto, Helvetica,
    sans-serif;
}

.h2-darkmode {
  color: #fff;
}

.h2-lightmode {
  color: #000;
}

.data-field {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  padding: 0.8rem 1.875rem;
  gap: 1.25rem;
  border-radius: 0.625rem;
}

.data-field#customer-id {
  padding: 0.625rem 1.875rem;
}

.data-field#street-no {
  padding-left: 3.2rem;
}

.label {
  letter-spacing: 0.03rem;
  font: 400 0.875rem/1.875rem Overpass, sans-serif;
}

.h2-input-darkmode {
  background-color: #212121;
}

.h2-input-lightmode {
  background-color: #EBEBEB;
}

pre {
  margin: 0;
}

.pre-darkmode {
  color: #ffffff;
}

.pre-lightmode {
  color: #000000;
}

.saveNewItem-darkmode {
  background: #2c2c2c;
  color: #fff;
  font: 400 0.875rem/1.875rem Overpass, sans-serif;
  border: none;
  padding: 1rem 1.875rem;
  width: 8%;
  border-radius: 0.625rem;
  box-shadow: 0.25rem 0.25rem 0.25rem 0rem rgba(0, 0, 0, 0.25);
}

.saveNewItem-lightmode {
  background: #EBEBEB;
  color: #212121;
  font: 400 0.875rem/1.875rem Overpass, sans-serif;
  border: none;
  padding: 1rem 1.875rem;
  width: 8%;
  border-radius: 0.625rem;
  box-shadow: 0.25rem 0.25rem 0.25rem 0rem rgba(0, 0, 0, 0.25);
}

.saveNewItem-darkmode:hover {
  background-color: #444444;
  cursor: pointer;
}

.saveNewItem-lightmode:hover {
  background-color: #ACACAC;
  cursor: pointer;
}

#saveNewItem {
  text-align: center;
}

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

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

.data-lightmode {
  background-color: #ebebeb;
  color: #000000;
}

.customer-data {
  display: flex;
  flex: auto;
  flex-direction: row;
  padding: 1.25rem 1.875rem;
  align-items: center;
  justify-content: stretch;
  gap: 0.625rem
}

.contact,
.address {
  display: flex;
  flex: auto;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 0.625rem;
}

.area-title {
  letter-spacing: 0.03rem;
  font: 400 0.875rem/1.875rem Overpass, sans-serif;
}

.h3-darkmode {
  color: #ffffff;
}

.h3-lightmode {
  color: #000000;
}


.street-address,
.postcode-city {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  align-self: stretch;
  gap: 0.625rem;
}

.notes {
  display: flex;
  flex-direction: column;
  padding: 0.625rem 1.875rem;
  align-items: flex-start;
  justify-content: center;
  gap: 0.625rem;
}

#notes {
  align-self: stretch;
}

.input {
  border: none;
}
</style>