<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use App\Result;
use App\Position;
use Illuminate\Support\Facades\DB;


class ResultController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index2(Request $request)
    {
        //dd()
        // calculatePercentage(votes, positionId) {
		// 	const totalVotes = this.results
		// 		.filter(result => result.position_id === positionId)
		// 		.reduce((total, result) => total + result.votes, 0);

		// 	return ((votes / totalVotes) * 100).toFixed(2);
		// },

        $positions = Position::all();
        
        $results = Result::with('nominee')->get();
        dd($results->toArray());
       

        $pdf = App::make('dompdf.wrapper');
        $pdf->loadView('report.result', compact('results', 'positions'));
        return $pdf->stream();
    }

    public function index($id)
    {
        $results = $this->getResult($id);
        $total_votes = Result::where('election_id', $id)->count();
        $positions = \App\Position::where('election_id', $id)->get();
        $nominees = \App\Nominee::where('election_id', $id)->get();
        $no_votes = \App\Nominee::whereNotIn('id', $results->pluck('nominee_id') )->get();
       

        $pdf = App::make('dompdf.wrapper');
        $pdf->loadView('report.result', compact('results', 'positions','nominees', 'no_votes', 'total_votes'));
        return $pdf->stream();
    }

    private function getResult($id)
    {
        return \App\Result::select(DB::raw('position_id,nominee_id,count(*) as votes'))
                ->groupBy('position_id', 'nominee_id')
                ->where('election_id', $id)
                ->orderBy('votes', 'DESC')
                ->get();
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * 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)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
