<?php

namespace App\Http\Controllers\User;

use Alert;
use App\Http\Controllers\Controller;
use App\Repositories\User\UnitBisnisRepository;
use Crypt;
use Illuminate\Http\Request;

class UnitBisnisController extends Controller
{
    private $unitbisnisRepo;

    public function __construct(UnitBisnisRepository $unitbisnisRepo)
    {
        $this->unitbisnisRepo = $unitbisnisRepo;
    }

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

        $data = [
            'unitbisnis' => $unitbisnis,
            'menu' => $menu
        ];
        return view('user.unitbisnis.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('user.unitbisnis.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;
        $unitbisnis = $this->unitbisnisRepo->store($data);

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

        Alert::success('Berhasil', 'Unit Bisnis berhasil disimpan');

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

        $data = [

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

        return view('user.unitbisnis.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;
        $unitbisnis = $this->unitbisnisRepo->findId(null, $id);
        $this->unitbisnisRepo->update($data, $unitbisnis);

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

        Alert::success('Berhasil', 'Unit Bisnis berhasil diupdated');

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

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

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

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