<?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\Customer;
use App\Finance;
use Gate;
use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;
use Symfony\Component\HttpFoundation\Response;

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

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

        return view('admin.customers.index', compact('customers', 'finances'));
    }
 
    public function customerSearch(Request $request)
    {
        
        $finance =  $request['orgCode'];
       

        $registered_customer = \App\Customer::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_customer)
         ->addIndexColumn()
         
         ->addColumn('finance', function($registered_customer){
             $finance = \App\Finance::where('orgCode',$registered_customer->orgCode)->first();
            return $finance->name;
   
            })
        
         ->addColumn('action', function($registered_customer){
            
              
                   return '<a target="_blank" href="'.route("admin.customers.show", $registered_customer->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 create()
    {
        abort_if(Gate::denies('customer_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $finances = Finance::all()->pluck('finance_name', 'id');

        return view('admin.customers.create', compact('finances'));
    }

    public function store(Request $request)
    {
        $customer = Customer::create($request->all());
        //dd($customer);
       
        return redirect()->route('admin.customers.index');

    }

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

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

    public function update(Request $request, Subject $subject)
    {
        $subject->update($request->all());

        return redirect()->route('admin.subjects.index');

    }

    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);

    }
}