<!-- src/pages/ticket/ticket.vue -->
<template>
    <div class=" max-w-5xl  text-black bg-white font-mono text-md">
        <div class="grid grid-cols-12 pt-10">
            <div class="col-span-6">

                <div class="grid grid-cols-12">
                    <div class="col-span-2 pt-2 pl-4">#2 </div>
                    <div class="col-span-2 pt-2"></div>
                    <div class="col-span-2 pt-2"></div>
                    <div class="col-span-2 pt-2 ">{{ customer_tank.tank_size }}</div>
                    <div class="col-span-1 pt-2 ">{{ customer_description.fill_location }}</div>
                    <div class="col-span-3 text-xs  pt-3 ">{{ customer.customer_phone_number }}</div>
                </div>

                <div class="grid grid-cols-12 pt-2 pb-2">
                    <div class="col-span-9 pl-5">
                        {{ customer.customer_first_name }} {{ customer.customer_last_name }}
                    </div>
                    <div class="col-span-3 ">{{ customer.account_number }}</div>
                    <div class="col-span-12 pl-5">{{ customer.customer_address }}</div>
                    <div class="col-span-12 pl-5">{{ customer.customer_apt }}</div>
                    <div class="col-span-8 pl-5">
                        <div class="grid grid-cols-12">
                            <div class="col-span-5"> {{ customer.customer_town }}</div>
                            <div class="col-span-3">
                                <div v-if="customer.customer_state == 0">Ma</div>
                            </div>
                            <div class="col-span-4"> {{ customer.customer_zip }}</div>
                        </div>
                    </div>
                </div>


                <div class="grid grid-cols-12 pl-6 pb-2 gap-10 max-h-32">
                    <div class="col-span-6">
                        <div class="grid grid-cols-12">
                            <div class="col-span-12 ">{{ customer_description.description }}</div>
                            <div class="col-span-12 " v-if="delivery.promo_id !== null">Promo: {{ promo_text }}</div>
                            <div class="col-span-12 flex gap-2" v-if="delivery.prime == 1 || delivery.same_day == 1 || delivery.emergency == 1">
                                <span v-if="delivery.prime == 1">PRIME</span>
                                <span v-if="delivery.same_day == 1">SAME DAY</span>
                                <span v-if="delivery.emergency == 1">EMERGENCY</span>
                            </div>

                            <div class="col-span-12 text-lg" v-if="delivery.payment_type == 0">CASH</div>
                            <div class="col-span-12 text-lg" v-else-if="delivery.payment_type == 1">Credit Card</div>
                            <div class="col-span-12 text-lg" v-else-if="delivery.payment_type == 2">Credit Card/Cash</div>
                            <div class="col-span-12 text-lg" v-else-if="delivery.payment_type == 3">Check</div>
                            <div class="col-span-12 text-lg" v-else-if="delivery.payment_type == 4">Other</div>
                            <div class="col-span-12" v-else></div>
                            <div class="col-span-12  " v-if="delivery.customer_asked_for_fill == 0">
                                {{ delivery.gallons_ordered }}</div>
                            <div class="col-span-12 " v-else>Fill</div>
                        </div>
                    </div>
                    <div class="col-span-6 border-2" v-if="delivery.dispatcher_notes">
                        <div class="grid grid-cols-12">
                            <div class="col-span-12 p-2">{{ delivery.dispatcher_notes }}</div>
                        </div>
                    </div>
                </div>


                <div class="grid grid-cols-12">
                    <div class="col-span-6 ">
                        <div class="col-span-12 pl-5">Auburn Oil</div>
                        <div class="col-span-12 pl-5">PO BOX 174</div>
                        <div class="col-span-8 pl-5">
                            <div class="grid grid-cols-12">
                                <div class="col-span-5"> Auburn</div>
                                <div class="col-span-3">
                                    Ma
                                </div>
                                <div class="col-span-4">01501 </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-span-6 ">
                        <div v-if="past_deliveries.length > 0">
                            <div class="col-span-6" v-for="past_delivery in past_deliveries"
                                :key="past_delivery.when_delivered">
                                {{ past_delivery.automatic == 1 ? 'a' : 'wc' }}-{{ past_delivery.when_delivered }} - {{ past_delivery.gallons_delivered }}
                            </div>
                        </div>
                        <div v-else>
                            <div class="col-span-6 text-center">Have a Great day :)</div>
                        </div>
                    </div>
                </div>

            </div>

            <div class="col-span-6 ">
                <div class="col-span-4 ">
                    <div class="grid grid-cols-12 ">
                        <div class="col-span-12 h-7 pl-4 pt-2">{{ delivery.when_ordered }}</div>
                        <div class="col-span-12 h-7 pl-4 pt-2">{{ delivery.expected_delivery_date }}</div>

                        <div class="col-span-12 h-7 pl-4 pt-2" v-if="delivery.customer_asked_for_fill == 0">
                            {{ delivery.gallons_ordered }}</div>
                        <div class="col-span-12 h-7 pl-4 pt-2" v-else></div>

                        <div class="col-span-12 h-7 pl-4 pt-2" v-if="promo_active">
                            <div class="flex gap-2">
                                <div class="line-through"> {{ delivery.customer_price }}</div> ({{ promoprice}})
                            </div>
                        </div>
                        <div class="col-span-12 h-7 pl-4 pt-2" v-else>{{ delivery.customer_price }}</div>


                        <div class="col-span-12 h-7 pl-4 pt-4" v-if="delivery.customer_asked_for_fill == 0">
                            <div v-if="promo_active">
                                {{ total_amount_after_discount }}
                            </div>
                            <div v-else> {{ total_amount }}</div>

                        </div>
                        <div class="col-span-12 h-7 pl-4 pt-4" v-else></div>

                        <div class="col-span-12 h-7 pt-6"></div>
                        <div class="col-span-12 h-7"></div>
                        <div class="col-span-12 h-7 pl-8"></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="text-sm text-gray-400 pt-1 px-4">
            This heating oil is dyed and sold solely for home heating use only — not for on-road vehicle use or taxable purposes. Not responsible for driveways damaged from delivery.
        </div>
    </div>
