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 * as moment from 'moment';
import { RecetaPage } from '../receta/receta';
import { RecetaAddPage } from '../receta/add';
import { forkJoin } from 'rxjs/observable/forkJoin';
import { ProductoPage } from '../producto/producto';
import { CategoryPage } from '../category/category';
import { ModalCart } from '../../modals/cart/cart';
import { ModalTermsPage } from '../../modals/modal-terms/modal-terms';
import { UserService } from '../../services/user.service';

@Component({
  selector: 'page-farmacia',
  templateUrl: 'farmacia.html'
})
export class FarmaciaPage {
  products: any = [];
  results: any = [];
  categorys: any = [];
  balance: number = 0;
  loadFeatured: any = null;
  loadBalance: any = null;
  loadCategory: any = null;
  search: string = '';
  cartItems: number = 0;
  searching: boolean = false;
  searchSub: any = null;
  running: boolean = false;
  appInvite: boolean = true;
  user: any = {id: '0'};
  storeName: string = "Productos";

  constructor(
    public navCtrl: NavController,
    public modalCtrl: ModalController,
    public loadingCtrl: LoadingController,
    private alertCtrl: AlertController,
    public service: Pata,
    private userService: UserService,
    private doctorService: DoctorService) {

      this.appInvite = this.service.appInvite;
      this.userService.getProfile().subscribe((result:any)=>{
        this.user = result;
      });
      let loading = this.loadingCtrl.create();
      loading.present();
      /*
      this.doctorService.getCart().subscribe((data:any)=> {
        this.cartItems = data.short.length;
      });      
      */
      this.loadBalance = this.doctorService.getBalanceGrow();
      this.loadFeatured = this.doctorService.featured();
      this.loadCategory = this.doctorService.getCategory();

      this.doctorService.getTerms().subscribe((tdata:any)=> {
        let termsModal = this.modalCtrl.create(ModalTermsPage, { obj: tdata.data }, { enableBackdropDismiss: false, showBackdrop: true });
        let presentModal = 0;
        if (tdata.res == "ERR") {
          presentModal = 1;
        }
        forkJoin(this.loadBalance, this.loadFeatured, this.loadCategory).subscribe((data:any) => {
          loading.dismiss();
          if (presentModal == 1) {
            termsModal.present();
          }          
          this.balance = data[0].balance;
          this.products = data[1].data;
          this.categorys = data[2].data;
        });
      });
  }
  ionViewDidEnter(){
    this.storeName = this.service.storeName;
    this.doctorService.getCart().subscribe((data:any)=> {
      this.cartItems = data.short.length;
    });   
  }
  onCancel(evt:any) {
    this.search = '';
    this.getItems('nothing');
  }
  getItems(evt:any) {
    
    if (this.search.trim() == '') {
      this.searching = false;
      this.results = [];
    }
    else {
      this.searching = true;
    }
    try {
      this.searchSub.unsubscribe();
      this.running = false;
    } catch (err) {

    }

    if (this.searching) {
      this.running = true;
      this.searchSub = this.doctorService.search(this.search);
      
      this.searchSub.subscribe((data:any) => {
        this.running = false;
        this.results = data.data;
      });
    }



  }
  cart() {
    let cartModal = this.modalCtrl.create(ModalCart);
    cartModal.onDidDismiss((data:any) => {
      if (data) {
        this.cartItems = data.items.length;
        this.doctorService.cartFull = data;
        this.doctorService.syncCart();
        if (data.hasOwnProperty("finish") && data.finish == 1) {
          this.service.showOk(data.msg);
        }
      }
    });
    cartModal.present();
  }
  goRecetasAdd() {
    this.navCtrl.push(RecetaAddPage);
  }
  
  dateformat(fecha){
    var fecha_mm = moment(fecha);
    return moment(fecha_mm.format(),moment.ISO_8601).format();
  }

  detail(product: any) {
    this.navCtrl.push(ProductoPage, {id: product.id});
  }
  navcategory(category: any) {
    this.navCtrl.push(CategoryPage, {id: category.id});
  }
}


