import {observable, action, computed} from 'mobx';

export default class Airports {

  @observable lists = [];
  @observable searchResults = [];
  @observable isLoading = false;
  @observable selectedDepartureAirport = "";
  @observable selectedArrivalAirport = "";

  @computed
  get listForSearch() {
    return this.lists;
  }

  constructor(context) {
    this.http = context.http;
    this.context = context;
  }

  @action
  setSelectedAirport(item, type) {
    console.log(item, type)
    if (type == 'departure') {
      this.selectedDepartureAirport = item;
    } else {
      this.selectedArrivalAirport = item;
    }
  }

  @action
  getAllAirports() {
    this.isLoading = true;
    return this.http.get(`master/airports`).then(res => {
      this.lists = res;
      this.isLoading = false;
    });
  }

  @action
  searchAirports(name) {
    this.isLoading = true;
    return this.http.get(`master/airports?search=${name}`)
      .then(res => {
        this.isLoading = false;
        this.lists = res;
      })
      .catch(err => {
        this.isLoading = false;
        throw err;
      });
  }
}
