import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { WebService } from './../../providers/web.service';
import { ValidationService } from './../../providers/validation.service';
import { FormControl, FormGroup, FormBuilder, Validators, ValidationErrors } from '@angular/forms';
import { ImageStorage } from '../../../environments/server.config';

@Component({
  selector: 'app-service',
  templateUrl: './service.component.html',
  styleUrls: ['./service.component.scss']
})
export class ServiceComponent implements OnInit {
  configForm: FormGroup;
  numeric = '^[0-9]*$';
  configSubmit: boolean;
  lang =  'en';
  error_msg: any[];
  funcName: any;
  loader: boolean;
  responseError: any;
  error: boolean;
  success: boolean;
  file: File;
  config: any;
  image_url: any;
  loginDetails: any;
  userDetails: any;

  constructor(private router: Router,
    private route: ActivatedRoute,
    public vs: ValidationService,
    public service: WebService,
    private formBuilder: FormBuilder
    ) {
      this.loader = false;
      this.error = false;
      this.success = false;
      this.configSubmit = false;
      this.error_msg = this.vs.errorList[this.lang];
      this.loginDetails = JSON.parse(this.service.getLocalStorageItem('userData'));
    }

    ngOnInit() {
      this.configForm = this.formBuilder.group({
        'tax':  ['', Validators.compose([Validators.required, Validators.pattern('^[0-9]*$')])],
        'hand':  ['', Validators.compose([Validators.required, Validators.pattern('^[0-9]*$')])],
        'provider_id':  [''],
        'airport':  ['', Validators.compose([Validators.required, Validators.pattern('^[0-9]*$')])],
        'cancel_time':  ['', Validators.compose([Validators.required, Validators.pattern('^[0-9]*$')])],
        'waiting_time': ['', Validators.compose([Validators.required, Validators.pattern('^[0-9]*$')])],
        'type': [0]
      });
      this.getProvider();
    }

    getProvider() {
        this.funcName = 'get_user_details';
        const post_data = {'user_id': this.loginDetails.user_id};
        this.service.post_data(this.funcName, post_data).subscribe(response => {
          if (response.code === 1) {
            this.userDetails = response.responseResult;
            this.getConfig(this.userDetails.id);
            this.configForm.patchValue({'provider_id': this.userDetails.id});
            // this.providerDriver(this.userDetails.id);
          } else {
            this.error = true;
            this.responseError = this.vs.errorCode[this.lang][response.errorCode];
          }
        }, (error) => {
          this.error = true;
          this.responseError =  this.vs.errorCode[this.lang]['ER08'];
      });
    }

  configProcess() {
      const This = this;
      this.error = false;
      this.success = false;
      console.log(this.configForm.value);
      this.configSubmit = true;
      if (this.configForm.valid) {
        this.loader = true;
        const post_data = this.configForm.value;
        this.funcName = 'config_save';
        this.service.post_data(this.funcName, post_data).subscribe(response => {
          this.loader = false;
          console.log(response);
          if (response.code === 1) {
            this.success = true;
            this.configSubmit = false;
            this.configForm.patchValue({'type': 0});
          } else {
            this.error = true;
            this.responseError = this.vs.errorCode[this.lang][response.errorCode];
          }
        }, (error) => {
          this.error = false;
          this.responseError = this.vs.errorCode[this.lang]['ER08'];
        });
      }
      console.log('tested');
    }


    getConfig(id) {
      this.loader = true;
      this.funcName = 'config_details';
      const post_data = {'provider_id': id};
      this.service.post_data(this.funcName, post_data).subscribe(response => {
        this.loader = false;
        console.log(response);
        if (response.code === 1) {
          this.config = response.responseResult;
          this.configForm.patchValue({
            tax: this.config.tax,
            hand: this.config.hand,
            airport: this.config.airport,
            cancel_time: this.config.cancel_time,
            waiting_time: this.config.waiting_time
          });
        } else {
          // this.error = true;
          this.configForm.patchValue({'type': 1});
          // this.responseError = this.vs.errorCode[this.lang][response.errorCode];
        }
      }, (error) => {
        this.error = true;
        this.responseError =  this.vs.errorCode[this.lang]['ER08'];
    });

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

    getImage() {
      return ImageStorage + this.image_url;
    }

    cancelClick() {
      this.router.navigate(['/forex']);
    }

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

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

}
