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-manageprovider',
  templateUrl: './manageprovider.component.html',
  styleUrls: ['./manageprovider.component.scss']
})
export class ManageproviderComponent implements OnInit {
  providerList: any[];
  fname: any;
  lang =  'en';
  responseMsg: any;
  error: boolean;
  success: boolean;
  loader: boolean;
  page_number: any;
  meta: any;
  searchtxt: 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.page_number = 1;
    this.searchtxt = '';
  }

  ngOnInit() {
    this.getProvider(1);
  }

  getProvider(page_number: number) {
    console.log(page_number);
    this.page_number = page_number;
    this.loader = true;
    this.fname = 'all_providers';
    const post_data = {'type': 3, 'page': page_number, 'searchtxt': this.searchtxt};
      this.service.post_data(this.fname, post_data).subscribe(response => {
        this.loader = false;
        console.log(response);
        if (response.code === 1) {
          this.providerList = response.responseResult.data;
          this.meta = response.responseResult.meta;
        } else {
          if (response.errorCode === 'ER36') {
            this.page_number = this.page_number - 1;
            if (this.page_number >= 1) {
              this.getProvider(this.page_number);
            } else {
              this.error = true;
            this.responseMsg = this.vs.errorCode[this.lang][response.errorCode];
            this.providerList = [];
            }
          } else {
            this.error = true;
            this.responseMsg = this.vs.errorCode[this.lang][response.errorCode];
            this.providerList = [];
          }
        }
      }, (error) => {
        this.error = true;
        this.responseMsg =  this.vs.errorCode[this.lang]['ER08'];
    });

    const This = this;
    setTimeout(function() {
      This.responseMsg = '';
    }, 3000);
  }

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

  onSearchChange(text: string) {
    this.searchtxt = text;
    this.getProvider(1);
  }

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

  reset() {
    this.error = false;
    this.success = false;
  }

  deleteClick(provider_id: any) {
    this.reset();
    const res = confirm('Do you want to remove this user ?');
    if (res === true) {
      this.loader = true;
      this.fname = 'provider_delete';
      const post_data = {'provider_id': provider_id};
        this.service.post_data(this.fname, post_data).subscribe(response => {
          this.loader = false;
          console.log(response);
          if (response.code === 1) {
            this.success = true;
            console.log(this.success);
            this.getProvider(this.page_number);
          } 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'];
      });
    }
    /* const This = this;
    setTimeout(function() {
      This.responseMsg = '';
    }, 3000); */
  }

  editClick(id: number) {
    this.router.navigate(['maker/delivery_edit/' + id]);
  }

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

  get_status(status: number) {
    let state = '';
    switch (status) {
      case 0:
        state = 'Pending';
        break;
      case 1:
        state = 'Approved';
        break;
      case 2:
        state = 'Reject';
        break;
      default:
        break;
    }
    return state;
  }

}