<template>
  <q-page>
    <div class="text-center">
      <q-spinner v-if="!loadpage" color="primary" size="3em" :thickness="2" />
    </div>
    <div v-if="loadpage" class="panel">
      <div>
        <CSelectUserActive></CSelectUserActive>
        <div class="text-center text-h7 text-blue">Filtra Prodotti per:</div>
        <div class="text-center">
          <q-btn-toggle
            v-model="cosa"
            push
            :size="tools.isMobile() ? '0.75rem' : '1rem'"
            rounded
            glossy
            toggle-color="primary"
            :options="[
              { value: shared_consts.PROD.GAS, slot: 'gas' },
              { value: shared_consts.PROD.BOTTEGA, slot: 'bottega' },
            ]"
          >
            <template v-slot:tutti>
              <div class="row items-center no-wrap">
                <div class="text-center">
                  {{ t("ecomm.tutti") }}
                </div>
                <q-icon right name="fas fa-check-square" />
              </div>
            </template>

            <template v-slot:gas>
              <div class="row items-center no-wrap">
                <div class="text-center">
                  {{ t("gas.ordina_sul_gas") }}
                  <!--<br />
                  {{
                    t('gas.x_prodotti_gas', {
                      qta: productStore.getNumQtaGas(),
                    })
                  }}-->
                </div>
                <q-icon right name="fas fa-user-friends" />
              </div>
            </template>

            <template v-slot:bottega>
              <div class="row items-center no-wrap">
                <div class="text-center">
                  {{ t("gas.bottega") }}
                  <!--<br />
                  {{
                    t('gas.x_prodotti_bottega', {
                      qta: productStore.getNumQtaBottega(),
                    })
                  }}-->
                </div>
                <q-icon right name="fas fa-store" />
              </div>
            </template>
          </q-btn-toggle>
        </div>
      </div>

      <div class="container">
        <q-slide-transition>
          <div
            v-show="isFixed || tools.scrollTop() < 300"
            :class="
              'column text-center q-mx-auto q-py-sm q-mb-sm ' +
              (isFixed ? 'fixed-group ' : '')
            "
            style="width: 350px; max-width: 100%"
          >
            <div>
              <q-input
                ref="componentToFixRef"
                filled
                stack-label
                rounded
                :dense="tools.isMobile() ? true : false"
                :label="t('ecomm.code_o_text_search')"
                v-model="search"
                debounce="300"
                class="q-ml-md"
              >
                <template v-slot:append>
                  <q-icon name="search" />
                </template>
              </q-input>
            </div>
          </div>
        </q-slide-transition>
        <div v-if="cosa === shared_consts.PROD.GAS">
          <div v-if="!idGasSel">
            <div class="text-center text-h6 text-red">Ordini Attivi:</div>
          </div>
          <div class="row q-gutter-xs justify-center q-mx-auto">
            <div
              v-for="(recgas, index) in productStore.getGasordinesActives()"
              :key="index"
            >
              <q-btn
                push
                dense
                :size="tools.isMobile() ? '0.9rem' : '1.05rem'"
                :color="idGasSel === recgas._id ? 'primary' : undefined"
                :text-color="idGasSel === recgas._id ? 'white' : 'black'"
                :label="recgas.name"
                @click="idGasSel = recgas._id"
              >
              </q-btn>
            </div>
          </div>
          <div class="row q-gutter-xs justify-center q-mx-auto">
            <div
              v-for="(reccat, index) in getCatProdsByGas(idGasSel)"
              :key="index"
            >
              <q-btn
                :push="cat === reccat.value"
                dense
                :size="tools.isMobile() ? '0.70rem' : '0.85rem'"
                :icon="reccat.icon ? reccat.icon : undefined"
                :color="cat === reccat.value ? 'primary' : undefined"
                :text-color="cat === reccat.value ? 'white' : 'black'"
                rounded
                :label="reccat.label"
                @click="cat = reccat.value"
              >
              </q-btn>
            </div>
          </div>
          <q-separator class="q-ma-md">&nbsp;</q-separator>
          <div
            v-if="getSubCatProdsByGas(idGasSel, cat)"
            class="row q-gutter-xs justify-center q-mx-auto"
          >
            <div
              v-for="(recsubcat, index) in getSubCatProdsByGas(idGasSel, cat)"
              :key="index"
            >
              <q-btn
                :push="subcat === recsubcat.value"
                dense
                :size="tools.isMobile() ? '0.70rem' : '0.85rem'"
                :icon="recsubcat.icon ? recsubcat.icon : undefined"
                :color="subcat === recsubcat.value ? 'positive' : undefined"
                :text-color="subcat === recsubcat.value ? 'white' : 'blue'"
                rounded
                :label="recsubcat.label"
                @click="subcat = recsubcat.value"
              >
              </q-btn>
            </div>
          </div>

          <div class="text-center q-py-sm prod_trov">
            <span
              v-show="productStore.getNumProdTot() !== arrProducts.length"
              >{{
                t("ecomm.prodotti_trovati", {
                  qta: arrProducts.length,
                  qtatot: productStore.getNumProdTot(),
                })
              }}</span
            >
          </div>
          <div class="row justify-around" v-if="tools.isManager()">
            <q-toggle
              v-model="show_hide"
              push
              label="Mostra Nascosti"
              rounded
              glossy
              toggle-color="primary"
            ></q-toggle>
          </div>
          <div class="row justify-around">
            <q-infinite-scroll
              v-if="arrLoaded && arrLoaded.length > 0"
              ref="myinfscroll"
              :initial-index="0"
              @load="onLoadScroll"
              :offset="2000"
              debounce="200"
              class="q-pa-xs row items-start"
            >
              <div
                class="q-pa-xs"
                v-for="(product, index) in arrLoaded"
                :key="index"
              >
                <CProductCard
                  v-if="product.active || show_hide"
                  :id="product._id"
                  :complete="false"
                  :cosa="cosa"
                />
              </div>
              <template v-slot:loading>
                <div class="text-center">
                  <q-spinner-dots color="primary" size="40px" />
                </div>
              </template>
            </q-infinite-scroll>
          </div>
        </div>
        <div v-else>
          <div class="row q-gutter-xs justify-center q-mx-auto">
            <div v-for="(reccat, index) in getCatProds()" :key="index">
              <q-btn
                :push="cat === reccat.value"
                dense
                :size="tools.isMobile() ? '0.70rem' : '1rem'"
                :icon="reccat.icon ? reccat.icon : undefined"
                :color="cat === reccat.value ? 'primary' : undefined"
                :text-color="cat === reccat.value ? 'white' : 'black'"
                rounded
                :label="reccat.label"
                @click="cat = reccat.value"
              >
              </q-btn>
            </div>
          </div>
          <div class="text-center q-py-sm prod_trov">
            <span
              v-show="productStore.getNumProdTot() !== arrProducts.length"
              >{{
                t("ecomm.prodotti_trovati", {
                  qta: arrProducts.length,
                  qtatot: productStore.getNumProdTot(),
                })
              }}</span
            >
          </div>
          <div class="row justify-around" v-if="tools.isManager()">
            <q-toggle
              v-model="show_hide"
              push
              label="Mostra Nascosti"
              rounded
              glossy
              toggle-color="primary"
            ></q-toggle>
          </div>
          <div class="row justify-around">
            <q-infinite-scroll
              v-if="arrLoaded && arrLoaded.length > 0"
              ref="myinfscroll"
              :initial-index="0"
              @load="onLoadScroll"
              :offset="2000"
              debounce="200"
              class="q-pa-xs row items-start"
            >
              <div
                class="q-pa-xs"
                v-for="(product, index) in arrLoaded"
                :key="index"
              >
                <CProductCard
                  v-if="product.active || show_hide"
                  :id="product._id"
                  :complete="false"
                  :cosa="cosa"
                />
              </div>
              <template v-slot:loading>
                <div class="text-center">
                  <q-spinner-dots color="primary" size="40px" />
                </div>
              </template>
            </q-infinite-scroll>
          </div>
        </div>
      </div>
    </div>
  </q-page>
</template>

<script lang="ts" src="./productsList.ts">
</script>

<style lang="scss" scoped>
@import "./productsList";
</style>
