<?php

namespace App\Http\Controllers\tables;

use App\Exports\KartuKendaliExport;
use Barryvdh\DomPDF\Facade\Pdf;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Models\KartuKendali;
use App\Models\Siswa;
use App\Models\User;
use App\Notifications\UserNotification;
use Illuminate\Support\Facades\Notification;

class KartuKendaliController extends Controller
{
    public function index()
    {
        $user = Auth::user();
        if ($user->role == "Admin") {
            $kartukendali = KartuKendali::join("siswa", "siswa.user_id", "=", "kartukendali.user_id")->get(["siswa.*", "kartukendali.*", "kartukendali.id as kkid"]);
        } else {
            $kartukendali = KartuKendali::join("siswa", "siswa.user_id", "=", "kartukendali.user_id")->where("kartukendali.user_id", "=", $user->id)->get(["siswa.*", "kartukendali.*", "kartukendali.id as kkid"]);
        }

        return view('content.kartukendali.index', compact('kartukendali'));
    }

    public function create()
    {
        $siswa = Siswa::join("users", "users.id", "=", "siswa.user_id")->where("users.active", "=", 1)->get();
        return view('content.kartukendali.create', compact('siswa'));
    }

    public function insert(Request $request)
    {
        $year = date("Y");
        $month = date("F");
        $week = 0;
        for ($i = date('j'); $i > 0; $i -= 7) {
            $week += 1;
        }

        $kk = KartuKendali::where("user_id", "=", $request->id)->first();

        if ($kk) {
            $kk->update(["jumlah" => $kk->jumlah + 1]);
        } else {
            KartuKendali::create([
                "user_id" => $request->id,
                "jumlah" => 1,
            ]);
        }

        return redirect()->route("kartukendali")->with("success", "Data Berhasil Disimpan!");
    }

    public function add(Request $request, $kartukendaliId)
    {
        $kk = KartuKendali::where("id", "=", $kartukendaliId)->first();
        $kk->update(["jumlah" => $kk->jumlah += 1]);
        $siswa = User::find($kk->user_id);

        if ($kk->jumlah >= 40) {
            Notification::send($siswa, new UserNotification("Akses Quiz Dibuka", "Akses untuk menyelesaikan quiz telah diberikan!", "/tables/quiz"));
        }

        return redirect()->route("kartukendali")->with("success", "Data Berhasil Disimpan!");
    }

    public function sub(Request $request, $kartukendaliId)
    {
        $kk = KartuKendali::where("id", "=", $kartukendaliId)->first();
        $kk->update(["jumlah" => $kk->jumlah -= 1]);
        return redirect()->route("kartukendali")->with("success", "Data Berhasil Disimpan!");
    }

    public function destroy(Request $request, $kartukendaliId)
    {
        $id = $kartukendaliId;
        $kartukendali = KartuKendali::find($id);

        $kartukendali->delete();

        return redirect()->route("kartukendali")->with("success", "Data Berhasil Dihapus!");
    }

    public function export(Request $request)
    {
        switch ($request->output) {
            case "pdf":
                $kartukendali = KartuKendali::join("siswa", "siswa.user_id", "=", "kartukendali.user_id")->get(["siswa.*", "kartukendali.*", "kartukendali.id as kkid"]);
                $pdf = Pdf::loadView('content.kartukendali.export-pdf', ['kartukendali' => $kartukendali]);
                $pdf->setPaper('A4', 'portrait');
                return $pdf->download('data-kartukendali.pdf');
            default:
                return Excel::download(new KartuKendaliExport, 'kartukendali.xlsx');
        }
    }
}
