<?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 App\Transaction;
use Gate;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

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

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

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

     

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

        $registered_trans = \App\Transaction::select('*')->where('status','=',0)->latest()->where(function($query) use ($finance)  {
            
            if(isset($finance)) {
                $query->where('maintable.orgCode',"=" ,$finance);
            }
            

         })
         ->select('maintable.*')->latest("maintable.created_at");
         $reg = Datatables::of($registered_trans)
         ->addIndexColumn()
         
         ->addColumn('finance', function($registered_trans){
             $finance = \App\Finance::where('orgCode',$registered_trans->orgCode)->first();
            return $finance->name;
   
            })

            ->addColumn('collector', function($registered_trans){
                $collector = \App\Collector::where('collectorId',$registered_trans->collectorId)->first();
               return $collector->name;
      
               })
        
         ->addColumn('action', function($registered_trans){
            
              
                   return '<a target="_blank" href="'.route("admin.customers.show", $registered_trans->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'));
    }


}