import { NgIf } from '@angular/common';
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import { api } from '@enums/api.enum';
import { Roots } from '@enums/root.enum';
import { IonicModule, NavController } from '@ionic/angular';
import { AlertService } from '@services/alert.service';
import { HttpRequestService } from '@services/http-request.service';

@Component({
  selector: 'app-sangh-role-select',
  templateUrl: './sangh-role-select.page.html',
  styleUrls: ['./sangh-role-select.page.scss'],
  standalone: true,
  imports: [IonicModule, NgIf, FormsModule]
})
export class SanghRoleSelectPage {
  roleValue: string;
  userValue: any;
  id: string;

  constructor(
    private httpRequestService: HttpRequestService,
    private alert: AlertService,
    private navCtrl: NavController,
    private router: Router
  ) {
    this.userValue = this.router.getCurrentNavigation().extras?.state?.userValue;
    this.id = this.router.getCurrentNavigation().extras?.state?.id;
  }
  addMember() {
    const params = {
      sangh_id: this.id,
      user_id: [this.userValue.id],
      core_committee: 1,
      role: this.roleValue,
    };
    this.httpRequestService.post(api.addMemberForSangh, params).then((res: any) => {
      this.alert.showToaster(res.message);
      this.navCtrl.navigateBack([Roots.page + '/' + Roots.sanghProfile, { sanghId: this.id }]);
    }).catch((err) => {
      this.alert.showToasterError(err.error);
    });
  }

}