</template>


<script setup lang="ts">
import { ref, watch, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import axios from 'axios'
import authHeader from '../../services/auth.header'
import { notify } from "@kyvg/vue3-notification"
import { deliveryService } from '../../services/deliveryService'
import { customerService } from '../../services/customerService'
import { adminService } from '../../services/adminService'
import { queryService } from '../../services/queryService'

interface PastDelivery {
    when_delivered: string
    gallons_delivered: number
    automatic: number
}

const route = useRoute()

// State
const loaded = ref(false)
const past_deliveries = ref<PastDelivery[]>([])
const promo_active = ref(false)
const promoprice = ref(0)
const total_amount = ref(0)
const discount = ref(0)
const total_amount_after_discount = ref(0)
const delivery = ref<any>({
    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: 0,
    customer_temperature: '',
    dispatcher_notes: '',
    prime: 0,
    same_day: 0,
    emergency: 0,
    payment_type: 0,
    payment_card_id: 0,
    driver_employee_id: 0,
    driver_first_name: '',
    driver_last_name: '',
    promo_id: 0,
})
const customer_tank = ref<any>({
    id: 0,
    last_tank_inspection: null,
    tank_status: false,
    outside_or_inside: false,
    tank_size: 0,
})
const customer = ref<any>({
    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: '',
})
const customer_description = ref<any>({
    id: 0,
    customer_id: 0,
    account_number: '',
    company_id: '',
    fill_location: 0,
    description: '',
})
const promo = ref<any>(null)
const promo_text = ref('')
const todays_price = ref(0)

// Methods
async function getOrder(deliveryId: string | number) {
    try {
        const response = await deliveryService.getById(Number(deliveryId))
        delivery.value = (response.data as any)?.delivery || response.data
        getCustomer(delivery.value.customer_id)
        if (delivery.value.promo_id != null) {
            getPromo(delivery.value.promo_id)
            promo_active.value = true
            getPromoPrice(deliveryId)
        }
    } catch (error) {
        notify({
            title: "Error",
            text: "Could not get delivery",
            type: "error",
        })
    }
}

async function getCustomer(userId: number) {
    try {
        const response = await customerService.getById(userId)
        customer.value = (response.data as any)?.customer || response.data
        getPastDeliveries(customer.value.id)
        getCustomerDescription(customer.value.id)
        getCustomerTank(customer.value.id)
    } catch (error) {
        console.error('Failed to fetch customer:', error)
    }
}

async function getCustomerTank(userId: number) {
    try {
        const response = await customerService.getTank(userId)
        customer_tank.value = (response.data as any)?.tank || response.data
    } catch (error) {
        console.error('Failed to fetch tank:', error)
    }
}

async function getCustomerDescription(userId: number) {
    try {
        const response = await customerService.getDescription(userId)
        customer_description.value = response.data
    } catch (error) {
        console.error('Failed to fetch description:', error)
    }
}

async function getPromo(promoId: number) {
    try {
        const response = await adminService.promos.getById(promoId)
        promo.value = response.data
        promo_text.value = promo.value?.text_on_ticket || ''
    } catch (error) {
        console.error('Failed to fetch promo:', error)
    }
}

async function getTodayPrice() {
    try {
        const response = await queryService.getOilPrice()
        if ((response.data as any).ok) {
            todays_price.value = (response.data as any).price_for_customer
        }
    } catch (error) {
        notify({
            title: "Error",
            text: "Could not get oil pricing",
            type: "error",
        })
    }
}

async function getPastDeliveries(userId: number) {
    try {
        const response = await deliveryService.getTicketPast(userId)
        past_deliveries.value = (response.data as any)?.deliveries || []
    } catch (error) {
        console.error('Failed to fetch past deliveries:', error)
    }
}

async function sumdelivery(deliveryId: string | number) {
    try {
        const response = await deliveryService.getTotal(Number(deliveryId))
        if ((response.data as any).ok) {
            total_amount.value = (response.data as any).total_amount
            discount.value = (response.data as any).discount
            total_amount_after_discount.value = (response.data as any).total_amount_after_discount
        }
    } catch (error) {
        console.error('Failed to fetch delivery total:', error)
    }
}

async function getPromoPrice(deliveryId: string | number) {
    try {
        const path = import.meta.env.VITE_BASE_URL + '/promo/promoprice/' + deliveryId
        const response = await axios.get(path, {
            withCredentials: true,
            headers: authHeader()
        })
        if (response.data) {
            promoprice.value = response.data.price
        }
    } catch (error) {
        console.error('Failed to fetch promo price:', error)
    }
}

// Watchers
watch(() => route.params.id, (newId) => {
    if (newId) {
        getOrder(newId as string)
        getTodayPrice()
    }
})

// Lifecycle
onMounted(() => {
    if (route.params.id) {
        getOrder(route.params.id as string)
        getTodayPrice()
        sumdelivery(route.params.id as string)
    }
})
</script>
