import {Component, OnInit, ViewChild} from '@angular/core';
import {AlertController, NavController, ToastController, LoadingController} from "@ionic/angular";
import {Router} from "@angular/router";
import {SessionDataProviderService} from "../../store/session-data-provider.service";
import {WrenchService} from "../../services/wrench.service";
import {RecipientsService} from "../../store/recipients.service";

@Component({
  selector: 'app-addbank',
  templateUrl: './addbank.page.html',
  styleUrls: ['./addbank.page.scss'],
})
export class AddbankPage implements OnInit {

    @ViewChild('acc_type')  acc_type;
    @ViewChild('sel_bank') sel_bank;
    @ViewChild('account_no') account_no;
    @ViewChild('confirm_account_no') confirm_account_no;
    @ViewChild('account_city') account_city;
    @ViewChild('account_state') account_state;
    currency_code:string='';
    currency_description : string='';
    isDisabled:boolean = true;
    walletData:any;
    wallet_uid:string='';
    constructor(
               private navctr: NavController,
               private router: Router,
               public sessionDataProviderService: SessionDataProviderService,
               private wrenchService: WrenchService,
               private alertController: AlertController,
               private toastController: ToastController,
               public recipientsService: RecipientsService,
               private loadingCtrl: LoadingController
    )
    {
        this.walletData  =  this.router.getCurrentNavigation().extras.state;
        if ( this.walletData == undefined){
            this.onBack();
        }
        this.currency_code = this.walletData.code;
        this.currency_description = this.walletData.description;
        this.wallet_uid= this.walletData.wallet_uid;
    }

  ngOnInit() {
        this.getcountryBanksData();
        this. getaccountTypesData();
        this.isDisabled = true;
        this.account_state = this.sessionDataProviderService.account_state;
        this.account_city =  this.sessionDataProviderService.city;
        //debugger;
    }
  onBack() {
    this.navctr.back();
  }

  refreshAccNo(){
        console.log("ACCCC -> " , String(this.account_no).length,  this.confirm_account_no, this.confirm_account_no );
        console.log("ACC22 -> " , String(this.account_city).length,  String(this.account_city), this.account_state );
        this.isDisabled  = true;

        if(
            this.account_no != undefined &&
            this.account_city != undefined &&
            this.account_state != undefined &&
            String(this.account_no).length == 10
            && this.confirm_account_no ===  this.account_no
            && String(this?.account_state).length > 3
            && String(this?.account_city).length > 3
            && String(this.sel_bank).length>1
            && String(this.acc_type).length>0
        ){
            //debugger;
            this.isDisabled  = false;
        }
    }

  countryBanksResult:any;
  countryBanksData: any;
  usrData: {
    action:number,
      member_id: number,
      uid: string,
      sessionid: string,
      limit:20, page:1
  };
  getcountryBanksData(){
    this.usrData = {action:11200, member_id: this.sessionDataProviderService.member_id, uid: this.sessionDataProviderService.member_uid, sessionid: this.sessionDataProviderService.session, limit:20, page:1 }
    this.wrenchService.countryBanks(this.usrData).subscribe(
        countryBanksResult => {
          this.countryBanksResult = countryBanksResult;
          console.log("countryBanks RETURN->", this.countryBanksResult);
          this.countryBanksData = this.countryBanksResult.result_list;
          // debugger;
          console.log("countryBanks RETURN DATA->", this.countryBanksData);
        }
    );
  }

  accountTypesResult:any;
  accountTypesData: [];

  getaccountTypesData(){
    this.usrData = {action:11200, member_id: this.sessionDataProviderService.member_id, uid: this.sessionDataProviderService.member_uid, sessionid: this.sessionDataProviderService.session, limit:20, page:1 }
    this.wrenchService.accountTypes(this.usrData).subscribe(
        accountTypesResult => {
          this.accountTypesResult = accountTypesResult;
          console.log("accountTypes RETURN->", this.accountTypesResult);
          this.accountTypesData = this.accountTypesResult.result_list;
          // debugger;
          console.log("accountTypes RETURN DATA->", this.accountTypesData);
        }
    );
  }

    reqData: {
        action:number,
        member_id: number,
        uid: string,
        sessionid: string,
        firstname: string,
        lastname: string,
        bank_code: string,
        account_no: string,
        account_type: number,
        country: string,
        state: string,
        city: string,
    };

      accountAddResult :any;
    async addRecipientAccount(){

         const loading = await this.loadingCtrl.create({
             message: 'Adding recipent account',
             duration: 5000,
         });

         this.reqData = {
             action:11176,
             member_id: this.sessionDataProviderService.member_id,
             uid: this.sessionDataProviderService.member_uid,
             sessionid: this.sessionDataProviderService.session,
             firstname: this.sessionDataProviderService.firstname,
             lastname: this.sessionDataProviderService.lastname,
             bank_code: this.sel_bank,
             account_no: this.account_no,
             account_type: this.acc_type,
             country: this.sessionDataProviderService.account_country,
             state: this.account_state,
             city: this.account_city,
         };
        loading.present();
         this.wrenchService.addRecipientAccount(this.reqData).subscribe(
             accountAddResult => {
                 this.accountAddResult = accountAddResult;
                 console.log("accountAddResult RETURN->", this.accountAddResult);
                 if ( this.accountAddResult.internal_return != undefined && this.accountAddResult.internal_return > 0 ){
                     this.presentToast("Recipient Accound Added",'middle');
                     loading.dismiss();
                     setTimeout(() => {
                         this.recipientsService.getMyRecipientsData();
                         this.onBack();
                     }, 3000);
                 }
             }
         );
     }

    async presentToast(message, position: 'top' | 'middle' | 'bottom') {
        const toast = await this.toastController.create({
            message: message,
            duration: 1500,
            position: position,
        });

        await toast.present();
    }

}
