import { Component, Inject } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { ActivatedRoute } from '@angular/router';
import { MessageService } from 'primeng/api';
import { ContractDetailDto } from 'src/app/core/models/flight-service/flight-service-manage/edit-contract-details.model';
import { API } from 'src/app/core/services/api.service';

@Component({
  templateUrl: './edit-contract-details.html',
})
export class EditContractDetailsComponents {

  Formdata: FormGroup;
  constructor(@Inject(MAT_DIALOG_DATA) public data: any, public ApiServ: API,
    public Fb: FormBuilder, public route: ActivatedRoute, public MsgService: MessageService) { }
  AircabinList;
  AirpaxList;
  cabin_type = [];
  pax_type = [];
  ngOnInit(): void {
    this.Formdata = this.Fb.group({
      contract_name: [''],
      contract_description: [''],
      contracted_cabins: [null],
      contracted_pax_types: [null]
    });

    this.GetLOVHotelPickupPointType();
  }


  GetLOVHotelPickupPointType() {
    let formdata = {
      "lov_types": [
        { "lov_type": "AirCabinClass" },
        { "lov_type": "AirPaxType" },
      ],
      "lov_lang": "en"
    }
    this.ApiServ.Post("/LovMaster/GetLov", formdata).subscribe(data => {
      if (data) {
        this.AircabinList = data.responseObject[0].keys;
        this.AirpaxList = data.responseObject[1].keys;
        this.ApiServ.Get('/FlightContract/GetFlightContract?ContractId=' + this.data.contractId).subscribe(data => {
          console.log(data)
          this.SetFormValue(data.responseObject)
        });
      }
    });
  };




SetFormValue(data) {
  this.cabin_type = []
  this.pax_type = []
  Object.keys(data).forEach(e => {
    if (e == 'contract_name' || e == 'contract_description') {
      this.Formdata.get(e).setValue(data[e])
    } else if (e == 'is_all_pax_type' || e == 'is_premium_economy' || e == 'is_economy' || e == 'is_business' || e == 'is_first_class') {
      let arr = []

      'is_economy' == e && data[e] ? this.cabin_type.push(1) : ''
      'is_premium_economy' == e && data[e] ? this.cabin_type.push(2) : ''
      'is_business' == e && data[e] ? this.cabin_type.push(3) : ''
      'is_first_class' == e && data[e] ? this.cabin_type.push(4) : ''
      this.Formdata.get('contracted_cabins').setValue(this.cabin_type);
    } else if (e == 'is_adult' || e == 'is_child' || e == 'is_infant') {
      e == 'is_adult' && data[e] ? this.pax_type.push(1) : ''
      e == 'is_child' && data[e] ? this.pax_type.push(2) : ''
      e == 'is_infant' && data[e] ? this.pax_type.push(3) : ''
      this.Formdata.get('contracted_pax_types').setValue(this.pax_type)
    }
  });
};
saveformData() {
  let value = this.Formdata.getRawValue();
  let postdata = new ContractDetailDto();
  postdata.contract_name = value.contract_name;
  postdata.contract_description = value.contract_description;
  postdata.contract_id = this.data.contractId;
  postdata.is_economy = false;
  postdata.is_premium_economy = false;
  postdata.is_business = false;
  postdata.is_first_class = false;
  value.contracted_cabins ? value.contracted_cabins.map(e => {
    e == 1 ? postdata.is_economy = true : '';
    e == 2 ? postdata.is_premium_economy = true : '';
    e == 3 ? postdata.is_business = true : '';
    e == 4 ? postdata.is_first_class = true : '';
  }) : ''
  postdata.is_adult = false;
  postdata.is_child = false;
  postdata.is_infant = false
  value.contracted_pax_types ? value.contracted_pax_types.map(e => {
    e == 1 ? postdata.is_adult = true : '';
    e == 2 ? postdata.is_child = true : '';
    e == 3 ? postdata.is_infant = true : '';
  }) : ''
  postdata.is_all_pax_type = postdata.is_adult && postdata.is_child && postdata.is_infant ? true : false;

  this.ApiServ.Post("/FlightContract/UpdateContractDetails", postdata).subscribe(data => {
    if (data) {
      this.MsgService.add({
        severity: data["status"] ? "success" : "error",
        detail: data["message"],
      });
    }
  });
}


  
}

