<?php

namespace App\Http\Controllers\Admin;

# Models
use App\Models\Game;
use App\Models\Store;
use App\Models\GameGroup;

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

class StoresController extends Controller
{
       # Define Base view
    protected $view = 'admin.stores.';

     #Bind GameGroup model
    protected $gameGroup;

     #Bind Game model
    protected $game;

     #Bind Store model
    protected $store;


    /**
      * Defining the default constructor for controller
      *
      */
    public function __construct(
                                GameGroup $gameGroup,
                                Game $game,
                                Store $store
                            )
    {
        $this->gameGroup  = $gameGroup;
        $this->game       = $game;
        $this->store      = $store;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
         try
        {
           
            $input = $request->all();

            #get all paymentUser resources
            $realtion = ['storesDay'];
            $stores = $this->store->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'] = $stores->paginate(10);
            dd($details['lists']);

            #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',
                                                         
                                                        ]);
            if($validate->fails()){

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

            #get the requested data in Data variable
            $data = [
                    'name'       => $request->name,
                    'email'      => 'test@gmail.com',
                    'mobile'     => 9898989898,
                    'user_id'    => Auth::user()->id ,
                    ];
            #store the newly creted resoures
            $this->store->create($data);
            // #return redirect back to view
             toastr()->success('Store Create Sucess fully ...');
            return redirect('/admin/stores');
         }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)
    {
        //
    }

    /**
     * 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)
    {
        //
    }
}
