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

      <div class="grid grid-cols-1 rounded-md p-6 mb-5">
        <div class=" col-span-12 text-2xl">
          Confirm Payment Oil Delivery {{ delivery.id }}
        </div>
        <div class="grid grid-cols-12 mb-5 gap-5">
          
          <div class="col-span-12 xl:col-span-6 ">
            <div class="col-span-12 font-bold flex pb-5 text-lg">
              {{ customer.account_number }}
            </div>
            <div class="col-span-12 font-bold flex">
              {{ customer.customer_first_name }}
              {{ customer.customer_last_name }}
            </div>
            <div class="col-span-12 font-bold flex">
              {{ customer.customer_address }}
              <div v-if="customer.customer_apt != 'None'">
                {{ customer.customer_apt }}
              </div>
            </div>
            <div class="col-span-12 font-bold 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 font-bold flex" v-if="customer.customer_apt !== 'None'">
              {{ customer.customer_apt }}
            </div>
            <div class="col-span-12 font-bold 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 font-bold flex">
              {{ customer.customer_phone_number }}
            </div>


            <div class="col-span-12  ">
              <div class="grid grid-cols-12 mb-5">
                <div class="col-span-12 pt-10 font-bold">Delivery Status</div>
                <div class="col-span-12 text-gray-500">
                  <div v-if="delivery.delivery_status == 0"> Waiting</div>
                  <div v-else-if="delivery.delivery_status == 1"> delivered</div>
                  <div v-else-if="delivery.delivery_status == 2"> Out for Delivery</div>
                  <div v-else-if="delivery.delivery_status == 3">Cancelled</div>
                  <div v-else-if="delivery.delivery_status == 4"> Partial Delivery</div>
                  <div v-else-if="delivery.delivery_status == 5">Issue</div>
                  <div v-else></div>
                </div>
                <div class="col-span-12 pt-3 font-bold">
                  Expected Delivery:
                </div>
                <div class="col-span-12 text-gray-500">
                  {{ delivery.expected_delivery_date }}
                </div>
              </div>
            </div>
          </div>


          <div class="col-span-12 xl:col-span-6">
            <div class="grid grid-cols-12 mb-5 ">
              <div v-for="card in credit_cards" class="col-span-12">
                <div class="col-span-12  ">
                  <div v-if="card.main_card" class="basis-1/3 p-2">
                    <div class="bg-secondary rounded-md border-2 ">
                      <div class="flex p-3">
                        {{ card.type_of_card }}
                      </div>
                      <div class="flex p-1 pl-4">
                        {{ card.name_on_card }}
                      </div>
                      <div class="flex p-1 pl-4">
                        {{ card.card_number }}
                      </div>
                      <div class="flex p-1 pl-4">
                        {{ card.expiration_month }}/ {{ card.expiration_year }}
                      </div>
                      <div class="flex p-1 pl-4">
                        {{ card.security_number }}
                      </div>
                    </div>
                  </div>
                  <div v-else class="basis-1/3 p-2">
                    <div class=" rounded-md border-2 ">
                      <div class="flex p-3">
                        {{ card.type_of_card }}
                      </div>
                      <div class="flex p-1 pl-4">
                        {{ card.name_on_card }}
                      </div>
                      <div class="flex p-1 pl-4">
                        {{ card.card_number }}
                      </div>
                      <div class="flex p-1 pl-4">
                        {{ card.expiration_month }}/ {{ card.expiration_year }}
                      </div>
                      <div class="flex p-1 pl-4">
                        {{ card.security_number }}
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <div class="grid grid-cols-12 mb-5">
              <div class="col-span-12 pt-3 font-bold">
                Payment Type:
              </div>
              <div class="col-span-12 text-gray-500" v-if="delivery.payment_type == 0">
                Cash
              </div>
              <div class="col-span-12 text-gray-500" v-if="delivery.payment_type == 1">
                Credit Card
              </div>
              <div class="col-span-12 text-gray-500" v-if="delivery.payment_type == 2">
                Credit Card / Cash
              </div>
              <div class="col-span-12 text-gray-500" v-if="delivery.payment_type == 3">
                Check with CC Hold
              </div>
              <div class="col-span-12 text-gray-500" v-if="delivery.payment_type == 4">
                Other (Rent, etc):
              </div>
              <div v-if="promo_active" class="col-span-12">
                <div class="col-span-12 pt-3 font-bold">
                Promo
                </div>

                <div class="col-span-12 text-gray-500">
                  {{ promo.name_of_promotion }}
                </div>
                <div class="col-span-12 pt-3 font-bold">
                  Promo Discount:
                </div>
                <div class="col-span-12 text-gray-500">
                  {{ promo.money_off_delivery }} off a gallon
                </div>
                <div class="col-span-12 text-gray-500">
                  {{ discount }} off total delivery
                </div>
                <div class="col-span-12 text-gray-500">
                  {{ total_amount_after_discount }} total price after discount
                </div>
                <div class="col-span-12 pt-3 font-bold">
                  Price / Gallon:
                </div>
                <div class="col-span-12 text-gray-500">
                  {{ delivery.customer_price }} ({{ delivery.customer_price - promo.money_off_delivery}})
                </div>
              </div>



              <div class="col-span-12 pt-3 font-bold">
                Gallons Ordered:
              </div>

              <div class="col-span-12 text-gray-500">
                <div v-if="delivery.customer_asked_for_fill == 1"> FILL (250)</div>
                <div v-else> Gallons Ordered: {{ delivery.gallons_ordered }}</div>
              </div>

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


              <div class="col-span-12 py-3" v-if="delivery.promo_id != null">
                <div class="col-span-12  " v-if="delivery.payment_type == 0">
                <div class="font-bold py-5">
                  CASH Total: 
                </div>
                <div class="col-span-12 text-gray-500">
                  ${{ total_amount_after_discount }}
                </div>
              </div>
              <div class="col-span-12 font-bold py-5 text-accent" v-if="delivery.payment_type == 1">
                <div class="">
                  Pre Charge Credit Card Total: ${{ total_amount_after_discount }}
                </div>
              </div>
              <div class="col-span-12 font-bold py-5 text-accent" v-if="delivery.payment_type == 2">
                <div class="">
                  Pre Charge Credit Card Total: ${{ total_amount_after_discount }}
                </div>
              </div>
              <div class="col-span-12 font-bold py-5 text-accent" v-if="delivery.payment_type == 3">
                <div class="">
                  Check - Pre Charge Credit Card Total: ${{ total_amount_after_discount }}
                </div>
              </div>
              </div>
              <div class="col-span-12 py-3" v-else>
              <div class="col-span-12  " v-if="delivery.payment_type == 0">
                <div class="font-bold py-5">
                  CASH Total: 
                </div>
                <div class="col-span-12 text-gray-500">
                  ${{ total_amount }}
                  </div>
              </div>
              <div class="col-span-12 font-bold py-5 text-accent" v-if="delivery.payment_type == 1">
                <div class="">
                  Pre Charge Credit Card Total: ${{ total_amount }}
                </div>
              </div>
              <div class="col-span-12 font-bold py-5 text-accent" v-if="delivery.payment_type == 2">
                <div class="">
                  Pre Charge Credit Card Total: ${{ total_amount }}
                </div>
              </div>
              <div class="col-span-12 font-bold py-5 text-accent" v-if="delivery.payment_type == 3">
                <div class="">
                  Check - Pre Charge Credit Card Total: ${{ total_amount }}
                </div>
              </div>
            </div>
          </div>


            <div class="flex justify-between">
           
              <div class="" v-if="delivery.payment_type == 0">
                <button class="btn bg-green-800  btn-sm" @click="checkoutOilUpdatePayment(0)">
                  Confirm Payment
                </button>
              </div>
              <div class="" v-if="delivery.payment_type == 1">
                <button class="btn bg-green-800 btn-sm" @click="checkoutOilUpdatePayment(1)">
                  Confirm Payment
                </button>
              </div>
              <div class="" v-if="delivery.payment_type == 2">
                <button class="btn bg-green-800 btn-sm" @click="checkoutOilUpdatePayment(2)">
                  Confirm Payment
                </button>
              </div>
          
              <div class="" v-if="delivery.payment_type == 3">
                <button class="btn bg-green-800 btn-sm" @click="checkoutOilUpdatePayment(3)">
                  Confirm Payment
                </button>
              </div>
              <div class="" v-if="delivery.payment_type == 4">
                <button class="btn bg-green-800 btn-sm" @click="checkoutOilUpdatePayment(3)">
                  Confirm Payment
                </button>
              </div>
            </div>
            <div class="col-span-12 py-10">

            <router-link :to="{ name: 'deliveryEdit', params: { id: delivery.id } }">
              <button class="btn btn-sm btn-secondary">Edit Delivery</button>
            </router-link>
           
            </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'
