<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>
          <li>
            <router-link :to="{ name: 'customer' }">
              Customers
            </router-link>
          </li>
        </ul>
      </div>
      <div class="grid grid-cols-12">
        <div class="col-span-6 bg-neutral p-5">
          <div class="grid grid-cols-12">
                 <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">
              <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>
        </div>
      </div>
      <div class="grid grid-cols-1 rounded-md p-6 ">
        <div class="text-[24px]">Create Oil Delivery
        </div>
        <form class="rounded-md px-8 pt-6 pb-8 mb-4 w-full" enctype="multipart/form-data" @submit.prevent="onSubmit">

          <div class="col-span-12 md:col-span-4 mb-5 md:mb-0 gap-10">
            <label class="block text-white text-sm font-bold cursor-pointer label">Fill </label>
            <input v-model="CreateOilOrderForm.basicInfo.customer_asked_for_fill"
                   class="checkbox"
                   id="fill"
                   type="checkbox"/>
          </div>

          <div class="mb-4">
            <label class="block text-white text-sm font-bold mb-2">Gallons Ordered</label>
            <input v-model="CreateOilOrderForm.basicInfo.gallons_ordered"
                   :disabled="CreateOilOrderForm.basicInfo.customer_asked_for_fill == true"
                   class="input input-bordered w-full max-w-xs"
                   id="title" type="text" placeholder="# gallons"/>
            <span v-if="v$.CreateOilOrderForm.basicInfo.gallons_ordered.$error" class="text-red-600 text-center">
              {{ v$.CreateOilOrderForm.basicInfo.gallons_ordered.$errors[0].$message }}
            </span>
          </div>

          <div class="mb-4">
            <label class="block text-white text-sm font-bold mb-2">Expected </label>
            <input v-model="CreateOilOrderForm.basicInfo.expected_delivery_date"
                   class="input input-bordered w-full max-w-xs"
                   id="title" type="date"
                   min="2023-01-01" max="2030-01-01"/>
            <span v-if="v$.CreateOilOrderForm.basicInfo.expected_delivery_date.$error"
                  class="text-red-600 text-center">
                  {{ v$.CreateOilOrderForm.basicInfo.expected_delivery_date.$errors[0].$message }}
              </span>
          </div>

          <div class="mb-4">
            <label class="form-control">
              <div class="label">
                <span class="label-text">Delivery notes</span>
              </div>
              <textarea class="textarea textarea-bordered h-24"
                        placeholder="Describe any thing given from the customer .."
                        v-model="CreateOilOrderForm.basicInfo.dispatcher_notes_taken"></textarea>
            </label>
          </div>


          <div class="col-span-12 md:col-span-4 mb-5 md:mb-0 gap-10">
            <label class="block text-white text-sm font-bold cursor-pointer label">Prime</label>
            <input v-model="CreateOilOrderForm.basicInfo.prime"
                   class="checkbox"
                   id="prime"
                   type="checkbox"/>
          </div>


          <div class="col-span-12 md:col-span-4 mb-5 md:mb-0 gap-10">
            <label class="block text-white text-sm font-bold cursor-pointer label">Same Day </label>
            <input v-model="CreateOilOrderForm.basicInfo.same_day"
                   class="checkbox"
                   id="same_day"
                   type="checkbox"/>
          </div>

          <div class="bg-neutral">

            <div class="col-span-12 md:col-span-4 mb-5 md:mb-0 gap-10">
              <label class="block text-white text-sm font-bold cursor-pointer label">Cash</label>
              <input v-model="CreateOilOrderForm.basicInfo.cash"
                     class="checkbox"
                     id="cash"
                     type="checkbox"/>
            </div>

            <div class="col-span-12 md:col-span-4 mb-5 md:mb-0 gap-10">
              <label class="block text-white text-sm font-bold cursor-pointer label">Credit </label>
              <input v-model="CreateOilOrderForm.basicInfo.credit"
                     class="checkbox"
                     id="Credit"
                     type="checkbox"/>
            </div>

            <div class="flex-1 mb-4">
              <label class="block text-white text-sm font-bold mb-2">Customer Cards Payment</label>
              <select
                  class="select select-bordered w-full max-w-xs"
                  aria-label="Default select example" id="userCards"
                  v-model="CreateOilOrderForm.basicInfo.userCards">
                <option class="text-white" v-for="(card, index) in userCards"
                        :key="index"
                        :value="card['id']">
                  {{ card['type_of_card'] }} {{ card['card_number'] }}
                </option>
              </select>
            </div>
          </div>


          <div class="flex-1 mb-4">
            <label class="block text-white text-sm font-bold mb-2">Delivery Driver </label>
            <select class="select select-bordered w-full max-w-xs" aria-label="Default select example" id="customer_state"
              v-model="CreateOilOrderForm.basicInfo.driver_driver">
              <option class="text-white" v-for="(driver, index) in truckDriversList" :key="index" :value="driver['id']">
                  {{ driver['employee_first_name'] }}  {{ driver['employee_last_name'] }}
              </option>
            </select>

          </div>


          <div class="col-span-12 md:col-span-12 flex mt-5 mb-5">
            <button
                class="btn">
              Create Oil Order
            </button>
          </div>
        </form>
      </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: 'deliveryCreate',

  components: {
    Header,
    SideBar,
    Footer,
  },

  data() {
    return {
      v$: useValidate(),
      user: null,
      userCards: [],
      truckDriversList: [],
      CreateOilOrderForm: {
        basicInfo: {
          gallons_ordered: '',
          customer_asked_for_fill: false,
          expected_delivery_date: '',
          dispatcher_notes_taken: '',
          prime: false,
          userCards: [],
          same_day: false,
          credit: false,
          cash: false,
          credit_card_id: 0,
          driver_driver: '',
        },
      },
        customer: {
          id: 0,
          customer_last_name: '',
          customer_first_name: '',
          customer_town: '',
          customer_state: 0,
          customer_zip: '',
          customer_first_call: '',
          customer_email: '',
          customer_automatic: '',
          customer_phone_number: '',
          customer_home_type: 0,
          customer_apt: '',
          customer_address: '',
          account_number: '',
        },
    }
  },
  validations() {
    return {
      CreateOilOrderForm: {
        basicInfo: {
          gallons_ordered: {required, minLength: minLength(1)},
          expected_delivery_date: {required},
        },
      },
    };
  },
  created() {
    this.userStatus()
      this.getDriversList()
  },
  watch: {
    $route() {
      this.getCustomer(this.$route.params.id);
      this.getPaymentCards(this.$route.params.id);
    },
  },
  mounted() {
    this.getCustomer(this.$route.params.id)
     this.getPaymentCards(this.$route.params.id);
  },
  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;
            }
          })
          .catch(() => {
            this.user = null
          })
    },

    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",
            });
          });
    },

    getPaymentCards(user_id: any) {
      let path = import.meta.env.VITE_BASE_URL + "/payment/cards/" + user_id;
      axios({
        method: "get",
        url: path,
        withCredentials: true,
      })
          .then((response: any) => {
            this.userCards = response.data;
          })
          .catch(() => {
          });
    },
    CreateOilOrder(payload: {
      gallons_ordered: string;
      customer_asked_for_fill: boolean;
      prime: boolean;
      same_day: boolean;
      cash: boolean;
      credit: boolean;
      expected_delivery_date: string;
      dispatcher_notes_taken: string;
      credit_card_id: any;
      driver_employee_id: string,

    }) {
      let path = import.meta.env.VITE_BASE_URL + "/delivery/create/" + this.customer.id;
      axios({
        method: "post",
        url: path,
        data: payload,
        withCredentials: true,
        headers: authHeader(),
      })
          .then((response: any) => {
            console.log(response.data)
            if (response.data.ok) {
              this.$router.push({name: "payOil", params: {id: response.data.delivery_id}});
            }
            if (response.data.error) {
              this.$router.push("/");
            }
          })
    },

    onSubmit() {
      let payload = {
        gallons_ordered: this.CreateOilOrderForm.basicInfo.gallons_ordered,
        customer_asked_for_fill: this.CreateOilOrderForm.basicInfo.customer_asked_for_fill,
        expected_delivery_date: this.CreateOilOrderForm.basicInfo.expected_delivery_date,
        dispatcher_notes_taken: this.CreateOilOrderForm.basicInfo.dispatcher_notes_taken,
        prime: this.CreateOilOrderForm.basicInfo.prime,
        same_day: this.CreateOilOrderForm.basicInfo.same_day,
        cash: this.CreateOilOrderForm.basicInfo.cash,
        credit: this.CreateOilOrderForm.basicInfo.credit,
        credit_card_id: this.CreateOilOrderForm.basicInfo.userCards,
        driver_employee_id: this.CreateOilOrderForm.basicInfo.driver_driver,
      };
      this.CreateOilOrder(payload);
    },


    getDriversList() {
      let path = import.meta.env.VITE_BASE_URL + "/employee/drivers";
      axios({
        method: "get",
        url: path,
        withCredentials: true,
         headers: authHeader(),
      })
          .then((response: any) => {
            this.truckDriversList = response.data;
          })
          .catch(() => {
          });
    },

  },
})
</script>

<style scoped>

</style>