<?php

namespace App\Http\Controllers\tables;

use App\Exports\TutorExport;
use Barryvdh\DomPDF\Facade\Pdf;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Tutor;

class TutorController extends Controller
{
    public function index()
    {
        $tutor = Tutor::all();
        return view('content.tutor.index', compact('tutor'));
    }

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

    public function edit(Request $request, $tutorId)
    {
        $tutor = Tutor::find($tutorId);
        return view('content.tutor.edit', compact('tutor'));
    }

    public function insert(Request $request)
    {
        $imageName = $request->nama . ".jpg";
        $request->file("foto")->move(public_path("images/tutor"), $imageName);

        Tutor::create([
            "nama" => $request->nama,
            "nik" => $request->nik,
            "alamat" => $request->alamat,
            "foto" => $imageName,
        ]);

        return redirect()->route("tutor")->with("success", "Data Berhasil Disimpan!");
    }
    public function update(Request $request, $tutorId)
    {
        $tutor = Tutor::find($tutorId);
        if ($request->foto) {
            $imageName = $request->nama . ".jpg";
            $request->file("foto")->move(public_path("images/tutor"), $imageName);

            $request->merge(["foto" => $imageName]);
        }

        $tutor->fill($request->input())->save();

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

    public function destroy(Request $request, $tutorId)
    {
        $id = $tutorId;
        $tutor = Tutor::find($id);

        $tutor->delete();
        try {
            unlink(public_path("images/tutor/") . $tutor->foto);
        } catch (\Exception $e) {
        }

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

    public function export(Request $request)
    {
        switch ($request->output) {
            case "pdf":
                $tutor = Tutor::all();
                $pdf = Pdf::loadView('content.tutor.export-pdf', ['tutor' => $tutor]);
                $pdf->setPaper('A4', 'portrait');
                return $pdf->download('data-tutor.pdf');
            default:
                return Excel::download(new TutorExport, 'tutor.xlsx');
        }
    }
}
