<template>
  <div v-if="tools.isUserOk()">
    <div v-if="grp">
      <q-item class="q-my-sm" clickable>
        <q-item-section avatar @click="naviga(tools.getPathByGroup(grp))">
          <q-avatar size="60px">
            <q-img
              :src="getImgGroup(grp)"
              :alt="grp.groupname"
              img-class="imgprofile"
              height="60px"
            />
          </q-avatar>
        </q-item-section>

        <q-item-section @click="naviga(tools.getPathByGroup(grp))">
          <q-item-label
            ><strong>{{ grp.title }}</strong> ({{ grp.groupname }})
          </q-item-label>
          <q-item-label v-if="grp.descr" caption lines="3"
            ><em>{{ grp.descr }}</em></q-item-label
          >
          <q-item-label v-if="grp.account && circuitname" caption lines="2">
            <CSaldo
              :small="true"
              :account="grp.account"
              :symbol="tools.getSymbolByCircuit(circuit)"
              :color="tools.getColorByCircuit(circuit)"
              :saldo="grp.account.saldo"
            >
            </CSaldo>
          </q-item-label>
        </q-item-section>

        <q-item-label>
          <q-btn
            v-if="circuitname"
            icon-right="img: /images/1ris_rosso_100.png"
            color="green"
            size="md"
            dense
            @click="showsendCoinTo = true"
          >
          </q-btn>
        </q-item-label>
        <q-item-section side>
          <q-item-label v-if="visu === costanti.MY_GROUPS">
            <q-btn rounded icon="fas fa-ellipsis-h">
              <q-menu>
                <q-list style="min-width: 150px">
                  <q-item
                    clickable
                    icon="fas fa-user-minus"
                    outline
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.REMOVE_FROM_MYGROUP,
                        myusername(),
                        '',
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('groups.remove_from_mygroups')
                    }}</q-item-section>
                  </q-item>
                </q-list>
                <q-list
                  v-if="tools.iAmAdminGroup(grp.groupname)"
                  style="min-width: 200px"
                >
                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.DELETE_GROUP,
                        myusername(),
                        '',
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('groups.delete_group')
                    }}</q-item-section>
                  </q-item>
                </q-list>
                <q-list style="min-width: 150px">
                  <q-item
                    v-if="!tools.iAmAdminGroup(grp.groupname)"
                    clickable
                    icon="fas fa-ban"
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.BLOCK_GROUP,
                        myusername(),
                        '',
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('groups.block_group')
                    }}</q-item-section>
                  </q-item>
                </q-list>
              </q-menu>
            </q-btn>
          </q-item-label>
          <q-item-label v-if="visu === costanti.REQ_ADD_USER_TO_CIRCUIT">
            <q-btn rounded icon="fas fa-ellipsis-h">
              <q-menu>
                <q-list style="min-width: 150px">
                  <q-item
                    clickable
                    v-close-popup
                    dense
                    @click="showsendCoinTo = true"
                  >
                    <q-item-section avatar>
                      <q-icon color="positive" name="img: /images/1ris_rosso_100.png" />
                    </q-item-section>
                    <q-item-section>{{
                      $t('circuit.sendcoins')
                    }}</q-item-section>
                  </q-item>
                  <q-item
                    v-if="tools.iAmAdminGroup(grp.groupname)"
                    clickable
                    v-close-popup
                    dense
                    @click="tools.receiveRisGroup(grp.groupname, $q, $t)"
                  >
                    <q-item-section avatar>
                      <q-icon color="positive" name="fas fa-download" />
                    </q-item-section>
                    <q-item-section>{{
                      $t('circuit.receive_coins')
                    }}</q-item-section>
                  </q-item>
                  <q-item
                    clickable
                    v-close-popup
                    @click="showAccountInfo = true"
                  >
                    <q-item-section avatar>
                      <q-icon name="fas fa-info" />
                    </q-item-section>
                    <q-item-section>{{
                      $t('groups.infoaccount')
                    }}</q-item-section>
                  </q-item>
                </q-list>
              </q-menu>
            </q-btn>
          </q-item-label>
          <q-item-label v-else-if="visu === costanti.USER_GROUPS">
            <q-btn
              rounded
              :icon="
                userStore.IsMyGroupByGroupname(grp.groupname)
                  ? `fas fa-ellipsis-h`
                  : `fas fa-user`
              "
            >
              <q-menu>
                <q-list
                  v-if="
                    !userStore.IsMyGroupByGroupname(grp.groupname) &&
                    !userStore.IsAskedGroupByGroupname(grp.groupname) &&
                    !userStore.IsRefusedGroupByGroupname(grp.groupname)
                  "
                  style="min-width: 200px"
                >
                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.REQGROUP,
                        myusername(),
                        true,
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('groups.ask_group')
                    }}</q-item-section>
                  </q-item>
                </q-list>
                <q-list
                  v-else-if="
                    !userStore.IsMyGroupByGroupname(grp.groupname) &&
                    userStore.IsAskedGroupByGroupname(grp.groupname) &&
                    !userStore.IsRefusedGroupByGroupname(grp.groupname)
                  "
                  style="min-width: 200px"
                >
                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.REFUSE_REQ_GROUP,
                        myusername(),
                        false,
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('shared.refuse_ask')
                    }}</q-item-section>
                  </q-item>
                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.REQGROUP,
                        myusername(),
                        false,
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('shared.cancel_ask')
                    }}</q-item-section>
                  </q-item>
                </q-list>
                <q-list
                  v-else-if="userStore.IsMyGroupByGroupname(grp.groupname)"
                  style="min-width: 200px"
                >
                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.REMOVE_FROM_MYGROUP,
                        myusername(),
                        '',
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('groups.exit_group')
                    }}</q-item-section>
                  </q-item>
                </q-list>
                <q-list
                  v-if="tools.iAmAdminGroup(grp.groupname)"
                  style="min-width: 200px"
                >
                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.DELETE_GROUP,
                        myusername(),
                        '',
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('groups.delete_group')
                    }}</q-item-section>
                  </q-item>
                </q-list>
              </q-menu>
            </q-btn>
          </q-item-label>
          <q-item-label v-else-if="visu === costanti.REQ_GROUP">
            <q-btn rounded icon="fas fa-ellipsis-h">
              <q-menu>
                <q-list style="min-width: 150px">
                  <q-item
                    clickable
                    icon="fas fa-user-minus"
                    outline
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.REQGROUP,
                        myusername(),
                        false,
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('groups.reject_ask_group')
                    }}</q-item-section>
                  </q-item>
                </q-list>
              </q-menu>
            </q-btn>
          </q-item-label>
          <q-item-label v-else-if="visu === costanti.ASK_SENT_GROUP">
            <q-btn rounded icon="fas fa-ellipsis-h">
              <q-menu>
                <q-list style="min-width: 150px">
                  <q-item
                    clickable
                    icon="fas fa-user-minus"
                    outline
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.REFUSE_REQ_GROUP,
                        myusername(),
                        '',
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('shared.refuse_ask')
                    }}</q-item-section>
                  </q-item>
                  <q-item
                    clickable
                    icon="fas fa-user-minus"
                    outline
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.CANCEL_REQ_GROUP,
                        myusername(),
                        '',
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('shared.cancel_ask')
                    }}</q-item-section>
                  </q-item>
                </q-list>
              </q-menu>
            </q-btn>
          </q-item-label>
          <q-item-label v-else-if="visu === costanti.FIND_GROUP">
            <q-btn
              rounded
              :icon="
                userStore.IsMyGroupByGroupname(grp.groupname)
                  ? `fas fa-ellipsis-h`
                  : `fas fa-user`
              "
            >
              <q-menu>
                <q-list>
                  <q-item
                    clickable
                    v-close-popup
                    dense
                    @click="showsendCoinTo = true"
                  >
                    <q-item-section avatar>
                      <q-icon color="positive" name="img: /images/1ris_rosso_100.png" />
                    </q-item-section>
                    <q-item-section>{{
                      $t('circuit.sendcoins')
                    }}</q-item-section>
                  </q-item>
                </q-list>
                <q-list
                  v-if="
                    !userStore.IsMyGroupByGroupname(grp.groupname) &&
                    !userStore.IsAskedGroupByGroupname(grp.groupname)
                  "
                  style="min-width: 200px"
                >
                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.REQGROUP,
                        myusername(),
                        true,
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('groups.ask_group')
                    }}</q-item-section>
                  </q-item>
                </q-list>
                <q-list
                  v-else-if="
                    !userStore.IsMyGroupByGroupname(grp.groupname) &&
                    userStore.IsAskedGroupByGroupname(grp.groupname)
                  "
                  style="min-width: 200px"
                >
                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.REFUSE_REQ_GROUP,
                        myusername(),
                        false,
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('shared.refuse_ask')
                    }}</q-item-section>
                  </q-item>
                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.REQGROUP,
                        myusername(),
                        false,
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('shared.cancel_ask')
                    }}</q-item-section>
                  </q-item>
                </q-list>
                <q-list
                  v-else-if="userStore.IsMyGroupByGroupname(grp.groupname)"
                  style="min-width: 200px"
                >
                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.REMOVE_FROM_MYGROUP,
                        myusername(),
                        '',
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('groups.exit_group')
                    }}</q-item-section>
                  </q-item>

                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.removeFromMyCircuits(
                        $q,
                        userStore.my.username,
                        circuitname,
                        grp.groupname,
                        $t('circuit.domanda_exit_fromcircuit', {
                          circuitname,
                        })
                      )
                    "
                  >
                    <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>
                <q-list
                  v-if="tools.iAmAdminGroup(grp.groupname)"
                  style="min-width: 200px"
                >
                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.setCmd(
                        $q,
                        shared_consts.GROUPSCMD.DELETE_GROUP,
                        myusername(),
                        '',
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section>{{
                      $t('groups.delete_group')
                    }}</q-item-section>
                  </q-item>
                </q-list>
              </q-menu>
            </q-btn>
          </q-item-label>
          <q-item-label v-else-if="visu === costanti.REQ_ADD_GROUP_TO_CIRCUIT">
            <q-btn 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.addGroupToMyCircuits($q, grp.groupname, circuitname)
                    "
                  >
                    <q-item-section avatar>
                      <q-icon color="positive" name="fas fa-user-plus" />
                    </q-item-section>
                    <q-item-section>
                      {{ $t('circuit.accept') }}
                    </q-item-section>
                  </q-item>
                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.addFidoGroupToMyCircuits(
                        $q,
                        grp.groupname,
                        circuitname
                      )
                    "
                  >
                    <q-item-section avatar>
                      <q-icon color="positive" name="img: /images/1ris_rosso_100.png" />
                    </q-item-section>
                    <q-item-section>
                      {{ $t('circuit.enablefido') }}
                    </q-item-section>
                  </q-item>
                </q-list>
                <q-list style="min-width: 200px">
                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.refuseReqCircuit(
                        $q,
                        myusername(),
                        circuitname,
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section avatar>
                      <q-icon color="negative" name="fas fa-user-minus" />
                    </q-item-section>

                    <q-item-section>{{
                      $t('shared.refuse_ask')
                    }}</q-item-section>
                  </q-item>
                  <q-item
                    clickable
                    v-close-popup
                    @click="
                      tools.cancelReqCircuit(
                        $q,
                        myusername(),
                        circuitname,
                        grp.groupname
                      )
                    "
                  >
                    <q-item-section avatar>
                      <q-icon color="negative" name="fas fa-user-minus" />
                    </q-item-section>

                    <q-item-section>{{
                      $t('shared.cancel_ask_short')
                    }}</q-item-section>
                  </q-item>
                </q-list>
              </q-menu>
            </q-btn>
          </q-item-label>
        </q-item-section>
      </q-item>
    </div>
  </div>
  <div v-else>
    <CUserNonVerif></CUserNonVerif>
  </div>
  <div v-if="showsendCoinTo">
    <CSendCoins
      :showprop="showsendCoinTo"
      :to_group="grp"
      :circuitname="circuitname"
      @close="showsendCoinTo = false"
    >
    </CSendCoins>
  </div>
  <q-dialog v-model="showAccountInfo" full-height full-width>
    <q-card v-if="circuit">
      <q-toolbar class="bg-primary text-white">
        <q-toolbar-title>
          {{ grp.title }}
        </q-toolbar-title>
        <q-btn flat round color="white" icon="close" v-close-popup></q-btn>
      </q-toolbar>
      <CInfoAccount
        :grp="grp"
        :circuitname="circuitname"
        :admin="tools.iAmAdminCircuit(circuitname)"
      />
    </q-card>
  </q-dialog>
</template>

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

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