<?php

namespace App\Http\Controllers\Admin;

# Models
use App\Models\Bit;
use App\Models\Store;
use App\Models\SinglePattiBit;
use App\Models\SinglePattiPannaBit;
use App\Exports\singlePattiExport;

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

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

     #Bind SinglePattiBit model
    protected $singlePattiBit;

         #Bind SinglePattiPannaBit model
    protected $singlePattiPannaBit;

    #Bind Store model
    protected $store;


    /**
      * Defining the default constructor for controller
      *
      */
    public function __construct(
                                Bit $bit,
                                Store $store,
                                SinglePattiBit $singlePattiBit,
                                SinglePattiPannaBit $singlePattiPannaBit
                            )
    {
        $this->bit  = $bit;
        $this->store                = $store;
        $this->singlePattiBit       = $singlePattiBit;
        $this->singlePattiPannaBit       = $singlePattiPannaBit;
    }
    /**
     * 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();
            }

            #relationship
            $relation = ['singlepatti'=> function ($query) use ($search_date) {$query->where('created_at', 'like', $search_date.'%');},'singlepatti.singlepatti','singlepattiClose'=> function ($query) use ($search_date) {$query->where('created_at', 'like', $search_date.'%');},'singlepattiClose.singlepatti'];
            #get all paymentUser resources
           
            $details['lists'] = $this->store->with($relation)->paginate(10);
              // dd($details['lists'][4]['singlepatti'][0]['singlepatti'][0]['SinglePattiPannaBit']);
            #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 singlePattiExport($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)
    {
        //
    }
}
