<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\LoginHistory;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class ProfileController extends Controller
{
    
    public function index(){

    	$histories = LoginHistory::with('user')->orderBy('id' , 'DESC')->paginate(10);
    	$dates = Carbon::now()->format('Y-m-d');
    	$date = explode('-', $dates);
    	$day = User::where('register_year' , $date[0])->where('register_month' , $date[1])->where('register_day' , $date[2])->count('id');
    	$week = User::where('register_year' , $date[0])->where('register_month' , $date[1])->where('register_day' , '<=' , $date[2])->count('id');
    	$month = User::where('register_month' , '=' , $date[1])->where('register_year' , '=' , $date[0])->count('id');
    	$year = User::where('register_year' , $date[0])->count('id');

    	return view('Admin.dashboard' , compact('histories' , 'day' , 'week' , 'month' , 'year'));
    }

    public function changePasswordView(Request $request){
        return view('Admin.ChangePassword');
    }

    public function savePassword(Request $request){
        $this->validate($request, [
            'password' => 'required',
            'confirm' => ['required','same:password']
        ]);
        Auth::guard('admin')->user()->update([
            'password' => Hash::make($request->password)
        ]);
        return redirect()->back()->with('success_message', 'Password successfully changed!');
    }

}
