import { Component, ViewChild } from '@angular/core';
import { IonicPage, NavController, NavParams, ViewController, LoadingController } from 'ionic-angular';
import { Pata } from '../../pata';
import { DoctorService } from '../../services/doctor.service';
import { Storage } from '@ionic/storage';

@Component({
  selector: 'page-modalvalorizacion',
  templateUrl: 'modalvalorizacion.html',
})
export class ModalvalorizacionPage {
  comentario:any;
  range: any[] = [];
  public profileActive: any;
  starMin: number = 0;
  starMin2: number = 0;
  starMin3: number = 0;
  editable: boolean;
  data:any;
  rate: number;
  rate2: number;
  rate3: number;
  serviceID: any;
  // edita: any;
  constructor(public navCtrl: NavController,
    public service: Pata, public viewCtrl: ViewController,
    private docService: DoctorService,
    public loadingCtrl: LoadingController,
    public navParams: NavParams,
    private storage: Storage) {
      this.storage.get("active").then((active) => {
        console.log(active["profile_id"]);
        this.profileActive = active["profile_id"];
      });
      this.serviceID = this.navParams.get('id');
       let edita = this.navParams.get('editable');
       this.data = this.navParams.get('data');
      if (edita){
        this.editable = true;
      }else{
        this.editable = false;
      }
  }
  dismiss() {
    this.viewCtrl.dismiss();
  }
  onModelChange(ev, id) {
    console.log('evento ', ev, 'id ' + id);
    let data = { 'star': ev, 'id': id }
    let range = this.range;
    for (let i = 0; i < range.length; i++) {
      if (range[i]["id"] == id) {
        this.range[i]["id"] = "no";
      }
    }

    this.range.push(data)
  }

  // saveRating() {
  //   let newArray = [];
  //   for (let i = 0; i < this.range.length; i++) {
  //     if (this.range[i]["id"] != "no") {
  //       newArray.push(this.range[i]);
  //     }
  //   }

  //   console.log(newArray);
  //   // this.service.showMsg("Rating Enviado (No conectado)");

  // }

  ratingRate(rate) {
    this.rate = rate;
  }
  ratingRate2(rate) {
    this.rate2 = rate;
  }
  ratingRate3(rate) {
    this.rate3 = rate;
  }

  sendRate() {
    let param = {
      "service_id": this.serviceID,
      "comments": this.comentario,     
      "rate": this.rate,
      "rate2":this.rate2,
      "rate3":this.rate3
    }
    console.log(param)
    let loading = this.loadingCtrl.create();
    loading.present();
    this.docService.sendRate(param).subscribe((data)=>{
      loading.dismiss();
      this.dismiss();
      this.service.showMsg(data["msg"]);
    }, (err)=>{
      loading.dismiss();
      console.error(err);
      this.service.showMsg("Algo ocurrio");
    })

  }

}
