<template>
  <div class="top" :class="[{ filters: isFilters }]">
    <div class="top__sorting">
      <p>{{ t('store.sorting') }}</p>
      <client-only>
        <el-select
            v-model="select"
            size="large"
            style="width: 240px"
        >
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </client-only>
    </div>
    <div class="top__view">
      <button
          class="top__view-button"
          :class="{ active: productView === 'list' }"
          @click="setView('list')"
      >
        <icon name="material-symbols:view-list-sharp" size="16" />
      </button>
      <button
          class="top__view-button"
          :class="{ active: productView === 'grid' }"
          @click="setView('grid')"
      >
        <icon name="material-symbols:grid-view" size="16" />
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
const {t} = useI18n();
const { $appHelpers } = useNuxtApp();

const props = defineProps<{
  modelValue: string;
  isFilters: boolean;
}>();
const emit = defineEmits<{
  (e: 'update:modelValue', value: string): void;
  (e: 'toggle-filter'): void;
}>();

const productView = useCookie($appHelpers.COOKIES_PRODUCT_VIEW_KEY as string);
function setView(view: 'list' | 'grid') {
  productView.value = view;
}

const select = ref(props.modelValue || 'created');
const options = [
  {
    value: 'created',
    label: 'New',
  },
  {
    value: 'rating',
    label: 'Rating',
  },
  {
    value: 'price',
    label: 'Сheap first',
  },
  {
    value: '-price',
    label: 'Expensive first',
  }
];

watch(select, value => {
  emit('update:modelValue', value);
});
</script>

<style scoped lang="scss">
.top {
  margin-bottom: 20px;
  width: 100%;
  position: relative;
  z-index: 2;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 40px;
  border-radius: 8px;
  border: 1px solid #e5e7eb;
  padding: 16px;

  &.filters {
    justify-content: flex-end;
  }

  &__sorting {
    display: flex;
    align-items: center;
    gap: 20px;

    & p {
      font-weight: 400;
      font-size: 14px;
      letter-spacing: -0.5px;
      color: #4b5563;
    }
  }

  &__view {
    display: flex;
    align-items: center;
    border-radius: 4px;
    border: 1px solid #e5e7eb;

    &-button {
      cursor: pointer;
      background-color: transparent;
      display: grid;
      place-items: center;
      padding: 10px;
      transition: 0.2s;
      color: #000;

      @include hover {
        background-color: rgba(0, 0, 0, 0.1);
      }

      &.active {
        background-color: rgba(0, 0, 0, 0.1);
      }
    }
  }
}
</style>