<?php

namespace App\Http\Controllers\Admin;

# Models
use App\Models\ReferalCode;
use App\Models\Users;

# Other Classes
use DB;
use Validator;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ExportBanner;

class RefrenceController extends Controller
{
    # Define Base view
    protected $view = 'admin.refrence.';

    #Bind Banner model
    protected $referalCode;

    /**
      * Defining the default constructor for controller
      *
      */

    public function __construct(
        ReferalCode $referalCode,
         Users $user
                            )
    {
        
        $this->user  = $user;
        $this->referalCode  = $referalCode;
    }

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

            $input = $request->all();

            #get all paymentUser resources
            $relation =['refelCodeHis.user'];
            $banners = $this->referalCode->with($relation)->where('created_at','!=',null);
            
            $search_name   = '';
            $search_status = '';
           
            if(isset($input['search_name']) || isset($input['search_status']))
            {

                if(isset($input['search_name']))
                {
                     $search_name = $input['search_name'];
                  $banners->where('user_id','LIKE','%'.$search_name.'%');
                }
                 if(isset($input['search_status'])){
                    $search_status = $input['search_status'];
                    $banners->where('status', $search_status);
                }
            }  
            $details['lists'] = $banners->paginate(10);
            

          
            #render view
            return view($this->view.'index',$details);
        }catch(\Exception $ex)
        {
            toastr()->success($ex->getMessage());
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
         try{
            
            $details['user'] = $this->user->where('status',1)->get();
           
            #render view
            return view($this->view.'create',$details);
         }catch(\Exception $ex)
        {
            toastr()->success($ex->getMessage());
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
         try{
             #validate the request
            $validate = Validator::make($request->all(),[
                    'user_id'          => 'required',
            
            ]);
            
           
            if($validate->fails()){
               toastr()->error($validate->messages());
                return redirect()->back()->withInput()->with('error', $validate->messages())->withErrors($validate->messages());
            }

            $data = [
                'user_id'           => $request->user_id,
                'code'              => "SARA" . $request->user_id,
               
            ];
        
            $this->referalCode->create($data);
            #return redirect back to view
             toastr()->success('Added Successfully');
             
            return redirect('/admin/refrence')->with('message','refrence No Added Successfully');
         }catch(\Exception $ex)
        {
            toastr()->success($ex->getMessage());
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * 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['detail'] = $this->referalCode->where('id', $id)->first();
            
            #return to view
            return view($this->view.'edit', $details);
         }catch(\Exception $ex)
        {
            toastr()->success($ex->getMessage());
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        try{
             #validate the request
            $validate = Validator::make($request->all(),[
                'name'          => 'required',
              
            ]);
            
            if($validate->fails()){
                 toastr()->error($validate->messages());
                return redirect()->back()->withInput()->with('error', $validate->messages())->withErrors($validate->messages());
            } 
            $banners = $this->referalCode->whereId($id)->first();
            $banners->name          = $request->name;
            $banners->update();
            #return redirect back to view
            toastr()->success('Updated Successfully');
           return redirect('admin/refrence')->with('message', 'Banner Updated Successfully!');
        }catch(\Exception $ex)
        {
            toastr()->success($ex->getMessage());
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $caste = $this->referalCode::find($id);
        $caste ->delete();
         toastr()->success('You are successfully Deleted');
       return redirect('admin/refrence')->with('message', 'Banner Deleted Successfully!');
    }
     /**
     * change status of specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function changeStatus(Request $request,$id)
    {
        #get the detail of requested resources
        $banners = $this->referalCode->whereId($request->id)->first();
    
        if($banners->status == 1){
             $status = '0';
        }else{
             $status = '1';
        }        
        #get the status in data variable
        $data = ['status' => $status];

        #update status
        $this->referalCode->whereId($request->id)->update($data);
        $banners = $this->referalCode->whereId($request->id)->first();
        $message = $banners->status ==0 ? 'InActive' : 'Active';
        toastr()->success('You are successfully '.$message);
        return redirect('admin/refrence')->with('message', 'Banner Status Changed Successfully!');
    }
}
