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

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

  getProvider(page_number: number) {
    this.page_number = page_number;
    this.loader = true;
    this.fname = 'all_providers';
    const post_data = {'type': 2, '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);
  }

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

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

  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/forex_edit/' + id]);
  }

  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);
  }

  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;
  }

  providerDetails(id: any) {
    this.loader = true;
    this.fname = 'providers_details';
    const post_data = {'provider_id': id};
      this.service.post_data(this.fname, post_data).subscribe(response => {
        this.loader = false;
        console.log(response);
        if (response.code === 1) {
          this.provider = response.responseResult;
          this.getProvider_name(this.provider.deli_provider_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'];
    });

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

  getProvider_name(id: number) {
    this.fname = 'provider_name';
    const post_data = {'provider_id': id};
      this.service.post_data(this.fname, post_data).subscribe(response => {
        console.log(response);
        if (response.code === 1) {
          const resData = response.responseResult;
          this.deli_provider = resData.name;
          console.log(this.deli_provider);
        } else {
        }
      }, (error) => {
    });
  }

  providerState(state: number, id: number) {
    this.loader = true;
    this.fname = 'provider_approve';
    const post_data = {'user_id': id, 'status': state};
      this.service.post_data(this.fname, post_data).subscribe(response => {
        this.loader = false;
        console.log(response);
        if (response.code === 1) {
          this.provider = response.responseResult;
          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);
  }

}
