import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {ICustomerContact} from '../../../../model/entities/customer-contact';
import {Factory} from '../../../../factory/factory';
import {NgForm} from '@angular/forms';
import {isNull} from 'util';
import {ScrollToService} from '@nicky-lenaers/ngx-scroll-to';
import {AppService} from '../../../../services/app.service';
import {IConfig} from '../../../../model/virtual/config';
import {FormComponent} from '../../../../components/form-component';
import {Utils} from '../../../../utils/utils';

@Component({
  selector: 'app-customer-contact-person-edit',
  templateUrl: './customer-contact-person-edit.component.html',
  styleUrls: ['./customer-contact-person-edit.component.scss']
})

export class CustomerContactPersonEditComponent extends FormComponent implements OnInit {

    @Output() createCustomerContact: EventEmitter<ICustomerContact> = new EventEmitter<ICustomerContact>();
    @Output() editCustomerContact: EventEmitter<ICustomerContact> = new EventEmitter<ICustomerContact>();
    @ViewChild('customerContactForm', { static: true }) customerContactForm: NgForm;

    public customerContact: ICustomerContact;
    public config: IConfig;

    constructor(private appService: AppService, protected scrollToService: ScrollToService) {
        super(scrollToService);
    }

    ngOnInit() {
        this.customerContact = Factory.getEmptyCustomerContact();
        this.config = this.appService.getConfig();
    }

    /**
     * Sets data
     * @param customerContact
     */
    public setData(customerContact: ICustomerContact): void {
        this.customerContact = customerContact;
        this.isCreationMode = isNull(this.customerContact.id);
        this.errorMsg = '';
        this.resetFormValidation();
    }

    /**
     * Resets Form Validation
     */
    public resetFormValidation(): void {
        this.customerContactForm.form.markAsUntouched();
    }

    /**
     * Saves customer
     * @param value
     * @param valid
     */
    public onFormSubmit({value, valid}: { value: any, valid: boolean }): void {
        this.errorMsg = 'Bitte beheben Sie alle Fehler.';
        if (valid) {
            // Check if date is valid
            if (isNull(this.customerContact.date_of_birth) ||
                (!isNull(this.customerContact.date_of_birth) && Utils.isValidDate(this.customerContact.date_of_birth))) {
                if (this.isCreationMode) {
                    this.createCustomerContact.emit(this.customerContact);
                } else {
                    this.editCustomerContact.emit(this.customerContact);
                }
                this.errorMsg = '';
            } else {
                this.errorMsg = 'Bitte korrektes Datum eingeben.';
                this.scrollUp(this.isCreationMode ? 'customer-contact-person-modal' : 'customer-contact-person-edit-modal');
            }
        } else {
            this.scrollUp(this.isCreationMode ? 'customer-contact-person-modal' : 'customer-contact-person-edit-modal');
        }
    }

}
