<template>
  <div class="orders">
    <div class="orders__top">
      <h2>{{ t('profile.orders.title') }}</h2>
      <el-tooltip
          :visible="isSearchFocused"
          :content="t('profile.orders.searchTooltip')"
          placement="top-start"
      >
        <form class="orders__search" @submit.prevent="submitSearch">
          <input
              type="text"
              inputmode="search"
              v-model="searchInput"
              :placeholder="t('fields.searchOrder')"
              @focus="isSearchFocused = true"
              @blur="isSearchFocused = false"
          >
          <button type="submit">
            <icon name="tabler:search" size="16" />
          </button>
        </form>
      </el-tooltip>
      <el-select
          v-model="status"
          size="large"
          style="width: 240px"
          :placeholder="t('profile.orders.chooseStatus')"
      >
        <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
        />
      </el-select>
    </div>
    <div class="orders__inner" v-if="orders.length">
      <el-collapse
          v-model="collapse"
          class="orders__list"
      >
        <cards-order
            v-for="order in orders"
            :key="order.node.uuid"
            :order="order.node"
        />
      </el-collapse>
      <div class="orders__list" v-if="pending">
        <skeletons-cards-order
            v-for="idx in 5"
            :key="idx"
        />
      </div>
      <div class="orders__list-observer" ref="observer"></div>
    </div>
    <div class="orders__empty" v-if="!orders.length && !pending">
      <p>{{ t('profile.orders.empty') }}</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import {usePageTitle} from "~/composables/utils";
import {useOrders} from "~/composables/orders";
import {CURRENCY, orderStatuses} from "~/config/constants";
import {useDate} from "~/composables/date";
import {useRouteQuery} from "@vueuse/router";

const {t} = useI18n();
const userStore = useUserStore();

const userEmail = computed(() => {
  return userStore.user ? userStore.user.email : '';
});

const status = useRouteQuery<string>('status', '');
const search = useRouteQuery<string>('search', '');
const searchInput = ref<string>('');

const { pending, orders, pageInfo, variables } = await useOrders({
  userEmail: userEmail.value,
  status: status.value,
  search: search.value,
  after: ''
});

const options = [
  {
    value: '',
    label: t('profile.orders.statuses.all')
  },
  {
    value: orderStatuses.FAILED,
    label: t('profile.orders.statuses.failed')
  },
  {
    value: orderStatuses.PAYMENT,
    label: t('profile.orders.statuses.payment')
  },
  {
    value: orderStatuses.CREATED,
    label: t('profile.orders.statuses.created')
  },
  {
    value: orderStatuses.DELIVERING,
    label: t('profile.orders.statuses.delivering')
  },
  {
    value: orderStatuses.FINISHED,
    label: t('profile.orders.statuses.finished')
  },
  {
    value: orderStatuses.MOMENTAL,
    label: t('profile.orders.statuses.momental')
  }
];

const collapse = ref([]);
const isSearchFocused = ref<boolean>(false);

const observer = ref(null);
useIntersectionObserver(
  observer,
  async ([{ isIntersecting }]) => {
    if (isIntersecting && pageInfo.value?.hasNextPage && !pending.value) {
      variables.after = pageInfo.value.endCursor;
    }
  },
);

const submitSearch = () => {
  search.value = searchInput.value;
  variables.search = searchInput.value || '';
};

watch(status, newVal => {
  variables.status = newVal || '';
});

const { setPageTitle } = usePageTitle();

setPageTitle(t('breadcrumbs.orders'));
</script>

<style lang="scss" scoped>
.orders {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 50px;
  flex: 1;
  min-height: 0;

  &__top {
    width: 100%;
    background-color: $white;
    padding: 20px;
    display: flex;
    align-items: stretch;
    justify-content: space-between;
    gap: 50px;
    box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.2);
    border-radius: $default_border_radius;
  }

  &__search {
    border-radius: $default_border_radius;
    border: 1px solid #dedede;
    overflow: hidden;
    background-color: $white;
    display: flex;
    align-items: center;
    width: 100%;

    & input {
      background-color: transparent;
      height: 100%;
      padding-inline: 15px;
      width: 100%;

      color: #606266;
    }

    & button {
      height: 100%;
      padding-inline: 10px;
      cursor: pointer;
      border-radius: $default_border_radius;
      background-color: rgba($accent, 0.2);
      border: 1px solid $accent;
      display: grid;
      place-items: center;
      transition: 0.2s;

      color: $accent;

      @include hover {
        background-color: $accent;
        color: $white;
      }
    }
  }

  &__list {
    display: flex;
    flex-direction: column;
    gap: 30px;

    &-observer {
      background-color: transparent;
      width: 100%;
      height: 10px;
    }
  }

  &__empty {
    background-color: $white;
    padding: 20px;
    box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.2);
    border-radius: $default_border_radius;

    & p {
      font-weight: 600;
    }
  }
}
</style>