<ion-header>
    <ion-toolbar>
        <ion-buttons slot="start">
            <ion-back-button [text]="'core.back' | translate"></ion-back-button>
        </ion-buttons>
        <ion-title>{{ 'addon.competency.coursecompetencies' | translate }}</ion-title>
    </ion-toolbar>
</ion-header>
<ion-content>
    <ion-refresher slot="fixed" [disabled]="!competenciesLoaded" (ionRefresh)="refreshCourseCompetencies($event.target)">
        <ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
    </ion-refresher>
    <core-loading [hideUntil]="competenciesLoaded">
        <ion-card *ngIf="!user && competencies && competencies.statistics.competencycount > 0">
            <ng-container *ngIf="competencies.cangradecompetencies">
                <ion-item class="ion-text-wrap" *ngIf="competencies.settings.pushratingstouserplans">
                    <ion-label>{{ 'addon.competency.coursecompetencyratingsarepushedtouserplans' | translate }}</ion-label>
                </ion-item>
                <ion-item class="ion-text-wrap" *ngIf="!competencies.settings.pushratingstouserplans" color="danger">
                    <ion-label>{{ 'addon.competency.coursecompetencyratingsarenotpushedtouserplans' | translate }}</ion-label>
                </ion-item>
            </ng-container>
            <ion-item class="ion-text-wrap" *ngIf="competencies.statistics.canbegradedincourse">
                <ion-label>
                    {{ 'addon.competency.xcompetenciesproficientoutofyincourse' | translate: {$a:
                        {x: competencies.statistics.proficientcompetencycount, y: competencies.statistics.competencycount} } }}
                    <core-progress-bar [progress]="competencies.statistics.proficientcompetencypercentage"></core-progress-bar>
                </ion-label>
            </ion-item>
            <ion-item class="ion-text-wrap"
                *ngIf="competencies.statistics.canmanagecoursecompetencies && competencies.statistics.leastproficientcount > 0">
                <ion-label>
                    <strong>{{ 'addon.competency.competenciesmostoftennotproficientincourse' | translate }}</strong>:
                    <p *ngFor="let comp of competencies.statistics.leastproficient">
                        <a (click)="openCompetencySummary(comp.id)">
                            {{ comp.shortname }} - {{ comp.idnumber }}
                        </a>
                    </p>
                </ion-label>
            </ion-item>
        </ion-card>

        <h3  class="ion-margin-horizontal" *ngIf="competencies && competencies.statistics.competencycount > 0">
            {{ 'addon.competency.coursecompetencies' | translate }}
        </h3>
        <ion-card *ngIf="user">
             <ion-item class="ion-text-wrap">
                <core-user-avatar [user]="user" slot="start"></core-user-avatar>
               <ion-label><h2>{{ user.fullname }}</h2></ion-label>
            </ion-item>
        </ion-card>
        <core-empty-box *ngIf="competencies && competencies.statistics.competencycount == 0"
            icon="fas-award" message="{{ 'addon.competency.nocompetenciesincourse' | translate }}">
        </core-empty-box>

        <div *ngIf="competencies">
            <ion-card *ngFor="let competency of competencies.competencies">
                <ion-item class="ion-text-wrap" (click)="openCompetency(competency.competency.id)"
                    [attr.aria-label]="competency.competency.shortname" detail="true" button>
                    <ion-label>
                        <h2><strong>{{competency.competency.shortname}} <em>{{competency.competency.idnumber}}</em></strong></h2>
                    </ion-label>
                    <ion-badge slot="end" *ngIf="competency.usercompetencycourse && competency.usercompetencycourse.gradename"
                        [color]="competency.usercompetencycourse.proficiency ? 'success' : 'danger'">
                        {{ competency.usercompetencycourse.gradename }}
                    </ion-badge>
                </ion-item>
                <ion-item class="ion-text-wrap">
                    <ion-label>
                        <p *ngIf="competency.competency.description">
                            <core-format-text [text]="competency.competency.description" contextLevel="course"
                                [contextInstanceId]="courseId">
                            </core-format-text>
                        </p>
                        <div>
                            <strong>{{ 'addon.competency.path' | translate }}</strong>
                            <a *ngIf="competency.comppath.showlinks"
                                [href]="competency.comppath.pluginbaseurl + '/competencies.php?competencyframeworkid=' +
                                competency.comppath.framework.id + '&pagecontextid=' + competency.comppath.pagecontextid"
                                core-link [title]="competency.comppath.framework.name">
                                {{ competency.comppath.framework.name }}
                            </a>
                            <ng-container *ngIf="!competency.comppath.showlinks">
                                {{ competency.comppath.framework.name }}
                            </ng-container>
                            &nbsp;/&nbsp;
                            <span *ngFor="let ancestor of competency.comppath.ancestors">
                                <a *ngIf="competency.comppath.showlinks" (click)="openCompetencySummary(ancestor.id)">
                                    {{ ancestor.name }}
                                </a>
                                <ng-container *ngIf="!competency.comppath.showlinks">{{ ancestor.name }}</ng-container>
                                <ng-container *ngIf="!ancestor.last">&nbsp;/&nbsp;</ng-container>
                            </span>
                        </div>
                        <div *ngIf="competencies.statistics.canmanagecoursecompetencies">
                            <strong>{{ 'addon.competency.uponcoursecompletion' | translate }}</strong>
                             <ng-container *ngFor="let ruleoutcome of competency.ruleoutcomeoptions">
                                <span *ngIf="ruleoutcome.selected">{{ ruleoutcome.text }}</span>
                            </ng-container>
                        </div>
                        <div>
                            <strong>{{ 'addon.competency.activities' | translate }}</strong>
                            <p *ngIf="competency.coursemodules.length == 0">
                                {{ 'addon.competency.noactivities' | translate }}
                            </p>
                            <ion-item class="ion-text-wrap core-course-module-handler item-media" [attr.aria-label]="activity.name"
                                core-link *ngFor="let activity of competency.coursemodules" [href]="activity.url" capture="true">
                                <img slot="start" [src]="activity.iconurl" core-external-content alt=""
                                    *ngIf="activity.iconurl" class="core-module-icon">
                                <ion-label>
                                    <core-format-text [text]="activity.name" contextLevel="module" [contextInstanceId]="activity.id"
                                        [courseId]="courseId">
                                    </core-format-text>
                                </ion-label>
                            </ion-item>
                        </div>
                        <div *ngIf="competency.plans">
                            <strong>{{ 'addon.competency.userplans' | translate }}</strong>
                            <p *ngIf="competency.plans.length == 0">
                                {{ 'addon.competency.nouserplanswithcompetency' | translate }}
                            </p>
                            <ion-item class="ion-text-wrap" *ngFor="let plan of competency.plans" [href]="plan.url"
                                [attr.aria-label]="plan.name" core-link capture="true">
                                <ion-label>
                                   <core-format-text [text]="plan.name" contextLevel="user" [contextInstanceId]="plan.userid">
                                   </core-format-text>
                                </ion-label>
                            </ion-item>
                        </div>
                    </ion-label>
                </ion-item>
            </ion-card>
        </div>
    </core-loading>
</ion-content>
