/**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function index()
 {
     $headers = apache_request_headers();
     $response = array();
     if (isset($headers['api_key'])) {
         $apiKey = UserNew::where('api_key', '=', $headers['api_key'])->first();
         if (sizeof($apiKey) > 0) {
             $categories = $this->category->get();
             $subCategories = array();
             foreach ($categories as $category) {
                 $subCategories['id'] = $category['id'];
                 $subCategories['name'] = $category['name'];
                 $subCats = DepartmentSubCategory::where('category', '=', $category['id'])->get();
                 $tmpArrayAll = [];
                 foreach ($subCats as $subCat) {
                     $tmpArray['cat_id'] = $category['id'];
                     $tmpArray['id'] = $subCat['id'];
                     $tmpArray['name'] = $subCat['name'];
                     $subsubCats = DepartmentSubSubCategory::where('sub_category', '=', $subCat['id'])->get();
                     $tmpArrayAll2 = [];
                     foreach ($subsubCats as $subsubCat) {
                         $tmpA['cat_id'] = $subCat['id'];
                         $tmpA['id'] = $subsubCat['id'];
                         $tmpA['name'] = $subsubCat['name'];
                         $tmpArrayAll2[] = $tmpA;
                     }
                     $tmpArray['subs'] = $tmpArrayAll2;
                     $tmpArrayAll[] = $tmpArray;
                 }
                 $subCategories['subs'] = $tmpArrayAll;
                 $tmp[] = $subCategories;
             }
             $response['categories'] = $tmp;
             $response['error'] = FALSE;
             return \Response::json($response, 201);
         } else {
             $response['message'] = 'Access Denied. Invalid Api key';
             $response['error'] = TRUE;
             return \Response::json($response, 401);
         }
     } else {
         $response['message'] = 'Access Denied. Invalid Api key';
         $response['error'] = TRUE;
         return \Response::json($response, 401);
     }
 }
 public function store(Report $report, Request $request)
 {
     \Log::info("Request " . $request);
     $category = \Input::get('category');
     \Log::info('GET Category ' . $category);
     $sub_category = \Input::get('sub_category');
     \Log::info('GET Sub Category ' . $sub_category);
     $sub_sub_category = \Input::get('sub_sub_category');
     \Log::info('GET Sub Sub Category ' . $sub_sub_category);
     $sub_sub_category = empty($sub_sub_category) ? " " : $sub_sub_category;
     $description = \Input::get('description');
     \Log::info('Get Description :' . $description);
     $description = empty($description) ? " " : $description;
     $gps_lat = \Input::get('gps_lat');
     \Log::info('GPS Lat :' . $gps_lat);
     $gps_lng = \Input::get('gps_lng');
     \Log::info('GPS Lng :' . $gps_lng);
     $user_email = \Input::get('user_email');
     \Log::info('Email :' . $user_email);
     $priority = \Input::get('priorities');
     $priority = empty($priority) ? "Normal" : $priority;
     \Log::info('Priority :' . $priority);
     $headers = apache_request_headers();
     $response = array();
     \Log::info("Request " . $request);
     if (count($_FILES) > 0) {
         $files = $_FILES['img'];
         $name = uniqid('img-' . date('Ymd') . '-');
         $temp = explode(".", $files['name']);
         $name = $name . '.' . end($temp);
         if (file_exists("uploads/" . $name)) {
             echo $_FILES["img"]["name"] . "already exists. ";
         } else {
             $img_url = "uploads/" . $name;
             $target_file = "uploads/{$name}";
             $resized_file = "uploads/{$name}";
             $wmax = 600;
             $hmax = 480;
             $fileExt = 'jpg';
             if (move_uploaded_file($_FILES["img"]["tmp_name"], $img_url)) {
                 $this->ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
             }
         }
     }
     $img_url = isset($img_url) ? $img_url : "uploads/noimage.png";
     if (isset($headers['api_key'])) {
         $userNew = UserNew::where('api_key', '=', $headers['api_key_new'])->first();
         if (sizeof($userNew) > 0) {
             $objCat = DepartmentCategory::where('name', '=', $category)->first();
             \Log::info('Category Object :' . $objCat);
             $department = Department::where('id', '=', $objCat->department)->first();
             \Log::info('Department Object : ' . $department);
             $objSubCat = DepartmentSubCategory::where('name', '=', $sub_category)->first();
             $SubCatName = sizeof($objSubCat) > 0 ? $objSubCat->name : "";
             if (strlen($sub_sub_category) > 1) {
                 $objSubSubCat = DepartmentSubSubCategory::where('name', '=', $sub_sub_category)->first();
                 $objSubSub = $objSubSubCat->id;
             } else {
                 $objSubSubCat = 0;
                 $objSubSub = 0;
             }
             $case = new CaseReport();
             $case->description = $description;
             $case->user = $userNew->id;
             $case->reporter = $userNew->id;
             $case->department = $department->id;
             $case->category = $objCat->id;
             $case->sub_category = $objSubCat->id;
             $case->sub_sub_category = $objSubSub;
             $case->priority = $priority;
             $case->status = 'Pending';
             $case->gps_lat = $gps_lat;
             $case->precinct = 5;
             $case->img_url = $img_url;
             $case->gps_lng = $gps_lng;
             $case->save();
             $caseOwner = new CaseOwner();
             $caseOwner->user = $userNew->id;
             $caseOwner->caseId = $case->id;
             $caseOwner->type = 0;
             $caseOwner->active = 1;
             $caseOwner->save();
             $response["message"] = "Report created successfully";
             $response['error'] = FALSE;
             $data = array('name' => $userNew->name, 'caseID' => $case->id, 'caseDesc' => $case->description);
             \Mail::send('emails.sms', $data, function ($message) use($userNew) {
                 $message->from('*****@*****.**', 'Siyaleader');
                 $message->to($userNew->username)->subject("Siyaleader Notification - New Case Reported:");
             });
             if (is_object($objSubSubCat)) {
                 $firstRespondersObj = CaseResponder::where("sub_sub_category", '=', $objSubSubCat->id)->select('firstResponder')->first();
                 /*$secondRespondersObj = CaseResponder::where("sub_sub_category",'=',$objSubSubCat->id)
                                                                 ->select('secondResponder')->first();
                 
                                     $thirdRespondersObj  = CaseResponder::where("sub_sub_category",'=',$objSubSubCat->id)
                                                                 ->select('thirdResponder')->first();
                                     */
                 if (sizeof($firstRespondersObj) > 0) {
                     $case->status = "Referred";
                     $case->referred_at = \Carbon\Carbon::now('Africa/Johannesburg')->toDateTimeString();
                     $case->save();
                     $firstResponders = explode(",", $firstRespondersObj->firstResponder);
                     if ($firstRespondersObj->firstResponder > 0) {
                         foreach ($firstResponders as $firstResponder) {
                             $firstResponderUser = UserNew::find($firstResponder);
                             $caseOwner = new CaseOwner();
                             $caseOwner->user = $firstResponder;
                             $caseOwner->caseId = $case->id;
                             $caseOwner->type = 1;
                             $caseOwner->active = 1;
                             $caseOwner->save();
                             $data = array('name' => $firstResponderUser->name, 'caseID' => $case->id, 'caseDesc' => $case->description, 'caseReporter' => $case->description);
                             \Mail::send('emails.responder', $data, function ($message) use($firstResponderUser) {
                                 $message->from('*****@*****.**', 'Siyaleader');
                                 $message->to($firstResponderUser->username)->subject("Siyaleader Notification - New Case Reported:");
                             });
                             $cellphone = $firstResponderUser->email;
                             \Mail::send('emails.caseEscalatedSMS', $data, function ($message) use($cellphone) {
                                 $message->from('*****@*****.**', 'Siyaleader');
                                 $message->to('*****@*****.**')->subject("REFER: {$cellphone}");
                             });
                         }
                     }
                 }
                 /*     if (sizeof($secondRespondersObj) > 0) {
                 
                                         $secondResponders  = explode(",",$secondRespondersObj->secondResponder);
                 
                                         if($secondRespondersObj->secondResponder > 0) {
                 
                                             foreach ($secondResponders as $secondResponder) {
                 
                 
                                                 $secondResponderUser = UserNew::find($secondResponder);
                                                 $caseOwner          = new CaseOwner();
                                                 $caseOwner->user    = $secondResponder ;
                                                 $caseOwner->caseId  = $case->id;
                                                 $caseOwner->type    = 2;
                                                 $caseOwner->active  = 1;
                                                 $caseOwner->save();
                 
                                                  $data = array(
                                                         'name'   =>$secondResponderUser->name,
                                                         'caseID' =>$case->id,
                                                         'caseDesc' => $case->description,
                                                         'caseReporter' => $case->description,
                                                     );
                 
                                                 \Mail::send('emails.responder',$data, function($message) use ($secondResponderUser)
                                                 {
                                                     $message->from('*****@*****.**', 'Siyaleader');
                                                     $message->to($secondResponderUser->username)->subject("Siyaleader Notification - New Case Reported:");
                 
                                                 });
                 
                                             }
                 
                 
                 
                                         }
                                     }
                 */
                 /*        if (sizeof($thirdRespondersObj) > 0) {
                 
                                         $thirdResponders  = explode(",",$thirdRespondersObj->thirdResponder);
                 
                                         if($thirdRespondersObj->thirdResponder > 0) {
                 
                                             foreach ($thirdResponders as $thirdResponder) {
                 
                 
                                                 $thirdResponderUser = UserNew::find($thirdResponder);
                                                 $caseOwner          = new CaseOwner();
                                                 $caseOwner->user    = $thirdResponder ;
                                                 $caseOwner->caseId  = $case->id;
                                                 $caseOwner->type    = 3;
                                                 $caseOwner->active  = 1;
                                                 $caseOwner->save();
                 
                                                  $data = array(
                                                         'name'   =>$thirdResponderUser->name,
                                                         'caseID' =>$case->id,
                                                         'caseDesc' => $case->description,
                                                         'caseReporter' => $case->description,
                                                     );
                 
                                                 \Mail::send('emails.responder',$data, function($message) use ($thirdResponderUser)
                                                 {
                                                     $message->from('*****@*****.**', 'Siyaleader');
                                                     $message->to($thirdResponderUser->username)->subject("Siyaleader Notification - New Case Reported:");
                 
                                                 });
                 
                                             }
                 
                 
                 
                                         }
                                     }*/
             }
             if (sizeof($objSubCat) > 0 && $objSubSubCat == "") {
                 $firstRespondersObj = CaseResponder::where("sub_category", '=', $objSubCat->id)->select('firstResponder')->first();
                 /*  $secondRespondersObj = CaseResponder::where("sub_category",'=',$objSubCat->id)
                                                                 ->select('secondResponder')->first();
                 
                                     $thirdRespondersObj  = CaseResponder::where("sub_category",'=',$objSubCat->id)
                                                                 ->select('thirdResponder')->first();
                 
                                 */
                 if (sizeof($firstRespondersObj) > 0) {
                     $case->status = "Referred";
                     $case->referred_at = \Carbon\Carbon::now('Africa/Johannesburg')->toDateTimeString();
                     $case->save();
                     $firstResponders = explode(",", $firstRespondersObj->firstResponder);
                     if ($firstRespondersObj->firstResponder > 0) {
                         foreach ($firstResponders as $firstResponder) {
                             $firstResponderUser = UserNew::find($firstResponder);
                             $caseOwner = new CaseOwner();
                             $caseOwner->user = $firstResponder;
                             $caseOwner->caseId = $case->id;
                             $caseOwner->type = 1;
                             $caseOwner->active = 1;
                             $caseOwner->save();
                             $data = array('name' => $firstResponderUser->name, 'caseID' => $case->id, 'caseDesc' => $case->description, 'caseReporter' => $case->description);
                             \Mail::send('emails.responder', $data, function ($message) use($firstResponderUser) {
                                 $message->from('*****@*****.**', 'Siyaleader');
                                 $message->to($firstResponderUser->username)->subject("Siyaleader Notification - New Case Reported:");
                             });
                             $cellphone = $firstResponderUser->cellphone;
                             \Mail::send('emails.caseEscalatedSMS', $data, function ($message) use($cellphone) {
                                 $message->from('*****@*****.**', 'Siyaleader');
                                 $message->to('*****@*****.**')->subject("REFER: {$cellphone}");
                             });
                         }
                     }
                 }
                 /*     if (sizeof($secondRespondersObj) > 0) {
                 
                                         $secondResponders  = explode(",",$secondRespondersObj->secondResponder);
                 
                                         if($secondRespondersObj->secondResponder > 0) {
                 
                                             foreach ($secondResponders as $secondResponder) {
                 
                 
                                                 $secondResponderUser = UserNew::find($secondResponder);
                                                 $caseOwner          = new CaseOwner();
                                                 $caseOwner->user    = $secondResponder ;
                                                 $caseOwner->caseId  = $case->id;
                                                 $caseOwner->type    = 2;
                                                 $caseOwner->active  = 1;
                                                 $caseOwner->save();
                 
                                                  $data = array(
                                                         'name'   =>$secondResponderUser->name,
                                                         'caseID' =>$case->id,
                                                         'caseDesc' => $case->description,
                                                         'caseReporter' => $case->description,
                                                     );
                 
                                                 \Mail::send('emails.responder',$data, function($message) use ($secondResponderUser)
                                                 {
                                                     $message->from('*****@*****.**', 'Siyaleader');
                                                     $message->to($secondResponderUser->username)->subject("Siyaleader Notification - New Case Reported:");
                 
                                                 });
                 
                                             }
                 
                 
                 
                                         }
                                     }*/
                 /*if (sizeof($thirdRespondersObj) > 0) {
                 
                                         $thirdResponders  = explode(",",$thirdRespondersObj->thirdResponder);
                 
                                         if($thirdRespondersObj->thirdResponder > 0) {
                 
                                             foreach ($thirdResponders as $thirdResponder) {
                 
                 
                                                 $thirdResponderUser = UserNew::find($thirdResponder);
                                                 $caseOwner          = new CaseOwner();
                                                 $caseOwner->user    = $thirdResponder ;
                                                 $caseOwner->caseId  = $case->id;
                                                 $caseOwner->type    = 3;
                                                 $caseOwner->active  = 1;
                                                 $caseOwner->save();
                 
                                                  $data = array(
                                                         'name'   =>$thirdResponderUser->name,
                                                         'caseID' =>$case->id,
                                                         'caseDesc' => $case->description,
                                                         'caseReporter' => $case->description,
                                                     );
                 
                                                 \Mail::send('emails.responder',$data, function($message) use ($thirdResponderUser)
                                                 {
                                                     $message->from('*****@*****.**', 'Siyaleader');
                                                     $message->to($thirdResponderUser->username)->subject("Siyaleader Notification - New Case Reported:");
                 
                                                 });
                 
                                             }
                 
                 
                 
                                         }
                                     }*/
             }
             return \Response::json($response, 201);
         } else {
             $response['message'] = 'Access Denied. Invalid Api key';
             $response['error'] = TRUE;
             return \Response::json($response, 401);
         }
     } else {
         $response['message'] = 'Access Denied. Invalid Api key';
         $response['error'] = TRUE;
         return \Response::json($response, 401);
     }
 }