<!-- Buttons to add to the header. -->
<core-navbar-buttons slot="end">
    <ion-button fill="clear" (click)="openModuleSummary()" aria-haspopup="true" [attr.aria-label]="'core.info' | translate">
        <ion-icon name="fas-circle-info" slot="icon-only" aria-hidden="true" />
    </ion-button>
</core-navbar-buttons>

<!-- Content. -->
<core-loading [hideUntil]="!showLoading">
    <!-- Activity info. -->
    <core-course-module-info [module]="module" [description]="description" [component]="component" [componentId]="componentId"
        [courseId]="courseId" [hasDataToSync]="hasOffline" (completionChanged)="onCompletionChange()" />

    <!-- Activity availability messages -->
    <ion-card class="core-info-card" *ngIf="choiceNotOpenYet">
        <ion-item>
            <ion-icon name="fas-circle-info" slot="start" aria-hidden="true" />
            <ion-label>
                <p *ngIf="options.length">{{ 'addon.mod_choice.previewonly' | translate:{$a: openTimeReadable} }}</p>
                <p *ngIf="!options.length">{{ 'addon.mod_choice.notopenyet' | translate:{$a: openTimeReadable} }}</p>
            </ion-label>
        </ion-item>
    </ion-card>

    <ion-card class="core-info-card" *ngIf="choiceClosed">
        <ion-item>
            <ion-icon name="fas-circle-info" slot="start" aria-hidden="true" />
            <ion-label>
                <p *ngIf="options.length">
                    {{ 'addon.mod_choice.yourselection' | translate }}
                    <core-format-text [text]="options[0].text" contextLevel="module" [contextInstanceId]="module.id"
                        [courseId]="courseId" />
                </p>
                <p>{{ 'addon.mod_choice.expired' | translate:{$a: closeTimeReadable} }}</p>
            </ion-label>
        </ion-item>
    </ion-card>

    <!-- Inform what will happen with the choices. -->
    <ion-card class="core-info-card" *ngIf="canEdit && publishInfo && options.length">
        <ion-item>
            <ion-icon name="fas-circle-info" slot="start" aria-hidden="true" />
            <ion-label>{{ publishInfo | translate }}</ion-label>
        </ion-item>
    </ion-card>

    <!-- Choice options -->
    <ion-card *ngIf="options.length && choice">
        <ng-container *ngIf="choice.allowmultiple">
            <ion-item class="ion-text-wrap" *ngFor="let option of options">
                <ion-checkbox [(ngModel)]="option.checked" [disabled]="option.disabled || !canEdit">
                    <ng-container *ngTemplateOutlet="optionLabelTemplate; context: {option: option}" />
                </ion-checkbox>
            </ion-item>
        </ng-container>
        <ion-radio-group *ngIf="!choice.allowmultiple" [(ngModel)]="selectedOption.id">
            <ion-item class="ion-text-wrap" *ngFor="let option of options">
                <ion-radio [value]="option.id" [disabled]="option.disabled || !canEdit">
                    <ng-container *ngTemplateOutlet="optionLabelTemplate; context: {option: option}" />
                </ion-radio>
            </ion-item>
        </ion-radio-group>
    </ion-card>
    <!-- Choice results -->
    <div *ngIf="canSeeResults && choice">
        <ion-item-divider>
            <ion-label>
                <h2>{{ 'addon.mod_choice.responses' | translate }}</h2>
            </ion-label>
        </ion-item-divider>
        <ion-grid class="ion-no-padding">
            <ion-row>
                <ion-col size="12" size-lg="5">
                    <ion-item class="ion-text-wrap core-warning-item" *ngIf="hasOffline">
                        <ion-icon slot="start" name="fas-triangle-exclamation" color="warning" aria-hidden="true" />
                        <ion-label>{{ 'addon.mod_choice.resultsnotsynced' | translate }}</ion-label>
                    </ion-item>
                    <ion-item>
                        <ion-label>
                            <core-chart type="pie" [data]="data" [labels]="labels" height="300" contextLevel="module"
                                [contextInstanceId]="module.id" [courseId]="courseId" />
                        </ion-label>
                    </ion-item>
                </ion-col>
                <ion-col *ngIf="choice.publish && results" size="12" size-lg="7">
                    <ion-item-group *ngFor="let result of results">
                        <ion-item [button]="result.numberofuser > 0" class="divider ion-text-wrap" (click)="toggle(result)"
                            [attr.aria-label]="(result.expanded ? 'core.collapse' : 'core.expand') | translate" [detail]="false">
                            <ion-icon [name]="result.numberofuser > 0 ? 'fas-chevron-right' : ''" flip-rtl slot="start" aria-hidden="true"
                                class="expandable-status-icon" [class.expandable-status-icon-expanded]="result.expanded" />
                            <ion-label>
                                <h3 class="item-heading">
                                    <core-format-text [text]="result.text" contextLevel="module" [contextInstanceId]="module.id"
                                        [courseId]="courseId" />
                                </h3>
                                <p>
                                    {{ 'addon.mod_choice.numberofuser' | translate }}: {{ result.numberofuser }}
                                    ({{ 'core.percentagenumber' | translate: {$a: result.percentageamountfixed} }})
                                </p>
                                <p *ngIf="choice.limitanswers && choice.showavailable">
                                    {{ 'addon.mod_choice.limita' | translate:{$a: result.maxanswer} }}
                                </p>
                            </ion-label>
                        </ion-item>
                        <ng-container *ngIf="result.expanded">
                            <ion-item *ngFor="let user of result.userresponses" core-user-link [courseId]="courseId" [userId]="user.userid"
                                [attr.aria-label]="user.fullname" class="ion-text-wrap">
                                <core-user-avatar [user]="user" slot="start" [courseId]="courseId" [linkProfile]="false" />
                                <ion-label>
                                    <p>{{user.fullname}}</p>
                                </ion-label>
                            </ion-item>
                        </ng-container>
                    </ion-item-group>
                </ion-col>
            </ion-row>
        </ion-grid>
    </div>

    <ion-card class="core-info-card" *ngIf="!canSeeResults && !choiceNotOpenYet">
        <ion-item>
            <ion-icon name="fas-circle-info" slot="start" aria-hidden="true" />
            <ion-label>
                <p>{{ 'addon.mod_choice.noresultsviewable' | translate }}</p>
            </ion-label>
        </ion-item>
    </ion-card>


    <div collapsible-footer *ngIf="!showLoading" slot="fixed">
        <div class="list-item-limited-width adaptable-buttons-row" *ngIf="options.length && choice && (canEdit || canDelete)">
            <ion-button *ngIf="canDelete" expand="block" fill="outline" (click)="delete()" class="ion-margin ion-text-wrap">
                {{ 'addon.mod_choice.removemychoice' | translate }}
            </ion-button>
            <ion-button *ngIf="canEdit" expand="block" (click)="save()" [disabled]="!canSave()" class="ion-margin ion-text-wrap">
                {{ 'addon.mod_choice.savemychoice' | translate }}
            </ion-button>
        </div>

        <core-course-module-navigation [courseId]="courseId" [currentModuleId]="module.id" />
    </div>
</core-loading>



<!-- Template to render a choice option label. -->
<ng-template #optionLabelTemplate let-option="option">
    <p class="item-heading">
        <core-format-text [text]="option.text" contextLevel="module" [contextInstanceId]="module.id" [courseId]="courseId" />
        <span *ngIf="choice!.limitanswers && option.countanswers >= option.maxanswers">
            {{ 'addon.mod_choice.full' | translate }}
        </span>
    </p>
    <ng-container *ngIf="choice!.limitanswers && choice!.showavailable">
        <p>{{ 'addon.mod_choice.responsesa' | translate:{$a: option.countanswers} }}</p>
        <p>{{ 'addon.mod_choice.limita' | translate:{$a: option.maxanswers} }}</p>
    </ng-container>
</ng-template>
