<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \App\User;
use \App\Helper;
use \App\History;
use \App\Balance;
use Illuminate\Support\Facades\DB;

class HomeController extends Controller
{

    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            if(auth()->check()){
                Helper::checkLevel(['admin', 'reseller', 'member', 'priority']);
            }
            else{
                return redirect('/login');
            }
            return $next($request);
        });
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $userinfo['subserver'] = \App\History::where('submitted_by', \Auth::User()->username)
                                            ->whereDate('created_at', '=', date('Y-m-d'))->count();
        $a = \App\History::where('submitted_by', \Auth::User()->username)
                                            ->where('type', 'Like Server 1')
                                            ->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
                                            
        $b = \App\History::where('submitted_by', \Auth::User()->username)
                                            ->where('type', 'Like Server 2')
                                            ->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
                                            
        
        
        $userinfo['sublike'] = $a+$b;
        
        $jmlx['useractivemem'] = \App\User::where('level', 'member')->where('status', 'aktif')->whereDate('expired_date', '>=', date('Y-m-d'))->count();
        $jmlx['useractiveres'] = \App\User::where('level', 'reseller')->where('status', 'aktif')->whereDate('expired_date', '>=', date('Y-m-d'))->count();
        $jmlx['jmllike1'] = \App\History::where('type', 'Like Server 1')->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
        $jmlx['jmllike2'] = \App\History::where('type', 'Like Server 2')->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
        $jmlx['jmllike3'] = \App\History::where('type', 'Like Server 3')->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
        $jmlx['jmlsub1'] = \App\History::where('type', 'Like Server 1')->whereDate('created_at', '=', date('Y-m-d'))->count();
        $jmlx['jmlsub2'] = \App\History::where('type', 'Like Server 2')->whereDate('created_at', '=', date('Y-m-d'))->count();
        $jmlx['jmlsub3'] = \App\History::where('type', 'Like Server 3')->whereDate('created_at', '=', date('Y-m-d'))->count();
        $jmlx['pview'] = \App\History::where('type', 'Post Views')->whereDate('created_at', '=', date('Y-m-d'))->where('submitted_by', \Auth::User()->username)->count();
        $jmlx['comment'] = \App\History::where('type', 'Comment')->whereDate('created_at', '=', date('Y-m-d'))->where('submitted_by', \Auth::User()->username)->count();
        $jmlx['spview'] = \App\History::where('type', 'Post Views')->whereDate('created_at', '=', date('Y-m-d'))->count();
        $jmlx['scomment'] = \App\History::where('type', 'Comment')->whereDate('created_at', '=', date('Y-m-d'))->count();
        $jmlx['prio'] = \App\User::where('level', 'priority')->count();

        
        $totalUser = User::count();
        $totalSubmit = History::count();
        $latestUser = User::orderBy('id', 'desc')->take(5)->get();
        if(auth()->user()->level == 'admin'){
          $latestSubmit = History::orderBy('id', 'desc')->take(5)->get();
          $latestBalance = Balance::orderBy('id', 'desc')->take(5)->get();
        }
        else{
          $latestSubmit = History::where('submitted_by', auth()->user()->username)->orderBy('id', 'desc')->take(5)->get();
          $latestBalance = Balance::where('id_user', auth()->user()->id)->orderBy('id', 'desc')->take(5)->get();
        }
        return view('index', compact('totalUser', 'totalSubmit', 'latestSubmit', 'latestBalance', 'latestUser', 'userinfo', 'jmlx'));
    }
}
