import Ember from 'ember';
import $ from 'jquery';

export default Ember.Controller.extend({
  transportApiSrv: Ember.inject.service('trasport-api'),
  searchLocation: null,
  destinations: null,
  selectedDestination: null,
  timetable: null,

  setInitialState () {
    const initialStation = this.get('model').stations[0];
    this.set('searchLocation', initialStation.name);
    $('.departures-select').children().eq(1).attr('selected', 'selected');

    this.setDestinationSelectList(initialStation.station_code);
  },

  setDestinationSelectList (stationCode) {
    return this.get('transportApiSrv').getTrainSchedule({stationCode})
      .then(response => {
        this.set('destinations', response.departures.all);
        Ember.run.later(() => {
          $('select').material_select();
        }, 500);
      });
  }
});

@@ -518,16 +518,557 @@
 ;%0A  %7D,%0A%0A
+  actions: %7B%0A    destinationSelected (destination) %7B%0A      let selectedService = this.get('destinations').filterBy('destination_name', destination);%0A      this.set('timetable', selectedService);%0A    %7D,%0A    departureSelected (departureStation) %7B%0A      const $selectEl = $('.station-select');%0A%0A      $selectEl.attr('disabled', 'disabled');%0A      $('select').material_select();%0A      this.set('timetable', null);%0A      this.setDestinationSelectList(departureStation).then(() =%3E %7B%0A        $selectEl.removeAttr('disabled');%0A      %7D);%0A    %7D%0A  %7D,%0A%0A
   setDes

Set actions for select fields