orderplaced.page.ts 3.85 KB
import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { Router, ActivatedRoute } from '@angular/router';
import { ServiceService } from './../../config/service.service';
import { MyordersService } from './../../config/myorder.service';

@Component({
  selector: 'app-orderplaced',
  templateUrl: './orderplaced.page.html',
  styleUrls: ['./orderplaced.page.scss']
})
export class OrderplacedPage implements OnInit {
  deliveryTime: any;
  cancelDelivery: any;
  timeinterval: any;
  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private location: Location,
    private service: ServiceService,
    private myOrder: MyordersService
  ) {

  }



  data: any;

  relatedProd = {
    slidesPerView: 2
  };

  ionViewWillEnter() {
    this.deliveryTime = '00:00:00';
    this.service.get('order').then(val => {
      console.log('My Orders value', val);
      this.data = val;
      const This = this;
      console.log(Math.round(Date.now() / 1000) + ' < ' + this.data.bookDate.seconds);
      this.timeinterval = setInterval(() => {
        This.getDeliveryTime(this.data.bookDate.seconds, this.data.time);
      }, 1000);
    });
  }

  unEscape(text: string) {
    const regex = '%uFFFD';
    return unescape(text.replace(/%uFFFD/g, ''));
  }

  getDeliveryTime(timeStamp, minute) {
    let endDate = new Date(timeStamp * 1000);
    endDate = new Date(endDate.getTime() + minute * 60000);

    if (Math.round(Date.now() / 1000) < Math.round(endDate.getTime() / 1000)) {
      // console.log(endDate);
      const t = this.getTimeRemaining(endDate, new Date());
      const minutes = t.minutes < 10 ? '0' + t.minutes : t.minutes;
      const seconds = t.seconds < 10 ? '0' + t.seconds : t.seconds;
      const hours = t.hours < 10 ? '0' + t.hours : t.hours;
      this.deliveryTime = t.hours + ' : ' + minutes + ' : ' + seconds;
      if (t.t <= 0) {
        clearInterval(this.timeinterval);
      }
    }

  }

  getTimeRemaining(endtime, currDate) {
    const t = Date.parse(endtime) - Date.parse(currDate);
    this.cancelDelivery = t;
    const hours = Math.floor((((t / 1000) / 60) / 60) % 60);
    const seconds = Math.floor((t / 1000) % 60);
    const minutes = Math.floor((t / 1000 / 60) % 60);
    return {
      t,
      hours,
      minutes,
      seconds
    };
  }

  ngOnInit() { }

  goToPage(path, data = null) {
    this.router.navigateByUrl(path, { queryParams: data });
    document.body.scrollTop = document.documentElement.scrollTop = 0;
  }

  cancelOrder(orderId) {
    this.service.set('orderId', orderId);
    this.router.navigateByUrl('contact');
  }

  changeAddress(orderId) {
    this.service.set('orderId', orderId);
    this.router.navigateByUrl('changeaddress');
    document.body.scrollTop = document.documentElement.scrollTop = 0;
  }

  goBack() {
    this.location.back();
  }

  cancellOrder(orderId) {
    console.log(orderId);
    this.myOrder.cancelOrder(orderId).then(() => {
      this.service.showToast('Order cancelled!', 'top', 'my-toast', 1000);
      const This = this;
      setTimeout(() => {
        This.goToPage('ordercancelled');
      }, 1100);
    });
  }

  getStatus(status: number) {
    switch (status) {
      case 1:
        return 'Booked';
        break;
      case 2:
        return 'Shop Accepted';
        break;
      case 3:
        return 'Rider Assigned';
        break;
      case 4:
        return 'Reached Shop';
        break;
      case 5:
        return 'Picked Up';
        break;
      case 6:
        return 'Reached Location';
        break;
      case 7:
        return 'Delivered';
        break;
      case 9:
        return 'Completed';
        break;
      case 8:
        return 'Rejected';
        break;
      case 10:
        return 'Not Received';
        break;
      case 0:
        return 'Cancelled';
        break;
      default:
        return 'Finished';
    }
  }
}