<?php

namespace App\Http\Controllers\Admin;

# Models
use App\Models\GameGroup;
use App\Models\GamePlace;

# Other Classes
use DB;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;

class GameGroupController extends Controller
{
     # Define Base view
    protected $view = 'admin.game-group.';

     #Bind GameGroup model
    protected $gameGroup;

    /**
      * Defining the default constructor for controller
      *
      */
    public function __construct(
                                GameGroup $gameGroup
                            )
    {
        $this->gameGroup  = $gameGroup;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //   $place = array("Sp Motor"=> array(
        //         'O'=>array('01','02','03','04','05','06','07','08','09','10'),
        //         '1'=> array('22','23','34','25','26','27','28','29','30'),
        //         '2'=>array('33','34','35','36','37','38','39','40'),
        //         '3'=>array('44','45','46','47','48','49','50'),
        //         '4'=>array('55','56','57','58','59','60'),
        //         '5'=>array('66','67','68','69','70'),
        //         '6'=>array('77','78','79','80'),
        //         '7'=>array('88','89','90'),
        //         '8'=>array('99','00'),
        //     ),
        // );
       
         try
        {

            $input = $request->all();

            #get all paymentUser resources
            $gameGroups = $this->gameGroup->where('created_at','!=',null);

            $search_name = '';
            $search_status = '';
            if(isset($input['search_name']) || isset($input['search_status'])){
                $search_name = $input['search_name'];
                $search_status = $input['search_status'];
                if(isset($search_name) && $search_name != null){
                $day->where('name','LIKE','%'.$search_name.'%');
                }
                if($search_status != null && $search_status == 1){
                    $day->where('status', $search_status);
                }else if($search_status != null && $search_status == 0){
                    $day->where('status',$search_status);
                }
               
            }  
            $details['lists'] = $gameGroups->paginate(10);

            #render view
            return view($this->view.'index',$details);
        }catch(\Exception $ex)
        {
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        try{
            
            #render view
            return view($this->view.'create');
         }catch(\Exception $ex)
        {
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try{
             #validate the request
            
            $validate = Validator::make($request->all(),[
                                                         'name'          => 'required|unique:game_group',
                                                        ]);
            if($validate->fails()){

                return redirect()->back()->withErrors($validate);
            }

            #get the requested data in Data variable
            $data = [
                    'name'       => $request->name,
                    'user_id'    => Auth::user()->id ,
                    ];
            #store the newly creted resoures
            $this->gameGroup->create($data);
            // #return redirect back to view
             toastr()->success('Game Group Create Sucess fully ...');
            return redirect('/admin/game-group');
         }catch(\Exception $ex)
        {
             toastr()->success($ex->getMessage());
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
         try{
            $details = $this->gameGroup->find($id);
            #render view
            return view($this->view.'edit');
         }catch(\Exception $ex)
        {
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    
}
