<script setup>
const props = defineProps({
  order: Object,
  selected: Boolean,
});

const { $dayjs, $shortenCurrency } = useNuxtApp();
const emit = defineEmits(["selectOrder", "unselect"]);
</script>
<template>
  <tr
    :class="['is-clickable', selected && 'is-selected']"
    @click="selected ? emit('unselect') : emit('selectOrder', order.id)"
  >
    <td>
      <div>
        <p class="fs-15 font-semibold">{{ order.code }}</p>
        <p class="has-text-grey">{{ order.employee__name }}</p>
      </div>
    </td>
    <td>
      <div>
        <p>{{ order.customer__name }}</p>
        <div class="is-flex is-gap-0.5 is-align-items-center mt-1 has-text-grey">
          <Icon
            name="material-symbols:call-outline-rounded"
            :size="15"
          />
          <span class="fs-12">{{ order.customer__phone }}</span>
        </div>
      </div>
    </td>
    <td class="has-text-right">
      <div>
        <p class="fs-14 font-semibold">{{ $shortenCurrency(order.total) }}</p>
        <p class="has-text-grey">{{ order.order__products.length }} SP</p>
      </div>
    </td>
    <td class="has-text-centered">
      <span
        :class="['tag rounded-full', `has-background-${order.status__color}-80 has-text-${order.status__color}-25`]"
      >
        {{ order.status__name }}
      </span>
    </td>
    <td>
      <p :class="`has-text-${order.payment_status__color}-40`">
        {{ order.payment_status__name }}
      </p>
    </td>
    <td>
      <p :class="`has-text-${order.delivery_status__color}-40`">
        {{ order.delivery_status__name }}
      </p>
    </td>
    <td>
      <div>
        <div class="is-flex is-gap-0.5">
          <Icon
            :size="18"
            name="material-symbols:calendar-today-outline-rounded"
          />
          <span>{{ $dayjs(order.create_time).format("L") }}</span>
        </div>
        <p class="has-text-grey fs-12">
          {{ $dayjs(order.create_time).format("HH:mm") }}
        </p>
      </div>
    </td>
    <td>
      <button
        v-if="order.status__name !== 'Hoàn thành'"
        :class="[
          'button fs-12 has-text-white rounded-lg',
          order.status__name === 'Nháp'
            ? 'is-primary'
            : order.status__name === 'Đã xác nhận'
              ? 'is-orange'
              : 'is-success',
        ]"
      >
        {{
          order.status__name === "Nháp" ? "Xác nhận" : order.status__name === "Đã xác nhận" ? "Giao hàng" : "Hoàn thành"
        }}
      </button>
    </td>
  </tr>
</template>
<style scoped>
tr.is-selected {
  --bulma-table-row-active-background-color: var(--bulma-primary-95);
  color: unset;
}
td {
  vertical-align: middle;
  --bulma-table-cell-padding: 0.75em;
}
</style>
