<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Biodata;
use App\Models\User\BukuAjar;
use App\Repositories\User\BukuAjarRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Alert;
use Exception;
use Crypt;
use Validator;

class BukuAjarController extends Controller
{
    private $bukuajarRepo;

    public function __construct(BukuAjarRepository $bukuajarRepo)
    {
        $this->bukuajarRepo = $bukuajarRepo;
    }

    public function index()
    {
        //
        $menu = 'bukuajar';
        $bukuajar = $this->bukuajarRepo->get(['rDosen']);
        $data = [
            'bukuajar' => $bukuajar,
            'menu' => $menu
        ];
        return view('user.bukuajar.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $menu = 'bukuajar';
        $edit = false;

        $data = [
            'menu' => $menu,
            'edit' => $edit
        ];
        return view('user.bukuajar.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $data = $request->except('_token');
        Validator::make($data, BukuAjar::RULES, BukuAjar::ERROR_MESSAGES)->validate();

        try {
            $biodata = Biodata::query()->where('nidn', $request->nidn)->firstOrFail();
            $data['userid_created'] = Auth::user()->id;
            $data['userid_updated'] = Auth::user()->id;
            //dd($data);
            $bukuajar = $this->bukuajarRepo->store($data);
        } catch (Exception $ex) {
            return redirect()->back()->withInput();
        }

        $data = [
            'bukuajar' => $bukuajar->id,
        ];

        return redirect()->route('bukuajar.index', $data);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $menu = 'unitbisnis';
        $edit = true;

        try {
            $menu = 'unitbisnis';
            $bukuajar = $this->bukuajarRepo->findId(null, $id);

            $data = [
                'menu' => $menu,
                'edit' => $edit,
                'bukuajar' => $bukuajar,
            ];

            return view('user.bukuajar.create', $data);

        } catch (Exception $ex) {
            return redirect()->back();
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->except('_token');
        Validator::make($data, BukuAjar::RULES, BukuAjar::ERROR_MESSAGES)->validate();

        try {
            $biodata = Biodata::query()->where('nidn', $request->nidn)->firstOrFail();
            $data['userid_updated'] = auth()->user()->id;
            $bukuajar = $this->bukuajarRepo->findId(null, decrypt($id));
            //dd($bukuajar);
            $this->bukuajarRepo->update($data, $bukuajar);

        } catch (Exception $ex) {
            return redirect()->back()->withInput();
        }

        $data = [
            'bukuajar' => $bukuajar->id,
        ];

        return redirect()->route('bukuajar.index', $data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $model = $this->bukuajarRepo->findId(null, Crypt::decrypt($id));
        $this->bukuajarRepo->destroy($model);

        Alert::success('Data berhasil dihapus')->persistent('Ok');

        return redirect()->route('bukuajar.index');
    }
}
