import { ChangeDetectionStrategy, Component, input, OnInit, output } from '@angular/core';
import { FormGroup, FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { Toast } from '../../lib/toast/toast.state';
import { IViewMode } from '../../models/viewmode.model';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { InputDirective } from 'src/app/lib/input/input.directive';
import { OlInputPartial } from 'src/app/lib/input/input.partial';

@Component({
    _selector_
    templateUrl: './viewpath.html',
    changeDetection: ChangeDetectionStrategy.OnPush
    , imports: [CommonModule, RouterModule, ReactiveFormsModule, OlInputPartial, InputDirective]
})
export class MajorNameFormPartial implements OnInit {

    mode = input<IViewMode>({ forNew: true });
    onSave = output<any>();


    majorForm: FormGroup;

    constructor(private fb: FormBuilder, private toast: Toast) {
        //
    }
    ngOnInit(): void {
        //
        this.majorForm = this.fb.group({
            name: [],
            currency: [],
            notes: []
        });
    }

    // use to compare for initial select... keep '=='
    compareFn(c1: any, c2: any): boolean {
        return c1 && c2 ? c1.id == c2.id : false;
    }

    saveMajor(): void {
        this.toast.Hide();


        if (this.majorForm.valid) {
            // clone into a new object
            const _value = this.majorForm.value;

            const _major = { ..._value};

            // then emit
            this.onSave.emit(_major);
        } else {
            this.toast.ShowError('INVALID_FORM');
        }
    }
}
