import { Component } from '@angular/core';
import {CurrencyPipe} from '@angular/common';
import { NavController, ModalController, LoadingController, AlertController } from 'ionic-angular';

import { DoctorService } from '../../services/doctor.service';
import { Pata } from '../../pata';
import { environment } from "../../environments/environment"
import * as moment from 'moment';
import { VerRecorridoPage } from '../ver-recorrido/ver-recorrido';
import { ModalDetallesPage } from '../../modals/modal-detalles/modal-detalles';
import { UserService } from '../../services/user.service';

@Component({
  selector: 'page-pedidos',
  templateUrl: 'pedidos.html'
})
export class PedidosPage {
  
  public orders: any = [];
  public user: any = { id: '0'};

  constructor(
    public navCtrl: NavController,
    public modalCtrl: ModalController,
    public loadingCtrl: LoadingController,
    private alertCtrl: AlertController,
    public service: Pata,
    public userService: UserService,
    private doctorService: DoctorService) {
      
      this.getOrders();
      this.userService.getProfile().subscribe((result:any)=>{
        this.user = result;
      });
  }
  ionViewDidEnter(){
  
  }

  getOrders() {
    let load = this.loadingCtrl.create({content:'cargando...'});
    load.present();
    this.doctorService.getOrders().subscribe((data:any)=>{
      load.dismiss();
      this.orders = data.data;
    });
  }
  detalles(index:number) {
    let detallesModal = this.modalCtrl.create(ModalDetallesPage, { id: this.orders[index].id });
    detallesModal.onDidDismiss(data => {
      
    });
    detallesModal.present();
  }
  seguimiento(index:number) {
    this.navCtrl.push(VerRecorridoPage, { 
      id: this.orders[index].id,
      origen: {
        lat: -33.4049614,
        lng: -70.5998516
      }, 
      destino: {
        lat: parseFloat(this.orders[index].lat),
        lng: parseFloat(this.orders[index].lng)
      }
    });
  }
  
  cancelarCompra(index:number) { 
    let id = this.orders[index].id;
    let alert = this.alertCtrl.create({
      title: 'Anular el abono',
      message: 'Solo podrás anular el abono que aún no estén en reparto. ¿Deseas continuar con la anulación?',
      buttons: [
        {
          text: 'Cancelar',
          role: 'cancel',
          handler: () => {
            
          }
        },
        {
          text: 'Anular abono',
          role: 'destructive',
          handler: () => {
            this.anularBackend(id,index);
          }
        }
      ]
    });
    alert.present();
  }
  anularBackend(id:string,index:number) {
    let load = this.loadingCtrl.create({content:'anulando...'});
    load.present();
    this.doctorService.deleteOrder(id).subscribe((data:any)=>{
      load.dismiss();
      if (data.res == "OK") {
        this.orders.splice(index,1);
        this.service.showOk("Abono anulado exitosamente");
      }
      else {
        let alert = this.alertCtrl.create({
          title: 'Anulación Fallida',
          subTitle: 'No fue posible anular el abono '+data.msg,
          buttons: ['Aceptar']
        });
        alert.present();
      }
    });
  }
  dateformat(fecha){
    var fecha_mm = moment(fecha);
    return moment(fecha_mm.format(),moment.ISO_8601).format();
  }

}


