/**
  * Show the form for creating a new resource.
  *
  * @return Response
  */
 public function populateRoutes()
 {
     $inputs = ['origin' => rawurlencode(Input::get('origin')), 'destination' => rawurlencode(Input::get('destination')), 'pax' => Input::get('pax')];
     $airports = [];
     $user_id = Input::get('user_id');
     $contributor = User::find($user_id);
     $now = Carbon::now();
     if ($contributor == null) {
         return response()->json('user not found.', 404);
     }
     $data = Rome2Rio::call($inputs['origin'], $inputs['destination']);
     if (isset($data->airports)) {
         foreach ($data->airports as $airport) {
             $airports[$airport->code] = $airport->pos;
         }
     }
     foreach ($data->routes as $route) {
         $iterinary = new Iterinary();
         $iterinary->origin = $data->places[0]->name;
         $iterinary->destination = $data->places[1]->name;
         $iterinary->creator_id = $contributor->id;
         $iterinary->duration = $route->duration;
         $iterinary->distance = $route->distance;
         $iterinary->price = Rome2Rio::getRome2RioPrice($route);
         $new_route = new Route();
         $new_route->name = $route->name;
         $new_route->distance = $route->distance;
         $new_route->duration = $route->duration;
         $new_route->price = Rome2Rio::getRome2RioPrice($route);
         $new_route->save();
         $iterinary->route()->associate($new_route);
         $iterinary->save();
         $contributor->iterinaries()->attach($iterinary->id, ['date_start' => $now]);
         $i = 1;
         foreach ($route->segments as $segment) {
             $new_segment = new Segment();
             if ($segment->kind == "flight") {
                 $segment = Rome2Rio::convertToFlightSegment($segment, $data);
             }
             $new_segment->mode = !isset($segment->subkind) ? $segment->kind : $segment->subkind;
             $new_segment->sequence = $i;
             $new_segment->origin_name = !isset($segment->sName) ? "" : $segment->sName;
             $new_segment->destination_name = !isset($segment->tName) ? "" : $segment->tName;
             $new_segment->origin_pos = $segment->sPos;
             $new_segment->destination_pos = $segment->tPos;
             $new_segment->price = Rome2Rio::getRome2RioPrice($segment);
             $new_segment->path = $segment->kind == "flight" ? Rome2Rio::getFlightPath($airports[$segment->sCode], $airports[$segment->tCode]) : $segment->path;
             $new_segment->distance = $segment->distance;
             $new_segment->duration = $segment->duration;
             $new_route->segments()->save($new_segment);
             $activity = new Activity();
             $activity->iterinary_id = $iterinary->id;
             $activity->day = 1;
             $activity->start_time = Carbon::now()->toTimeString();
             $activity->end_time = Carbon::now()->addMinute($segment->duration)->toTimeString();
             $new_segment->activity()->save($activity);
             $i++;
         }
         unset($i);
         // unset index for segments sequence
     }
     //dd($data);
 }
 /**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function get_recommend($origin = "cebu", $destination = "paris")
 {
     // $data = \Input::all();
     /**
      * $origin and $destination pwede ra e static
      * tang tanga lang ang parameter na $request
      */
     // $origin = \Input::get('origin');
     // $destination = \Input::get('destination');
     // $origin = "cebu";
     // $destination = "manila";
     $origin = urlencode($origin);
     $destination = urlencode($destination);
     /**
      * $url = API url
      * kani ray ilisi earl
      */
     $url = "http://free.rome2rio.com/api/1.2/json/Search?key=nKYL3BZS&oName=" . $origin . "&dName=" . $destination;
     $ch = curl_init($url);
     curl_setopt($ch, CURLOPT_TIMEOUT, 5);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $data = curl_exec($ch);
     $data = json_decode($data);
     // dd($data);
     $airports = [];
     if (isset($data->airports)) {
         foreach ($data->airports as $airport) {
             $airports[$airport->code] = $airport->pos;
         }
     }
     // dd($data);
     // start debug
     $routes = \Rome2RioData::getRoutes($data, 0);
     // dd($routes);
     $segments = \Rome2RioData::getSegments($routes);
     foreach ($segments as $value) {
         if ($value->kind == "flight") {
             $value = Rome2Rio::convertToFlightSegment($value, $data);
             $flightpath = Rome2Rio::getFlightPath($airports[$value->sCode], $airports[$value->tCode]);
             $value->{"path"} = $flightpath;
         }
     }
     foreach ($segments as &$segment) {
         $segment->mode = $segment->kind;
         unset($segment->kind);
         $segment->origin_name = $segment->sName;
         unset($segment->sName);
         $segment->destination_name = $segment->tName;
         unset($segment->tName);
         $segment->origin_pos = $segment->sPos;
         unset($segment->sPos);
         $segment->destination_pos = $segment->tPos;
         unset($segment->tPos);
     }
     curl_close($ch);
     return $segments;
     // dd($segments);
     // dd($segments);
     //end debug
     // dd($data,$ch);
     // return response()->json([$request->all(),$data],'200');
 }