<?php /** @noinspection PhpUndefinedMethodInspection */

namespace App\Providers;

use App\category;
use App\Client;
use App\Resolution;
use App\Voters;
use App\Votes;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(100);

        $allResolutions=Resolution::query()->get();

        foreach ($allResolutions AS $resolution){
            $id= $resolution->title;


        }



        view()->composer('*', function ($view) {

            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE approval_of_final_dividend=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE approval_of_final_dividend=3) AS abstain_num
              );
                    ");

            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE ebenezer_twum_asante=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE ebenezer_twum_asante=3) AS abstain_num
              );");

            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE modupe_agbolade_kadri=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE modupe_agbolade_kadri=3) AS abstain_num
              );");

            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE fatima_daniels=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE fatima_daniels=3) AS abstain_num
              );");

            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE approval_of_directors_remuneration=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE approval_of_directors_remuneration=3) AS abstain_num
              );");

            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE authorization_of_directors=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE authorization_of_directors=3) AS abstain_num
              );");

            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE resolution7=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE resolution7=3) AS abstain_num
              );");


            $response = [];
            $output = array();

            $response2 = [];
            $output2 = array();

            $response3 = [];
            $output3 = array();

            $response4 = [];
            $output4 = array();

            $response5 = [];
            $output5 = array();

            $response6 = [];
            $output6 = array();

            $response7 = [];
            $output7 = array();



            foreach ($res1 as $value){
                $response['for_total']     =  $value->for_total;
                $response['against_total'] =  $value->against_total;
                $response['abstain_total'] =  $value->abstain_total;
                $response['for'] =  !empty($value->for) ? $value->for : 0;
                $response['against'] =  !empty($value->against) ? $value->against : 0;
                $response['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output,$response);
            }

            foreach ($res2 as $value){
                $response2['for_total']     =  $value->for_total;
                $response2['against_total'] =  $value->against_total;
                $response2['abstain_total'] =  $value->abstain_total;
                $response2['for'] = !empty($value->for) ? $value->for : 0;
                $response2['against'] = !empty($value->against) ? $value->against : 0;
                $response2['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output2,$response2);
            }

            foreach ($res3 as $value){
                $response3['for_total']     =  $value->for_total;
                $response3['against_total'] =  $value->against_total;
                $response3['abstain_total'] =  $value->abstain_total;
                $response3['for'] = !empty($value->for) ? $value->for : 0;
                $response3['against'] =  !empty($value->against) ? $value->against : 0;
                $response3['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output3,$response3);
            }

            foreach ($res4 as $value){
                $response4['for_total']     =  $value->for_total;
                $response4['against_total'] =  $value->against_total;
                $response4['abstain_total'] =  $value->abstain_total;
                $response4['for'] = !empty($value->for) ? $value->for : 0;
                $response4['against'] =  !empty($value->against) ? $value->against : 0;
                $response4['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output4,$response4);
            }

            foreach ($res5 as $value){
                $response5['for_total']     =  $value->for_total;
                $response5['against_total'] =  $value->against_total;
                $response5['abstain_total'] =  $value->abstain_total;
                $response5['for'] = !empty($value->for) ? $value->for : 0;
                $response5['against'] =  !empty($value->against) ? $value->against : 0;
                $response5['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output5,$response5);
            }

            foreach ($res6 as $value)
            {

                $response6['for_total']     =  $value->for_total;
                $response6['against_total'] =  $value->against_total;
                $response6['abstain_total'] =  $value->abstain_total;
                $response6['for'] = !empty($value->for) ? $value->for : 0;
                $response6['against'] =  !empty($value->against) ? $value->against : 0;
                $response6['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output6,$response6);
            }

            foreach ($res7 as $value)
            {

                $response7['for_total']     =  $value->for_total;
                $response7['against_total'] =  $value->against_total;
                $response7['abstain_total'] =  $value->abstain_total;
                $response7['for'] = !empty($value->for) ? $value->for : 0;
                $response7['against'] =  !empty($value->against) ? $value->against : 0;
                $response7['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output7,$response7);
            }


            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE resolution8=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE resolution8=3) AS abstain_num
              );");

            $response8 = [];
            $output8 = array();

            foreach ($res8 as $value)
            {

                $response8['for_total']     =  $value->for_total;
                $response8['against_total'] =  $value->against_total;
                $response8['abstain_total'] =  $value->abstain_total;
                $response8['for'] = !empty($value->for) ? $value->for : 0;
                $response8['against'] =  !empty($value->against) ? $value->against : 0;
                $response8['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output8,$response8);
            }

            $res8Voted = DB::select("SELECT * FROM votes WHERE resolution8 IS NOT NULL");





            $allResolutions= Resolution::query()->get();
            if (!$allResolutions){
                $allResolutions=array();
            }
            $activeResolution= Resolution::query()->where('status',1)->first();

            if ($activeResolution){
                $activeResolution=$activeResolution;
            }else{
                $activeResolution=(object)["title"=>"No active resolution"];
            }

            $verified = Voters::query()->where('verified',true)->get()->count();
            $total = Votes::query()->get()->groupBy('msisdn')->count();

            $res1Voted = DB::select("SELECT * FROM votes WHERE approval_of_final_dividend IS NOT NULL AND approval_of_final_dividend <> ''");
            $res2Voted = DB::select("SELECT * FROM votes WHERE ebenezer_twum_asante IS NOT NULL");
            $res3Voted = DB::select("SELECT * FROM votes WHERE modupe_agbolade_kadri IS NOT NULL");
            $res4Voted = DB::select("SELECT * FROM votes WHERE fatima_daniels IS NOT NULL");
            $res5Voted = DB::select("SELECT * FROM votes WHERE approval_of_directors_remuneration IS NOT NULL");
            $res6Voted = DB::select("SELECT * FROM votes WHERE authorization_of_directors IS NOT NULL");
            $res7Voted = DB::select("SELECT * FROM votes WHERE resolution7 IS NOT NULL");


            //Resolution 9
            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE resolution9=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE resolution9=3) AS abstain_num
              );");

            $response9 = [];
            $output9 = array();

            foreach ($res9 as $value)
            {

                $response9['for_total']     =  $value->for_total;
                $response9['against_total'] =  $value->against_total;
                $response9['abstain_total'] =  $value->abstain_total;
                $response9['for'] = !empty($value->for) ? $value->for : 0;
                $response9['against'] =  !empty($value->against) ? $value->against : 0;
                $response9['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output9,$response9);
            }

            $res9Voted = DB::select("SELECT * FROM votes WHERE resolution9 IS NOT NULL");


            //Resolution 9
            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE resolution9=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE resolution9=3) AS abstain_num
              );");

            $response9 = [];
            $output9 = array();

            foreach ($res9 as $value)
            {

                $response9['for_total']     =  $value->for_total;
                $response9['against_total'] =  $value->against_total;
                $response9['abstain_total'] =  $value->abstain_total;
                $response9['for'] = !empty($value->for) ? $value->for : 0;
                $response9['against'] =  !empty($value->against) ? $value->against : 0;
                $response9['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output9,$response9);
            }

            $res9Voted = DB::select("SELECT * FROM votes WHERE resolution9 IS NOT NULL");

//Resolution 10

            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE resolution10=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE resolution10=3) AS abstain_num
              );");

            $response10 = [];
            $output10 = array();

            foreach ($res10 as $value)
            {

                $response10['for_total']     =  $value->for_total;
                $response10['against_total'] =  $value->against_total;
                $response10['abstain_total'] =  $value->abstain_total;
                $response10['for'] = !empty($value->for) ? $value->for : 0;
                $response10['against'] =  !empty($value->against) ? $value->against : 0;
                $response10['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output10,$response10);
            }

            $res10Voted = DB::select("SELECT * FROM votes WHERE resolution10 IS NOT NULL");

            //Resolution 11

            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE resolution11=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE resolution11=3) AS abstain_num
              );");

            $response11 = [];
            $output11 = array();

            foreach ($res11 as $value)
            {

                $response11['for_total']     =  $value->for_total;
                $response11['against_total'] =  $value->against_total;
                $response11['abstain_total'] =  $value->abstain_total;
                $response11['for'] = !empty($value->for) ? $value->for : 0;
                $response11['against'] =  !empty($value->against) ? $value->against : 0;
                $response11['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output11,$response11);
            }

            $res11Voted = DB::select("SELECT * FROM votes WHERE resolution11 IS NOT NULL");

            //Resolution 12

            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE resolution12=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE resolution12=3) AS abstain_num
              );");

            $response12 = [];
            $output12 = array();

            foreach ($res12 as $value)
            {

                $response12['for_total']     =  $value->for_total;
                $response12['against_total'] =  $value->against_total;
                $response12['abstain_total'] =  $value->abstain_total;
                $response12['for'] = !empty($value->for) ? $value->for : 0;
                $response12['against'] =  !empty($value->against) ? $value->against : 0;
                $response12['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output12,$response12);
            }

            $res12Voted = DB::select("SELECT * FROM votes WHERE resolution12 IS NOT NULL");

            //Resolution 13

            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE resolution13=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE resolution13=3) AS abstain_num
              );");

            $response13 = [];
            $output13 = array();

            foreach ($res13 as $value)
            {

                $response13['for_total']     =  $value->for_total;
                $response13['against_total'] =  $value->against_total;
                $response13['abstain_total'] =  $value->abstain_total;
                $response13['for'] = !empty($value->for) ? $value->for : 0;
                $response13['against'] =  !empty($value->against) ? $value->against : 0;
                $response13['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output13,$response13);
            }

            $res13Voted = DB::select("SELECT * FROM votes WHERE resolution13 IS NOT NULL");

            //Resolution 14

            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE resolution14=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE resolution14=3) AS abstain_num
              );");

            $response14 = [];
            $output14 = array();

            foreach ($res14 as $value)
            {

                $response14['for_total']     =  $value->for_total;
                $response14['against_total'] =  $value->against_total;
                $response14['abstain_total'] =  $value->abstain_total;
                $response14['for'] = !empty($value->for) ? $value->for : 0;
                $response14['against'] =  !empty($value->against) ? $value->against : 0;
                $response14['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output14,$response14);
            }

            $res14Voted = DB::select("SELECT * FROM votes WHERE resolution14 IS NOT NULL");

            //Resolution 15

            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE resolution15=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE resolution15=3) AS abstain_num
              );");

            $response15 = [];
            $output15 = array();

            foreach ($res15 as $value)
            {

                $response15['for_total']     =  $value->for_total;
                $response15['against_total'] =  $value->against_total;
                $response15['abstain_total'] =  $value->abstain_total;
                $response15['for'] = !empty($value->for) ? $value->for : 0;
                $response15['against'] =  !empty($value->against) ? $value->against : 0;
                $response15['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output15,$response15);
            }

            $res15Voted = DB::select("SELECT * FROM votes WHERE resolution15 IS NOT NULL");

            //Resolution 16

            $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,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE resolution16=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE resolution16=3) AS abstain_num
              );");

            $response16 = [];
            $output16 = array();

            foreach ($res16 as $value)
            {

                $response16['for_total']     =  $value->for_total;
                $response16['against_total'] =  $value->against_total;
                $response16['abstain_total'] =  $value->abstain_total;
                $response16['for'] = !empty($value->for) ? $value->for : 0;
                $response16['against'] =  !empty($value->against) ? $value->against : 0;
                $response16['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output16,$response16);
            }

            $res16Voted = DB::select("SELECT * FROM votes WHERE resolution16 IS NOT NULL");


            // ============================== NEW PERCENTAGE CALCULATION ==============================  //
            $outputPercentageTotal = $output[0]['for_total'] + $output[0]['against_total'] + $output[0]['abstain_total'];
            $output[0]['for'] = $outputPercentageTotal != 0 ? round(100 * $output[0]['for_total'] / $outputPercentageTotal,2) : 0;
            $output[0]['against'] = $outputPercentageTotal != 0 ? round(100 * $output[0]['against_total'] / $outputPercentageTotal,2) : 0;
            $output[0]['abstain'] = $outputPercentageTotal != 0 ? round(100 * $output[0]['abstain_total'] / $outputPercentageTotal,2) : 0;


            $output2PercentageTotal = $output2[0]['for_total'] + $output2[0]['against_total'] + $output2[0]['abstain_total'];
            $output2[0]['for'] = $output2PercentageTotal != 0 ? round(100 * $output2[0]['for_total'] / $output2PercentageTotal,2) : 0;
            $output2[0]['against'] = $output2PercentageTotal != 0 ? round(100 * $output2[0]['against_total'] / $output2PercentageTotal,2) : 0;
            $output2[0]['abstain'] = $output2PercentageTotal != 0 ? round(100 * $output2[0]['abstain_total'] / $output2PercentageTotal,2) : 0;


            $output3PercentageTotal = $output3[0]['for_total'] + $output3[0]['against_total'] + $output3[0]['abstain_total'];
            $output3[0]['for'] = $output3PercentageTotal != 0 ? round(100 * $output3[0]['for_total'] / $output3PercentageTotal,2) : 0;
            $output3[0]['against'] = $output3PercentageTotal != 0 ? round(100 * $output3[0]['against_total'] / $output3PercentageTotal,2) : 0;
            $output3[0]['abstain'] = $output3PercentageTotal != 0 ? round(100 * $output3[0]['abstain_total'] / $output3PercentageTotal,2) : 0;


            $output4PercentageTotal = $output4[0]['for_total'] + $output4[0]['against_total'] + $output4[0]['abstain_total'];
            $output4[0]['for'] = $output4PercentageTotal != 0 ? round(100 * $output4[0]['for_total'] / $output4PercentageTotal,2) : 0;
            $output4[0]['against'] = $output4PercentageTotal != 0 ? round(100 * $output4[0]['against_total'] / $output4PercentageTotal,2) : 0;
            $output4[0]['abstain'] = $output4PercentageTotal != 0 ? round(100 * $output4[0]['abstain_total'] / $output4PercentageTotal,2) : 0;


            $output5PercentageTotal = $output5[0]['for_total'] + $output5[0]['against_total'] + $output5[0]['abstain_total'];
            $output5[0]['for'] = $output5PercentageTotal != 0 ? round(100 * $output5[0]['for_total'] / $output5PercentageTotal,2) : 0;
            $output5[0]['against'] = $output5PercentageTotal != 0 ? round(100 * $output5[0]['against_total'] / $output5PercentageTotal,2) : 0;
            $output5[0]['abstain'] = $output5PercentageTotal != 0 ? round(100 * $output5[0]['abstain_total'] / $output5PercentageTotal,2) : 0;


            $output6PercentageTotal = $output6[0]['for_total'] + $output6[0]['against_total'] + $output6[0]['abstain_total'];
            $output6[0]['for'] = $output6PercentageTotal != 0 ? round(100 * $output6[0]['for_total'] / $output6PercentageTotal,2) : 0;
            $output6[0]['against'] = $output6PercentageTotal != 0 ? round(100 * $output6[0]['against_total'] / $output6PercentageTotal,2) : 0;
            $output6[0]['abstain'] = $output6PercentageTotal != 0 ? round(100 * $output6[0]['abstain_total'] / $output6PercentageTotal,2) : 0;


            $output7PercentageTotal = $output7[0]['for_total'] + $output7[0]['against_total'] + $output7[0]['abstain_total'];
            $output7[0]['for'] = $output7PercentageTotal != 0 ? round(100 * $output7[0]['for_total'] / $output7PercentageTotal,2) : 0;
            $output7[0]['against'] = $output7PercentageTotal != 0 ? round(100 * $output7[0]['against_total'] / $output7PercentageTotal,2) : 0;
            $output7[0]['abstain'] = $output7PercentageTotal != 0 ? round(100 * $output7[0]['abstain_total'] / $output7PercentageTotal,2) : 0;


//            dd($output8[0]['for']);
            $output8PercentageTotal = $output8[0]['for_total'] + $output8[0]['against_total'] + $output8[0]['abstain_total'];
            $output8[0]['for'] = $output8PercentageTotal != 0 ? round(100 * $output8[0]['for_total'] / $output8PercentageTotal,2) : 0;
            $output8[0]['against'] = $output8PercentageTotal != 0 ? round(100 * $output8[0]['against_total'] / $output8PercentageTotal,2) : 0;
            $output8[0]['abstain'] = $output8PercentageTotal != 0 ? round(100 * $output8[0]['abstain_total'] / $output8PercentageTotal,2) : 0;


            $output9PercentageTotal = $output9[0]['for_total'] + $output9[0]['against_total'] + $output9[0]['abstain_total'];
            $output9[0]['for'] = $output9PercentageTotal != 0 ? round(100 * $output9[0]['for_total'] / $output9PercentageTotal,2) : 0;
            $output9[0]['against'] = $output9PercentageTotal != 0 ? round(100 * $output9[0]['against_total'] / $output9PercentageTotal,2) : 0;
            $output9[0]['abstain'] = $output9PercentageTotal != 0 ? round(100 * $output9[0]['abstain_total'] / $output9PercentageTotal,2) : 0;


            $output10PercentageTotal = $output10[0]['for_total'] + $output10[0]['against_total'] + $output10[0]['abstain_total'];
            $output10[0]['for'] = $output10PercentageTotal != 0 ? round(100 * $output10[0]['for_total'] / $output10PercentageTotal,2) : 0;
            $output10[0]['against'] = $output10PercentageTotal != 0 ? round(100 * $output10[0]['against_total'] / $output10PercentageTotal,2) : 0;
            $output10[0]['abstain'] = $output10PercentageTotal != 0 ? round(100 * $output10[0]['abstain_total'] / $output10PercentageTotal,2) : 0;

            $output11PercentageTotal = $output11[0]['for_total'] + $output11[0]['against_total'] + $output11[0]['abstain_total'];
            $output11[0]['for'] = $output11PercentageTotal != 0 ? round(100 * $output11[0]['for_total'] / $output11PercentageTotal,2) : 0;
            $output11[0]['against'] = $output11PercentageTotal != 0 ? round(100 * $output11[0]['against_total'] / $output11PercentageTotal,2) : 0;
            $output11[0]['abstain'] = $output11PercentageTotal != 0 ? round(100 * $output11[0]['abstain_total'] / $output11PercentageTotal,2) : 0;


            $output12PercentageTotal = $output12[0]['for_total'] + $output12[0]['against_total'] + $output12[0]['abstain_total'];
            $output12[0]['for'] = $output12PercentageTotal != 0 ? round(100 * $output12[0]['for_total'] / $output12PercentageTotal,2) : 0;
            $output12[0]['against'] = $output12PercentageTotal != 0 ? round(100 * $output12[0]['against_total'] / $output12PercentageTotal,2) : 0;
            $output12[0]['abstain'] = $output12PercentageTotal != 0 ? round(100 * $output12[0]['abstain_total'] / $output12PercentageTotal,2) : 0;


            $output13PercentageTotal = $output13[0]['for_total'] + $output13[0]['against_total'] + $output13[0]['abstain_total'];
            $output13[0]['for'] = $output13PercentageTotal != 0 ? round(100 * $output13[0]['for_total'] / $output13PercentageTotal,2) : 0;
            $output13[0]['against'] = $output13PercentageTotal != 0 ? round(100 * $output13[0]['against_total'] / $output13PercentageTotal,2) : 0;
            $output13[0]['abstain'] = $output13PercentageTotal != 0 ? round(100 * $output13[0]['abstain_total'] / $output13PercentageTotal,2) : 0;


            $output14PercentageTotal = $output14[0]['for_total'] + $output14[0]['against_total'] + $output14[0]['abstain_total'];
            $output14[0]['for'] = $output14PercentageTotal != 0 ? round(100 * $output14[0]['for_total'] / $output14PercentageTotal,2) : 0;
            $output14[0]['against'] = $output14PercentageTotal != 0 ? round(100 * $output14[0]['against_total'] / $output14PercentageTotal,2) : 0;
            $output14[0]['abstain'] = $output14PercentageTotal != 0 ? round(100 * $output14[0]['abstain_total'] / $output14PercentageTotal,2) : 0;


            $output15PercentageTotal = $output15[0]['for_total'] + $output15[0]['against_total'] + $output15[0]['abstain_total'];
            $output15[0]['for'] = $output15PercentageTotal != 0 ? round(100 * $output15[0]['for_total'] / $output15PercentageTotal,2) : 0;
            $output15[0]['against'] = $output15PercentageTotal != 0 ? round(100 * $output15[0]['against_total'] / $output15PercentageTotal,2) : 0;
            $output15[0]['abstain'] = $output15PercentageTotal != 0 ? round(100 * $output15[0]['abstain_total'] / $output15PercentageTotal,2) : 0;


            $output16PercentageTotal = $output16[0]['for_total'] + $output16[0]['against_total'] + $output16[0]['abstain_total'];
            $output16[0]['for'] = $output16PercentageTotal != 0 ? round(100 * $output16[0]['for_total'] / $output16PercentageTotal,2) : 0;
            $output16[0]['against'] = $output16PercentageTotal != 0 ? round(100 * $output16[0]['against_total'] / $output16PercentageTotal,2) : 0;
            $output16[0]['abstain'] = $output16PercentageTotal != 0 ? round(100 * $output16[0]['abstain_total'] / $output16PercentageTotal,2) : 0;

            // ============================== NEW PERCENTAGE CALCULATION ==============================  //

//            dd($output8);
            $view->with([
               'output1'=> $output[0],
               'output2'=> $output2[0],
               'output3'=> $output3[0],
               'output4'=> $output4[0],
               'output5'=> $output5[0],
               'output6'=> $output6[0],
               'output7'=> $output7[0],
               'output8'=> $output8[0],
               'output9'=> $output9[0],
               'output10'=> $output10[0],
               'output11'=> $output11[0],
               'output12'=> $output12[0],
               'output13'=> $output13[0],
               'output14'=> $output14[0],
               'output15'=> $output15[0],
               'output16'=> $output16[0],
                'verified' => $verified,
                'total_voted' => $total,
                'activeResolution'=>$activeResolution,
                'allResolutions'=>$allResolutions,
                'res1Voted'=>$res1Voted,
                'res2Voted'=>$res2Voted,
                'res3Voted'=>$res3Voted,
                'res4Voted'=>$res4Voted,
                'res5Voted'=>$res5Voted,
                'res6Voted'=>$res6Voted,
                'res7Voted'=>$res7Voted,
                'res8Voted'=>$res8Voted,
                'res9Voted'=>$res9Voted,
                'res10Voted'=>$res10Voted,
                'res11Voted'=>$res11Voted,
                'res12Voted'=>$res12Voted,
                'res13Voted'=>$res13Voted,
                'res14Voted'=>$res14Voted,
                'res15Voted'=>$res15Voted,
                'res16Voted'=>$res16Voted,
            ]);
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}