<template>
    <div class="seller-order">
        <div>
            <div>
                <img :src="coverImage" alt="">
                <h2>{{ title }}</h2>
            </div>
            <div>
                <span>{{ $t('number') }} : {{ data.quantity }}</span>
                <span>{{ $t('amount') }} : {{ numberFormat(data.price) }}</span>
            </div>
        </div>
        <div>
            <Tag v-bind="tag" rounded />
            <div v-if="status == 'Pending'">
                <Button icon="isax isax-tick-square" link severity="secondary" :loading="accepting" @click="accept()" />
                <Button icon="isax isax-close-square" link severity="secondary" :loading="canceling"
                    @click="cancel()" />
            </div>
            <small v-else>{{ new Date(sendDate).toLocaleDateString('fa-IR') }}</small>
        </div>
    </div>
</template>

<script setup>
const props = defineProps(['data'])
let { product, status, _id: id, sendDate } = props.data
const { coverImage, title } = product
const { t } = inject('service')
const tags = reactive({
    pending: { severity: 'warn', value: t('pending') },
    accepted: { severity: 'info', value: t('accepted') },
    received: { severity: 'success', value: t('received') },
    returned: { severity: 'error', value: t('returned') },
    canceled: { severity: 'error', value: t('canceled') },
})

const tag = ref(tags[status.toLowerCase()])

const accepting = ref(false)
const canceling = ref(false)

const accept = async () => {
    accepting.value = true
    const result = await useFetch('/api/shop/product/accept/' + id, {
        headers: {
            Authorization: useCookie('token')
        }
    })
    accepting.value = false

    if (result.status.value == 'success') {
        status = 'accepted'
        tag.value = tags[status]
        sendDate = Date.now()
    }
}

const cancel = async () => {
    canceling.value = true
    const result = await useFetch('/api/shop/product/cancel/' + id, {
        headers: {
            Authorization: useCookie('token')
        }
    })
    canceling.value = false

    if (result.status.value == 'success') {
        status = 'canceled'
        tag.value = tags[status]
        sendDate = Date.now()
    }
}

const numberFormat = (number) =>
    new Intl.NumberFormat('fa-IR', { maximumSignificantDigits: 3 }).format(number)
</script>

<style lang="scss">
.seller-order {
    @apply w-full h-[12.1rem] rounded-[0.6rem] border border-[#E5E5E5] px-[1.1rem] lg:px-[1.9rem] flex flex-col;

    &>div:first-child {
        @apply w-full flex max-lg:flex-col max-lg:gap-[1.9rem] justify-between items-center py-[1.1rem] lg:py-5 border-b border-[#E5E5E5];

        &>div:first-child {
            @apply flex items-center gap-1.5 lg:gap-5;

            img {
                @apply w-[3.8rem] h-[3.8rem] lg:w-20 lg:h-20 bg-gray-200 shrink-0;
            }

            h2 {
                @apply text-[#333333] text-sm lg:text-base font-medium font-vazir;
            }
        }

        &>div:last-child {
            @apply max-lg:w-full lg:h-full flex lg:flex-col justify-between items-end;

            span {
                @apply text-[#333333] text-xs lg:text-base font-normal font-vazir;
            }

        }
    }

    &>div:last-child {
        @apply w-full h-full flex justify-between items-center;

        .p-tag {
            @apply h-[1.8rem];
        }

        &>div {
            @apply flex gap-1.5 lg:gap-3;

            .p-button {
                @apply p-0 w-max h-max #{!important};

                .p-button-icon {
                    @apply text-xl lg:text-2xl text-[#333333];
                }
            }

        }

        &>small {
            @apply text-[#333333] text-sm lg:text-base font-vazir;
        }

    }
}
</style>