import {Component, OnInit } from '@angular/core';
import { NavController, NavParams, LoadingController, ModalController } from 'ionic-angular';

import { environment } from "../../environments/environment"
import {SocialSharing} from "@ionic-native/social-sharing";
import * as moment from 'moment';
import { DoctorService } from '../../services/doctor.service';
import { ProductoPage } from '../producto/producto';
import { ModalCart } from '../../modals/cart/cart';
import { Pata } from '../../pata';
import { UserService } from '../../services/user.service';
@Component({
  selector: 'page-category',
  templateUrl: 'category.html'
})
export class CategoryPage implements OnInit{

  public image = '';
  public link = '';
  id: string = '';
  private isSharing: boolean;
  public loaded: boolean = false;
  public category: any = null;
  public products: any = [];
  public categorys: any = [];
  public loading: any = null;
  public cartItems: number = 0;
  public user: any = { id: '0' };
  public storeName: string = "Productos";

  constructor(public navCtrl: NavController,
              private navParams: NavParams,
              private socialSharing: SocialSharing,
              public loadingCtrl: LoadingController,
              public modalCtrl: ModalController,
              public service: Pata,
              public userService: UserService,
              public doctorService: DoctorService)
  {
    let loading = this.loadingCtrl.create();
    loading.present();
    this.id = this.navParams.get("id");
    this.userService.getProfile().subscribe((result:any)=>{
      this.user = result;
    });
    this.doctorService.getCategory(this.id).subscribe((data:any) => {    
      this.category = data.data;
      this.categorys = data.subcats;
      this.products = data.products;
      this.loaded = true;
      loading.dismiss();
    });

  }

  ngOnInit(){
    
  }
  ionViewDidEnter(){
    this.storeName = this.service.storeName;
    this.doctorService.getCart().subscribe((data:any)=> {
      this.cartItems = data.short.length;
    });  
  }
  share(){
    /*
    if(this.isSharing) return false;
    this.isSharing = true;
    this.socialSharing.share(this.news.title.rendered,this.news.description, this.image, this.news.link).then(() => {
      this.isSharing = false;
    }).catch(() => {
      this.isSharing = false;
    });
    */
  }
  navproduct(product: any) {
    this.navCtrl.push(ProductoPage, {id: product.id});
  }
  navcategory(category: any) {
    this.navCtrl.push(CategoryPage, {id: category.id});
  }
  dateformat(fecha){
    var fecha_mm = moment(fecha);
    return moment(fecha_mm.format(),moment.ISO_8601).format();
  }
  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();
  }
}
