<script setup lang="ts">
import { ref } from 'vue';
import { RowDeleteAction, RowEditAction, RowTransferAction } from '@/components/button';
import RetailStockTransferModal from '@/components/modal/RetailStockTransferModal.vue';
import OwnerVerificationRowActions from '@/components/owner-verification/OwnerVerificationRowActions.vue';
import { useCan } from '@/composables/useCan';
import { edit, destroy } from '@/routes/admin/master/products';
import type { ProductListItem } from '@/types/product';

const props = defineProps<{
    product: ProductListItem;
}>();

const { can } = useCan();

const transferModalOpen = ref(false);
const transferVariantId = ref(0);
const transferVariantName = ref('');
const transferGoodStock = ref(0);
const transferRetailStock = ref(0);

function openTransferModal(variantId: number, variantName: string, goodStock: number, retailStock: number) {
    transferVariantId.value = variantId;
    transferVariantName.value = variantName;
    transferGoodStock.value = goodStock;
    transferRetailStock.value = retailStock;
    transferModalOpen.value = true;
}

function handleTransferClick() {
    const variants = props.product.variants ?? [];

    if (variants.length === 1) {
        const v = variants[0];
        openTransferModal(v.id, v.name, v.stock, v.retail_stock);
    } else if (variants.length > 1) {
        transferVariantId.value = 0;
        transferVariantName.value = '';
        transferGoodStock.value = 0;
        transferRetailStock.value = 0;
        transferModalOpen.value = true;
    }
}
</script>

<template>
    <div class="flex items-center justify-end gap-1">
        <OwnerVerificationRowActions v-if="product.has_pending_request && product.pending_request_id" type="request"
            :id="product.pending_request_id" />

        <RowTransferAction
            v-if="can('stocks.view')"
            :disabled="product.has_pending_request"
            :tooltip="product.has_pending_request ? 'Menunggu verifikasi owner' : 'Transfer Stok Ecer'"
            @click="handleTransferClick"
        />
        <RowEditAction v-if="can('products.update')" :href="edit.url(product.id)"
            :disabled="product.has_pending_request"
            :tooltip="product.has_pending_request ? 'Menunggu verifikasi owner' : 'Ubah'" />
        <RowDeleteAction v-if="can('products.delete')" :action-url="destroy.url(product.id)"
            :disabled="product.has_pending_request"
            :tooltip="product.has_pending_request ? 'Menunggu verifikasi owner' : 'Hapus'" title="Hapus produk?"
            :description="`Pengajuan hapus produk ${product.name} akan dikirim ke owner untuk verifikasi.`"
            error-message="Gagal mengajukan penghapusan produk." />
    </div>

    <RetailStockTransferModal
        v-model:open="transferModalOpen"
        :variant-id="transferVariantId"
        :variant-name="transferVariantName"
        :product-name="product.name"
        :good-stock="transferGoodStock"
        :retail-stock="transferRetailStock"
        :variants="(product.variants ?? []).map(v => ({ id: v.id, name: v.name, stock: v.stock, retail_stock: v.retail_stock }))"
        @submitted="() => {}"
    />
</template>
