import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { NgxSpinnerService } from 'ngx-spinner';
import { ToastrService } from 'ngx-toastr';
import { Cities } from '../../entities/cities';
import { CommonService } from '../../shared/services/common/common.service';
import { GlobalService } from '../../shared/services/common/global.service';

@Component({
  selector: 'app-refer-partner',
  templateUrl: './refer-partner.component.html',
  styleUrls: ['./refer-partner.component.scss']
})
export class ReferPartnerComponent implements OnInit {
  public dsaSignUpForm: FormGroup;
  public city: string = '';
  public jobList: any[] = ["Full Time", "Part Time"];
  public cities: Cities[] = new Array<Cities>();
  public selectedCity: Cities[]  = [];

  public referRole: string = '';
  public referCode: string = '';
  public referedRoleId: number = 0;

  constructor(
    private formBuilder: FormBuilder,
    private commonService: CommonService,
    private globalService: GlobalService,
    private toastrService: ToastrService,
    private route: ActivatedRoute,
    private spinnerService: NgxSpinnerService,
    private router: Router
  ) {
    this.dsaSignUpForm = formBuilder.group({
      'fullName': [null, [Validators.required]],
      'contactNo': [null, [Validators.required, Validators.minLength(10)]],
      'cityId': [null, [Validators.required]],
      'commitment': [null, [Validators.required]],
      'pincode': [null, [Validators.required, Validators.maxLength(6), Validators.minLength(6)]],
      'gender': [null, [Validators.required]],
      'panCardNo': [null, [Validators.required, Validators.pattern('[A-Z]{5}[0-9]{4}[A-Z]{1}')]],
      'aadhaarCardNo': [null, [Validators.required, Validators.pattern('^[2-9]{1}[0-9]{3}[0-9]{4}[0-9]{4}$')]],
      'jobType': [null, [Validators.required]],
      'addressLine1': [null, [Validators.required]],
      'addressLine2': [null],
      'district': [null],
      'state': [null],
    })
    debugger;
    this.referRole = this.route.snapshot.params['role']
    this.referCode = this.route.snapshot.params['code']
    this.referedRoleId = this.route.snapshot.params['roleId'] ? parseInt(this.route.snapshot.params['roleId']) : (this.referRole === "employee" ? 5 : 6)
  }

  ngOnInit(): void {
  }

  public bindState() {
    this.dsaSignUpForm.value.cityId;
    this.dsaSignUpForm.controls['district'].setValue(this.dsaSignUpForm.value.cityId.districtName)
    this.dsaSignUpForm.controls['state'].setValue(this.dsaSignUpForm.value.cityId.stateName)
  }

  public async getCityByPincode(event: KeyboardEvent) {
    this.cities =[];
    const value = (event.target as HTMLInputElement)!.value;;
    if (value && value.length == 6) {
      try {
        let res = await this.globalService.getCityByPincode(value);
        if (res && res.recordList.length > 0) {
          this.cities = res.recordList;
          this.selectedCity = [...this.cities]
        }
      } catch (error: any) {
        let err = '';
        if (error && error.error.message) {
          err = error.error.message
        }
        else if (error && error.message) {
          err = error.message
        }
        this.toastrService.error(error);
      }

    }
  }

  public async insertPartnerDetail() {
    try {
      this.spinnerService.show();
      //this.isShow = true;
      let dsaObj = {
        fullName: this.dsaSignUpForm.value.fullName,
        panCardNo: this.dsaSignUpForm.value.panCardNo,
        cityId: this.dsaSignUpForm.value.cityId.id,
        district: this.dsaSignUpForm.value.district,
        state: this.dsaSignUpForm.value.state,
        addressLine1: this.dsaSignUpForm.value.addressLine1,
        addressLine2: this.dsaSignUpForm.value.addressLine2,
        gender: this.dsaSignUpForm.value.gender,
        contactNo: this.dsaSignUpForm.value.contactNo,
        aadharCardNo: this.dsaSignUpForm.value.aadhaarCardNo,
        jobType: this.dsaSignUpForm.value.jobType,
        businessCommitement: this.dsaSignUpForm.value.commitment,
        pincode: this.dsaSignUpForm.value.pincode,
        roleId: this.referedRoleId,
        referCode: this.referCode,
        referRole: this.referRole,
      }

      let res = await this.globalService.referPartner(dsaObj);
      if (res && res.status == 200) {
        this.commonService.showSucessSignUp();
        this.sucessReponse();
        //this.showTab = 0;

      }
      this.spinnerService.hide();
    } catch (error: any) {
      this.spinnerService.hide();
      let err = error;
      if (error && error.error && error.error.message) {
        err = error.error.message
      }
      this.toastrService.error(err, "Error");
    }
  }

  public sucessReponse() {
    this.dsaSignUpForm.reset();
    this.router.navigate(["/"])
    // this.dsaSignUpForm.controls['aadharcardPath'].setValue("No File Chosen");
    // this.dsaSignUpForm.controls['pancardPath'].setValue("No File Chosen");
    // this.dsaSignUpForm.controls['photoPath'].setValue("No File Chosen");
    // this.dsaSignUpForm.controls['haveOffice'].setValue("true");
  }

}
