<?php

namespace App\Http\Controllers\Admin;

use Alert;
use Crypt;
use App\Http\Controllers\Controller;
use App\Repositories\Admin\MsBidangPenelitianRepository;
use Illuminate\Http\Request;

class BidangPenelitianController extends Controller
{
    private $bidangpenelitianRepo;

    public function __construct(MsBidangPenelitianRepository $bidangpenelitianRepo)
    {
        $this->bidangpenelitianRepo = $bidangpenelitianRepo;
    }

    public function index()
    {
        //
        $menu = 'bidangpenelitian';
        $bidangpenelitian = $this->bidangpenelitianRepo->get();

        $data = [
            'bidangpenelitian' => $bidangpenelitian,
            'menu' => $menu
        ];

        return view('admin.bidang.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.bidangpenelitian.create');
    }

    /**
     * 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');
        $data['user_id'] = auth()->user()->id;
        $bidangpenelitian = $this->bidangpenelitianRepo->store($data);

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

        Alert::success('Berhasil', 'bidangpenelitian berhasil disimpan');

        return redirect()->route('bidang.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 = 'bidangpenelitian';
        $bidangpenelitian = $this->bidangpenelitianRepo->findId(null, $id);

        $data = [

            'menu' => $menu,
            'bidangpenelitian' => $bidangpenelitian,
        ];

        return view('admin.bidang.edit', $data);
    }

    /**
     * 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');
        $data['userid_updated'] = auth()->user()->id;
        $bidangpenelitian = $this->bidangpenelitianRepo->findId(null, $id);
        $this->bidangpenelitianRepo->update($data, $bidangpenelitian);

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

        Alert::success('Berhasil', 'bidangpenelitian berhasil diupdated');

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

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

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

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