<!--
  Questo file fa parte di MyTown.
  MyTown è software libero: puoi ridistribuirlo e/o modificarlo secondo
  i termini della GNU Affero General Public License come pubblicata dalla
  Free Software Foundation, versione 3.
  Distribuito senza ALCUNA GARANZIA; vedi la GNU AGPL v3 per i dettagli.
  Copyright (C) Kondor Consulting & Services S.r.l.
  SPDX-License-Identifier: AGPL-3.0-or-later
-->
<ion-content class="asd-content">
  <ion-refresher slot="fixed" (ionRefresh)="load($event)">
    <ion-refresher-content></ion-refresher-content>
  </ion-refresher>

  @if (asm) {
    <!-- Header card -->
    <div class="asd-header" [attr.data-status]="asm.asm_status">
      <div class="asd-header-top">
        <span class="asd-chip" [ngClass]="asm.asm_type === 'straordinaria' ? 'chip-tertiary' : 'chip-primary'">{{ tipoLabel }}</span>
        <ion-badge [color]="statusColor">{{ statusLabel }}</ion-badge>
      </div>
      <h1>{{ asm.asm_title }}</h1>
      <div class="asd-calls">
        <div class="asd-call">
          <span class="lbl">1ª convocazione</span>
          <span class="val"><ion-icon name="calendar-clear-outline"></ion-icon>{{ asm.asm_first_call_dt ? (asm.asm_first_call_dt | date: 'EEE dd/MM/yyyy HH:mm') : '—' }}</span>
        </div>
        @if (asm.asm_second_call_dt) {
          <div class="asd-call">
            <span class="lbl">2ª convocazione</span>
            <span class="val"><ion-icon name="calendar-outline"></ion-icon>{{ asm.asm_second_call_dt | date: 'EEE dd/MM/yyyy HH:mm' }}</span>
          </div>
        }
      </div>
      @if (asm.asm_location) {
        <div class="asd-loc"><ion-icon name="location-outline"></ion-icon>{{ asm.asm_location }}</div>
      }
      @if (asm.asm_description) {
        <p class="asd-descr">{{ asm.asm_description }}</p>
      }
    </div>
    <!-- Ordine del giorno -->
    @if (odgRighe.length > 0) {
      <div class="asd-section">
        <h3 class="asd-section-title"><ion-icon name="list-outline"></ion-icon> Ordine del giorno</h3>
        <ol class="asd-odg">
          @for (v of odgRighe; track v) {
            <li>{{ v }}</li>
          }
        </ol>
      </div>
    }
    <!-- Quorum -->
    @if (showQuorum) {
      <div class="asd-section">
        <h3 class="asd-section-title"><ion-icon name="speedometer-outline"></ion-icon> Quorum millesimale</h3>
        <div class="asd-quorum-card">
          <div class="q-top">
            <span class="q-num">{{ millesimiPresenti }} <small>/ {{ MILLESIMI_TOTALI }}</small></span>
            <span class="q-cap">millesimi presenti</span>
          </div>
          <div class="q-bar"><div class="q-fill" [style.width.%]="millesimiPct"></div></div>
          <div class="q-chips">
            <div class="q-chip" [class.ok]="quorumPrima"><ion-icon [name]="quorumPrima ? 'checkmark-circle' : 'remove-circle-outline'"></ion-icon> 1ª conv.</div>
            <div class="q-chip" [class.ok]="quorumSeconda"><ion-icon [name]="quorumSeconda ? 'checkmark-circle' : 'remove-circle-outline'"></ion-icon> 2ª conv.</div>
          </div>
        </div>
      </div>
    }
    <!-- Partecipanti -->
    <div class="asd-section">
      <h3 class="asd-section-title"><ion-icon name="people-outline"></ion-icon> Partecipanti <span class="cnt">{{ partecipanti.length }}</span></h3>
      @for (p of partecipanti; track p) {
        <div class="asd-part" [class.mine]="isMioPartecipante(p)">
          <div class="asd-part-info">
            <div class="asd-part-name">
              {{ p.cst_display }}
              @if (isMioPartecipante(p)) {
                <span class="asd-tu">Tu</span>
              }
            </div>
            <div class="asd-part-sub">
              @if (showQuorum) {
                <span class="asd-mill"><ion-icon name="pie-chart-outline"></ion-icon>{{ p.cst_millesimi || 0 }}‰</span>
              }
              <ion-badge [color]="presenzaColor(p)">{{ presenzaLabel(p) }}</ion-badge>
            </div>
          </div>
          @if (canEditRsvp(p)) {
            <div class="asd-rsvp">
              <button class="rsvp-btn ok" [class.active]="p.asp_presente === true" (click)="setRsvp(p, true)"><ion-icon name="checkmark"></ion-icon></button>
              <button class="rsvp-btn ko" [class.active]="p.asp_presente === false" (click)="setRsvp(p, false)"><ion-icon name="close"></ion-icon></button>
            </div>
          }
        </div>
      }
    </div>
    <!-- Lettere di convocazione -->
    <div class="asd-section">
      <div class="asd-section-head">
        <h3 class="asd-section-title"><ion-icon name="mail-outline"></ion-icon> Lettere di convocazione</h3>
        @if (!isCondomino) {
          <button class="asd-mini-btn" (click)="generaConvocazioni()"><ion-icon name="documents-outline"></ion-icon> Genera</button>
        }
      </div>
      @if (convocazioni.length === 0) {
        <div class="asd-hint">Nessuna lettera disponibile.</div>
      }
      @for (l of convocazioni; track l) {
        <div class="asd-file" (click)="scaricaFile(l.wsd_drfl_id, l.wsd_filename)">
          <ion-icon class="asd-file-ic" name="document-text-outline"></ion-icon>
          <div class="asd-file-info">
            <span class="nm">{{ l.cst_display }}</span>
            <span class="fn">{{ l.wsd_filename }}</span>
          </div>
          <ion-icon class="asd-file-dl" name="download-outline"></ion-icon>
        </div>
      }
    </div>
    <!-- Verbali -->
    <div class="asd-section">
      <div class="asd-section-head">
        <h3 class="asd-section-title"><ion-icon name="folder-open-outline"></ion-icon> Verbali</h3>
        @if (!isCondomino && asm.asm_status !== 'annullata' && asm.asm_folder_id) {
          <button class="asd-mini-btn" [disabled]="uploadingVerbale" (click)="verbaleInput.click()">
            <ion-icon [name]="uploadingVerbale ? 'hourglass-outline' : 'cloud-upload-outline'"></ion-icon> {{ uploadingVerbale ? 'Carico…' : 'Carica' }}
          </button>
        }
        <input #verbaleInput type="file" accept="application/pdf" hidden (change)="onVerbaleSelected($event)" />
      </div>
      @if (verbali.length === 0) {
        <div class="asd-hint">Nessun verbale caricato.</div>
      }
      @for (v of verbali; track v) {
        <div class="asd-file" (click)="scaricaFile(verbaleId(v), verbaleName(v))">
          <ion-icon class="asd-file-ic" name="document-attach-outline"></ion-icon>
          <div class="asd-file-info">
            <span class="nm">{{ verbaleName(v) }}</span>
          </div>
          <ion-icon class="asd-file-dl" name="download-outline"></ion-icon>
        </div>
      }
    </div>
  }

  <!-- Action bar gestore (solo se programmata: conclusa/annullata non si modificano) -->
  @if (asm && !isCondomino && isProgrammata) {
    <div class="asd-actionbar">
      <button class="ab-btn light" (click)="modifica()"><ion-icon name="create-outline"></ion-icon> Modifica</button>
      <button class="ab-btn ok" (click)="concludi()"><ion-icon name="checkmark-done-outline"></ion-icon> Concludi</button>
      <button class="ab-btn danger" (click)="annulla()"><ion-icon name="ban-outline"></ion-icon> Annulla</button>
    </div>
  }
</ion-content>
