import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { EventData } from '@nativescript/core';
import { Select } from '@ngxs/store';
import { Observable } from 'rxjs';
import { PickerProvider } from '~/core/entities/picker-provider';
import { EvaluationSearchMyUnit } from '../../entities/evaluation-search-myUnit';
import { EvaluationPermissionState } from '../../state-mgmt/evaluation-permission.state';
import { UnitDetailOutput } from '~/pages/unit/entities/output/unitDetailOutput';

@Component({
    selector: 'EvaluationCreateForm',
    moduleId: module.id,
    templateUrl: './evaluation-create-form.component.html',
    styleUrls: ['./evaluation-create-form.component.scss']
})

export class EvaluationCreateFormComponent implements OnInit {

    evaluationCreate: EvaluationSearchMyUnit;
    @Input() yearsCreate$: Observable<PickerProvider[]>;
    @Input() isLoading: boolean;
    @Input() unitDetail: UnitDetailOutput;

    @Output() createEvaluation = new EventEmitter<EvaluationSearchMyUnit>();

    @Select(EvaluationPermissionState.hasPersonEvalPermission) isAllowedToPersonalEval: boolean;

    defaultYear: number = 0;

    ngOnInit() {
        this.yearsCreate$.subscribe(yearsCreate => {
            if (yearsCreate.length > 0) {
                this.defaultYear = yearsCreate[0].key as number;
            }
        });

        this.evaluationCreate = new EvaluationSearchMyUnit(this.defaultYear);
    }

    // Create button action
    onCreate(args: EventData) {
        this.createEvaluation.emit(this.evaluationCreate);
    }
}
