<?php

namespace App\Http\Controllers\Admin;

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

class BidangPenelitianController extends Controller
{
    private $bidangpenelitianRepo;

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

    public function index()
    {
        //
        $menu = 'bidangpenelitian';
        $bidangpenelitian = $this->bidangpenelitianRepo->get(['rKategori']);
        $bidangpenelitiankategori = $this->bidangpenelitiankategoriRepo->get();
        //dd($bidangpenelitiankategori);
        $data = [
            'bidangpenelitian' => $bidangpenelitian,
            'bidangpenelitiankategori' => $bidangpenelitiankategori,
            'menu' => $menu
        ];

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

    public function bidangkategori()
    {
        //
        $menu = 'bidangpenelitian';
        $bidangpenelitiankategori = $this->bidangpenelitiankategoriRepo->get();
        //dd($bidangpenelitiankategori);
        $data = [
            'bidangpenelitiankategori' => $bidangpenelitiankategori,
            'menu' => $menu
        ];

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


    public function create()
    {
        //
        return view('admin.bidangpenelitian.create');
    }


    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('admin.bidangpenelitian.index', $data);
    }

    public function kategoristore(Request $request)
    {
        //
        $data = $request->except('_token');
        $data['user_id'] = auth()->user()->id;
        $bidangpenelitiankategori = $this->bidangpenelitiankategoriRepo->store($data);

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

        Alert::success('Berhasil', 'Kategori Bidang Penelitian berhasil disimpan');

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


    public function show($id)
    {
        //
    }


    public function edit($id)
    {
        //
        $menu = 'bidangpenelitian';
        $bidangpenelitian = $this->bidangpenelitianRepo->findId(null, $id);

        $data = [

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

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

    public function kategoriedit($id)
    {
        //
        $menu = 'bidangpenelitian';
        $bidangpenelitian = $this->bidangpenelitianRepo->findId(null, $id);

        $data = [

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

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


    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);
    }

    public function kategoriupdate(Request $request, $id)
    {
        //
        $data = $request->except('_token');
        $data['userid_updated'] = auth()->user()->id;
        $bidangpenelitiankategori = $this->bidangpenelitiankategoriRepo->findId(null, $id);
        $this->bidangpenelitiankategoriRepo->update($data, $bidangpenelitiankategori);

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

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

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


    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');
    }
}
