<template>

  <div class="q-gutter-sm q-pa-sm q-pb-md">
    <div v-if="!circuit && !loading">
      <div v-if="mystatus === 403">
        <h3>Non hai i permessi per accedere al Gruppo.<br>

          Occorre prima registrarsi alla App </h3>
      </div>
      <div v-else>
        <h3>Circuito non Esistente</h3>
      </div>
    </div>
    <div v-else>

      <div v-if="!tools.isLogged()">
        <CCheckIfIsLogged></CCheckIfIsLogged>
      </div>
      <div>

        <div v-if="circuit.name" class="fit column no-wrap justify-evenly items-center content-start">

          <div class="">
            <q-avatar size="140px">
              <q-img :src="getImgCircuit()" :alt="circuit.name" img-class="imgprofile" height="140px" @click="showPic = true"/>
            </q-avatar>
          </div>

          <div class="text-h6">
            <span v-if="checkifShow('name')"> {{ circuit.name }}</span>
          </div>

          <q-banner
            v-if="userStore.IsRefusedCircuitByName(circuit.name)"
            rounded
            class="bg-red text-white"
            style="text-align: center;"
          >
            <em style="font-weight: bold">{{ $t('db.youarerefusedcircuit') }}</em><br>
          </q-banner>


          <div>
            <q-btn
              v-if="!userStore.IsMyCircuitByName(circuit.name) && !userStore.IsAskedCircuitByName(circuit.name) && !userStore.IsRefusedCircuitByName(circuit.name)"
              icon="fas fa-user-plus"
              color="primary" :label="$t('circuit.ask')"
              @click="tools.setRequestCircuit($q, userStore.my.username, circuit.name, true)"
            />
            <q-btn
              v-if="userStore.IsMyCircuitByName(circuit.name)"
              rounded icon="fas fa-ellipsis-h">
              <q-menu>
                <q-list v-if="true" style="min-width: 150px">
                  <q-item clickable v-close-popup
                          @click="tools.removeFromMyCircuits($q, userStore.my.username, circuit.name, $t('db.domanda_exit_fromcircuit', {name: circuit.name }))">
                    <q-item-section avatar>
                      <q-icon color="negative" name="fas fa-user-minus"/>
                    </q-item-section>
                    <q-item-section>{{ $t('circuit.exit_circuit') }}</q-item-section>
                  </q-item>

                  <q-list v-if="tools.iAmAdminCircuit(circuit.name)" style="min-width: 200px">
                    <q-item clickable v-close-popup @click="tools.setCmd($q, shared_consts.CIRCUITCMD.DELETE, userStore.my.username, '', circuit.name)">
                      <q-item-section avatar>
                        <q-icon color="negative" name="fas fa-trash-alt"/>
                      </q-item-section>
                      <q-item-section>{{ $t('circuit.delete') }}</q-item-section>
                    </q-item>
                  </q-list>


                </q-list>
              </q-menu>
            </q-btn>

            <q-btn
              v-if="userStore.IsAskedCircuitByName(circuit.name) && !userStore.IsMyCircuitByName(circuit.name)"
              icon="fas fa-user-minus"
              flat :label="$t('shared.cancel_ask_short')"
              @click="tools.cancelReqCircuits($q, userStore.my.username, circuit.name)"
            />
          </div>

        </div>
        <div v-if="circuit.name" class="no-wrap justify-evenly items-center content-start">


          <!--
          <q-btn
            v-if="tools.iAmAdminCircuit(name)" icon="fas fa-pencil-alt"
            color="blue"
            size="md"
            :label="$t('otherpages.modifgrp')"
            to="/editgrp">
          </q-btn>
          -->

          <q-tabs v-model="tabgrp" class="text-blue">
            <q-tab :label="t('shared.info1')" name="info" icon="fas fa-info"></q-tab>
            <q-tab v-if="!!circuit.note" :label="t('circuit.page')" name="page" icon="fas fa-file-word"></q-tab>
            <q-tab v-if="tools.iCanShowCircuitsMember(circuit) || tools.iAmAdminCircuit(name)"
                   :label="t('shared.subscribes')" name="members" icon="fas fa-users"></q-tab>
          </q-tabs>

          <q-tab-panels v-model="tabgrp" animated>
            <q-tab-panel name="info">
              <div>
                <q-card>
                  <q-card-section>
                    <div class="text-h6">{{ t('circuit.info') }}</div>
                  </q-card-section>

                  <q-separator/>

                  <q-card-section>
                    <div v-if="circuit.createdBy" class="element">
                      <q-icon name="fas fa-lightbulb"></q-icon>
                      {{ $t('circuit.createdby', {
                      username: circuit.createdBy,
                      date: tools.getstrDateYY(circuit.date_created), })
                      }}
                    </div>
                    <div v-if="!!circuit.date_updated && circuit.date_updated !== circuit.createdBy" class="element">
                      <q-icon name="fas fa-pencil-alt"></q-icon>
                      {{ $t('circuit.lastmodify', {
                      date: tools.getstrDateYY(circuit.date_updated), })
                      }}
                    </div>

                    <div class="members">
                      <q-icon name="fas fa-users"></q-icon>
                      {{ numUsers() }} {{ numUsers() === 1 ? t('circuit.member') : t('shared.members') }}
                    </div>
                    <div class="admins">
                      <q-icon name="fas fa-user-cog"></q-icon>
                      {{ numAdmins() }} {{ numAdmins() === 1 ? t('circuit.admin') : t('shared.admins') }}
                    </div>
                    <div v-for="(user, index) of circuit.admins" :key="index">
                      <CMyUser
                        :mycontact="user"
                        :visu="costanti.FIND_PEOPLE"
                        @setCmd="tools.setCmd"
                      >
                      </CMyUser>
                    </div>
                  </q-card-section>
                </q-card>

                <br>
              </div>
              <q-card v-if="circuit.name">
                <q-card-section>
                  <div class="text-h6">Descrizione:</div>
                </q-card-section>
                <q-separator/>

                <q-card-section>
                  <div class="col-12 text-h7">
                    <span v-if="checkifShow('descr')">{{ circuit.longdescr }}</span>
                  </div>
                </q-card-section>
              </q-card>

            </q-tab-panel>

            <q-tab-panel name="page">
              <div v-if="circuit.note">
                <br>
                <div class="q-ma-sm q-gutter-sm q-pa-xs">
                  <div v-if="circuit.note" v-html="circuit.note">
                  </div>
                </div>
              </div>
            </q-tab-panel>

            <q-tab-panel name="members" style="max-width: 400px;" v-if="tools.iCanShowCircuitsMember(circuit)">

              <q-tabs
                v-show="tools.iAmAdminCircuit(name)"
                v-model="tabmembers" class="text-blue">
                <q-tab label="Tutti" name="all" icon="fas fa-users"></q-tab>
                <q-tab v-if="tools.iAmAdminCircuit(name)" label="Richieste" name="rich" icon="fas fa-user-plus"></q-tab>
                <q-tab v-if="tools.iAmAdminCircuit(name)" label="Rifiutati" name="refused" icon="fas fa-user-minus"></q-tab>
              </q-tabs>

              <q-tab-panels v-model="tabmembers" animated>
              <q-tab-panel name="all">

              <CGridTableRec
                ref="tabMembri"
                prop_mytable="users"
                prop_mytitle=""
                :prop_mycolumns="colmyUserPeople"
                prop_colkey="_id"
                col_title="username"
                :vertical="costanti.VISUTABLE_LISTA"
                nodataLabel="Nessun Iscritto"
                :prop_search="true"
                hint="Username da trovare"
                :finder="false"
                :choose_visutype="true"
                :finder_noNull="false"
                :options="shared_consts.OPTIONS_SEARCH_USER_ALL_WORDS"
                :butt_modif_new="false"
                noresultLabel="Username non trovato"
                :arrfilters="arrfilterand"
                :filtercustom="filtercustom"
                :prop_searchList="searchList"
                :showType="costanti.SHOW_USERINFO"
                :showCol="false"
                :extrafield="name"
                :extraparams="extraparams()"
                :visufind="tools.iAmAdminCircuit(name) ? costanti.REQ_REMOVE_USER_TO_CIRCUIT : costanti.FIND_PEOPLE"
              >

              </CGridTableRec>
              </q-tab-panel>
              <q-tab-panel name="rich">

                <CGridTableRec
                  prop_mytable="mycircuits"
                  prop_mytitle=""
                  :prop_mycolumns="colmyUserPeople"
                  prop_colkey="_id"
                  col_title=""
                  :vertical="costanti.VISUTABLE_USER_TABCIRCUIT"
                  nodataLabel="Nessuna Richiesta in sospeso"
                  :prop_search="false"
                  hint="Username da trovare"
                  :finder="false"
                  :choose_visutype="false"
                  :finder_noNull="false"
                  :options="shared_consts.OPTIONS_SEARCH_USER_ALL_WORDS"
                  :butt_modif_new="false"
                  noresultLabel="Username non trovato"
                  :arrfilters="arrfilterand"
                  :filtercustom="filtercustom_rich"
                  :prop_searchList="searchList"
                  :showType="costanti.SHOW_USERINFO"
                  keyMain=""
                  :showCol="false"
                  :extraparams="extraparams_rich()"
                  :extrafield="name"
                  :visufind="costanti.REQ_ADD_USER_TO_CIRCUIT"
                >

                </CGridTableRec>
              </q-tab-panel>
              <q-tab-panel name="refused">

                <CGridTableRec
                  prop_mytable="mycircuits"
                  prop_mytitle=""
                  :prop_mycolumns="colmyUserPeople"
                  prop_colkey="_id"
                  col_title=""
                  :vertical="costanti.VISUTABLE_USER_TABCIRCUIT"
                  nodataLabel="Nessun utente Rifiutato"
                  :prop_search="false"
                  hint="Username da trovare"
                  :finder="false"
                  :choose_visutype="false"
                  :finder_noNull="false"
                  :options="shared_consts.OPTIONS_SEARCH_USER_ALL_WORDS"
                  :butt_modif_new="false"
                  noresultLabel="Username non trovato"
                  :arrfilters="arrfilterand"
                  :filtercustom="filtercustom_rich"
                  :prop_searchList="searchList"
                  :showType="costanti.SHOW_USERINFO"
                  keyMain=""
                  :showCol="false"
                  :extraparams="extraparams_refused()"
                  :extrafield="name"
                  :visufind="costanti.REQ_ADD_USER_TO_CIRCUIT"
                >

                </CGridTableRec>
              </q-tab-panel>
              </q-tab-panels>

            </q-tab-panel>
          </q-tab-panels>

        </div>
        <div v-else class="fit column no-wrap justify-evenly items-center content-start">
          <q-skeleton type="QAvatar" size="140px" height="140px" animation="fade"/>
          <q-card flat bordered style="width: 250px">
            <div class="text-h6">
              <q-skeleton :animation="animation"/>
            </div>
            <div class="col-12 text-h7 text-grey text-center">
              {{ name }}
            </div>
            <div class="col-12 text-h7">
              <q-skeleton :animation="animation"/>
            </div>

            <div class="col-12 text-h8 q-mt-sm">
              <q-skeleton :animation="animation"/>
            </div>
            <div class="col-12 text-h8 q-mt-sm">
              <q-skeleton :animation="animation"/>
            </div>
          </q-card>
        </div>

      </div>
      <q-dialog
        v-model="showPic"
        full-height full-width
      >

        <img :src="getImgGrp()" :alt="name" class="full-width">

      </q-dialog>
    </div>
  </div>

</template>

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

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

