<?php

namespace App\Http\Controllers\AdminAPI\GameManagement;

# Models
use App\User;
use App\Models\Day;
use App\Models\NewBit;
use App\Models\NewGame;
use App\Models\Game;
use App\Models\NewStore;
use App\Models\NewJodiBit;
use App\Models\GameRate;
use App\Models\GameGroup;
use App\Models\NewStoreGame;
use App\Models\NewGameWinner;
use App\Models\NewSingleDigitBit;
use App\Models\NewChoicePannaBit;
use App\Models\NewSinglePattiBit;
use App\Models\NewSinglePattiPannaBit;
use App\Models\NewSinglePannaBit;
use App\Models\NewDoublePattiBit;
use App\Models\NewDoublePattiPannaBit;
use App\Models\NewDoublePannaBit;
use App\Models\NewTripplePattiBit;
use App\Models\NewTripplePannaBit;
use App\Models\NewHalfSangamBit;
use App\Models\NewFullSangamBit;
use App\Models\NewJodiFamilyBit;
use App\Models\NewRedJodiBit;
use App\Models\NewRedFamilyJodiBit;
use App\Models\NewCyclePattiBit;
use App\Models\NewPannaFamilyBit;
use App\Models\BeadOnMotor;
use App\Exports\gameWinnerExport;
use App\Models\NewUserWinnersAmount;

# Other Classes
use DB;
use Excel;
use Auth;
use Validator;
use Carbon\Carbon;
use App\Traits\FirebaseTraits;
use App\Traits\WinnerTrait;
use App\Traits\GameTrait;
use Illuminate\Http\Request;
use App\Http\Controllers\API\BaseController as BaseController;

class NewGameWinnerController extends BaseController
{
    use FirebaseTraits;
    use WinnerTrait;
    use GameTrait;
    # Define Base view
    protected $view = 'admin.new-game-winner.';

    #Bind GameGroup model
    protected $gameGroup;

    #Bind StoreGame model
    protected $storeGame;

    #Bind Game model
    protected $game;

    #Bind Store model
    protected $store;

    #Bind Day model
    protected $day;

    #Bind GameRate model
    protected $gameRate;

    #Bind GameWinner model
    protected $gameWinner;

    #Bind Bit model
    protected $bit;

    #Bind SingleDigitBit model
    protected $singleDigitBit;

    #Bind ChoicePannaBit model
    protected $choicePannaBit;

    #Bind JodiBit model
    protected $jodiBit;

    #Bind SinglePattiBit model
    protected $singlePattiBit;

    #Bind SinglePattiPannaBit model
    protected $singlePattiPannaBit;

    #Bind DoublePattiBit model
    protected $doublePattiBit;

    #Bind DoublePattiPannaBit model
    protected $doublePattiPannaBit;

    #Bind BeadOnMotor model
    protected $beadOnMotor;

    /**
     * Defining the default constructor for controller
     *
     */
    public function __construct(
        Day $day,
        User $user,
        NewBit $bit,
        Game $game,
        NewStore $store,
        GameGroup $gameGroup,
        NewStoreGame $storeGame,
        GameRate $gameRate,
        NewGameWinner $gameWinner,
        NewSingleDigitBit $singleDigitBit,
        NewChoicePannaBit $choicePannaBit,
        NewJodiBit $jodiBit,
        NewSinglePattiBit $singlePattiBit,
        NewSinglePattiPannaBit $singlePattiPannaBit,
        NewDoublePattiBit $doublePattiBit,
        NewDoublePattiPannaBit $doublePattiPannaBit,
        BeadOnMotor $beadOnMotor
    ) {
        $this->bit                 = $bit;
        $this->gameGroup           = $gameGroup;
        $this->game                = $game;
        $this->store               = $store;
        $this->day                 = $day;
        $this->storeGame           = $storeGame;
        $this->gameRate            = $gameRate;
        $this->gameWinner          = $gameWinner;
        $this->singleDigitBit      = $singleDigitBit;
        $this->choicePannaBit      = $choicePannaBit;
        $this->jodiBit             = $jodiBit;
        $this->singlePattiBit      = $singlePattiBit;
        $this->singlePattiPannaBit = $singlePattiPannaBit;
        $this->doublePattiBit      = $doublePattiBit;
        $this->doublePattiPannaBit = $doublePattiPannaBit;
        $this->beadOnMotor         = $beadOnMotor;
        $this->user                = $user;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // try
        // {

        $input = $request->all();

        #get all paymentUser resources
        $currentDay = Carbon::now()->format('l');
        $today = $this->day->where('name', $currentDay)->first();
        $relation = ['storesDay' => function ($q) use ($today) {
            $q->with('winners')->where('stores_day_id', $today->id);
        }];
        $details['lists'] = $this->store->with($relation)->paginate(150);


        // return $details['lists'];
        #render view
            return $this->sendResponse($details, 'Stores Game Schedule successfully.');
        // }catch(\Exception $ex)
        // {
        //     
            // return $this->sendError($ex, 'Error');
        // }
    }

