/**
  * Show the application dashboard to the user.
  *
  * @return Response
  */
 public function map()
 {
     $current_user = User::where('id', Auth::id())->get();
     if (count($current_user) == 0) {
         $user_operation_areas = [];
         foreach (Operation_area::select()->get() as $op_area) {
             $user_operation_areas[] = $op_area->id;
         }
     } else {
         $user_operation_areas = explode(',', $current_user[0]->operation_areas);
     }
     $operation_areas = Operation_area::select()->whereIn('id', $user_operation_areas)->get();
     $emergency_cases = emergencyCase::select()->whereIn('operation_area', $user_operation_areas)->orderBy('created_at', 'desc')->get();
     $vehicles = Vehicle::select()->where('public', '=', true)->get();
     return view('pages.home_map', compact('operation_areas', 'emergency_cases', 'vehicles'));
 }
 /** store vehicles license */
 public function store_license(Request $request)
 {
     $license_types = LicenseTypeVehicle::where('vehicle_id', $request->vehicle_id)->get();
     $vehicle = Vehicle::select('plate_number', 'client_id')->findOrFail($request->vehicle_id);
     $cur_date = date("Y-m-d H:i:s");
     foreach ($license_types as $license_type) {
         $license_type_id = $license_type->license_type_id;
         if (isset($request->{'status_' . $license_type_id})) {
             $save = LicenseRenewalRequest::where('license_type_id', '=', $license_type->license_type_id)->where('vehicle_id', '=', $request->vehicle_id)->where('status', '!=', $request->{'status_' . $license_type_id})->update(['status' => $request->{'status_' . $license_type_id}]);
             //update renewal request where there is chnage in the status.
             if ($save) {
                 // create notification.
                 switch ($request->{'status_' . $license_type_id}) {
                     case 'pending':
                         $status = "The renewal request for the vehicle {$vehicle->plate_number} status has marked pending";
                         $type = "renewal_request";
                         break;
                     case 'in-progress':
                         $status = "Your renewal request for the vehicle {$vehicle->plate_number} is in progress";
                         $type = "renewal_request";
                         break;
                     case 'completed':
                         $status = "Your renewal request for the vehicle {$vehicle->plate_number} has completed";
                         $type = "renewal_completed";
                         break;
                 }
                 $input_data = array('sent_to' => $vehicle->client_id, 'sent_on' => $cur_date, 'message' => $status, 'type' => $type);
                 LicenseNotification::create($input_data);
             }
             //
         }
         global $formated_start_date;
         $start_date = $request->{'license_type_start_' . $license_type->license_type_id};
         $formated_start_date = date('Y-m-d', strtotime($start_date));
         $formated_end_date = date('Y-m-d', strtotime('+365 days', strtotime($start_date)));
         $saved_license = LicenseTypeVehicle::where('vehicle_id', $request->vehicle_id)->where('license_type_id', $license_type_id)->where(function ($query) {
             global $formated_start_date;
             $query->where('license_start_on', '=', $formated_start_date)->orWhere('license_start_on', '=', '0000-00-00 00:00:00');
         })->get()->toArray();
         // check the form license date with the db license date
         if (empty($saved_license)) {
             // license date has changed
             LicenseTypeVehicle::where('id', $license_type->id)->update(['deleted_at' => date("Y-m-d H:i:s")]);
             $insert_data = array('license_type_id' => $license_type->license_type_id, 'vehicle_id' => $request->vehicle_id, 'license_start_on' => $formated_start_date, 'license_end_on' => $formated_end_date);
             LicenseTypeVehicle::create($insert_data);
             //soft delete the previous license date
             //insert the new date
         } else {
             LicenseTypeVehicle::where('id', $license_type->id)->update(['license_start_on' => $formated_start_date, 'license_end_on' => $formated_end_date]);
         }
         /* $start_date = $request->{'license_type_start_'.$license_type->license_type_id};
               $formated_start_date = date('Y-m-d', strtotime($start_date));
               $formated_end_date = date('Y-m-d', strtotime('+365 days',strtotime($start_date)));
               LicenseTypeVehicle::where('id', $license_type->id)
               ->update(['license_start_on' => $formated_start_date, 'license_end_on' => $formated_end_date, 'updated_at' => date("Y-m-d H:i:s")]);
            */
     }
     Session::flash('flash_message', trans('vehicle.vehicle_on_update'));
     return Redirect::to($request->redirect_to);
 }