<?php

namespace App\Http\Controllers;

use App\Models\Riwayat;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Http\Request;

class RiwayatController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $riwayat = Riwayat::get();
        return view('riwayat.index', compact('riwayat'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validate = $request->validate([
            'id_pengaduan_bk' => 'required',
            'tindakan_bk' => 'required|string',
        ]);

        // dd($validate);

        Riwayat::create([
            'id_pengaduan_bk' => $validate['id_pengaduan_bk'],
            'tindakan_bk' => $validate['tindakan_bk'],
        ]);

        $notification = array(
            'message' => 'Berhasil memberikan tanggapan!',
            'alert-type' => 'success'
        );

        return redirect()->route('dashboard')->with($notification);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $jenis)
    {
        $riwayat = Riwayat::whereHas('pengaduan', function ($query) use ($jenis) {
            $query->where('jenis_pengaduan', $jenis);
        })->with('pengaduan')->get();

        return view('riwayat.index', compact('riwayat', 'jenis'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id_riwayat_bk)
    {
        $riwayat = Riwayat::findOrFail($id_riwayat_bk);

        $validate = $request->validate([
            'id_pengaduan_bk' => 'required',
            'tindakan_bk' => 'required|string',
        ]);

        // dd($validate);

        $riwayat->update([
            'id_pengaduan_bk' => $validate['id_pengaduan_bk'],
            'tindakan_bk' => $validate['tindakan_bk'],
        ]);

        $notification = array(
            'message' => 'Berhasil mengedit tanggapan!',
            'alert-type' => 'success'
        );

        return redirect()->route('dashboard')->with($notification);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id_riwayat_bk)
    {
        $riwayat = Riwayat::findOrFail($id_riwayat_bk);

        $riwayat->delete();

        $notification = array(
            'message' => 'Berhasil menghapus tanggapan!',
            'alert-type' => 'success'
        );

        return redirect()->route('dashboard')->with($notification);
    }

    public function print()
    {
        $riwayat = Riwayat::with('pengaduan')
            ->whereHas('pengaduan', function ($query) {
                $query->where('jenis_pengaduan', 'Karier');
            })
            ->get();

        $pdf = Pdf::loadView('riwayat.print', compact('riwayat'))
            ->setPaper('A4', 'portrait');

        return $pdf->stream('riwayat_karier.pdf');
    }
}
