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-list-driver',
  templateUrl: './list-driver.component.html',
  styleUrls: ['./list-driver.component.scss']
})
export class ListDriverComponent implements OnInit {
  providerList: any[];
  fname: any;
  lang =  'en';
  responseMsg: any;
  error: boolean;
  success: boolean;
  loader: boolean;
  driverList: any[];
  loginDetails: 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'));
  }

  ngOnInit() {
    this.getProvider();
  }

  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.providerDriver(this.userDetails.id);
      } 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'];
  });
  }

  providerDriver(provider_id: any) {
    this.loader = true;
    this.fname = 'get_drivers';
    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.providerList = response.responseResult;
        } else {
          this.providerList = [];
        }
      }, (error) => {
        // this.error = true;
        // this.responseMsg =  this.vs.errorCode[this.lang]['ER08'];
    });
    const This = this;
    setTimeout(function() {
      This.responseMsg = '';
    }, 3000);
}

  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();
          } 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(['delivery/driveredit/' + id]);
  }

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

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

}
