<?php

namespace App\Http\Controllers\Admin;

# Models
use App\Models\Bit;
use App\Models\Store;
use App\Models\SingleDigitBit;
use App\Exports\singleDigitExport;

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

class SingleDigitController extends Controller
{
      # Define Base view
    protected $view = 'admin.single-digit.';
     #Bind Bit model
    protected $bit;

     #Bind SingleDigitBit model
    protected $singleDigitBit;

    #Bind Store model
    protected $store;


    /**
      * Defining the default constructor for controller
      *
      */
    public function __construct(
                                Bit $bit,
                                Store $store,
                                SingleDigitBit $singleDigitBit
                            )
    {
        $this->bit                  = $bit;
        $this->store                = $store;
        $this->singleDigitBit       = $singleDigitBit;
        
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

         try
        {
             $input = $request->all();  
            #relationship
            $search_date = '';
            if(isset($input['search_date'])){
                $search_date = $input['search_date'];
                 
            }else{
                $search_date = currentDate();
            }
            $relation = ['bit' => function ($query) use ($search_date) {$query->where('created_at', 'like', $search_date.'%');},'bit.game','storeGame','bit.username'];
            #get all paymentUser resources
             

            $details['lists'] = $this->store->with($relation)->paginate(10);
             $details['store'] = $this->store->get();
            #render view
            return view($this->view.'index',$details);
        }catch(\Exception $ex)
        {
            return back()->with('error', $ex->getMessage());
        }
    }
    public function downloadExcel(Request $request)
    {

         try
        {
            
           $input = $request->all();
             $search_date = '';
            if(isset($input['excel_date'])){
                $search_date = $input['excel_date'];
                $stores_id = $input['stores_id'];
                $type = $input['type'];
                
            }else{
                $search_date = currentDate();
            }
           return Excel::download(new singleDigitExport($search_date,$stores_id,$type), 'single_digit.xlsx');
        }catch(\Exception $ex)
        {
            return back()->with('error', $ex->getMessage());
        }
    }
    /**
     * Display a Summary listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function Summar()
    {
         try
        {
              
            #relationship
            $relation = ['SingleDigitBit','store'];
            #get all paymentUser resources
            $bit_id = $this->bit->with($relation)->where('created_at','!=',null)->pluck('id')->unique( array('stores_id', 'open_time','game_id') );
           
            $details['lists'] = $this->bit->with($relation)->where('created_at','!=',null)->whereIn('id',$bit_id)->paginate(10);
              
           
            #render view
            return view($this->view.'summary',$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()
    {
        //
    }

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