import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { NgxSpinnerService } from 'ngx-spinner';
import { ToastrService } from 'ngx-toastr';
import { Customer } from '../../models/customer';
import { CustomerService } from '../../shared/services/customer.service';

@Component({
  selector: 'app-customer-detail',
  templateUrl: './customer-detail.component.html',
  styleUrls: ['./customer-detail.component.scss']
})
export class CustomerDetailComponent implements OnInit {
  public token!: string;
  private alertMessage!: string;
  public customer = new Customer();
  public LoanDisplayColumns = ['id', 'serviceName', 'loanAmount', 'disbursedAmount', 'emi', 'ROI', 'tenure', 'loanTransactionDate', 'status', 'action'];
  public leadDisplayColumn = ['id', 'type', 'serviceName', 'leadStatus', 'loanAmount', 'assignPartner', 'assignOn', 'createdDate'];
  public walletColumns = ['id', 'coin', 'rewardType', 'isWithdrawal', 'createdDate'];
  public OtherLoanDisplayColumns = ['id', 'serviceType', 'serviceName', 'fullName', 'contactNo', 'createdDate', 'employmentType'];
  public OrderDisplayColumns = ['id', 'productName', 'quantity', 'unitCoin', 'totalcoin', 'orderstatus', 'transactiondate'];
  public currency!: any;
  public customerId:any = '';
  constructor(
    private spinnerService: NgxSpinnerService,
    private toastrService: ToastrService,
    private actRoute: ActivatedRoute,
    private router: Router,
    private translate: TranslateService,
    private customerService: CustomerService,
  ) { translate.setDefaultLang('en'); }

  async ngOnInit() {
    this.token = sessionStorage.getItem("SessionToken")!;
    let currency = JSON.parse(sessionStorage.getItem("Credential") ?? 'null');
    this.currency = currency.currency;
    let customerId = this.actRoute.snapshot.paramMap.get('id');
    this.customerId = customerId;
    await this.getCustomerById(customerId);
    let lang = localStorage.getItem("selectedLanguage")!;;
    // translate.setDefaultLang(lang);
    this.translate.use(lang);
  }

  public async getCustomerById(customerId: any) {
    try {
      let res = await this.customerService.getCustomerById(customerId, this.token);
      if (res && res.status == 200) {
        this.customer = res.recordList;
      }
    } catch (error: any) {
      this.spinnerService.hide();
      this.alertMessage = error;
      if (error?.message) {
        this.alertMessage = error.message
      }
      if (error?.error && error.error.message) {
        this.alertMessage = error.error.message
      }
      if (error?.error && error.error.error && error.error.error.functionErrorMessage) {
        this.alertMessage = error.error.error.functionErrorMessage
      }
      this.toastrService.error(this.alertMessage)
    }
  }

  public viewLoanDetail(ele: any) {
    if (ele.serviceId == 2) {
      this.router.navigate(["admin/business-loans", ele.id]);
    }
    else if (ele.serviceId == 7) {
      this.router.navigate(["admin/home-loan", ele.id]);
    }
    else if (ele.serviceId == 9) {
      this.router.navigate(["admin/LAP", ele.id]);
    }
    else if (ele.serviceId == 1) {
      this.router.navigate(["admin/personal-loans", ele.id]);
    }
    else if (ele.serviceId == 4) {
      this.router.navigate(["admin/instant-loans", ele.id]);
    }
  }
}
