<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\MassDestroyCustomerRequest;
use App\Http\Requests\StoreCustomerRequest;
use App\Http\Requests\UpdateCustomerRequest;
use App\Collector;
use App\Finance;
use Gate;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class CollectorController extends Controller
{
    public function index()
    {
        abort_if(Gate::denies('collector_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $collectors = Collector::all();
        $finances = Finance::all()->pluck('name', 'orgCode');

        return view('admin.collectors.index', compact('collectors', 'finances'));
    }

    public function collectorSearch(Request $request)
    {
        
        $finance =  $request['orgCode'];
       

        $registered_collector = \App\Collector::select('*')->latest()->where(function($query) use ($finance)  {
            
            if(isset($finance)) {
                $query->where('collectorinfo.orgCode',"=" ,$finance);
            }
            

         })
         ->select('collectorinfo.*')->latest("collectorinfo.created_at");
         $reg = Datatables::of($registered_collector)
         ->addIndexColumn()
         
         ->addColumn('finance', function($registered_collector){
             $finance = \App\Finance::where('orgCode',$registered_collector->orgCode)->first();
            return $finance->name;
   
            })
        
         ->addColumn('action', function($registered_collector){
            
              
                   return '<a target="_blank" href="'.route("admin.collectors.show", $registered_collector->id).'">
                            
                   <span class="btn btn-primary  mt-2">Show</span>
               </a>
              
               ';
               
              
            //    <button onclick=approveform('.$registered_student->id.') class="btn btn-success mt-2 appbtn" style="width:60px" >Verify </button>
            //    <button onclick=rejectform('.$registered_student->id.') class="btn btn-danger mt-2 1-reject" style="width:60px" >Reject </button>

          
                   })
                   ->rawColumns(['action','finance'])

      ->make();
         //dd($reg);
               return $reg;
       
          //  dd($registrations);
       // return view('student::studentreports', compact('registrations','subjects'));
    }
 
        public function show(Subject $subject)
    {
        abort_if(Gate::denies('subject_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        return view('admin.subjects.show', compact('subject'));
    }

    public function destroy(Subject $subject)
    {
        abort_if(Gate::denies('subject_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $subject->delete();

        return back();

    }

    public function massDestroy(MassDestroySubjectRequest $request)
    {
        Subject::whereIn('id', request('ids'))->delete();

        return response(null, Response::HTTP_NO_CONTENT);

    }
}