import { Component, OnInit , Input, Output, EventEmitter } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { SmsdialogPage} from '../smsdialog.page';
import { SMSDialogService } from '../service/smsdialogservice';
import { SMSDialogModel } from 'src/app/hmg-common/ui/smsdialog/service/model/SmsDialogModel';
import { SharedDataService } from "src/app/hmg-common/services/shared-data-service/shared-data.service";
@Component({
  selector: 'sms-modal',
  templateUrl: './sms-modal.component.html',
  styleUrls: ['./sms-modal.component.scss'],
})
export class SmsModalComponent implements OnInit {
  
  countr_code: string;
  mobile_no: string;
  id_number : string;
  private dialoginfo = new SMSDialogModel();
  @Output() dismiss = new EventEmitter();
  @Output() cancelled = new EventEmitter();
  constructor(
    public modalController: ModalController,
    public sharedData: SharedDataService) { 
    }

  ngOnInit() {
    SMSDialogService.modal_success = false;
  }

  ngAfterViewInit() {
  }

  public async presentModal() {
    this.dialoginfo = this.sharedData.getSharedData( "smsdata",true);
    this.countr_code = this.dialoginfo.CountryCode;
    this.mobile_no = this.dialoginfo.MobileNo;
    this.id_number = this.dialoginfo.IdentificationNo;
    const modal = await this.modalController.create({
      component: SmsdialogPage,
      backdropDismiss : true,
      componentProps: {
        id : this.id_number,
        mobile : this.mobile_no,
        c_code : this.countr_code
     }
    });  
    modal.cssClass = 'sms-modal'; 
    modal.onDidDismiss().then((d: any) => this.handleModalDismiss(d));
    return await modal.present();
  }

handleModalDismiss = ({ data }) => {
      if (SMSDialogService.modal_success == false) {
        // alert that user cancelled
        this.cancelled.emit();
      } else {
        //save the data
        this.dismiss.emit();
      
      }


}
}
