<template>
  <Header />
  <div class="flex">
    <div class="">
      <SideBar />
    </div>
    <div class=" w-full px-10 ">
      <div class="text-sm breadcrumbs">
        <ul>
          <li>
            <router-link :to="{ name: 'home' }">
              Home
            </router-link>
          </li>
        </ul>
      </div>
      <div class="flex text-2xl mb-5">
        Welcome {{ employee.employee_first_name }} {{ employee.employee_last_name }}!
      </div>

      <div class="grid grid-cols-12 gap-5 ">
        <div class="col-span-12 bg-secondary  ">
          <div class="grid grid-cols-12 p-5 bg-neutral m-5">
            <div class="col-span-12 font-bold text-xl">Todays stats</div>
            <div class="col-span-6 py-2"> Total Deliveries: {{ delivery_count }}</div>
            <div class="col-span-6 py-2"> Completed: {{ delivery_count_delivered }} / {{ delivery_count }}</div>
          </div>
        </div>

        <div class="col-span-6 bg-secondary">
          <div class="grid grid-cols-12 p-5 bg-neutral m-5">
            <div class="col-span-12 font-bold text-xl">Todays Oil Price</div>
            <div class="col-span-12 py-2"> Price / Gallon: ${{ today_oil_price }}</div>
            <div class="col-span-12 py-2"> Same Day: ${{ price_same_day }}</div>
            <div class="col-span-12 py-2"> Prime: ${{ price_prime }}</div>
          
          </div>
        </div>
        <div class="col-span-6 bg-secondary">
          <div class="grid grid-cols-12 p-5 bg-neutral m-5">
            <div class="col-span-12 font-bold text-xl">Quick Tips</div>
            <div class="col-span-12 py-2"> search: @ = last name search</div>
            <div class="col-span-12 py-2"> search: ! = address</div>
            <div class="col-span-12 py-2"> search: # = phone number</div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <Footer />
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import axios from 'axios'
import authHeader from '../services/auth.header'
import Header from '../layouts/headers/headerauth.vue'
import SideBar from '../layouts/sidebar/sidebar.vue'
import Footer from '../layouts/footers/footer.vue'

export default defineComponent({
  name: 'Home',

  components: {
    Header,
    SideBar,
    Footer,
  },

  data() {
    return {
      token: null,
      delivery_count: 0,
      delivery_count_delivered: 0,
      price_from_supplier: 0,
      today_oil_price: 0,
      price_for_employee: 0,
      price_same_day: 0,
      price_prime: 0,
      user: {
        user_id: 0,
        user_name: '',
      },
      employee: {
        id: '',
        user_id: '',
        employee_last_name: "",
        employee_first_name: "",
        employee_town: "",
        employee_address: "",
        employee_apt: "",
        employee_zip: "",
        employee_birthday: "",
        employee_phone_number: "",
        employee_start_date: "",
        employee_end_date: "",
        employee_type: '',
        employee_state: '',
      },
      loaded: false,

    }
  },

  created() {
    this.userStatus()
    this.today_delivery_count()
    this.today_price_oil()

  },
  methods: {
    userStatus() {
      let path = import.meta.env.VITE_BASE_URL + '/auth/whoami';
      axios({
        method: "get",
        url: path,
        withCredentials: true,
        headers: authHeader(),
      })
        .then((response: any) => {
          if (response.data.ok) {
            this.user = response.data.user;
            this.employeeStatus()
          } else {

            localStorage.removeItem('user');
            this.$router.push('/login');
          }
        })

    },

    employeeStatus() {
      let path = import.meta.env.VITE_BASE_URL + '/employee/userid/' + this.user.user_id;
      axios({
        method: "get",
        url: path,
        withCredentials: true,
        headers: authHeader(),
      })
        .then((response: any) => {
          this.employee = response.data;
          this.loaded = true;

        })
    },

    today_delivery_count() {
      let path = import.meta.env.VITE_BASE_URL + '/stats/delivery/count/today'
      axios({
        method: "get",
        url: path,
        withCredentials: true,
        headers: authHeader(),
      })
        .then((response: any) => {
          this.delivery_count = response.data.data;
        })
    },
    today_delivery_delivered() {
      let path = import.meta.env.VITE_BASE_URL + '/stats/delivery/count/delivered/today'
      axios({
        method: "get",
        url: path,
        withCredentials: true,
        headers: authHeader(),
      })
        .then((response: any) => {
          this.delivery_count_delivered = response.data.data;
        })
    },
    today_price_oil() {
      let path = import.meta.env.VITE_BASE_URL + '/info/price/oil'
      axios({
        method: "get",
        url: path,
        withCredentials: true,
        headers: authHeader(),
      })
        .then((response: any) => {
          this.price_from_supplier = response.data.price_from_supplier;
          this.today_oil_price = response.data.price_for_customer;
          this.price_for_employee = response.data.price_for_employee;
          this.price_same_day = response.data.price_same_day;
          this.price_prime = response.data.price_prime;

        })
    },

  },
})
</script>

<style scoped></style>
<script setup lang="ts">
</script>