<template>
  <Header />

  <div class="flex">
    <div class="">
      <SideBar />
    </div>
    <div class=" w-full px-10">
      <div class="text-sm breadcrumbs mb-10">
        <ul>
          <li>
            <router-link :to="{ name: 'home' }">
              Home
            </router-link>
          </li>
          <li>
            <router-link :to="{ name: 'customer' }">
              Customers
            </router-link>
          </li>
        </ul>
      </div>

      <div class="grid grid-cols-1 rounded-md  pb-5">
        <div class="text-2xl  border-b-2 border-gray-500 mb-10">
          View Oil Order # {{ deliveryOrder.id }}
        </div>
      </div>
      <div class="grid grid-cols-12">

        <div class="col-span-6">
          <div class="col-span-12 font-bold">
            Customer
          </div>
          <div class="col-span-6 p-5 text-gray-500 text-sm">
            <div class="grid grid-cols-12">
              <div class="col-span-12  flex  ">
                {{ customer.account_number }}
              </div>
              <div class="col-span-12  flex">
                {{ customer.customer_first_name }}
                {{ customer.customer_last_name }}
              </div>
              <div class="col-span-12 flex ">
                <div class="pr-2">
                  {{ customer.customer_town }},
                </div>
                <div class="pr-2">
                  <div v-if="customer.customer_state == 0">Massachusetts</div>
                  <div v-else-if="customer.customer_state == 1">Rhode Island</div>
                  <div v-else-if="customer.customer_state == 2">New Hampshire</div>
                  <div v-else-if="customer.customer_state == 3">Maine</div>
                  <div v-else-if="customer.customer_state == 4">Vermont</div>
                  <div v-else-if="customer.customer_state == 5">Maine</div>
                  <div v-else-if="customer.customer_state == 6">New York</div>
                  <div v-else>Unknown state</div>
                </div>
                <div class="pr-2">
                  {{ customer.customer_zip }}
                </div>
              </div>

              <div class="col-span-12 flex" v-if="customer.customer_apt !== 'None'">
                {{ customer.customer_apt }}
              </div>
              <div class="col-span-12  flex">
                <div v-if="customer.customer_home_type == 0">Residential</div>
                <div v-else-if="customer.customer_home_type == 1">apartment</div>
                <div v-else-if="customer.customer_home_type == 2">condo</div>
                <div v-else-if="customer.customer_home_type == 3">commercial</div>
                <div v-else-if="customer.customer_home_type == 4">business</div>
                <div v-else-if="customer.customer_home_type == 5">construction</div>
                <div v-else-if="customer.customer_home_type == 6">container</div>
              </div>
              <div class="col-span-12 flex">
                {{ customer.customer_phone_number }}
              </div>
            </div>
          </div>
        </div>
        <div class="col-span-6  ">

        </div>
      </div>






      <div class="grid grid-cols-12">



        <div class="col-span-6">
          <div class="col-span-12 font-bold">
            Delivery Status
          </div>
          <div class="grid grid-cols-12 p-5">
            <div class="col-span-12 font-bold">
              Status
            </div>
            <div class="col-span-12 text-sm mb-5 text-gray-500">
              <div v-if="deliveryOrder.delivery_status == 0">waiting</div>
              <div v-else-if="deliveryOrder.delivery_status == 1">delivered</div>
              <div v-else-if="deliveryOrder.delivery_status == 2">Out for Delivery</div>
              <div v-else-if="deliveryOrder.delivery_status == 3">Cancelled</div>
              <div v-else-if="deliveryOrder.delivery_status == 4">Partial Delivery</div>
              <div v-else-if="deliveryOrder.delivery_status == 5">misdelivery</div>
              <div v-else-if="deliveryOrder.delivery_status == 6">unknown</div>
              <div v-else-if="deliveryOrder.delivery_status == 10">Finalized</div>
              <div v-else></div>
            </div>


            <div class="col-span-12 font-bold ">
              Scheduled date/time
            </div>
            <div class="col-span-12 mb-5 text-sm text-gray-500">
              {{ deliveryOrder.expected_delivery_date }}
            </div>


            <div class="col-span-12 font-bold">
              When Called
            </div>
            <div class="col-span-12 mb-5 text-sm text-gray-500">
              {{ format_date(deliveryOrder.when_ordered) }}
            </div>
            <div v-if="deliveryOrder.delivery_status == 10">
            <div class="col-span-12 font-bold">
              When Delivered
            </div>
            <div class="col-span-12 mb-5 text-sm text-gray-500">
              {{ format_date(deliveryOrder.when_delivered) }}
            </div>
            </div>


            <div class="col-span-12 font-bold ">
              Driver:
            </div>
            <div class="col-span-12 text-gray-500">
              {{ deliveryOrder.driver_first_name }} {{ deliveryOrder.driver_last_name }}
            </div>
          </div>
          <div class="col-span-12 font-bold">
            Info
          </div>
          <div class="grid grid-cols-12 p-5">
            <div class="col-span-12 text-gray-500">
              <div v-if="deliveryOrder.prime == 1">
                Prime Required: Yes
              </div>
              <div v-if="deliveryOrder.prime == 0">
                Prime Required: No
              </div>
            </div>
            <div class="col-span-12 text-gray-500">
              <div v-if="deliveryOrder.same_day == 1">
                Same Day: Yes
              </div>
              <div v-if="deliveryOrder.same_day == 0">
                Same Day: No
              </div>
            </div>
          </div>
        </div>



        <div class="col-span-6">
          <div class="col-span-12 font-bold">
            Gallons
          </div>
          <div class="grid grid-cols-12 p-5">
            <div class="col-span-12 text-sm text-gray-500">
              <div v-if="deliveryOrder.customer_asked_for_fill == 1">FILL</div>
              <div v-else>{{ deliveryOrder.gallons_ordered }} gallons</div>
            </div>
          </div>

          <div v-if="deliveryOrder.delivery_status !== 10">
          <div class="col-span-12 font-bold">
            Promo
          </div>
          <div class="grid grid-cols-12 p-5">
            <div class="col-span-12 text-sm text-gray-500">
              <div v-if="deliveryOrder.promo_id !== null">
                <div class="">{{ promo.name_of_promotion }}</div>
                <div class="">{{ promo.description }}</div>
                <div class="">{{ promo.money_off_delivery }} off a gallon</div>
                <div class="">{{ promo.text_on_ticket }}</div>
              </div>
              <div v-else>No Promo Added</div>
            </div>
          </div>
          </div>

          <div v-if="deliveryOrder.delivery_status !== 10">
            <div class="col-span-12 font-bold">
              Estimated Amount
            </div>
            <div class="grid grid-cols-12 p-5">
              <div class="col-span-12 text-sm text-gray-500">
                <div class="col-span-12 text-gray-500">
                <div v-if="deliveryOrder.customer_asked_for_fill == 1"> FILL (250)</div>
                <div v-else> Gallons Ordered: {{ deliveryOrder.gallons_ordered }}</div>
              </div>

              <div v-if="deliveryOrder.promo_id !== null">
                <div class="col-span-12 text-sm text-gray-500">
                  Before Discount: {{ total_amount }}
                </div>
                <div class="col-span-12 text-sm text-gray-500">
                 Discount Amount: ${{ discount }}
                </div>
                <div class="col-span-12 text-sm text-gray-500">
                 Promo Amount: ${{ total_amount_after_discount }}
                </div>
              </div>
              <div v-else>
                <div class="col-span-12 text-sm text-gray-500">
                  ${{ total_amount }} 
                </div>
              </div>

              <div class="col-span-12 py-3" v-if="deliveryOrder.prime == 1">
                Prime Fee: {{ pricing.price_prime }}
              </div>
              <div class="col-span-12 py-3" v-if="deliveryOrder.emergency == 1">
                Emergency Fee: {{ pricing.price_emergency }}
              </div>
              <div class="col-span-12 py-3" v-if="deliveryOrder.same_day == 1">
                Same Day: {{ pricing.price_same_day }}
              </div>                
              </div>
            </div>
          </div>


          <div v-if="deliveryOrder.delivery_status == 10">
            <div class="col-span-12 font-bold" v-if="deliveryOrder.delivery_status == 10">
              Finalized Amount
            </div>
            <div class="grid grid-cols-12 p-5">
              <div class="col-span-12 text-sm text-gray-500">
                <div>{{ deliveryMoney.total_amount_oil }}</div>
              </div>
            </div>
          </div>



          
          <div class="col-span-12 font-bold">
            Payment
          </div>
          <div class="grid grid-cols-12 p-5">
            <div class="col-span-12 text-sm">
              <div v-if="deliveryOrder.payment_type == 0">Cash</div>
              <div v-else-if="deliveryOrder.payment_type == 1">Credit Card</div>
              <div v-else-if="deliveryOrder.payment_type == 2">Credit Card & cash</div>
              <div v-else-if="deliveryOrder.payment_type == 3">Check</div>
              <div v-else-if="deliveryOrder.payment_type == 4">Check</div>
              <div v-else>No Payment Type Added</div>
            </div>

            <div class="col-span-12" v-if="deliveryOrder.payment_type == 1">
              <div class="flex" v-if="userCardfound">
                <div class="basis-1/3 p-2">
                  <div class="rounded-md border-2 bg-accent">
                    <div class="flex p-3">
                      {{ userCard.type_of_card }}
                    </div>
                    <div class="flex p-1 pl-4">
                      {{ userCard.name_on_card }}
                    </div>
                    <div class="flex p-1 pl-4">
                      ****-****-****-{{ userCard.last_four_digits }}
                    </div>
                    <div class="flex p-1 pl-4">
                      {{ userCard.expiration_month }}/ {{ userCard.expiration_year }}
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <div class="col-span-12" v-if="deliveryOrder.payment_type == 2">
              <div class="flex" v-if="userCardfound">
                <div class="basis-1/3 p-2">
                  <div class="bg-accent rounded-md border-2 ">
                    <div class="flex p-3">
                      {{ userCard.type_of_card }}
                    </div>
                    <div class="flex p-1 pl-4">
                      {{ userCard.name_on_card }}
                    </div>
                    <div class="flex p-1 pl-4">
                      ****-****-****-{{ userCard.last_four_digits }}
                    </div>
                    <div class="flex p-1 pl-4">
                      {{ userCard.expiration_month }}/ {{ userCard.expiration_year }}
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <div class="col-span-12" v-if="deliveryOrder.payment_type == 3">
              <div class="flex" v-if="userCardfound">
                <div class="basis-1/3 p-2">{{ userCard }}
                  <div class="bg-accent rounded-md border-2 ">
                    <div class="flex p-3">
                      {{ userCard.type_of_card }}
                    </div>
                    <div class="flex p-1 pl-4">
                      {{ userCard.name_on_card }}
                    </div>
                    <div class="flex p-1 pl-4">
                      ****-****-****-{{ userCard.last_four_digits }}
                    </div>
                    <div class="flex p-1 pl-4">
                      {{ userCard.expiration_month }}/ {{ userCard.expiration_year }}
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="col-span-12 flex justify-center" v-if="deliveryOrder.id">
          <router-link :to="{ name: 'deliveryEdit', params: { id: deliveryOrder.id } }">
            <button class="btn btn-accent btn-sm">Edit Oil Order</button>
          </router-link>
        </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'
