<?php

namespace App\Http\Controllers\Admin;

use App\Helpers\Helper;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\DB;
//modal
;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Auth;

//UTILS
use App\Utils\DashboardUtil;

//MODEL
use App\User;


class DashboardController extends Controller
{
    /**
     * Title untuk judul di web
     * route digunakan untuk tempat resource (file path) + routing (route/web) diusahain sama ya biar gak ngubah"
     */
    private $title = 'Admin';
    private $route = 'admin.dashboard.'; //path awal foldernya ajah
    protected $DashboardUtil;
    public function __construct(DashboardUtil $DashboardUtil)
    {
        $this->DashboardUtil = $DashboardUtil;
        DB::enableQueryLog();
    }

    public function index()
    {
        $reguler_points = User::select('users.name', DB::raw('SUM(point_logs.point) as total_point'))->rightJoin('point_logs', 'point_logs.user_id', 'users.id')->where('event_id', '=', 0)->groupBy('point_logs.user_id')->orderBy('total_point', 'desc')->limit(5)->get();

        $reguler_point_all = [];

        $reguler_user_all = [];

        foreach($reguler_points as $key => $value){
            array_push($reguler_point_all , intval($value->total_point));
            array_push($reguler_user_all , $value->name);
        }

        $event_points = User::select('users.name', DB::raw('SUM(point_logs.point) as total_point'))->rightJoin('point_logs', 'point_logs.user_id', 'users.id')->where('event_id', '=', 1)->groupBy('point_logs.user_id')->orderBy('total_point', 'desc')->limit(5)->get();

        $event_point_all = [];

        $event_user_all = [];

        foreach($event_points as $key => $value){
            array_push($event_point_all , intval($value->total_point));
            array_push($event_user_all , $value->name);
        }

        $data = [
            //bawaan
            'title'             => $this->title,
            'route'             => $this->route,
            'reguler_user_all'  => $reguler_user_all,
            'reguler_point_all' => $reguler_point_all,
            'event_user_all'    => $event_user_all,
            'event_point_all'   => $event_point_all,
        ];
        // dd($data['log_points']);
        return view($this->route . 'index', $data);
    }
}
