import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { WebService } from './../../providers/web.service';
import { ValidationService } from './../../providers/validation.service';

@Component({
  selector: 'app-transaction',
  templateUrl: './transaction.component.html',
  styleUrls: ['./transaction.component.scss']
})
export class TransactionComponent implements OnInit {
  transactionList: any[];
  fname: any;
  lang =  'en';
  responseMsg: any;
  error: boolean;
  success: boolean;
  loader: boolean;
  loginDetails: any;
  meta: any;
  userDetails: any;

  constructor(private router: Router, private route: ActivatedRoute, public vs: ValidationService, public service: WebService) {
    this.responseMsg = '';
    this.error = false;
    this.success = false;
    this.loader = false;
    this.loginDetails = JSON.parse(this.service.getLocalStorageItem('userData'));
    console.log(this.loginDetails);
  }

  ngOnInit() {
    this.getProvider();
  }

  getTransaction(id: any, page_number: number) {
    this.error = false;
    this.loader = true;
    this.fname = 'transaction_list';
    const post_data = {'type': 2, 'user_id': id, 'page': page_number};
      this.service.post_data(this.fname, post_data).subscribe(response => {
        this.loader = false;
        console.log(response);
        if (response.code === 1) {
          this.transactionList = response.responseResult.data;
          this.meta = response.responseResult.meta;
          console.log(this.transactionList);
        } else {
          this.transactionList = [];
          this.error = true;
          this.responseMsg = this.vs.errorCode[this.lang][response.errorCode];
        }
      }, (error) => {
        this.error = true;
        this.responseMsg =  this.vs.errorCode[this.lang]['ER08'];
    });

  }

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

  getProvider() {
    this.fname = 'get_user_details';
    const post_data = {'user_id': this.loginDetails.user_id};
    this.service.post_data(this.fname, post_data).subscribe(response => {
      if (response.code === 1) {
        this.userDetails = response.responseResult;
        this.getTransaction(this.userDetails.id, 1);
      } else {
        this.error = true;
        this.responseMsg = this.vs.errorCode[this.lang][response.errorCode];
      }
    }, (error) => {
      this.error = true;
      this.responseMsg =  this.vs.errorCode[this.lang]['ER08'];
  });
  }

  get_status(status: any) {
    switch (status) {
      case 0:
        return 'Cancelled';
        break;
      case 1:
        return 'Requested';
        break;
      case 2:
        return 'Assigned to Driver';
        break;
      case 3:
        return 'Delivered to Driver';
        break;
      case 4:
        return 'Delivered to Customer';
        break;
      case 5:
        return 'Refund';
        break;
      default:
      break;
    }
  }

  getCustomer(data: any, type: any) {
    if (data !== null){
      const userData = JSON.parse(data);
      return userData[type];
    } else {
      return '';
    }
  }

  getNumber(num) {
    return new Array(num);
  }

  logout() {
    this.service.logout();
  }

}
