// Angular Core
import { Component, Input, OnInit, Output, EventEmitter } from '@angular/core';
import { FormGroup, ControlContainer } from '@angular/forms';

// Interfaces
import { UserDataInterface } from 'src/app/interfaces/user/user-data-interface';

// Components
import { StoreInfoComponent } from '../store-info.component';

// Interfaces
import { SessionDataInterface } from 'src/app/interfaces/session-data-interface';
import { BuyingGroupListInterface } from 'src/app/interfaces/user/buying-group-list-interface';
import { UtilitiesService } from 'src/app/services/utilities/utilities.service';

@Component({
  standalone: false,
  selector: "app-license-info-tab",
  templateUrl: "./license-info-tab.component.html",
  styleUrls: ["./license-info-tab.component.scss"],
})
export class LicenseInfoTabComponent implements OnInit {

    @Input() isLicenseCollapsed: boolean = true;
    @Input() userData!: UserDataInterface['data'];
    @Output() isLicenseCollapsedChange = new EventEmitter<boolean>();
    @Input() userStoreInfoForm: any = FormGroup;
    @Input() generalValidationMsg: string = '';
    @Input() selectBoxValidationMsg: string = '';
    @Input() buyingGroups!: BuyingGroupListInterface['data'];
    @Input() buyingGroupedList!: any[];
    @Input() session!: SessionDataInterface['data'];
    npiNumValidationMsg = 'Please enter valid data.';
    taxExemptNumValidateMsg = 'Please enter valid data.';
    expireDateValidationMsg = 'Please select valid date. Ex: (MM-DD-YYYY)';
    deaLabel = 'DEA License Number';
    deaExpiresLabel = 'DEA Expires';
    minDate: Date = new Date();

    @Output() update_user_data = new EventEmitter<string>();
    updateUserData(userEleName: string) {
        this.update_user_data.emit(userEleName);
    }

    constructor(private controlContainer: ControlContainer, public storeInfo: StoreInfoComponent, public utilities: UtilitiesService) { }

    ngOnInit(): void {
        this.userStoreInfoForm = <FormGroup>this.controlContainer.control;
        if (this.session.is_hin === true) {
            this.deaLabel = 'HIN License Number';
            this.deaExpiresLabel = 'HIN Expires';
        }
    }

    get userStoreInfoFormData() { return this.userStoreInfoForm.controls; }

    /**
      * Below method is introduced as part of TRX-7349.
      * When user initially selects Other option from Buying Group Dropdown and
      * If user given Other Buying Group value in Other Text box.
      * Later when user selects Regular Buying Group option from Dropdown
      * instead of Other option then the value in Other Text box need to be reset.
      * Below method will take care of that.
    */
    resetOtherOptionInput(type: string) {
        if (this.userStoreInfoFormData[type + "_buying_group_id"].value === '999999') {
            this.userStoreInfoFormData[type + "_buying_group_name"].setValue('');
        }
    };

    collapseTab() {
        this.isLicenseCollapsedChange.emit(!this.isLicenseCollapsed);
    }

    onSelectTaxExpireDate(value: Date) {
        if (new Date(value) > new Date()) {
            this.updateUserData('tax_exempt_expire_date');
        }
    }

    onBlurTaxExpireDate(event: any) {
        if (event.target.value !== '' && new Date(event.target.value) > new Date()) {
            this.updateUserData('tax_exempt_expire_date');
        }
    }
}
