<?php

namespace App\Http\Controllers\Admin;

# Models
use App\Models\Bit;
use App\Models\Store;
use App\Models\DoublePannaBit;
use App\Exports\jodiExport;

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


class DoublePannaController extends Controller
{
   # Define Base view
    protected $view = 'admin.doublepanna.';
     #Bind Bit model
    protected $bit;

     #Bind DoublePannaBit model
    protected $doublePannaBit;

    #Bind Store model
    protected $store;


    /**
      * Defining the default constructor for controller
      *
      */
    public function __construct(
                                Bit $bit,
                                Store $store,
                                DoublePannaBit $doublePannaBit
                            )
    {
        $this->bit  = $bit;
        $this->store                = $store;
        $this->doublePannaBit       = $doublePannaBit;
        
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //  try
        // {
             $input = $request->all();
              
              $details['store'] = $this->store->get();
            #relationship
              $search_date = '';
            if(isset($input['search_date'])){
                $search_date = $input['search_date'];
                 
            }else{
                $search_date = currentDate();
            }


            $relation = ['dpannabitopen'=> function ($query) use ($search_date) {$query->where('created_at', 'like', $search_date.'%');},'spannbitclose'=> function ($query) use ($search_date) {$query->where('created_at', 'like', $search_date.'%');},'dpannabitopen.doublePannai','dpannbitclose.doublePannai'];
            #get all paymentUser resources
           
            // $details['lists'] = $this->jodiBit->with($relation)->where('created_at','!=',null)->paginate(10);
            $details['lists'] = $this->store->with($relation)->paginate(10);
        
              
            #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 jodiExport($search_date,$stores_id,$type), 'jodi_bit.xlsx');
        }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)
    {
        //
    }
}
