<ion-header>
    <ion-navbar core-back-button>
        <ion-title><core-format-text [text]="title"></core-format-text></ion-title>
        <ion-buttons end [hidden]="!loaded">
            <button *ngIf="assessmentId" ion-button clear (click)="saveAssessment()" [attr.aria-label]="'core.save' | translate">
                {{ 'core.save' | translate }}
            </button>
            <button *ngIf="canAddFeedback" ion-button clear (click)="saveEvaluation()" [attr.aria-label]="'core.save' | translate">
                {{ 'core.save' | translate }}
            </button>
        </ion-buttons>
    </ion-navbar>
</ion-header>
<ion-content>
    <ion-refresher [enabled]="loaded" (ionRefresh)="refreshSubmission($event)">
        <ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
    </ion-refresher>
    <core-loading [hideUntil]="loaded">
        <ion-list *ngIf="submission">
            <addon-mod-workshop-submission [submission]="submission" [courseId]="courseId" [module]="module" [workshop]="workshop" [access]="access"></addon-mod-workshop-submission>
            <ion-item text-wrap *ngIf="canEdit || canDelete">
                <button ion-button block icon-start *ngIf="canEdit" (click)="editSubmission()">
                    <ion-icon name="create"></ion-icon>
                    {{ 'addon.mod_workshop.editsubmission' | translate }}
                </button>
                <button ion-button block icon-start *ngIf="!submission.deleted && canDelete" color="danger" (click)="deleteSubmission()">
                    <ion-icon name="trash"></ion-icon>
                    {{ 'addon.mod_workshop.deletesubmission' | translate }}
                </button>
                <button ion-button block icon-start outline *ngIf="submission.deleted && canDelete" color="danger" (click)="undoDeleteSubmission()">
                    <ion-icon name="undo"></ion-icon>
                    {{ 'core.restore' | translate }}
                </button>
            </ion-item>
        </ion-list>

        <ion-list *ngIf="!canAddFeedback && evaluate && evaluate.text">
            <ion-item text-wrap>
                <ion-avatar item-start *ngIf="evaluateByProfile">
                    <img [src]="evaluateByProfile.profileimageurl" core-external-content core-user-link [courseId]="courseId" [userId]="evaluateByProfile.id" [alt]="'core.pictureof' | translate:{$a: evaluateByProfile.fullname}" role="presentation" onError="this.src='assets/img/user-avatar.png'">
                </ion-avatar>
                <h2 *ngIf="evaluateByProfile && evaluateByProfile.fullname">{{ 'addon.mod_workshop.feedbackby' | translate : {$a: evaluateByProfile.fullname} }}</h2>
                <core-format-text [text]="evaluate.text"></core-format-text>
            </ion-item>
        </ion-list>

        <ion-list *ngIf="ownAssessment && !assessment">
            <ion-item text-wrap>
                <h2>{{ 'addon.mod_workshop.yourassessment' | translate }}</h2>
            </ion-item>
            <addon-mod-workshop-assessment [submission]="submission" [assessment]="ownAssessment" [courseId]="courseId" summary="true" [access]="access" [module]="module" [workshop]="workshop"></addon-mod-workshop-assessment>
        </ion-list>

        <ion-list *ngIf="submissionInfo && submissionInfo.reviewedby && submissionInfo.reviewedby.length && !assessment">
            <ion-item text-wrap>
                <h2>{{ 'addon.mod_workshop.receivedgrades' | translate }}</h2>
            </ion-item>
            <ng-container *ngFor="let reviewer of submissionInfo.reviewedby">
                <addon-mod-workshop-assessment *ngIf="!reviewer.ownAssessment" [submission]="submission" [assessment]="reviewer" [courseId]="courseId" summary="true" [access]="access" [workshop]="workshop"></addon-mod-workshop-assessment>
            </ng-container>
        </ion-list>

        <ion-list *ngIf="submissionInfo && submissionInfo.reviewerof && submissionInfo.reviewerof.length && !assessment">
            <ion-item text-wrap>
                <h2>{{ 'addon.mod_workshop.givengrades' | translate }}</h2>
            </ion-item>
            <addon-mod-workshop-assessment *ngFor="let reviewer of submissionInfo.reviewerof" [assessment]="reviewer" [courseId]="courseId" summary="true" [workshop]="workshop" [access]="access"></addon-mod-workshop-assessment>
        </ion-list>

        <form ion-list [formGroup]="feedbackForm" *ngIf="canAddFeedback">
            <ion-item text-wrap>
                <h2>{{ 'addon.mod_workshop.feedbackauthor' | translate }}</h2>
            </ion-item>
            <ion-item text-wrap *ngIf="access.canpublishsubmissions">
                <ion-label>{{ 'addon.mod_workshop.publishsubmission' | translate }}</ion-label>
                <ion-toggle formControlName="published"></ion-toggle>
                <p class="item-help">{{ 'addon.mod_workshop.publishsubmission_help' | translate }}</p>
            </ion-item>

            <ion-item text-wrap>
                <h2>{{ 'addon.mod_workshop.gradecalculated' | translate }}</h2>
                <p>{{ submission.submissiongrade }}</p>
            </ion-item>
            <ion-item text-wrap>
                <ion-label stacked>{{ 'addon.mod_workshop.gradeover' | translate }}</ion-label>
                <ion-select formControlName="grade" interface="popover">
                    <ion-option *ngFor="let grade of evaluationGrades" [value]="grade.value">{{grade.label}}</ion-option>
                </ion-select>
            </ion-item>
            <ion-item>
                <ion-label stacked>{{ 'addon.mod_workshop.feedbackauthor' | translate }}</ion-label>
                <core-rich-text-editor item-content [control]="feedbackForm.controls['text']" formControlName="text"></core-rich-text-editor>
            </ion-item>
        </form>

        <addon-mod-workshop-assessment-strategy *ngIf="assessmentId" [workshop]="workshop" [access]="access" [assessmentId]="assessmentId" [userId]="assessmentUserId" [strategy]="strategy" [edit]="access.assessingallowed"></addon-mod-workshop-assessment-strategy>

        <ion-list *ngIf="assessmentId && !access.assessingallowed && assessment.feedbackreviewer">
            <ion-item text-wrap>
                <ion-avatar item-start *ngIf="evaluateGradingByProfile">
                    <img [src]="evaluateGradingByProfile.profileimageurl" core-external-content core-user-link [courseId]="courseId" [userId]="evaluateGradingByProfile.id" [alt]="'core.pictureof' | translate:{$a: evaluateGradingByProfile.fullname}" role="presentation" onError="this.src='assets/img/user-avatar.png'">
                </ion-avatar>
                <h2 *ngIf="evaluateGradingByProfile && evaluateGradingByProfile.fullname">{{ 'addon.mod_workshop.feedbackby' | translate : {$a: evaluateGradingByProfile.fullname} }}</h2>
                <core-format-text [text]="assessment.feedbackreviewer"></core-format-text>
            </ion-item>
        </ion-list>
    </core-loading>
</ion-content>