import useValidate from "@vuelidate/core";
import { notify } from "@kyvg/vue3-notification"
import { minLength, required } from "@vuelidate/validators";

export default defineComponent({
  name: 'PayOil',

  components: {
    Header,
    SideBar,
    Footer,
  },

  data() {
    return {
      v$: useValidate(),
      loaded: false,
      user: {
        user_id: 0,
      },
      delivery: {
        id: 0,
        customer_id: 0,
        customer_name: '',
        customer_address: '',
        customer_town: '',
        customer_state: 0,
        customer_zip: '',
        gallons_ordered: 0,
        customer_asked_for_fill: 0,
        gallons_delivered: 0,
        customer_filled: 0,
        delivery_status: 0,
        when_ordered: '',
        when_delivered: '',
        expected_delivery_date: '',
        automatic: 0,
        oil_id: 0,
        supplier_price: 0,
        customer_price: 0,
        customer_temperature: 0,
        dispatcher_notes: '',
        prime: 0,
        promo_id: 0,
        emergency: 0,
        same_day: 0,
        payment_type: 0,
        payment_card_id: 0,
        driver_employee_id: 0,
        driver_first_name: '',
        driver_last_name: '',
        pre_charge_amount: 0,
        total_price: 0,

      },
      credit_cards: [
        {
          id: 0,
          name_on_card: '',
          main_card: false,
          card_number: '',
          expiration_month: '',
          type_of_card: '',
          last_four_digits: '',
          expiration_year: '',
          security_number: '',

        }
      ],

      stripe: null,
      customer: {
        id: 0,
        user_id: 0,
        customer_first_name: '',
        customer_last_name: '',
        customer_town: '',
        customer_address: '',
        customer_state: 0,
        customer_zip: '',
        customer_apt: '',
        customer_home_type: 0,
        customer_phone_number: '',
        account_number: '',
      },
      pricing: {
        price_from_supplier: 0,
        price_for_customer: 0,
        price_for_employee: 0,
        price_same_day: 0,
        price_prime: 0,
        price_emergency: 0,
        date: "",
      },
      promo_active: false,
      promo: {
        name_of_promotion: '',
        description: '',
        money_off_delivery: 0,
        text_on_ticket: ''
      },
      priceprime: 0,
      pricesameday: 0,
      priceemergency: 0,
      total_amount: 0,
      discount: 0,
      total_amount_after_discount: 0,
    }
  },
  validations() {
    return {
      CreateOilOrderForm: {
        basicInfo: {
          gallons_ordered: { required, minLength: minLength(1) },
          expected_delivery_date: { required },
        },
      },
    };
  },
  created() {
    this.userStatus()

  },
  watch: {
    $route() {

    },
  },
  mounted() {
    this.getOilOrder(this.$route.params.id)
    this.sumdelivery(this.$route.params.id);
    this.getOilPricing()

  },

  methods: {
    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) {
            console.log(response.data)
            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
        
          }
        })
    },
    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;
          }
        })

    },
    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",
          });
        });
    },
    getOilOrder(delivery_id: any) {
      let path = import.meta.env.VITE_BASE_URL + "/delivery/order/" + delivery_id;
      axios({
        method: "get",
        url: path,
        withCredentials: true,
      })
        .then((response: any) => {
          this.delivery = response.data;
          this.getCustomer(this.delivery.customer_id)
          this.getCreditCards(this.delivery.customer_id)
          if (this.delivery.promo_id != null) {
              this.getPromo(this.delivery.promo_id);
              this.promo_active = true;
            }
        })
        .catch(() => {
          notify({
            title: "Error",
            text: "Could not get delivery",
            type: "error",
          });
        });
    },
    getCreditCards(user_id: any) {
      let path = import.meta.env.VITE_BASE_URL + '/payment/cards/' + user_id;
      axios({
        method: 'get',
        url: path,
        headers: authHeader(),
      }).then((response: any) => {

        this.credit_cards = response.data
      })
    },
    getCustomer(userid: any) {
      let path = import.meta.env.VITE_BASE_URL + '/customer/' + userid;
      axios({
        method: 'get',
        url: path,
        headers: authHeader(),
      }).then((response: any) => {
        this.customer = response.data
      })
    },
    checkoutOilUpdatePayment(payment_type: number) {
      let path = import.meta.env.VITE_BASE_URL + "/delivery/cash/" + this.delivery.id + '/' + payment_type;
      axios({
        method: "PUT",
        url: path,
      })
        .then((response: any) => {
          if (response.data.ok) {
            if (payment_type == 0) {
              notify({
                title: "Success",
                text: "marked payment as cash (COD)",
                type: "success",
              });
            }
            if (payment_type == 1) {
              notify({
                title: "Success",
                text: "marked payment as credit card",
                type: "success",
              });
            }

            if (payment_type == 2) {
              notify({
                title: "Success",
                text: "marked payment as cash (COD) and credit card partial payment",
                type: "success",
              });
            }
            if (payment_type == 3) {
              notify({
                title: "Success",
                text: "marked payment as check with credit card hold",
                type: "success",
              });
            }
            this.$router.push({ name: "customerProfile", params: { id: this.customer.id } });
          }
        })
        .catch(() => {
          notify({
            title: "Error",
            text: "Could not update wth cash",
            type: "error",
          });
        });
    },

  

  },
})
</script>

<style scoped></style>