    public function yourstoreplayOpen(Request $request, $id)
    {
        // try {

        $input = $request->all();
        $details['ids'] = $id;
        if ($request->store_id == 7) {
            $gamess = Game::where('id', '!=', 8)->orderBy('id', 'asc')->get();
        } else {
            $gamess = Game::whereIn('id', [2, 11, 12, 13])->orderBy('id', 'asc')->get();
        }
        $bittttt = $this->bit->where('created_at', 'like', currentDate() . '%')->where('time_id', $id)->first();
        $data = array();
        $datanew = array();
        $i = 0;
        foreach ($gamess as $key) {
            $result = '';
            $data[$i]['name'] = $key->name;
            $data[$i]['id'] = $key->id;
            $bsss = $this->bit->where('created_at', 'like', currentDate() . '%')->where('time_id', $id);
            if (isset($input['search_user'])) {

                if (isset($input['search_user'])) {

                    $bsss->where('user_id', 'LIKE', '%' . $input['search_user'] . '%');
                }
            }
            $data[$i]['amount'] = $bsss->where('game_id', $key->id)->where('type', '=', 'open')->sum('amount');


            $bitaa =  $this->bit->where('created_at', 'like', currentDate() . '%')->where('time_id', $id)->where('type', '=', 'open');
            if (isset($input['search_user'])) {

                if (isset($input['search_user'])) {

                    $bitaa->where('user_id', 'LIKE', '%' . $input['search_user'] . '%');
                }
            }
            $bitaata = $bitaa->where('game_id', $key->id)->pluck('id');
            if ($key->id == 1) {
                $result = NewSingleDigitBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->where('created_at', 'like', currentDate() . '%')
                    ->whereIn('bit_id', $bitaata)
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 2) {
                $result = NewJodiBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->groupBy('bit_place')
                    ->where('created_at', 'like', currentDate() . '%')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 3) {

                $result = NewSinglePattiBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 4) {

                $result = NewDoublePattiBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 5) {

                $result = NewSinglePannaBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 6) {

                $result = NewDoublePannaBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 7) {

                $result = NewTripplePannaBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            // if ($key->id == 8) {

            //     $result = NewTripplePattiBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
            //         ->whereIn('bit_id', $bitaata)
            //         ->where('created_at', 'like', currentDate() . '%')
            //         ->groupBy('bit_place')
            //         ->orderBy('bit_place')
            //         ->get();
            // }
            if ($key->id == 9) {
                //   dd($key->id);
                $result = NewHalfSangamBit::select('bit_place_panna', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place_panna')
                    ->orderBy('bit_place_panna')
                    ->get();
            }
            if ($key->id == 10) {
                //   dd($key->id);
                $result = NewFullSangamBit::select('bit_place_panna', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place_panna')
                    ->orderBy('bit_place_panna')
                    ->get();
            }
            if ($key->id == 11) {

                $result = NewJodiFamilyBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 12) {

                $result = NewRedJodiBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 13) {

                $result = NewRedFamilyJodiBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 14) {

                $result = NewCyclePattiBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 15) {

                $result = NewPannaFamilyBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            $datanew[] = $result;

            $i++;
        }

        $details['game'] = $data;
        $details['result'] = $datanew;
        $details['storess'] = $this->store->whereId($request->store_id)->first();
        // return $details['storess'];
        $totot = $this->bit->where('created_at', 'like', currentDate() . '%')->where('type', '=', 'open');
        if (isset($input['search_user'])) {

            if (isset($input['search_user'])) {

                $totot->where('user_id', 'LIKE', '%' . $input['search_user'] . '%');
            }
        }
        $details['totalamount'] =  $totot->where('time_id', $id)->sum('amount');

        $details['customerss'] = User::where('status', 1)->where('type', 'customer')->get();
        //   return $details;
        #render view
            return $this->sendResponse($details, 'Stores Game Schedule successfully.');
        // } catch (\Exception $ex) {
        //   
            // return $this->sendError($ex, 'Error');
        // }
    }
    public function yourstoreplayClose(Request $request, $id)
    {
        // try {

        $input = $request->all();
        $details['ids'] = $id;
        if ($request->store_id == 7) {
            $gamess = Game::where('id', '!=', 8)->orderBy('id', 'asc')->get();
        } else {
            $gamess = Game::whereIn('id', [2, 11, 12, 13])->orderBy('id', 'asc')->get();
        }
        $bittttt = $this->bit->where('created_at', 'like', currentDate() . '%')->where('time_id', $id)->first();
        $data = array();
        $datanew = array();
        $i = 0;
        foreach ($gamess as $key) {
            $result = '';
            $data[$i]['name'] = $key->name;
            $data[$i]['id'] = $key->id;
            $bsss = $this->bit->where('created_at', 'like', currentDate() . '%')->where('time_id', $id);
            if (isset($input['search_user'])) {

                if (isset($input['search_user'])) {

                    $bsss->where('user_id', 'LIKE', '%' . $input['search_user'] . '%');
                }
            }
            $data[$i]['amount'] = $bsss->where('game_id', $key->id)->where('type', '=', 'close')->sum('amount');


            $bitaa =  $this->bit->where('created_at', 'like', currentDate() . '%')->where('time_id', $id)->where('type', '=', 'close');
            if (isset($input['search_user'])) {

                if (isset($input['search_user'])) {

                    $bitaa->where('user_id', 'LIKE', '%' . $input['search_user'] . '%');
                }
            }
            $bitaata = $bitaa->where('game_id', $key->id)->pluck('id');
            if ($key->id == 1) {
                $result = NewSingleDigitBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->where('created_at', 'like', currentDate() . '%')
                    ->whereIn('bit_id', $bitaata)
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 2) {
                $result = NewJodiBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->groupBy('bit_place')
                    ->where('created_at', 'like', currentDate() . '%')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 3) {

                $result = NewSinglePattiBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 4) {

                $result = NewDoublePattiBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 5) {

                $result = NewSinglePannaBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 6) {

                $result = NewDoublePannaBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 7) {

                $result = NewTripplePannaBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            // if ($key->id == 8) {

            //     $result = NewTripplePattiBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
            //         ->whereIn('bit_id', $bitaata)
            //         ->where('created_at', 'like', currentDate() . '%')
            //         ->groupBy('bit_place')
            //         ->orderBy('bit_place')
            //         ->get();
            // }
            if ($key->id == 9) {
                //   dd($key->id);
                $result = NewHalfSangamBit::select('bit_place_panna', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place_panna')
                    ->orderBy('bit_place_panna')
                    ->get();
            }
            if ($key->id == 10) {
                //   dd($key->id);
                $result = NewFullSangamBit::select('bit_place_panna', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place_panna')
                    ->orderBy('bit_place_panna')
                    ->get();
            }
            if ($key->id == 11) {

                $result = NewJodiFamilyBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 12) {

                $result = NewRedJodiBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 13) {

                $result = NewRedFamilyJodiBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 14) {

                $result = NewCyclePattiBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            if ($key->id == 15) {

                $result = NewPannaFamilyBit::select('bit_place', \DB::raw('SUM(amount) as total_val'))
                    ->whereIn('bit_id', $bitaata)
                    ->where('created_at', 'like', currentDate() . '%')
                    ->groupBy('bit_place')
                    ->orderBy('bit_place')
                    ->get();
            }
            $datanew[] = $result;

            $i++;
        }

        $details['game'] = $data;
        $details['result'] = $datanew;
        $details['storess'] = $this->store->whereId($request->store_id)->first();
        // return $details['storess'];
        $totot = $this->bit->where('created_at', 'like', currentDate() . '%')->where('type', '=', 'close');
        if (isset($input['search_user'])) {

            if (isset($input['search_user'])) {

                $totot->where('user_id', 'LIKE', '%' . $input['search_user'] . '%');
            }
        }
        $details['totalamount'] =  $totot->where('time_id', $id)->sum('amount');

        $details['customerss'] = User::where('status', 1)->where('type', 'customer')->get();
        //   return $details;
        #render view
            return $this->sendResponse($details, 'Stores Game Schedule successfully.');
        // } catch (\Exception $ex) {
        //   
            // return $this->sendError($ex, 'Error');
        // }
    }

    public function downloadExcel(Request $request)
    {

        try {

            $input = $request->all();
            $search_date = '';
            if (isset($input['search_date'])) {
                $search_date = $input['search_date'];
            } else {
                $search_date = currentDate();
            }
            return Excel::download(new gameWinnerExport($search_date), 'game_winner.xlsx');
        } catch (\Exception $ex) {
            
            return $this->sendError($ex, 'Error');
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        try {
            $relation = ['storesDay'];
            $details['store'] = $this->store->with($relation)->get();

            #render view
            return $this->sendResponse($details, 'Stores Game Schedule successfully.');
        } catch (\Exception $ex) {
            
            return $this->sendError($ex, 'Error');
        }
    }

    public function gameWinner(Request $request, $id)
    {

        try {


            $input = $request->all();
            $realtion = ['gameRate'];
            $gamess = $this->game->with($realtion)->orderBy('id', 'asc')->get();

            $data = array();

            $i = 0;
            $totalamount = 0;
            foreach ($gamess as $key) {
                $result = '';
                $data[$i]['name'] = $key->name;
                $data[$i]['rate'] = $key->gameRate->rate;
                $data[$i]['id'] = $key->id;
                $data[$i]['amount'] = $key->id == 10  || $key->id == 2 ? 0 : $this->bit->where('created_at', 'like', currentDate() . '%')->where('time_id', $id)->where('game_id', $key->id)->where('type', '=', 'open')->sum('amount');
                $data[$i]['winneramount'] =  NewUserWinnersAmount::where('created_at', 'like', currentDate() . '%')->where('time_id', $id)->where('game_id', $key->id)->where('type', '=', 'open')->sum('amount');
                $totalamount = $totalamount + $data[$i]['winneramount'];
                $i++;
            }

            $details['game'] = $data;
            $details['storesid'] = $id;
            $storeGame = $this->storeGame->whereId($id)->first();
            $details['storess'] = $this->store->whereId($storeGame->stores_id)->first();
            $details['result'] = $this->gameWinner->where('stores_id', $storeGame->stores_id)->orderBy('created_at', 'DESC')->first();

            $details['totalamount'] =  $totalamount;


            #render view
            return $this->sendResponse($details, 'Stores Game Schedule successfully.');
        } catch (\Exception $ex) {
            
            return $this->sendError($ex, 'Error');
        }
    }

    public function gameWinnerClose(Request $request, $id)
    {

        try {



            $input = $request->all();
            $realtion = ['gameRate'];
            $gamess = $this->game->with($realtion)->orderBy('id', 'asc')->get();
            $data = array();
            $i = 0;
            foreach ($gamess as $key) {
                $result = '';
                $data[$i]['name'] = $key->name;
                $data[$i]['rate'] = $key->gameRate->rate;
                $data[$i]['id'] = $key->id;
                $data[$i]['amount'] = $key->id == 10   || $key->id == 2 ? $this->bit->where('created_at', 'like', currentDate() . '%')->where('time_id', $id)->where('game_id', $key->id)->where('type', '=', 'open')->sum('amount') : $this->bit->where('created_at', 'like', currentDate() . '%')->where('time_id', $id)->where('game_id', $key->id)->where('type', '=', 'close')->sum('amount');
                $data[$i]['winneramount'] =  NewUserWinnersAmount::where('created_at', 'like', currentDate() . '%')->where('time_id', $id)->where('game_id', $key->id)->where('type', '=', 'close')->sum('amount');
                $i++;
            }

            $details['game'] = $data;

            $storeGame = $this->storeGame->whereId($id)->first();
            $details['storess'] = $this->store->whereId($storeGame->stores_id)->first();

            $details['result'] = $this->gameWinner->where('stores_id', $storeGame->stores_id)->orderBy('created_at', 'DESC')->first();

            $details['totalamount'] =  $this->bit->where('created_at', 'like', currentDate() . '%')->where('type', '=', 'close')->where('stores_id', $storeGame->stores_id)->sum('amount');
            $details['storessnew'] = $this->store->whereId($storeGame->stores_id)->first();
            $details['storesid'] = $id;

            #render view
            return $this->sendResponse($details, 'Stores Game Schedule successfully.');
        } catch (\Exception $ex) {
            
            return $this->sendError($ex, 'Error');
        }
    }

    public function addOpenResult($id)
    {

        try {
            $relation = ['storesDay', 'Stores'];
            $details['store'] = $this->storeGame->whereId($id)->first();

            #render view
            return $this->sendResponse($details, 'Stores Game Schedule successfully.');
        } catch (\Exception $ex) {
            
            return $this->sendError($ex, 'Error');
        }
    }

    public function addOpenReverseResult($id)
    {
        try {
            $relation = ['storesDay', 'Stores'];
            $details['store'] = $this->storeGame->whereId($id)->first();

            #render view
            return $this->sendResponse($details, 'Stores Game Schedule successfully.');
        } catch (\Exception $ex) {
            
            return $this->sendError($ex, 'Error');
        }
    }

    public function postOpenResult(Request $request, $id)
    {



        DB::beginTransaction();

        $auth_id = Auth::user()->id;
        //   try{
        $data = [
            'stores_id'        => $id,
            'game_id'          => 1,
            'game_group_id'    => 1,
            'day_id'           => 2,
            'bit_place'        => 00000,
            'open_time'        => $request->result,
            'user_id'          => Auth::user()->id,
            'time_id'          => $request->time_id,
        ];

        #store the newly creted resoures
        $winer = $this->gameWinner->create($data);


        $bit_open = (str_replace('-', '', $request->result));

        $patti = $bit_open[0] . $bit_open[1] . $bit_open[2];
        $digit = $bit_open[3];

        $this->NewSingleDigitOpen($id, $digit, $winer->id, $request->time_id);
        $this->NewSinglePannaOpen($id, $patti, $winer->id, $request->time_id);
        $this->NewDoublePannaOpen($id, $patti, $winer->id, $request->time_id);
        $this->NewTripplePannaOpen($id, $patti, $winer->id, $request->time_id);
        $this->NewPannaSpOpen($id, $patti, $winer->id, $request->time_id);
        $this->NewPannaDpOpen($id, $patti, $winer->id, $request->time_id);
        $this->NewPannaFamilyOpen($id, $patti, $winer->id, $request->time_id);
        $this->NewCyclePattiOpen($id, $patti, $winer->id, $request->time_id);



        $postcust = $this->user->whereId($auth_id)->first();

        $relation = ['storesDay'];
        $stt = $this->store->with($relation)->whereId($id)->first();

        if ($stt && $stt->storesDay->isNotEmpty()) {
            $opent_time = $stt->storesDay->first()->opent_time;
        } else {
            $opent_time = ''; // handle the case where storesDay is empty
        }

        $name = $postcust->name . ' ' . ($opent_time ? timeShow($opent_time) : '');
        $body = $request->result . '-x-xxx';
        $title = $stt->name . " Open";
        $this->notification($title, $body, $id);


        DB::commit();
        return $this->sendResponse(true, 'Stores Game Schedule successfully.');
        //  }catch(\Exception $ex)
        // {
        //      DB::rollback();
        //     
            // return $this->sendError($ex, 'Error');
        // }
    }


    public function postOpenReverseResult(Request $request, $id)
    {



        DB::beginTransaction();

        // try {
        $data = [
            'stores_id'        => $id,
            'game_id'          => 1,
            'game_group_id'    => 1,
            'day_id'           => 2,
            'bit_place'        => 00000,
            'open_time'        => $request->result,
            'user_id'          => Auth::user()->id,
            'time_id'          => $request->time_id,
        ];
        #store the newly creted resoures

        $check =  $this->gameWinner->where('stores_id', $id)->where('time_id', $request->time_id)->where('created_at', 'like', currentDate() . '%')->first();

        if ($check) {
            $winer = $this->gameWinner->whereId($check->id)->update($data);

            //   $winer= $this->gameWinner->whereId($check->id)->first();
        }

        $userwinnercheck = NewUserWinnersAmount::where('game_winners_id', $check->id)->where('type', '=', 'open')->get();
        $userwinnercheckclose = NewUserWinnersAmount::where('game_winners_id', $check->id)->where('game_id', 9)->where('type', '=', 'close')->get();


        if (count($userwinnercheck) > 0) {
            foreach ($userwinnercheck as $vv) {

                debitReverseAmount($vv->user_id, $vv->amount);
                $remark = " Result Reverse  By Admin " . $vv->amount;
                PaymentNewRemark($vv->user_id, $remark, $vv->amount);
                NewUserWinnersAmount::whereId($vv->id)->delete();
            }
        }
        if (count($userwinnercheckclose) > 0) {
            foreach ($userwinnercheckclose as $vv) {

                debitReverseAmount($vv->user_id, $vv->amount);
                $remark = " Result Reverse  By Admin " . $vv->amount;
                PaymentNewRemark($vv->user_id, $remark, $vv->amount);
                NewUserWinnersAmount::whereId($vv->id)->delete();
            }
        }
        $bit_open = (str_replace('-', '', $request->result));
        $patti = $bit_open[0] . $bit_open[1] . $bit_open[2];
        $digit = $bit_open[3];
        $bitopenss = $check->close_time;
        $digit1 = isset($bitopenss[0]) ? $bitopenss[0] : '';
        $jodibb = $digit1 . '' . $digit;


        $patti_open = isset($bitopenss[2]) && isset($bitopenss[3]) && isset($bitopenss[4]) ? $bitopenss[2] . $bitopenss[3] . $bitopenss[4] : '';

        $single_open = isset($bitopenss[0]) ?  $bitopenss[0] : '';

        $this->NewSingleDigitOpen($id, $digit, $check->id, $request->time_id);
        $this->NewSinglePannaOpen($id, $patti, $check->id, $request->time_id);
        $this->NewDoublePannaOpen($id, $patti, $check->id, $request->time_id);
        $this->NewTripplePannaOpen($id, $patti, $check->id, $request->time_id);
        $this->NewPannaSpOpen($id, $patti, $check->id, $request->time_id);
        $this->NewPannaDpOpen($id, $patti, $check->id, $request->time_id);
        $this->NewPannaFamilyOpen($id, $patti, $check->id, $request->time_id);
        $this->NewCyclePattiOpen($id, $patti, $check->id, $request->time_id);


        if ($patti_open) {

            $this->NewHalfSangamOpen($id, $patti, $single_open, $check->id, $request->time_id);
            $this->NewHalfSangamClose($id, $patti_open, $digit, $check->id, $request->time_id);

            $this->NewFullSangamClose($id, $patti_open, $patti, $check->id, $request->time_id);
        }


        if ($digit1) {
            $this->NewJodiClose($id, $jodibb, $check->id, $request->time_id);
            $this->NewRedJodiClose($id, $jodibb, $check->id, $request->time_id);
            $this->NewRedJodiFamilyClose($id, $jodibb, $check->id, $request->time_id);
            $this->NewJodiFamilyClose($id, $jodibb, $check->id, $request->time_id);
        }


        $postcust = $this->user->whereId(1)->first();
        $relation = ['storesDayOne'];
        $stt = $this->store->with($relation)->whereId($id)->first();
        $user_id = 3;
        $name = $postcust->name;
        $body = $request->result . '' . isset($stt->storesDayOne) ? timeShow($stt->storesDayOne->opent_time) : '';
        $title = $stt->name . " Close";
        $this->notification($title, $body, $user_id);

        DB::commit();
        return $this->sendResponse(true, 'Stores Game Schedule successfully.');
        // } catch (\Exception $ex) {
        //     DB::rollback();
        //     
            // return $this->sendError($ex, 'Error');
        // }
    }

    public function addCloseResult($id)
    {
        try {
            $relation = ['storesDay', 'Stores'];
            $details['store'] = $this->storeGame->whereId($id)->first();
            #render view
            return $this->sendResponse($details, 'Stores Game Schedule successfully.');
        } catch (\Exception $ex) {
            
            return $this->sendError($ex, 'Error');
        }
    }

    public function postCloseResult(Request $request, $id)
    {
        DB::beginTransaction();


        $validate = Validator::make($request->all(), [
            'result'          => 'required',
        ]);
        if ($validate->fails()) {


            return $this->sendError($validate->messages(), 'Please Fill Mandatory field');
        }

        // try {


        $check = $this->gameWinner
            ->where('time_id', $request->time_id)
            ->where('created_at', 'like', currentDate() . '%')
            ->first();

        if ($id == 9) {
            $bit_open = str_replace('-', '', $request->result);
            $digit = $bit_open[1];
        } else {
            $bit_open = str_replace('-', '', $request->result);
            $patti = $bit_open[1] . $bit_open[2] . $bit_open[3];
            $bit_open_open = $check->open_time;
            $patti_open = $bit_open_open[0] . $bit_open_open[1] . $bit_open_open[2];
            $digit = $bit_open[0];
            $single_open = $bit_open_open[4];
        }


        $user_id = Auth::user()->id;
        // Check if there is already a winner record for the given time_id and current date




        // Prepare the data for updating or creating the winner record
        $data = [
            'stores_id'     => $id,
            'game_id'       => 1,
            'game_group_id' => 1,
            'day_id'        => 2,
            'bit_place'     => 00000,
            'close_time'    => $id == 9 ? $request->result[2] . '-***' : $request->result,
            'user_id'       => Auth::user()->id,
            'time_id'       => $request->time_id,
        ];

        // Update or create the winner record
        if ($check) {
            // Update only the 'close_time' field for the existing record
            $this->gameWinner->whereId($check->id)->update([
                'close_time' => $request->result
            ]);
            $winner = $check;
        } else {
            // Create a new record if one doesn't already exist
            if ($id == 9) {
                $data['open_time'] = '***-' . $request->result[0];
            }
            $winner = $this->gameWinner->create($data);
        }

        // Process the bit_open value


        if ($id == 9) {


            if ($winner) {

                $bitopenss = $winner->open_time; // Access the open_time property on the winner model.

                if (isset($bitopenss[4])) {
                    $digit1 = $bitopenss[4];
                    $jodibb = $digit1 . $digit;

                    $this->NewJodiClose($id, $jodibb, $winner->id, $request->time_id);
                    $this->NewRedJodiClose($id, $jodibb, $winner->id, $request->time_id);
                    $this->NewRedJodiFamilyClose($id, $jodibb, $winner->id, $request->time_id);
                    $this->NewJodiFamilyClose($id, $jodibb, $winner->id, $request->time_id);
                } else {
                    // Handle the case where open_time does not have the expected length
                    // Log a message or take appropriate action
                }
            }
        } else {
            $bitopenss = $winner->open_time;
            $digit1 = $bitopenss[4];
            $jodibb = $digit1 . $digit;
            // Call the respective methods with the winner ID
            $this->NewSingleDigitClose($id, $digit, $winner->id, $request->time_id);
            $this->NewSinglePannaClose($id, $patti, $winner->id, $request->time_id);
            $this->NewDoublePannaClose($id, $patti, $winner->id, $request->time_id);
            $this->NewTripplePannaClose($id, $patti, $winner->id, $request->time_id);
            $this->NewPannaSpClose($id, $patti, $winner->id, $request->time_id);
            $this->NewPannaDpClose($id, $patti, $winner->id, $request->time_id);
            $this->NewPannaFamilyClose($id, $patti, $winner->id, $request->time_id);
            $this->NewCyclePattiClose($id, $patti, $winner->id, $request->time_id);
            $this->NewFullSangamClose($id, $patti, $patti_open, $winner->id, $request->time_id);



            $this->NewHalfSangamOpen($id, $patti_open, $digit, $winner->id, $request->time_id);
            $this->NewHalfSangamClose($id, $patti, $single_open, $winner->id, $request->time_id);

            $this->NewJodiClose($id, $jodibb, $winner->id, $request->time_id);
            $this->NewRedJodiClose($id, $jodibb, $winner->id, $request->time_id);
            $this->NewJodiFamilyClose($id, $jodibb, $winner->id, $request->time_id);
        }

        // Send a notification to the user
        $postcust = $this->user->whereId($user_id)->first();
        $relation = ['storesDay'];

        $stt = $this->store->with($relation)->whereId($id)->first();
        $user_id = 3;
        $storesDay = $stt->storesDay; // storesDay is likely a collection

        // If you expect only one storesDay, you can use first() to get the first record
        $storeDay = $storesDay->first(); // This assumes you want the first record

        // Now, access the close_time safely
        $name = $postcust->name . (isset($storeDay) ? timeShow($storeDay->close_time) : '');
        // $name = $postcust->name . (isset($stt->storesDay) ? timeShow($stt->storesDay->close_time) : '');
        $body = $winner->open_time . ' ' . $request->result;
        $title = $stt->name . " Close";
        $this->notification($title, $body, $user_id);

        DB::commit();
        return $this->sendResponse(true, 'Stores Game Schedule successfully.');
        // } catch (\Exception $ex) {
        //     DB::rollback();
        //     
            // return $this->sendError($ex, 'Error');
        // }
    }


    public function addCloseReverseResult($id)
    {
        try {
            $relation = ['storesDay', 'Stores'];
            $details['store'] = $this->storeGame->whereId($id)->first();
            #render view
            return $this->sendResponse($details, 'Stores Game Schedule successfully.');
        } catch (\Exception $ex) {
            
            return $this->sendError($ex, 'Error');
        }
    }
    public function postCloseReverseResult(Request $request, $id)
    {



        DB::beginTransaction();

        //   try{
        $check = $this->gameWinner->where('stores_id', $id)
            ->where('time_id', $request->time_id)->where('created_at', 'like', currentDate() . '%')->first();

        $data = [
            'stores_id'        => $id,
            'game_id'          => 1,
            'game_group_id'    => 1,
            'day_id'           => 2,
            'bit_place'        => 00000,
            'close_time'        => $request->result,
            'user_id'          => Auth::user()->id,
            'time_id'          => $request->time_id,
        ];
        #store the newly creted resoures
        if ($check) {
            $this->gameWinner->whereId($check->id)->update($data);
            $winer = $check;
        } else {
            $winer = $this->gameWinner->create($data);
        }

        $userwinnercheck = NewUserWinnersAmount::where('game_winners_id', $check->id)->where('type', '=', 'open')->where('game_id', 2)->orWhere('game_id', 9)->get();



        if (count($userwinnercheck) > 0) {
            foreach ($userwinnercheck as $vv) {

                debitReverseAmount($vv->user_id, $vv->amount);

                $remark = " Result Reverse  By Admin " . $vv->amount;
                PaymentRemark($vv->user_id, $remark, $vv->amount);

                NewUserWinnersAmount::whereId($vv->id)->delete();
            }
        }

        $userwinnercheck = NewUserWinnersAmount::where('game_winners_id', $check->id)->where('type', '=', 'close')->get();
        if (count($userwinnercheck) > 0) {
            foreach ($userwinnercheck as $vv) {

                debitReverseAmount($vv->user_id, $vv->amount);
                $remark = " Result Reverse  By Admin " . $vv->amount;
                PaymentRemark($vv->user_id, $remark, $vv->amount);
                NewUserWinnersAmount::whereId($vv->id)->delete();
            }
        }

        $bit_open = (str_replace('-', '', $request->result));
        $bit_open_open = (str_replace('-', '', $winer->open_time));

        $patti = $bit_open[1] . $bit_open[2] . $bit_open[3];
        $patti_open = $bit_open_open[0] . $bit_open_open[1] . $bit_open_open[2];
        $digit = $bit_open[0];

        $single_open = $bit_open_open[3];

        $this->NewSingleDigitClose($id, $digit, $winer->id, $request->time_id);
        $this->NewSinglePannaClose($id, $patti, $winer->id, $request->time_id);
        $this->NewDoublePannaClose($id, $patti, $winer->id, $request->time_id);
        $this->NewTripplePannaClose($id, $patti, $winer->id, $request->time_id);
        $this->NewPannaSpClose($id, $patti, $winer->id, $request->time_id);
        $this->NewPannaDpClose($id, $patti, $winer->id, $request->time_id);
        $this->NewPannaFamilyClose($id, $patti, $winer->id, $request->time_id);
        $this->NewCyclePattiClose($id, $patti, $winer->id, $request->time_id);
        $this->NewFullSangamClose($id, $patti, $patti_open, $winer->id, $request->time_id);


        $this->NewHalfSangamOpen($id, $patti_open, $digit, $winer->id, $request->time_id);
        $this->NewHalfSangamClose($id, $patti, $single_open, $winer->id, $request->time_id);
        if ($check) {
            $bitopenss = $check->open_time;
            $digit1 = $bitopenss[4];
            $jodibb = $digit1 . '' . $digit;
            //   dd( $jodibb);
            $this->NewJodiClose($id, $jodibb, $winer->id, $request->time_id);
            $this->NewJodiFamilyClose($id, $jodibb, $winer->id, $request->time_id);
            $this->NewRedJodiClose($id, $jodibb, $winer->id, $request->time_id);
            $this->NewRedJodiFamilyClose($id, $jodibb, $winer->id, $request->time_id);
        }
        $postcust = $this->user->whereId(1)->first();
        $relation = ['storesDay'];
        //  $stt = $this->store->with($relation)->whereId($id)->first();
        //         $user_id = 3;
        //         $name = $postcust->name;
        //         $body = $request->result.''.isset( $stt->storesDay) ? timeShow( $stt->storesDay->close_time) : '';
        //         $title = $stt->name." Open";
        //         $this->notification($title,$body,$user_id);
        //   dd($digit);
        DB::commit();
        return $this->sendResponse(true, 'Stores Game Schedule successfully.');
        //  }catch(\Exception $ex)
        // {
        //      DB::rollback();
        //     
            // return $this->sendError($ex, 'Error');
        // }
    }

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

        DB::beginTransaction();
        try {
            #validate the request

            $validate = Validator::make($request->all(), [
                'stores_id'         => 'required'
            ]);
            if ($validate->fails()) {


                return $this->sendError($validate->messages(), 'Please Fill Mandatory field');
            }

            #update data


            #get the requested data in Data variable

            $data = [
                'stores_id'        => $request->stores_id,
                'game_id'          => 1,
                'game_group_id'    => 1,
                'day_id'           => 2,
                'bit_place'        => 00000,
                'open_time'        => $request->bit_open,
                'close_time'       => isset($request->bit_close) ? $request->bit_close : 'xxx-x',
                'user_id'          => Auth::user()->id,
            ];
            #store the newly creted resoures
            $winer = $this->gameWinner->create($data);
            $request['game_winners_id'] = $winer->id;
            $bit_open = (str_replace('-', '', $request->bit_open));
            $patti = $bit_open[0] . $bit_open[1] . $bit_open[2];
            $digit = $bit_open[3];
            $singlepatti = $this->SingleDigitOpen($request, $digit);

            $sp = $this->checkSp();
            $dp = $this->checkDp();
            $spdp = '';
            //  $bidonmotorcombination = bidOnMotorCombination($bit_open);
            // $jodiamount = $this->jodiCompare($request,$digit);
            if (!in_array($patti, $this->checkTripple())) {

                if (in_array($patti, $sp)) {

                    $spdpamount = $this->singlePattiOpen($request, $patti);
                    $bidonamount = $this->SpbedOnMotorOpen($request, $bidonmotorcombination);
                }
                if (in_array($patti, $dp)) {

                    $spdpamount = $this->doublePattiOpen($request, $patti);
                    $bidonamount = $this->DpbedOnMotorOpen($request, $bidonmotorcombination);
                }
            } else {
                $spdpamount = $this->triplePattiOpen($request, $patti);
                $bidonamount = 0;
            }



            if ($request->bit_close) {
                $bit_close = (str_replace('-', '', $request->bit_close));
                $patti_close = $bit_close[0] . $bit_close[1] . $bit_close[2];
                $digit_close = $bit_close[3];
                $jodi_digit = $digit . $digit_close;
                $singlepatti_close = $this->SingleDigitClose($request, $digit_close);
                $jodiamount = $this->jodiBitwinner($request, $jodi_digit);
                if (!in_array($patti_close, $this->checkTripple())) {

                    if (in_array($patti_close, $sp)) {

                        $Closespdpamount = $this->singlePattiClose($request, $patti_close);
                        $Closebidonamount = $this->SpbedOnMotorClose($request, $bidonmotorcombination);
                    }
                    if (in_array($patti_close, $dp)) {

                        $Closespdpamount = $this->doublePattiClose($request, $patti_close);
                        $Closebidonamount = $this->DpbedOnMotorClose($request, $bidonmotorcombination);
                    }
                } else {
                    $Closespdpamount = $this->triplePattiClose($request, $patti_close);
                    $Closebidonamount = 0;
                }
            }

            DB::commit();
            // #return redirect back to view
            return $this->sendResponse(true, 'Stores Game Schedule successfully.');
        } catch (\Exception $ex) {
            DB::rollback();
            toastr()->success($ex->getMessage());
            
            return $this->sendError($ex, 'Error');
        }
    }

    /**
     * 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 {
            # Fetch records
            $details['store'] = $this->store->get();
            $details['gameGroup'] = $this->gameGroup->get();
            $details['game']      = $this->game->get();
            $details['day'] = $this->day->get();
            $details['detail'] = $this->gameWinner->where('id', $id)->first();

            #return to view
            return $this->sendResponse($details, 'Stores Game Schedule successfully.');
        } catch (\Exception $ex) {
            
            return $this->sendError($ex, 'Error');
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        DB::beginTransaction();
        try {
            #validate the request

            $validate = Validator::make($request->all(), [
                'stores_id'         => 'required'
            ]);
            if ($validate->fails()) {


                return $this->sendError($validate->messages(), 'Please Fill Mandatory field');
            }

            #update data


            #get the requested data in Data variable

            $data = [
                'stores_id'        => $request->stores_id,
                'game_id'          => 1,
                'game_group_id'    => 1,
                'day_id'           => 2,
                'bit_place'        => 00000,
                'open_time'        => $request->bit_open,
                'close_time'       => isset($request->bit_close) ? $request->bit_close : 'xxx-x',
                'user_id'          => Auth::user()->id,
            ];
            #store the newly creted resoures
            $winer = $this->gameWinner->whereId($id)->update($data);
            $request['game_winners_id'] = $id;
            $bit_open = (str_replace('-', '', $request->bit_open));
            $patti = $bit_open[0] . $bit_open[1] . $bit_open[2];
            $digit = $bit_open[3];
            $singlepatti = $this->SingleDigitOpen($request, $digit);

            $sp = $this->checkSp();
            $dp = $this->checkDp();
            $spdp = '';
            $bidonmotorcombination = bidOnMotorCombination($bit_open);
            // $jodiamount = $this->jodiCompare($request,$digit);
            if (!in_array($patti, $this->checkTripple())) {

                if (in_array($patti, $sp)) {

                    $spdpamount = $this->singlePattiOpen($request, $patti);
                    $bidonamount = $this->SpbedOnMotorOpen($request, $bidonmotorcombination);
                }
                if (in_array($patti, $dp)) {

                    $spdpamount = $this->doublePattiOpen($request, $patti);
                    $bidonamount = $this->DpbedOnMotorOpen($request, $bidonmotorcombination);
                }
            } else {
                $spdpamount = $this->triplePattiOpen($request, $patti);
                $bidonamount = 0;
            }
            if ($request->bit_close) {
                $bit_close = (str_replace('-', '', $request->bit_close));
                $patti_close = $bit_close[0] . $bit_close[1] . $bit_close[2];
                $digit_close = $bit_close[3];
                $jodi_digit = $digit . $digit_close;
                $singlepatti_close = $this->SingleDigitClose($request, $digit_close);
                $jodiamount = $this->jodiBitwinner($request, $jodi_digit);
                if (!in_array($patti_close, $this->checkTripple())) {

                    if (in_array($patti_close, $sp)) {

                        $Closespdpamount = $this->singlePattiClose($request, $patti_close);
                        $Closebidonamount = $this->SpbedOnMotorClose($request, $bidonmotorcombination);
                    }
                    if (in_array($patti_close, $dp)) {

                        $Closespdpamount = $this->doublePattiClose($request, $patti_close);
                        $Closebidonamount = $this->DpbedOnMotorClose($request, $bidonmotorcombination);
                    }
                } else {
                    $Closespdpamount = $this->triplePattiClose($request, $patti_close);
                    $Closebidonamount = 0;
                }
            }

            DB::commit();
            // #return redirect back to view
            return $this->sendResponse(true, 'Stores Game Schedule successfully.');
        } catch (\Exception $ex) {
            DB::rollback();
            toastr()->success($ex->getMessage());
            
            return $this->sendError($ex, 'Error');
        }
    }

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

        $details['bitamount'] = $this->bit->where('stores_id', $request->stores_id)->where('type', $request->open)->sum('amount');
        $details['p_r_amount'] = 0;
        $details['winnamount'] = 0;
        #start open 
        $bit_open = (str_replace('-', '', $request->bit_open));

        $patti = $bit_open[0] . $bit_open[1] . $bit_open[2];
        $digit = $bit_open[3];
        $singlepatti = $this->SingleDigitCompare($request, $digit);

        $sp = $this->checkSp();
        $dp = $this->checkDp();
        $spdp = '';
        $bidonmotorcombination = bidOnMotorCombination($bit_open);
        // $jodiamount = $this->jodiCompare($request,$digit);
        if (!in_array($patti, $this->checkTripple())) {

            if (in_array($patti, $sp)) {

                $spdpamount = $this->sPDpCompare($request, $patti);
                $bidonamount = $this->bidOnSpCompare($request, $bidonmotorcombination);
            }
            if (in_array($patti, $dp)) {

                $spdpamount = $this->dPspCompare($request, $patti);
                $bidonamount = $this->bidOnDpCompare($request, $bidonmotorcombination);
            }
        } else {
            $spdpamount = $this->tripleCompare($request, $patti);
            $bidonamount = 0;
        }
        $details['winnamount'] = $singlepatti + $spdpamount + $bidonamount;
        $details['p_r_amount'] = $details['bitamount'] - $details['winnamount'];
        #endopen

        #start close 
        if ($request->bit_close) {
            $bit_close = (str_replace('-', '', $request->bit_close));
            $patti_close = $bit_close[0] . $bit_close[1] . $bit_close[2];
            $digit_close = $bit_close[3];
            $jodi_digit = $digit . $digit_close;
            $singlepatti_close = $this->SingleDigitCloseCompare($request, $digit_close);
            $jodiamount = $this->jodiCompare($request, $jodi_digit);
            if (!in_array($patti_close, $this->checkTripple())) {

                if (in_array($patti_close, $sp)) {

                    $Closespdpamount = $this->sPDpCloseCompare($request, $patti_close);
                    $Closebidonamount = $this->bidOnSpCloseCompare($request, $bidonmotorcombination);
                }
                if (in_array($patti_close, $dp)) {

                    $Closespdpamount = $this->dPspCloseCompare($request, $patti_close);
                    $Closebidonamount = $this->bidOnDpCloseCompare($request, $bidonmotorcombination);
                }
            } else {
                $Closespdpamount = $this->tripleClosedCompare($request, $patti_close);
                $Closebidonamount = 0;
            }
            $details['bitamountclose'] = $this->bit->where('stores_id', $request->stores_id)->where('type', '=', 'close')->sum('amount');
            $details['winnamountclose'] = $singlepatti_close + $jodiamount + $Closespdpamount + $Closebidonamount;
            $details['p_r_amountclose'] = $details['bitamountclose'] - $details['winnamountclose'];
            #end close

        }
        #render view
            return $this->sendResponse($details, 'Stores Game Schedule successfully.');
    }
}
