import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { CommonFunctionService } from 'src/app/core/common/common-function.service';
import { ValidationService } from 'src/app/core/common/validation.service';
import { API } from 'src/app/core/services/api.service';

@Component({
  selector: 'app-activity-category-add',
  templateUrl: './activity-category-add.component.html',
})
export class ActivityCategoryAddComponent {
  Edit_Id: string;
  ActivityCategoryForm: FormGroup;
  AllLang: any[] = [];
  AllParentCategory: any[] = [];
  editResp;

   constructor(
      private fb: FormBuilder,
      public validServ: ValidationService,
      private apiserv: API,
      private commonMasterSer: CommonFunctionService,
      private router: Router,
      private activateRoute: ActivatedRoute
    ) {}

    ngOnInit(): void {
    this.Edit_Id = this.activateRoute.snapshot.paramMap.get('RuleId') as string;
    this.initForm();
    this.getAllLanguages();
    this.getParentCategory();
    if (this.Edit_Id) {
      this.getById();
    }
  }

  initForm() {
    this.ActivityCategoryForm = this.fb.group({
      defaultLanguageCode: this.validServ.required,
      categoryName: this.validServ.required,
      parentCategoryId: '',
      activityCategoryDetailsId:''
    });
  }

    getAllLanguages() {
    this.apiserv.AdminGet('/LanguageMapping/GetLanguagesByOrgId').subscribe(data => {
      if (data) {
        this.AllLang = data;
      }
    });
  }

   getById() {
    this.apiserv.Get(`/ActivityCategory/GetById/${this.Edit_Id}`).subscribe(res => {
      if (res && res.status) {
        this.editResp = res.data;
        this.ActivityCategoryForm.get('categoryName').setValue(this.editResp.activityCategoryDetails.categoryName);
        this.ActivityCategoryForm.get('defaultLanguageCode').setValue(this.editResp.defaultLanguageCode);
        this.ActivityCategoryForm.get('parentCategoryId').setValue(this.editResp.parentCategoryId);
      }
   });
  }
   getParentCategory() {
    this.apiserv.Get(`/ActivityCategory/GetAllCategory`).subscribe(res => {
      if (res && res.status) {
        this.AllParentCategory = res.data;
      }
   });
  }

  
  BackButton() {
    this.router.navigateByUrl('ActivityConfiguration/ActivityCategory/Search');
  }

    SaveUpdateCategory() {
    if (this.ActivityCategoryForm.invalid) {
      this.validServ.showValidationsMsg(this.ActivityCategoryForm);
      setTimeout(() => this.validServ.scrollToError(), 50);
      return;
    }

    const formVal = this.ActivityCategoryForm.getRawValue();
    const postdata = {
      defaultLanguageCode:formVal.defaultLanguageCode,
      parentCategoryId:formVal.parentCategoryId || '',
      activityCategoryId:this.Edit_Id || '',
      activityCategoryDetails : {
        activityCategoryDetailsId : formVal.activityCategoryDetailsId,
        activityCategoryId: this.Edit_Id || '',
        categoryName:formVal.categoryName,
        languageCode:formVal.defaultLanguageCode,
      }
    };
    const url = this.Edit_Id ? 'Update' : 'Save';
    this.apiserv.Post(`/ActivityCategory/${url}`, postdata).subscribe(resp => {
      if (resp?.status) {
        this.commonMasterSer.showMessage(resp.status, resp.respMsg);
      } else {
        this.commonMasterSer.showMessage(false, resp?.message || 'Save failed');
      }
    });
  }

}
