/**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function index(Request $request)
 {
     $today = date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s")) + 60 * 60 * 6.5);
     /*$validator = Validator::make($request->all(), [
                 'user_id'  => 'required|exists:competition_group_users,user_id',
             ]);
     
             if ($validator->fails()) {
                 if($validator->errors()->has('user_id'))
                     return response()->json("You are not TLG member.", 400);
             }*/
     $competition_question = CompetitionQuestion::where('start_date', '<=', $today)->where('end_date', '>=', $today)->orderBy('id', 'desc')->first();
     if ($competition_question) {
         $check = CompetitionGroupUser::where('competition_question_id', $competition_question->id)->where('user_id', $request->input('user_id'))->first();
         if (!$check) {
             return response()->json("You are not TLG member.", 400);
         }
         $multiple_question = MutipleQuestion::where('question_id', $competition_question->id)->get();
         foreach ($multiple_question as $key => $value) {
             $multiple_question[$key]['option'] = MutipleOption::where('mutiple_question_id', $value->id)->orderBy('option', 'asc')->get();
         }
         $group_user = CompetitionGroupUser::where('competition_question_id', $competition_question->id)->where('user_id', $request->input('user_id'))->first();
         if ($group_user) {
             $competition_question['group_user_id'] = $group_user->id;
         } else {
             $competition_question['group_user_id'] = 0;
         }
         $competition_question['multiple_question'] = $multiple_question;
         $datetime1 = new DateTime($today);
         $datetime2 = new DateTime($competition_question->end_date);
         $interval = $datetime1->diff($datetime2);
         $competition_question['day_left'] = $interval->format('%a');
         $competition_question['current_datetime'] = $today;
         return response()->json($competition_question);
     } else {
         $competition_answer = CompetitionAnswer::with('competitiongroupuser')->where('correct', true)->orderBy('id', 'desc')->get();
         if (count($competition_answer) > 0) {
             $answer_list = array();
             foreach ($competition_answer as $i => $answer) {
                 $answer['status'] = $answer['status'] == 1 ? true : false;
                 $answer['correct'] = $answer['correct'] == 1 ? true : false;
                 $answer_list[] = $answer;
             }
             $competition_question = CompetitionQuestion::where('id', $competition_answer[0]->question_id)->first();
             $competition_question['correct_answer'] = $answer_list;
             return response()->json($competition_question);
         }
         $game_msg['error'] = 'The next game will start on November 1.';
         $game_msg['error_mm'] = 'ေနာက္ ဂိမ္းကို ႏိုဝင္ဘာ ၁ ရက္ေန႕တြင္ စပါမည္။';
         return response()->json($game_msg, 403);
     }
 }
 /**
  * Store a newly created User in storage.
  * POST /users
  *
  * @param Request $request
  *
  * @return Response
  */
 public function store(Request $request)
 {
     if (sizeof(User::$rules) > 0) {
         $validator = $this->validateRequestOrFail($request, User::$rules);
         if ($validator) {
             return $validator;
         }
     }
     $input = $request->all();
     $exist = User::where('username', $input['username'])->first();
     if ($exist) {
         return response()->json(['error' => 'This username is already exist.', 'error_mm' => 'အမည္တူရိွေနပါသည္။ အေနာက္တြင္ ဂဏန္း တစ္ခုခုထည့္ပါ။ ဥပမာ။။ Nyo Nyo1'], 400);
     }
     $input['objectId'] = str_random(10);
     $input['password'] = bcrypt($input['password']);
     $users = $this->userRepository->create($input);
     if (isset($input['tlg_city_address']) && $input['tlg_city_address'] && isset($input['isTlgTownshipExit']) && $input['isTlgTownshipExit']) {
         $role = new Role();
         $role->name = 'TLGUSER';
         $role->userId = $users->objectId;
         $role->save();
         $users['role'] = $role->name;
     } else {
         $users['role'] = 'User';
     }
     //Check for game
     $competition_question = CompetitionQuestion::where('start_date', '>', date('Y-m-d H:i:s'))->orwhere('end_date', '>', date('Y-m-d H:i:s'))->orderBy('id', 'desc')->first();
     //return response()->json($competition_question);
     if ($competition_question) {
         $group_user = CompetitionGroupUser::where('competition_question_id', $competition_question->id)->orderBy('id', 'desc')->first();
         //dd($group_user);
         if ($competition_question->user_count > 1) {
             $group_user_count = CompetitionGroupUser::where('competition_question_id', $competition_question->id)->where('group_name', $group_user->group_name)->count();
             if ($group_user_count == $competition_question->user_count) {
                 //Create New Group
                 $group_name = explode(" - ", $group_user->group_name);
                 if (count($group_name) > 1) {
                     $competition_group_user = new CompetitionGroupUser();
                     $competition_group_user->group_name = $group_name[0] . ' - ' . ($group_name[1] + 1);
                     $competition_group_user->group_city = isset($users->tlg_city_address) ? $users->tlg_city_address : '';
                     $competition_group_user->user_id = $users->id;
                     $competition_group_user->username = $users->username;
                     $competition_group_user->phone = $users->phoneNo;
                     $competition_group_user->profile_img = isset($users->user_profile_img) ? $users->user_profile_img : '';
                     $competition_group_user->competition_question_id = $competition_question->id;
                     $competition_group_user->save();
                 }
             } else {
                 //Create in Existing Group
                 $competition_group_user = new CompetitionGroupUser();
                 $competition_group_user->group_name = $group_user->group_name;
                 $competition_group_user->group_city = isset($users->tlg_city_address) ? $users->tlg_city_address : '';
                 $competition_group_user->user_id = $users->id;
                 $competition_group_user->username = $users->username;
                 $competition_group_user->phone = $users->phoneNo;
                 $competition_group_user->profile_img = isset($users->user_profile_img) ? $users->user_profile_img : '';
                 $competition_group_user->competition_question_id = $competition_question->id;
                 $competition_group_user->save();
             }
         } else {
             $group_name = explode(" - ", $group_user->group_name);
             if (count($group_name) > 1) {
                 $competition_group_user = new CompetitionGroupUser();
                 $competition_group_user->group_name = $group_name[0] . ' - ' . ($group_name[1] + 1);
                 $competition_group_user->group_city = isset($users->tlg_city_address) ? $users->tlg_city_address : '';
                 $competition_group_user->user_id = $users->id;
                 $competition_group_user->username = $users->username;
                 $competition_group_user->phone = $users->phoneNo;
                 $competition_group_user->profile_img = isset($users->user_profile_img) ? $users->user_profile_img : '';
                 $competition_group_user->competition_question_id = $competition_question->id;
                 $competition_group_user->save();
             }
         }
     }
     return $this->sendResponse($users->toArray(), "User saved successfully");
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     CompetitionQuestion::where('id', $id)->delete();
     return redirect('/admin/competition-question')->withMessages('Successfully deleted.');
 }
 public function getGroupUser(Request $request)
 {
     $validator = Validator::make($request->all(), ['question_id' => 'required', 'user_id' => 'required']);
     if ($validator->fails()) {
         if ($validator->errors()->has('question_id')) {
             return response()->json($validator->errors()->first('question_id'), 400);
         }
         if ($validator->errors()->has('group_user_id')) {
             return response()->json($validator->errors()->first('user_id'), 400);
         }
     }
     $group_name = CompetitionGroupUser::where('competition_question_id', $request->question_id)->where('user_id', $request->user_id)->pluck('group_name');
     $group_user_ids = CompetitionGroupUser::where('group_name', $group_name)->where('competition_question_id', $request->question_id)->lists('id');
     $group_list = CompetitionGroupUser::with('answer')->where('competition_question_id', $request->question_id)->where('group_name', $group_name)->get();
     $user_count = CompetitionQuestion::where('id', $request->question_id)->pluck('user_count');
     foreach ($group_list as $key => $value) {
         $profile_img = json_decode($value->profile_img, true);
         if ($profile_img) {
             $group_list[$key]['image_url'] = $profile_img['url'];
         } else {
             $group_list[$key]['image_url'] = "http://url";
         }
         $group_list[$key]['total_has_answer'] = count($group_list) * $user_count;
         $group_list[$key]['current_has_answer'] = MutipleAnswer::wherein('user_id', $group_user_ids)->count();
         $group_list[$key]['init_answer'] = null;
         foreach ($group_list[$key]['answer'] as $i => $value) {
             if ($i == 0) {
                 $group_list[$key]['init_answer'] = $value;
             }
         }
     }
     return response()->json($group_list);
 }