<?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)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

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