<?php
  
namespace App\Http\Controllers\Admin;
  
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Session;
use Illuminate\Support\Facades\Redirect;
use App\Models\User;

use Hash;
use DB; 
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
  
class CategoryController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */

     public function leaguedetails()
     {
         if (!Auth::check()) { 
             return redirect("login");
           }           
 
 
           $leagues = DB::table('leagues')->orderBy('id', 'DESC')->get();
 
         return view('admin.league.leaguedetails')->with('leagues',$leagues);
     } 
   
    public function addleague()
    {
        if (!Auth::check()) { 
            return redirect("login");
          }           

        return view('admin.league.addleague');
    } 


    public function saveleague(Request $request)
    {

        date_default_timezone_set("Asia/Calcutta"); 
		
		 $savedata =  $request->all(); 

         // echo '<pre>'; print_r($savedata); echo '</pre>'; exit; 
		  
        $allradydata = DB::table('leagues')->where('leaguename', 'LIKE',$savedata['pagename'])->get();

        if(count($allradydata) > 0 ){
           
            Session::flash('message', "League Name already exist");
            
            return Redirect::back(); 
        }  
        $photoid = 0;

        if(isset($savedata['photoid'])) {

            $photoid =     $savedata['photoid'];
    
            }
 
            $insertdata = array( 
                            
                    'leaguename'=>$savedata['pagename'], 
                    'leagueshortname'=>$savedata['pageorder'], 
                    'leagguetype'=>1, 
                    'leagueimg'=>$photoid, 
                    'leaguedetails'=>$savedata['DSC'], 
                    
                    'createdby'=>$savedata['userid'],                      
                    'createdtime'=>date("Y-m-d H:i:s"),
                
            );
            
            DB::table('leagues')->insert($insertdata); 
 
		
        Session::flash('sucmessage', "Successful, League has Added"); 
		 
		return Redirect::back(); 
    } 

 
         
    public function editleague($id) {

        if (!Auth::check()) { 
            return redirect("login");
          }    
          
          $leagues = DB::table('leagues')->where('id', $id)->first();          

        return view('admin.league.editleague')->with('leagues',$leagues);
    } 

    public function updateleague(Request $request)
    {

        date_default_timezone_set("Asia/Calcutta"); 
		
		 $savedata =  $request->all(); 

         // echo '<pre>'; print_r($savedata); echo '</pre>'; exit;  
 
            $insertdata = array( 
                            
                'leaguename'=>$savedata['pagename'], 
                'leagueshortname'=>$savedata['pageorder'], 
                'leaguedetails'=>$savedata['DSC'], 
                    
            );
            
            DB::table('leagues')->where('id', $savedata['editid'])->update($insertdata); 
 
		
        Session::flash('sucmessage', "Successful,   League  has Updated"); 
		 
		return Redirect::back(); 
    } 

    public function deleteleague($id) {

        if (!Auth::check()) { 
            return redirect("login");
          }    
          
           DB::table('leagues')->where('id', $id)->delete();  
          
        Session::flash('sucmessage', "Successful,  Institute / School Class OR Group has Deleted"); 
		 
		return Redirect::back(); 
        
    } 

    ///////////////
    //// Teams Details
    ///////////////
    public function teamsdetails(): View
    {  
        if (!Auth::check()) { 
            return redirect("login");
          }  

        $teamsdetails = DB::table('teamsdetails')->orderBy('id', 'DESC')->get();

        return view('admin.teams.teamsdetails')->with('teamsdetails',$teamsdetails);
    }
    
    public function addteam()
    {
        if (!Auth::check()) { 
            return redirect("login");
          }           

        return view('admin.teams.addteam');
    } 

    

    public function saveteam(Request $request)
    {

        date_default_timezone_set("Asia/Calcutta"); 
		
		 $savedata =  $request->all(); 

         // echo '<pre>'; print_r($savedata); echo '</pre>'; exit; 
		  
        $allradydata = DB::table('teamsdetails')->where('teamname', 'LIKE',$savedata['pagename'])->get();

        if(count($allradydata) > 0 ){
           
            Session::flash('message', "League Name already exist");
            
            return Redirect::back(); 
        }  
        $photoid = 0;

        if(isset($savedata['photoid'])) {

            $photoid =     $savedata['photoid'];
    
            }
 
            $insertdata = array( 
                            
                    'teamname'=>$savedata['pagename'], 
                    'teamshtname'=>$savedata['pageorder'], 
                    'teamtype'=>1, 
                    'teamimg'=>$photoid, 
                    'teamdetails'=>$savedata['DSC'], 
                    
                    'createdby'=>$savedata['userid'],                      
                    'createdtime'=>date("Y-m-d H:i:s"),
                
            );
            
            DB::table('teamsdetails')->insert($insertdata); 
 
		
        Session::flash('sucmessage', "Successful, Team has Added"); 
		 
		return Redirect::back(); 
    } 

             
    public function editteam($id) {

        if (!Auth::check()) { 
            return redirect("login");
          }    
          
          $teamsdetails = DB::table('teamsdetails')->where('id', $id)->first();          

        return view('admin.teams.editteam')->with('teamsdetails',$teamsdetails);
    } 


    public function updateteam(Request $request)
    {

        date_default_timezone_set("Asia/Calcutta"); 
		
		 $savedata =  $request->all(); 

         // echo '<pre>'; print_r($savedata); echo '</pre>'; exit;  

         if(isset($savedata['photoid'])) {

            $photoid =     $savedata['photoid'];
    
            }
 
            $insertdata = array( 
                            
                'teamname'=>$savedata['pagename'], 
                'teamshtname'=>$savedata['pageorder'], 
                'teamdetails'=>$savedata['DSC'], 
                'teamimg'=>$photoid, 
            );
            
            DB::table('teamsdetails')->where('id', $savedata['editid'])->update($insertdata); 
 
		
        Session::flash('sucmessage', "Successful,   Team  has Updated"); 
		 
		return Redirect::back(); 
    } 

    public function deleteteam($id) {

        if (!Auth::check()) { 
            return redirect("login");
          }    
          
           DB::table('teamsdetails')->where('id', $id)->delete();  
          
        Session::flash('sucmessage', "Successful,  Team has Deleted"); 
		 
		return Redirect::back(); 
        
    } 

    ///////////////
    //// Teams Details
    ///////////////
    public function manageplayers(): View
    {  
        if (!Auth::check()) { 
            return redirect("login");
          }  

        $players = DB::table('players')->orderBy('id', 'DESC')->get();

        return view('admin.players.manageplayers')->with('players',$players);
    }

    public function addplayer()
    {
        if (!Auth::check()) { 
            return redirect("login");
          }           

        return view('admin.players.addplayer');
    } 

    
    public function saveplayer(Request $request)
    {

        date_default_timezone_set("Asia/Calcutta"); 
		
		 $savedata =  $request->all(); 

         // echo '<pre>'; print_r($savedata); echo '</pre>'; exit; 
		  
        $allradydata = DB::table('players')->where('playername', 'LIKE',$savedata['pagename'])->get();

        if(count($allradydata) > 0 ){
           
            Session::flash('message', "Player Name already exist");
            
            return Redirect::back(); 
        }  
        $photoid = 0;

        if(isset($savedata['photoid'])) {

            $photoid =     $savedata['photoid'];
    
            }
 
            $insertdata = array( 
                            
                    'playername'=>$savedata['pagename'], 
                    'playershtname'=>$savedata['pageorder'], 
                    'playertype'=>$savedata['playertype'], 
                    'playerimg'=>$photoid, 
                    
                    
                    'createdby'=>$savedata['userid'],                      
                    'crwatedtime'=>date("Y-m-d H:i:s"),
                
            );
            
            DB::table('players')->insert($insertdata); 
 
		
        Session::flash('sucmessage', "Successful, Player has Added"); 
		 
		return Redirect::back(); 
    } 

    public function editplayer($id) {

        if (!Auth::check()) { 
            return redirect("login");
          }    
          
          $players = DB::table('players')->where('id', $id)->first();          

        return view('admin.players.editplayer')->with('players',$players);
    } 



    public function updateplayer(Request $request)
    {

        date_default_timezone_set("Asia/Calcutta"); 
		
		 $savedata =  $request->all(); 

         // echo '<pre>'; print_r($savedata); echo '</pre>'; exit;  

         if(isset($savedata['photoid'])) {

            $photoid =     $savedata['photoid'];
    
            }
 
            $insertdata = array( 
                            
                    'playername'=>$savedata['pagename'], 
                    'playershtname'=>$savedata['pageorder'], 
                    'playertype'=>$savedata['playertype'], 
                    'playerimg'=>$photoid, 
            );
            
            DB::table('players')->where('id', $savedata['editid'])->update($insertdata); 
 
		
        Session::flash('sucmessage', "Successful,   Players  has Updated"); 
		 
		return Redirect::back(); 
    } 

    

    public function deleteplayer($id) {

        if (!Auth::check()) { 
            return redirect("login");
          }    
          
           DB::table('players')->where('id', $id)->delete();  
          
        Session::flash('sucmessage', "Successful,  Player has Deleted"); 
		 
		return Redirect::back(); 
        
    } 


}
