<?php

namespace App\Http\Controllers\tables;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Siswa;
use App\Exports\StudentExport;
use Barryvdh\DomPDF\Facade\Pdf;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Auth;

class StudentController extends Controller
{
    public function index()
    {
        $siswa = Siswa::join("users", "users.id", "=", "siswa.user_id")->get(["users.*", "siswa.*", "siswa.id as sid"]);
        return view('content.student.index', compact('siswa'));
    }

    public function create()
    {
        return view('content.student.create');
    }

    public function edit(Request $request, $studentId)
    {
        $siswa = Siswa::find($studentId);
        return view('content.student.edit', compact('siswa'));
    }

    public function insert(Request $request)
    {
        $newUser = User::create([
            "username" => $request->nik,
            "password" => bcrypt(12345678),
            "role" => "Siswa",
        ]);

        Siswa::create([
            "nama" => $request->nama,
            "user_id" => $newUser->getKey(),
            "nik" => $request->nik,
            "tgl_lahir" => $request->tgl_lahir,
            "jenkel" => $request->jenkel,
            "agama" => $request->agama,
            "jenjang" => $request->jenjang,
            "alamat" => $request->alamat,
            "nama_ortu" => $request->nama_ortu,
            "no_telp" => $request->no_telp,
            "kerja_ortu" => $request->kerja_ortu,
            "level" => $request->level,
        ]);

        return redirect()->route("student")->with("success", "Data Berhasil Disimpan!");
    }
    public function update(Request $request, $studentId)
    {
        $siswa = Siswa::find($studentId);
        $siswa->fill($request->input())->save();

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

    public function changeStatus(Request $request, $studentId)
    {
        $siswa = Siswa::find($studentId);
        $siswa["aktif"] = !$siswa["aktif"];
        $siswa->fill($siswa->toArray())->save();

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

    public function editBiodata()
    {
        $user = Auth::user();
        $siswa = Siswa::where("user_id", "=", $user->id)->first();
        return view('content.student.edit-biodata', compact("siswa"));
    }

    public function updateBiodata(Request $request)
    {
        $user = Auth::user();
        $siswa = Siswa::where("user_id", "=", $user->id)->first();
        $siswa->fill($request->input())->save();

        return redirect()->to("/dashboard");
    }

    public function destroy(Request $request, $studentId)
    {
        $id = $studentId;
        $siswa = Siswa::find($id);

        $siswa->delete();

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

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