﻿import { Component, ViewChild, Injector, Output, EventEmitter, OnInit, ElementRef} from '@angular/core';
{{Import_Modal_Directive_Here}}
import { finalize } from 'rxjs/operators';
import { {{Entity_Name_Plural_Here}}ServiceProxy, CreateOrEdit{{Entity_Name_Here}}Dto, LanguageServiceProxy, ApplicationLanguageListDto, Get{{Entity_Name_Here}}ForEditOutput {{NP_Looped_Import_Dropdown_LookupTableDto_Here}}} from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
{{Import_Datetime_Here}}
{{Import_DateTime_Service_Here}}{{NP_Looped_Lookup_Component_Import_Here}}
{{Import_File_Uploader_Here}}
{{Import_HttpClient}}
{{NP_Import_PrimeNG_AutoComplete}}
import { FormArray, FormBuilder, FormGroup } from '@angular/forms';
import { filter as _filter } from 'lodash-es';

@Component({
    selector: '{{master_Detail_Child_Prefix_Here}}createOrEdit{{Entity_Name_Here}}Modal',
    templateUrl: './{{master_Detail_Child_Prefix_Here}}create-or-edit-{{entity_Name_Here}}-modal.component.html'
})
export class {{Master_Detail_Child_Prefix_Here}}CreateOrEdit{{Entity_Name_Here}}ModalComponent extends AppComponentBase implements OnInit{
   
    @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective;
{{NP_Looped_ViewChild_Here}}
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    {{NP_TYPEAHEAD_PROPERTIES_HERE}}

    form: FormGroup;
    languages: ApplicationLanguageListDto[];
    defaultLanguage: string;
    selectedLanguage: string;    placeholder: string;
    initialValues: any;

{{Property_Nullable_Datetime_Dec_Looped_Template_Here}}
{{NP_Looped_Display_Declaration_Here}}
{{NP_Looped_Dropdown_Property_Here}}
{{File_Uploader_Definition_Here}}

    constructor(
        injector: Injector,
        private fb: FormBuilder,
        private _languageService: LanguageServiceProxy,
        private _{{entity_Name_Plural_Here}}ServiceProxy: {{Entity_Name_Plural_Here}}ServiceProxy{{Get_DateTime_Service_Here}}{{Add_Token_Service_Here}}{{Define_HttpClient_In_Constructor}}
    ) {
        super(injector);
    }

    ngOnInit(): void {
        {{Get_File_Prop_Accepted_Types_On_Init}}

        this._languageService.getLanguages().subscribe((result) => {
            this.defaultLanguage = result.defaultLanguageName;
            this.selectedLanguage = this.defaultLanguage;
            this.languages = result.items;

            this.languages = this.languages.map((l) => {

                if (l.name === this.defaultLanguage) {
                    l.displayName = `${l.displayName} (${this.l('DefaultLanguageName')})`;
                }

                return l;
            });

            this.form = this.fb.group({
                translations: this.fb.array(this.languages.map(l => this.fb.group({
                    language: l.name,
                    id: [],
                    {{Multi_Lingual_Looped_Property_Here}}
                }))),
                {{Non_Multi_Lingual_Looped_Property_Here}}{{Np_Form_Control_Looped_Here}}{{Master_Detail_Child_Foreign_Form_Builder_Here}}{{Master_Detail_Child_Entity_Foreign_Form_Builder_Here}}
                id: []
            });
            this.initialValues = this.form.value;
        });

    }

    changeLanguage(language: string): void {
        this.selectedLanguage = language;
    }

    getFormGroup(language: string){
        const translations = this.form.get('translations') as FormArray;
        const index = translations.controls.findIndex(c => c.get('language').value === language);
        const group = translations.at(index) as FormGroup;
        return group;
    }

    show({{Master_Detail_Child_Base_Property_Parameter_Here}}{{entity_Name_Here}}Id?: {{Primary_Key_Type_Here}}): void {
{{Property_Nullable_Datetime_SetNull_Looped_Template_Here}}
        this.form.reset(this.initialValues);
        {{Master_Detail_Child_Base_Property_Parameter_Load_Here}}
        this.form.enable();
        if (!{{entity_Name_Here}}Id) {
{{NP_Looped_Display_Init_Here}}
            this.active = true;
            this.modal.show();
        } else {
            this._{{entity_Name_Plural_Here}}ServiceProxy.get{{Entity_Name_Here}}ForEdit({{entity_Name_Here}}Id).subscribe(( {{{entity_Name_Here}}{{File_Name_Results}} } = {} as Get{{Entity_Name_Here}}ForEditOutput) => {        
                {{Date_Properties_Set_Here}}
                this.form.patchValue({...{{entity_Name_Here}}{{Date_Properties_Patch_Here}}});
                {{Set_File_Property_Here}}
                this.active = true;
                this.modal.show();
            });
        }
        {{NP_Looped_Dropdown_GetAll_Here}}
        {{Init_File_Uploader_Here}}
    }

    save(): void {
            this.saving = true;
			{{Property_Nullable_Datetime_GetValue_Looped_Template_Here}}
            this._{{entity_Name_Plural_Here}}ServiceProxy.createOrEdit(this.form.value as CreateOrEdit{{Entity_Name_Here}}Dto)
             .pipe(finalize(() => { this.saving = false;this.form.reset(this.initialValues);}))
             .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
             });
    }

{{NP_Looped_OpenModal_Here}}

{{NP_Typeahead_Filter_Here}}

{{NP_Typeahead_Get_All_Here}}

{{NP_Looped_SetNull_Here}}

{{NP_Looped_GetNew_Here}}

{{Init_File_Properties_Functions_Here}}

{{File_Upload_Initializer_Func_Here}}

{{Get_Download_Url_Here}}

    close(): void {
        this.active = false;
        this.modal.hide();
    }
    
}