import useValidate from "@vuelidate/core";
import { notify } from "@kyvg/vue3-notification"
import moment from 'moment';

export default defineComponent({
  name: 'deliveryOrder',

  components: {
    Header,
    SideBar,
    Footer,
  },

  data() {
    return {
      v$: useValidate(),
      user: {
        id: 0
      },
      priceprime: 0,
      pricesameday: 0,
      priceemergency: 0,
      total_amount: 0,
      discount: 0,
      total_amount_after_discount: 0,
      deliveryNotesDriver: [],
      userCardfound: false,
      userCard: {
        date_added: '',
        user_id: '',
        card_number: '',
        last_four_digits: '',
        name_on_card: '',
        expiration_month: '',
        expiration_year: '',
        type_of_card: '',
        security_number: '',
        accepted_or_declined: '',
        main_card: '',
      },

      customer: {
        account_number: '',
        id: 0,
        user_id: 0,
        customer_first_name: '',
        customer_last_name: '',
        customer_town: '',
        customer_state: 0,
        customer_zip: '',
        customer_apt: '',
        customer_home_type: 0,
        customer_phone_number: '',
      },

      deliveryMoney: {
        time_added: '',
        total_amount_oil: '',
        total_amount_emergency: '',
        total_amount_same_day: '',
        total_amount_prime: '',
        total_amount_fee: '',
        total_discount_amount: '',
        total_discount_total: '',
        total_amount: '',
      },
      promo: {
        id: 0,
        name_of_promotion: '',
        description: '',
        money_off_delivery: '',
        text_on_ticket: ''
      },
      pricing: {
        price_from_supplier: 0,
        price_for_customer: 0,
        price_for_employee: 0,
        price_same_day: 0,
        price_prime: 0,
        price_emergency: 0,
        date: "",
      },
      deliveryOrder: {
        id: '',
        customer_id: 0,
        customer_name: '',
        customer_address: '',
        customer_town: '',
        customer_state: 0,
        customer_zip: '',
        gallons_ordered: 0,
        customer_asked_for_fill: 0,
        gallons_delivered: '',
        customer_filled: 0,
        delivery_status: 0,
        when_ordered: '',
        when_delivered: '',
        expected_delivery_date: '',
        automatic: 0,
        oil_id: 0,
        supplier_price: '',
        customer_price: '',
        customer_temperature: '',
        dispatcher_notes: '',
        prime: 0,
        same_day: 0,
        emergency: 0,
        payment_type: 0,
        payment_card_id: '',
        driver_employee_id: 0,
        driver_first_name: '',
        driver_last_name: '',
        promo_id: 0,
      },
    }
  },

  created() {
    this.userStatus()
  },
  watch: {
    $route() {
      this.getOilOrder(this.$route.params.id);
      this.getOilOrderMoney(this.$route.params.id);
      this.sumdelivery(this.$route.params.id);
    },
  },
  mounted() {
    this.getOilOrder(this.$route.params.id);
    this.getOilOrderMoney(this.$route.params.id);
    this.sumdelivery(this.$route.params.id);
    this.getOilPricing()
  },

  methods: {
    format_date(value: string) {
      if (value) {
        return moment(String(value)).format('LLLL')
      }
    },
    deleteCall(delivery_id: any) {
      let path = import.meta.env.VITE_BASE_URL + '/delivery/delete/' + delivery_id;
      axios({
        method: 'delete',
        url: path,
        headers: authHeader(),
      }).then((response: any) => {
        if (response.data.ok) {
          notify({
            title: "Success",
            text: "deleted oil order",
            type: "success",
          });
          this.$router.push({ name: "customerProfile", params: { id: this.customer.id } });
        } else {
          notify({
            title: "Failure",
            text: "error deleting oil order",
            type: "success",
          });
        }
      })
    },
    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.user.id = response.data.user_id;
          }
        })
    },
    getOilPricing() {
      let path = import.meta.env.VITE_BASE_URL + "/info/price/oil/table";
      axios({
        method: "get",
        url: path,
        withCredentials: true,
      })
        .then((response: any) => {
          this.pricing = response.data;
        })
        .catch(() => {
          notify({
            title: "Error",
            text: "Could not get oil pricing",
            type: "error",
          });
        });
    },
    getCustomer(user_id: any) {
      let path = import.meta.env.VITE_BASE_URL + "/customer/" + user_id;
      axios({
        method: "get",
        url: path,
        withCredentials: true,
      })
        .then((response: any) => {
          this.customer = response.data;
        })
        .catch(() => {
          notify({
            title: "Error",
            text: "Could not find customer",
            type: "error",
          });
        });
    },
    getPaymentCard(card_id: any) {
      if (card_id) {
        let path = import.meta.env.VITE_BASE_URL + "/payment/card/" + card_id;
        axios({
          method: "get",
          url: path,
          withCredentials: true,
        })
          .then((response: any) => {

            if (response.data.userCard.card_number === '') {
              this.userCard === null;
              this.userCardfound = false;
            }
            else {
              this.userCard = response.data;
              this.userCardfound = true;
            }
          })
          .catch(() => {
          });
      }
      else {
        this.userCardfound = false;
      }
    },

    getOilOrder(delivery_id: any) {
      let path = import.meta.env.VITE_BASE_URL + "/delivery/order/" + delivery_id;
      axios({
        method: "get",
        url: path,
        withCredentials: true,
        headers: authHeader(),
      })
        .then((response: any) => {
          if (response.data) {
            this.deliveryOrder = response.data
            this.getCustomer(this.deliveryOrder.customer_id)
            if (this.deliveryOrder.payment_type == 1) {
              this.getPaymentCard(this.deliveryOrder.payment_card_id);
            }
            if (this.deliveryOrder.payment_type == 2) {
              this.getPaymentCard(this.deliveryOrder.payment_card_id);
            }
            if (this.deliveryOrder.payment_type == 3) {
              this.getPaymentCard(this.deliveryOrder.payment_card_id);
            }
            if (this.deliveryOrder.promo_id != null) {
              this.getPromo(this.deliveryOrder.promo_id);
            }

          }
        })
    },
    getOilOrderMoney(delivery_id: any) {
      let path = import.meta.env.VITE_MONEY_URL + "/delivery/order/money/" + delivery_id;
      axios({
        method: "get",
        url: path,
        withCredentials: true,
        headers: authHeader(),
      })
        .then((response: any) => {
          if (response.data) {
            this.deliveryMoney = response.data
          }
        })
    },
    sumdelivery(delivery_id: any) {
      let path = import.meta.env.VITE_BASE_URL + "/delivery/total/" + delivery_id;
      axios({
        method: "get",
        url: path,
        withCredentials: true,
      })
        .then((response: any) => {
          if (response.data.ok) {
            this.priceprime = response.data.priceprime;
            this.pricesameday = response.data.pricesameday;
            this.priceemergency = response.data.priceemergency;
            this.total_amount = response.data.total_amount;
            this.discount = response.data.discount;
            this.total_amount_after_discount = response.data.total_amount_after_discount;
          }
        })
        .catch(() => {
          notify({
            title: "Error",
            text: "Could not get oil pricing",
            type: "error",
          });
        });
    },

    getPromo(promo_id: any) {
      let path = import.meta.env.VITE_BASE_URL + "/promo/" + promo_id;
      axios({
        method: "get",
        url: path,
        withCredentials: true,
        headers: authHeader(),
      })
        .then((response: any) => {
          if (response.data) {
            this.promo = response.data
          }
        })
    },
  },


})
</script>

<style scoped></style>