<?php

namespace App\Http\Controllers;

use App\Resolution;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ResController extends Controller
{
    public function one()
    {

        $res1 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE approval_of_final_dividend=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE approval_of_final_dividend=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE approval_of_final_dividend=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE approval_of_final_dividend=0) AS ag_num
              );
                    ");
        $output = array();



        foreach ($res1 as $value){
            $response =  !empty($value->for) ? $value->for : 0;
            array_push($output,$response);
            $response = !empty($value->against) ? $value->against : 0;
            array_push($output,$response);
            $response = 0;
            array_push($output,$response);
        }


        $resolution = Resolution::where('blade_file','one')->first();

        return view('graph.one',compact('output','resolution'));
    }

    public function two()
    {
        $res2 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE ebenezer_twum_asante=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE ebenezer_twum_asante=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE ebenezer_twum_asante=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE ebenezer_twum_asante=0) AS ag_num
              );");

        $output2 = array();


        foreach ($res2 as $value){

            $response2 =  !empty($value->for) ? $value->for : 0;
            array_push($output2,$response2);
            $response2 =  !empty($value->against) ? $value->against : 0;
            array_push($output2,$response2);

            $response2 = 0;
            array_push($output2,$response2);
        }


        $resolution = Resolution::where('blade_file','two')->first();

        return view('graph.two',compact('output2','resolution'));


    }

    public function three()
    {

        $res3 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE modupe_agbolade_kadri=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE modupe_agbolade_kadri=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE modupe_agbolade_kadri=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE modupe_agbolade_kadri=0) AS ag_num
              );");

        $output3 = array();

        foreach ($res3 as $value){
            $response3 = !empty($value->for) ? $value->for : 0;
            array_push($output3,$response3);
            $response3 =  !empty($value->against) ? $value->against : 0;
            array_push($output3,$response3);

            $response3 = 0;
            array_push($output3,$response3);
        }
        $resolution = Resolution::where('blade_file','three')->first();

        return view('graph.three',compact('output3','resolution'));

    }

    public function four()
    {

        $res4 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE fatima_daniels=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE fatima_daniels=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE fatima_daniels=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE fatima_daniels=0) AS ag_num
              );");

        $output4 = array();

        foreach ($res4 as $value){
            $response4 =  !empty($value->for) ? $value->for : 0;
            array_push($output4,$response4);
            $response4 = !empty($value->against) ? $value->against : 0;
            array_push($output4,$response4);
            $response4 = 0;
            array_push($output4,$response4);
        }


        $resolution = Resolution::where('blade_file','four')->first();

        return view('graph.four',compact('output4','resolution'));


    }

    public function five(){

        $res5 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE approval_of_directors_remuneration=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE approval_of_directors_remuneration=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE approval_of_directors_remuneration=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE approval_of_directors_remuneration=0) AS ag_num
              );");
        $output5 = array();

        foreach ($res5 as $value){
            $response5 =  !empty($value->for) ? $value->for : 0;
            array_push($output5,$response5);
            $response5 = !empty($value->against) ? $value->against : 0;
            array_push($output5,$response5);
            $response5 = 0;
            array_push($output5,$response5);
        }

        $resolution = Resolution::where('blade_file','five')->first();

        return view('graph.five',compact('output5','resolution'));

    }

    public function sixth()
    {
        $res6 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE authorization_of_directors=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE authorization_of_directors=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE authorization_of_directors=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE authorization_of_directors=0) AS ag_num
              );");

        $output6 = array();


        foreach ($res6 as $value)
        {

            $response6 =  !empty($value->for) ? $value->for : 0;
            array_push($output6,$response6);
            $response6 =  !empty($value->against) ? $value->against : 0;
            array_push($output6,$response6);
            $response6 = 0;
            array_push($output6,$response6);

        }

        $resolution = Resolution::where('blade_file','six')->first();

        return view('graph.six',compact('output6','resolution'));
    }

    public function seven()
    {
        $res7 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE resolution7=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE resolution7=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE resolution7=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE resolution7=0) AS ag_num
              );");

        $output7 = array();


        foreach ($res7 as $value)
        {

            $response7 =  !empty($value->for) ? $value->for : 0;
            array_push($output7,$response7);
            $response7 =  !empty($value->against) ? $value->against : 0;
            array_push($output7,$response7);
            $response7 = 0;
            array_push($output7,$response7);

        }

        $resolution = Resolution::where('blade_file','seven')->first();

        return view('graph.seven',compact('output7','resolution'));
    }

    public function eight()
    {
        $res8 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE resolution8=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE resolution8=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE resolution8=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE resolution8=0) AS ag_num
              );");

        $output8 = array();


        foreach ($res8 as $value)
        {

            $response8 =  !empty($value->for) ? $value->for : 0;
            array_push($output8,$response8);
            $response8 =  !empty($value->against) ? $value->against : 0;
            array_push($output8,$response8);
            $response8 = 0;
            array_push($output8,$response8);

        }

        $resolution = Resolution::where('blade_file','eight')->first();

        return view('graph.eight',compact('output8','resolution'));
    }

    public function nine()
    {
        $res9 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE resolution9=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE resolution9=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE resolution9=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE resolution9=0) AS ag_num
              );");

        $output9 = array();


        foreach ($res9 as $value)
        {

            $response9 =  !empty($value->for) ? $value->for : 0;
            array_push($output9,$response9);
            $response9 =  !empty($value->against) ? $value->against : 0;
            array_push($output9,$response9);
            $response9 = 0;
            array_push($output9,$response9);

        }

        $resolution = Resolution::where('blade_file','nine')->first();

        return view('graph.nine',compact('output9','resolution'));
    }

    public function ten()
    {
        $res10 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE resolution10=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE resolution10=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE resolution10=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE resolution10=0) AS ag_num
              );");

        $output10 = array();


        foreach ($res10 as $value)
        {

            $response10 =  !empty($value->for) ? $value->for : 0;
            array_push($output10,$response10);
            $response10 =  !empty($value->against) ? $value->against : 0;
            array_push($output10,$response10);
            $response10 = 0;
            array_push($output10,$response10);

        }

        $resolution = Resolution::where('blade_file','ten')->first();

        return view('graph.ten',compact('output10','resolution'));
    }

    public function eleven()
    {
        $res11 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE resolution11=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE resolution11=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE resolution11=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE resolution11=0) AS ag_num
              );");

        $output11 = array();


        foreach ($res11 as $value)
        {

            $response11 =  !empty($value->for) ? $value->for : 0;
            array_push($output11,$response11);
            $response11 =  !empty($value->against) ? $value->against : 0;
            array_push($output11,$response11);
            $response11 = 0;
            array_push($output11,$response11);

        }

        $resolution = Resolution::where('blade_file','eleven')->first();

        return view('graph.eleven',compact('output11','resolution'));
    }

    public function twelve()
    {
        $res12 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE resolution12=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE resolution12=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE resolution12=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE resolution12=0) AS ag_num
              );");

        $output12 = array();


        foreach ($res12 as $value)
        {

            $response12 =  !empty($value->for) ? $value->for : 0;
            array_push($output12,$response12);
            $response12 =  !empty($value->against) ? $value->against : 0;
            array_push($output12,$response12);
            $response12 = 0;
            array_push($output12,$response12);

        }

        $resolution = Resolution::where('blade_file','twelve')->first();

        return view('graph.twelve',compact('output12','resolution'));
    }

    public function thirteen()
    {
        $res13 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE resolution13=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE resolution13=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE resolution13=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE resolution13=0) AS ag_num
              );");

        $output13 = array();


        foreach ($res13 as $value)
        {

            $response13 =  !empty($value->for) ? $value->for : 0;
            array_push($output13,$response13);
            $response13 =  !empty($value->against) ? $value->against : 0;
            array_push($output13,$response13);
            $response13 = 0;
            array_push($output13,$response13);

        }

        $resolution = Resolution::where('blade_file','thirteen')->first();

        return view('graph.thirteen',compact('output13','resolution'));
    }

    public function fourteen()
    {
        $res14 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE resolution14=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE resolution14=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE resolution14=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE resolution14=0) AS ag_num
              );");

        $output14 = array();


        foreach ($res14 as $value)
        {

            $response14 =  !empty($value->for) ? $value->for : 0;
            array_push($output14,$response14);
            $response14 =  !empty($value->against) ? $value->against : 0;
            array_push($output14,$response14);
            $response14 = 0;
            array_push($output14,$response14);

        }

        $resolution = Resolution::where('blade_file','fourteen')->first();

        return view('graph.fourteen',compact('output14','resolution'));
    }

    public function fifteen()
    {
        $res15 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE resolution15=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE resolution15=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE resolution15=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE resolution15=0) AS ag_num
              );");

        $output15 = array();


        foreach ($res15 as $value)
        {

            $response15 =  !empty($value->for) ? $value->for : 0;
            array_push($output15,$response15);
            $response15 =  !empty($value->against) ? $value->against : 0;
            array_push($output15,$response15);
            $response15 = 0;
            array_push($output15,$response15);

        }

        $resolution = Resolution::where('blade_file','fifteen')->first();

        return view('graph.fifteen',compact('output15','resolution'));
    }

    public function sixteen()
    {
        $res16 = DB::select(
            "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE resolution16=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE resolution16=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE resolution16=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE resolution16=0) AS ag_num
              );");

        $output16 = array();


        foreach ($res16 as $value)
        {

            $response16 =  !empty($value->for) ? $value->for : 0;
            array_push($output16,$response16);
            $response16 =  !empty($value->against) ? $value->against : 0;
            array_push($output16,$response16);
            $response16 = 0;
            array_push($output16,$response16);

        }

        $resolution = Resolution::where('blade_file','sixteen')->first();

        return view('graph.sixteen',compact('output16','resolution'));
    }

    public function updateResolution(Request $request)
    {
        $resolution = $request->resolution;

        $resetResolutions=Resolution::query()->where('status','=',1)->update(array('status' => 0));

        try{
            $resolutionCheck = Resolution::find($resolution);

            if($resolutionCheck) {
                $resolutionCheck->status = 1;
                $resolutionCheck->save();
            }

            return $resolutionCheck->title." set as active resolution";
        }
        catch (Exception $e){
            return $e;
        }
    }


    public function closeResolution(Request $request)
    {
        try{
            $resetResolutions=Resolution::query()->where('status','=',1)->update(array('status' => 0));

            return "Resolution closed";
        }
        catch (Exception $e){
            return $e;
        }
    }
}
