import {Component, OnInit, ViewChild} from '@angular/core';
import {NavController} from "@ionic/angular";
import {Router} from "@angular/router";
import { WrenchService } from 'src/app/services/wrench.service';
import {SessionDataProviderService} from "../../store/session-data-provider.service";
import {last} from "rxjs/operators";
import { AlertController } from '@ionic/angular';

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

  @ViewChild('ref_firstname') ref_firstname;
  @ViewChild('lastname') lastname;
  @ViewChild('email') email;
  tabs = 'refer';

  constructor(
      private navctr: NavController,
      private router: Router,
      public sessionDataProviderService: SessionDataProviderService,
      private wrenchService: WrenchService,
      private alertController: AlertController
  ) {
  }
/*
 'member_id' => int 1
  'sessionid' => string '936571FD2E081B667930E7FFDF4D819938171883CA0CC951DC6C4FB184280EB3' (length=64)
  'uid' => string '3119b744-42ad-4834-bb83-b737588754ca' (length=36)
  'action' => int 11032
  'ref_firstname' => string 'Reffirst10' (length=10)
  'ref_lastname' => string 'Reflast86' (length=9)
  'ref_email' => string 'ameye+636@chiefsoft.com' (length=23)
 */
  referSendData: {
    action:number, member_id: number, uid: string, sessionid: string, ref_firstname:string, ref_lastname:string,ref_email: string
  };
  sendReferResult:any;
  sendRefer(){

    if (this.ref_firstname=="" || this.lastname=="" || !this.validateEmail(this.email) ){
      this.showAlert('Refer Error', 'Email and names required');
      return;
    }
    this.referSendData =
        {
      action:11032,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session ,
      ref_firstname:this.ref_firstname,
      ref_lastname:this.lastname,
      ref_email:this.email
    };
    this.wrenchService.sendRefer(this.referSendData).subscribe(
        sendReferResult => {
          this.sendReferResult = sendReferResult;
          if ( this.sendReferResult.internal_return == 0 ){
            this.showAlert('Refer Sent', 'Message sent to '+ this.email);
            this.ref_firstname=this.lastname=this.email="";
          }
          else {
            this.showAlert('Refer Error', this.sendReferResult.status);

          }
          this.getReferData();
        }
    );


  }

  validateEmail(email) {
    var re = /\S+@\S+\.\S+/;
    return re.test(email);
  }

  ngOnInit() {
    this.getReferData();
  }

  onBack() {
    this.navctr.back();
  }

  usrData: {
    action:22010, member_id: number, uid: string, sessionid: string, limit:20, page:1,offset: 0
  };
  referTotalData:any;
  referData: [];
  getReferData(){
    this.usrData = {action:22010,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session ,
      limit:20, page:1,offset: 0}

    this.wrenchService.referHx(this.usrData).subscribe(
        referTotalData => {
          this.referTotalData = referTotalData;
         // console.log("REFER  RETURN->", this.referTotalData);
          this.referData = this.referTotalData.result_list;
          console.log("REFER RETURN DATA->", this.referData);
        }
    );

  }

  async showAlert(mtitle: string, amessage: string) {

    const alert = await this.alertController.create({
      header: 'WrenchBoard',
      subHeader: mtitle,
      message: amessage,
      buttons: ['OK'],
    });

    await alert.present();
    //
    //    let alert = this.alertCtrl.create({
    //      title: mtitle,
    //      subTitle: amessage,
    //      buttons: ['OK']
    //    });
    //    alert.present();
    // //  alert(amessage);
  }
}
