<?php

namespace App\Http\Controllers\API;

# Models
use App\Models\Bit;
use App\Models\Day;
use App\Models\GameWinner;
use App\Models\NewGameWinner;
use App\Models\NewStoreGame;
use App\Models\NewSingleDigitBit;
use App\Models\NewSinglePannaBit;
use App\Models\NewJodiBit;
use App\Models\NewSinglePattiBit;
use App\Models\NewDoublePannaBit;
use App\Models\NewTripplePannaBit;
use App\Models\NewSinglePattiPannaBit;
use App\Models\NewDoublePattiBit;
use App\Models\NewTriplePattiBit;
use App\Models\NewDoublePattiPannaBit;
use App\Models\NewHalfSangamBit;
use App\Models\NewFullSangamBit;
use App\Models\NewJodiFamilyBit;
use App\Models\NewRedJodiBit;
use App\Models\NewRedFamilyJodiBit;
use App\Models\NewCyclePattiiBit;
use App\Models\NewPannaFamilyBit;
use App\Models\PaymentNewHistory;
use App\Models\NewStore;
use App\Models\NewBit;


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

class NewGameHistoryController extends BaseController
{
     #Bind NewBit model
    protected $bit;
    
      #Bind PaymentNewHistory model
    protected $paymentNewHistory;
    
     #Bind NewGameWinner model
    protected $newGameWinner;
    
     #Bind NewStoreGame model
    protected $newStoreGame;

