<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import { useRouter } from 'vue-router';

import { useUserStore } from '@vben/stores';

import { $t } from '@/locales';

import { getDeliveryPartnersApi, type UserApi } from '#/api';
import { useLoginModalStore } from '#/store';

interface Props {
  jurisdiction: boolean;
}

const props = defineProps<Props>();
const router = useRouter();
const loginModalStore = useLoginModalStore();
const userStore = useUserStore();
const isLogin = computed(() => !!userStore.userInfo);

function handleNavigate() {
  if (!isLogin.value) {
    loginModalStore.open();
    return;
  }
  router.push('/delivery-partners');
}

const deliveryPartnersList = ref<UserApi.DeliveryPartnerModel[]>([]);
const loading = ref(false);

async function fetchDeliveryPartnersList() {
  if (!isLogin.value) {
    return;
  }

  try {
    loading.value = true;
    const result = await getDeliveryPartnersApi({
      currentPage: 1,
      pageSize: 12,
      orderByProperty: 'name',
      Ascending: true,
      totalPage: 1,
      totalCount: 2,
      filters: [
        { name: 'name' },
        { name: 'account' },
        { name: 'status', value: 1 },
      ],
    });
    if (result?.result?.model) {
      deliveryPartnersList.value = result.result.model;
    }
  } catch {}
}

watch(
  () => isLogin.value,
  (newValue) => {
    if (newValue) {
      fetchDeliveryPartnersList();
    }
  },
  { immediate: true },
);
</script>

<template>
  <div class="info-card border-box h-[217px] w-full rounded-lg p-5 shadow-md">
    <div class="items-flex-start flex justify-between">
      <div>
        <h2 class="text-lg font-bold">
          {{ $t('homeModule.deliveryPartners') }}
        </h2>
        <div class="text-xs text-[#9A9BA3]">Delivery Partners</div>
      </div>
      <img
        v-if="props.jurisdiction"
        alt="more"
        class="h-[29px] w-[29px] cursor-pointer"
        src="@/assets/image/home-more.png"
        @click="handleNavigate"
      />
    </div>
    <p
      v-if="!isLogin"
      class="mt-4 w-[174px] text-sm text-xs leading-relaxed text-[#9A9BA3]"
    >
      {{ $t('homeModule.deliveryPartnersIntroduction') }}
    </p>

    <div v-if="props.jurisdiction && isLogin" class="mt-[63px]">
      <div v-if="deliveryPartnersList.length > 0" class="flex gap-[18px]">
        <img
          v-for="index in 4"
          :key="index"
          :src="`/src/assets/image/user${index}.png`"
          :style="{ marginLeft: index !== 1 ? '-35px' : '0' }"
          :text="index"
          alt=""
          class="h-[46px] w-[46px]"
        />
      </div>
      <div
        v-else
        class="mx-auto mt-[-40px] flex flex-col items-center justify-center text-center"
      >
        <img
          alt=""
          class="block h-[80px] w-[85px]"
          src="@/assets/image/jurisdiction.png"
        />
        {{ $t('auth.emptyContent') }}
      </div>
    </div>
    <div
      v-else
      class="mt-[10px] flex flex-col items-center justify-center text-center"
    >
      <img
        alt=""
        class="block h-[80px] w-[85px]"
        src="@/assets/image/empty.png"
      />
      {{ $t('auth.noPermission') }}
    </div>
  </div>
</template>

<style scoped>
.info-card {
  overflow: hidden;
  background: url('@/assets/image/DeliveryPartners.png') no-repeat center center;
  background-size: cover;
  border-radius: 27px;
}
</style>
