<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\MassDestroyFinanceRequest;
use App\Http\Requests\StoreFinanceRequest;
use App\Http\Requests\UpdateFinanceRequest;
use App\Finance;
use Gate;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

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

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

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



    public function show(Finance $finance)
    {
        abort_if(Gate::denies('finance_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        return view('admin.finances.show', compact('finance'));
    }

    public function destroy(Finance $finance)
    {
        abort_if(Gate::denies('finance_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $finance->delete();

        return back();

    }

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

        return response(null, Response::HTTP_NO_CONTENT);

    }

    public function fetch($id){
        $finance = Finance::find($id);
        $data= [];
        $customerurl = $finance->api_url+"/customer";
        $collectorurl = $finance->api_url+"/collector";
        $customer = UtilContoller::getAPIData($customerurl,$data);
        $collector = UtilContoller::getAPIData($collectorurl,$data);

        $oldcustomer = Customer::where('finance_id',$id);
        $oldcustomer->deleteall();
        $customer->saveall();

        $oldcollector = Collector::where('finance_id',$id);
        $oldcollector->deleteall();
        $collector->saveall();
    }

    public function fetchAll(){

        $finance = Finance::get();
        foreach($finance as $fin){
            $this->fetch($fin->id);
        }
        
        
    }

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

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

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

        
         ->addColumn('action', function($registered_org){
            
              
                   return '<a target="_blank" href="'.route("admin.finances.show", $registered_org->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'));
    }

}
