import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { CommonFunctionService } from 'src/app/core/common/common-function.service';
import { StaticDataService } from 'src/app/core/common/static-data.service';
import { ValidationService } from 'src/app/core/common/validation.service';
import { API } from 'src/app/core/services/api.service';
import { FlightContractService } from 'src/app/core/services/flight-contract.service.ts/flight-contract.service';

@Component({
  selector: 'app-flight-inventory-report-search',
  templateUrl: './flight-inventory-report-search.component.html',
})
export class FlightInventoryReportSearchComponent implements OnInit {

  Formdata: FormGroup;
  AirlineList;
  FlightAllReport;
  constructor(
    public ApiServ: API,
    public fb: FormBuilder,
    public router: Router,
    public _commonService: CommonFunctionService,
    public _staticService: StaticDataService,
    public _flightContractService: FlightContractService,
    public validationService: ValidationService
  ) { }

  trip_type_list = [
    {key: '', display_name:'Select'},
    {key: 'ONEWAY', display_name:'One way'},
    {key: 'ROUNDTRIP', display_name:'Round Trip'},
  ]
  cols: any[];
  ngOnInit(): void {
    this.pageSetUp()
  }
  pageSetUp() {
    this.OnInitForm();
    this.cols = [
      { field: 'validating_airline', header: 'Airline' },
      { field: 'flight_no', header: ' Flight No.' },
      { field: 'trip_type', header: ' Trip Type.' },
      { field: "origin", header: 'Origin' },
      { field: 'destination', header: 'Destination' },
      { field: 'departure_date', header: 'Date' },
      { field: 'return_date', header: 'Return Date' },
      { field: 'day', header: 'Day' },
      { field: 'rbd', header: 'RBD' },
      { field: 'fare_family_name', header: 'Fare Family Name' },
      { field: 'cabin_class', header: 'Cabin Class' },
      { field: 'seat_allocation', header: ' Seats Allocation' },
      { field: 'seat_booked', header: 'Booked Seats' },
      { field: 'seat_remains', header: 'Remaining Seats' },
      { field: 'seat_cancelled', header: 'cancelled Seats' },
      { field: 'pnr', header: 'PNR' },
      { field: 'is_stop_sale', header: 'Stop Sale' },
    ];
  };
  plusDate = new Date(new Date().setMonth(new Date().getMonth() + 1))
  OnInitForm() {
    this.Formdata = this.fb.group({
      vendor_id: [],
      validating_airline: [],
      flight_no: [],
      trip_type: [],
      origin: [],
      destination: [],
      return_date: [],
      from_date: [new Date(), Validators.required],
      to_date: [this.plusDate, Validators.required],
      rbd: [],
      cabin_class: [],
    });
    if (this._commonService.clientId == '2109111018200000021') {
      this.Formdata.get('trip_type').setValue('ONEWAY');
    }
  };
  changeFormDate() {
    let max = new Date(this.Formdata.get('to_date').value);
    this.plusDate = new Date(this.Formdata.get('from_date').value);
    if (this.plusDate > max) {
      this.Formdata.get('to_date').setValue(null)
    }
  };
  ResetForm() {
    this.Formdata.reset();
    // remove this when User want
    if (this._commonService.clientId == '2109111018200000021') {
      this.Formdata.get('trip_type').setValue('ONEWAY');
    }
    this.plusDate = new Date();
  };
  GetAllFlightInventory() {
    let value = this.Formdata.getRawValue();
    if (this.Formdata.valid) {
      let searchobject = {
        vendor_id: value.vendor_id ? value.vendor_id : '',
        validating_airline: value.validating_airline ? value.validating_airline : '',
        flight_no: value.flight_no ? value.flight_no : '',
        trip_type: value.trip_type ? value.trip_type : '',
        origin: value.origin ? value.origin : '',
        destination: value.destination ? value.destination : '',
        rbd: value.rbd ? value.rbd : '',
        cabin_class: value.cabin_class ? value.cabin_class : '',
        return_date: value.return_date ? value.return_date : '',
      };
      searchobject['from_date'] = this._commonService.convertDateToSave(value.from_date);
      searchobject['to_date'] = this._commonService.convertDateToSave(value.to_date);
      this.ApiServ.Post('/FlightContract/GetFlightInventoryReport', searchobject).subscribe(res => {
        if (res && res['data']) {
            this.FlightAllReport = res['data'];
            if (this.FlightAllReport && this.FlightAllReport.length > 0) {
                this.FlightAllReport.forEach(e => {
                    e['is_stop_sale'] = e.is_stop_sale ? 'Yes' : 'No';
                    e['departure_date'] = e.departure_date ? this._commonService.convertDateToShow(e.departure_date) : '';
                    e['return_date'] = (e.return_date === '0001-01-01T00:00:00Z' || !e.return_date)
                        ? 'NA'
                        : this._commonService.UtcToLocaleTime(e.return_date, 'DD MMM YYYY, HH:mm:ss');
                });
            }
        } else {
            this._commonService.showMessage(res['status'], res['message']);
            this.FlightAllReport = [];
        }
    });
    } else {
      this.validationService.showValidationsMsg(this.Formdata);
    }
  };

}
