import { NgFor, NgIf } from '@angular/common';
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ActivatedRoute } 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';
import { UtilityService } from '@services/utility.service';

@Component({
  selector: 'app-invite-members',
  templateUrl: './invite-members.page.html',
  styleUrls: ['./invite-members.page.scss'],
  standalone: true,
  imports: [IonicModule, FormsModule, NgFor, NgIf]
})
export class InviteMembersPage {
  sanghId: string;
  type: string;
  userList = [];
  searchValue: string;
  prevValue: string;
  pageNumber = 1;
  totalPage = 1;
  selectedUserList = [];
  constructor(
    private navCtrl: NavController,
    private cRoute: ActivatedRoute,
    private httpRequestService: HttpRequestService,
    private alert: AlertService,
    private utilityService: UtilityService
  ) { }

  ionViewWillEnter() {
    this.sanghId = this.cRoute.snapshot.paramMap.get('id');
    this.type = this.cRoute.snapshot.paramMap.get('type');
    this.getUserList(false, '');
  }

  searchSangh() {
    const isSameStr = this.utilityService.isSameStr(this.searchValue, this.prevValue);
    this.prevValue = this.searchValue;
    if (!isSameStr) {
      this.userList = [];
      this.pageNumber = 1;
      this.totalPage = 1;
      this.getUserList(false, '');
    }
  }

  getUserList(isFirstLoad, event) {
    if (this.pageNumber <= this.totalPage) {
      this.httpRequestService.post(api.memberListForAddInSangh, {
        page: this.pageNumber,
        limit: 15,
        name: this.searchValue ? this.searchValue.trim() : '',
        sangh_id: this.sanghId,
      }).then((res: any) => {
        if (isFirstLoad) { event.target.complete(); }
        if (!event) { this.userList = []; }
        this.totalPage = res.data.last_page;
        this.userList.push(...res.data.data);
        this.pageNumber++;
      }).catch((err) => {
        this.alert.showToasterError(err.error);
      });
    } else {
      event.target.complete();
    }
  }

  loadData($event) {
    this.getUserList(true, $event);
  }

  onChange(event, index: number, id: number) {
    if (event.detail.checked) {
      this.userList[index].checked = true;
      this.selectedUserList.push(id);
    } else {
      this.userList[index].checked = false;
      const indexValue = this.selectedUserList.indexOf(id);
      if (indexValue > -1) {
        this.selectedUserList.splice(indexValue, 1);
      }
    }
  }

  addMembers() {
    const params = {
      sangh_id: this.sanghId,
      user_id: this.selectedUserList,
    };
    this.httpRequestService.post(api.addMemberForSangh, params).then((res: any) => {
      this.alert.showToaster(res.message);
      this.navCtrl.navigateBack([Roots.page + '/' + Roots.sanghProfile, { sanghId: this.cRoute.snapshot.paramMap.get('id') }]);
    }).catch((err) => {
      this.alert.showToasterError(err.error);
    });
  }
}