     /**
      * Defining the default constructor for controller
      *
      */
    public function __construct(
                                 NewBit $bit,
                                 NewStore $storeNew,
                                NewGameWinner $newGameWinner,
                                NewStoreGame $newStoreGame,
                                 PaymentNewHistory $paymentNewHistory
                            )
    {
          $this->bit  = $bit;
          $this->newGameWinner  = $newGameWinner;
         $this->storeNew   = $storeNew;
          $this->paymentNewHistory    = $paymentNewHistory;
          $this->newStoreGame    = $newStoreGame;
        
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
   

         $success['list'] ='';
        return $this->sendResponse($success, 'Store List.');
        
    }
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function starlineHistory()
    {
         $relation = ['timename','game'];
            $details =$this->bit->with($relation)->where('stores_id',7)->where('user_id',auth('api')->user()->id)->orderBy('updated_at','DESC')->get();
            $checknewStoreGame = $this->newStoreGame->where('stores_id',7)->get();
            $data = array();
            $i= 0;
                foreach ($details as $key => $value) {
                        $data[$i]['bazar'] = "Real STARLINE 1 BAZI";
                        $data[$i]['game'] = $value->game->name ??'';
                        $data[$i]['amount'] = $value->amount;
                        $data[$i]['type'] = $value->type;
                        $data[$i]['sno'] = $i+1;
                         $bit_place = '';
            $date = '';
            $time = '';
             if($value->game_id==1){
                $single = NewSingleDigitBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                   $bit_place = $bit_place.$singles->bit_place.',';
                   $date =  dateShow($singles->created_at);
                   $time =  timeShow($singles->created_at);
                }
            }
             if($value->game_id==2){
                 $single = NewJodiBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                   $bit_place = $bit_place.$singles->bit_place.',';
                   $date =  dateShow($singles->created_at);
                   $time =  timeShow($singles->created_at);
                }
            }
             if($value->game_id==3){
                 $single = NewSinglePattiBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                   $bit_place = $bit_place.$singles->bit_place.',';
                   $date =  dateShow($singles->created_at);
                   $time =  timeShow($singles->created_at);
                }
            }
             if($value->game_id==4){
                 $single = NewDoublePattiBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                   $bit_place = $bit_place.$singles->bit_place.',';
                   $date =  dateShow($singles->created_at);
                   $time =  timeShow($singles->created_at);
                }
            }
              if($value->game_id==5){
                  $single = NewSinglePannaBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                  $bit_place = $bit_place.$singles->bit_place.',';
                  $date =  dateShow($singles->created_at);
                  $time =  timeShow($singles->created_at);
                }
            }
               if($value->game_id==6){
                  $single = NewDoublePannaBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                  $bit_place = $bit_place.$singles->bit_place.',';
                  $date =  dateShow($singles->created_at);
                  $time =  timeShow($singles->created_at);
                }
            }
              if($value->game_id==7){
                  $single = NewTripplePannaBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                  $bit_place = $bit_place.$singles->bit_place.',';
                  $date =  dateShow($singles->created_at);
                  $time =  timeShow($singles->created_at);
                }
            }
            if($value->game_id==9){
                  $single = NewHalfSangamBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                  $bit_place = $bit_place.$singles->bit_place_panna.','.$bit_place.$singles->bit_place_single;
                  $date =  dateShow($singles->created_at);
                  $time =  timeShow($singles->created_at);
                }
            }
            if($value->game_id==10){
                  $single = NewFullSangamBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                   $bit_place = $bit_place.$singles->bit_place_panna.','.$bit_place.$singles->bit_place_single;
                  $date =  dateShow($singles->created_at);
                  $time =  timeShow($singles->created_at);
                }
            }
            if($value->game_id==11){
                  $single = NewJodiFamilyBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                  $bit_place = $bit_place.$singles->bit_place.',';
                  $date =  dateShow($singles->created_at);
                  $time =  timeShow($singles->created_at);
                }
            }
            if($value->game_id==12){
                  $single = NewRedJodiBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                  $bit_place = $bit_place.$singles->bit_place.',';
                  $date =  dateShow($singles->created_at);
                  $time =  timeShow($singles->created_at);
                }
            }
            if($value->game_id==13){
                  $single = NewRedFamilyJodiBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                  $bit_place = $bit_place.$singles->bit_place.',';
                  $date =  dateShow($singles->created_at);
                  $time =  timeShow($singles->created_at);
                }
            }
            if($value->game_id==14){
                  $single = NewCyclePattiiBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                  $bit_place = $bit_place.$singles->bit_place.',';
                  $date =  dateShow($singles->created_at);
                  $time =  timeShow($singles->created_at);
                }
            }
            if($value->game_id==15){
                  $single = NewPannaFamilyBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                  $bit_place = $bit_place.$singles->bit_place.',';
                  $date =  dateShow($singles->created_at);
                  $time =  timeShow($singles->created_at);
                }
            }
          
             $data[$i]['bitplace'] = $bit_place;
             $data[$i]['date'] = $date;
             $data[$i]['time'] = $time;
                        $i++;
                }
         $success['list'] =$data;
        return $this->sendResponse($success, 'Store List.');
        
    }
    public function jackportlineHistory()
    {
         $relation = ['timename','game'];
            $details =$this->bit->with($relation)->where('stores_id',9)->where('user_id',auth('api')->user()->id)->orderBy('updated_at','DESC')->get();
            $checknewStoreGame = $this->newStoreGame->where('stores_id',7)->get();
            $data = array();
            $i= 0;
                foreach ($details as $key => $value) {
                        $data[$i]['bazar'] = "REAL JACKPOT 1 BAZI";
                        $data[$i]['game'] = $value->game->name ??'';
                        $data[$i]['amount'] = $value->amount;
                        $data[$i]['type'] = $value->type;
                        $data[$i]['sno'] = $i+1;
                        $bit_place = '';
            $date = '';
            $time = '';
             
             if($value->game_id==2){
                 $single = NewJodiBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                   $bit_place = $bit_place.$singles->bit_place.',';
                   $date =  dateShow($singles->created_at);
                   $time =  timeShow($singles->created_at);
                }
            }
            if($value->game_id==11){
                  $single = NewJodiFamilyBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                  $bit_place = $bit_place.$singles->bit_place.',';
                  $date =  dateShow($singles->created_at);
                  $time =  timeShow($singles->created_at);
                }
            }
            if($value->game_id==12){
                  $single = NewRedJodiBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                  $bit_place = $bit_place.$singles->bit_place.',';
                  $date =  dateShow($singles->created_at);
                  $time =  timeShow($singles->created_at);
                }
            }
            if($value->game_id==13){
                  $single = NewRedFamilyJodiBit::where('bit_id',$value->id)->get();
               
                foreach ($single as $key => $singles) {
                  $bit_place = $bit_place.$singles->bit_place.',';
                  $date =  dateShow($singles->created_at);
                  $time =  timeShow($singles->created_at);
                }
            }
           
          
             $data[$i]['bitplace'] = $bit_place;
             $data[$i]['date'] = $date;
             $data[$i]['time'] = $time;
                        $i++;
                }
         $success['list'] =$data;
        return $this->sendResponse($success, 'Store List.');
        
    }
    public function newgamewinner(Request $request){
        
        $t = isset($request->date) ? $request->date : date('d-m-Y');
        $day = date("D", strtotime($t));
        $day_id = Day::where('name', 'like', $day . '%')->first()->id;
        
         $date = isset($request->date) ? $request->date : currentDate();
         
        //  $relation = ['storesDay' => function ($q) use ($day_id,$date){
        //      $subRelation = ['gameWinner' => function ($j) use ($date) {
        //          $j->whereDate('created_at',$date);
        //      }];
        //         $q->with($subRelation)->select('id','stores_id','stores_day_id','close_time','opent_time');
        //     }];
        
        // $success = $this->storeNew->with($relation)->select('id','name')->where('id',$request->store_id)->first();
        $data = [];
        $storrr = $this->newStoreGame->where('stores_day_id',$day_id)->where('stores_id',$request->store_id)->get();
        $i=0;
        
        foreach( $storrr as $ss){
               $data[$i]["time"] =timeShow( $ss->opent_time);
               $gamewinner = $this->newGameWinner->where("time_id",$ss->id)->where('stores_id',$request->store_id)->where('created_at','LIKE','%'.$date.'%')->first();
              
               $bit_open = isset($gamewinner) ? ( $gamewinner->close_time!= null ?    (str_replace('-','',$gamewinner->open_time))  : '')  : '';
                $bit_close = isset($gamewinner) ? ( $gamewinner->close_time!= null ?    (str_replace('-','',$gamewinner->close_time))  : '')  : '';
                if($request->store_id == 7){
                    $data[$i]["result"] = isset($gamewinner) ? $gamewinner->open_time   : 'xxx-x';
                }
                
                if($request->store_id == 9){
                    $data[$i]["result"] = isset($gamewinner) ? ( $gamewinner->close_time!= null ?   $bit_open[3]. $bit_close[0]   : 'xx')  : 'xx';
                }
               
               $i++;
            
        }
        
        
        $success['list'] =$data;
        return $this->sendResponse($success, 'New Game Winner List.');
    }
    
      public function newjackpotwinner(Request $request){
        
        $t = date('d-m-Y');
        $day = date("D", strtotime($t));
        $day_id = Day::where('name', 'like', $day . '%')->first()->id;
        
        $date = isset($request->date) ? $request->date : currentDate();
         $relation = ['storesDay' => function ($q) use ($day_id,$date){
             $subRelation = ['gameWinner' => function ($j) use ($date) {
                 $j->whereDate('created_at',$date);
             }];
                $q->with($subRelation)->select('id','stores_id','stores_day_id','close_time');
            }];
        
        $success = $this->storeNew->with($relation)->select('id','name')->where('id',$request->store_id)->first();
        
        
        
        return $this->sendResponse($success, 'New Game Winner List.');
    }
    
    
     
      public function TransactionHistory(Request $request){
         try
        {
           
            $input = $request->all();
            $details = $this->paymentNewHistory->where('user_id',auth('api')->user()->id)->orderBy('id', 'DESC')->get();
            $data = array();
          $i= 0;
            foreach ($details as $key => $value) {
               
                $data[$i]['date']    =  dateShow($value->created_at);
                $data[$i]['time']    =  timeShow($value->created_at);
                $data[$i]['amount']  =  $value->amount;
                $data[$i]['remark']  =  $value->remark;
                $data[$i]['reaming'] = $value->reamount;
                $data[$i]['type']    = $value->type;
                $i++;
            }
              $success['lists'] = $data ;
               return $this->sendResponse($success, 'Store List.');
            #render view
         
        }catch(\Exception $ex)
        {
          
        }
    }
    
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

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