<?php

namespace App\Http\Controllers\User;

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

class KontrakKerjaController extends Controller
{
    private $kontrakkerjaRepo;

    public function __construct(KontrakKerjaRepository $kontrakkerjaRepo)
    {
        $this->kontrakkerjaRepo = $kontrakkerjaRepo;
    }

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

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

        return view('user.kontrakkerja.index', $data);
    }

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

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

        Alert::success('Berhasil', 'Kontrak Kerja berhasil disimpan');

        return redirect()->route('kontrakkerja.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->kontrakkerjaRepo->findId(null, Crypt::decrypt($id));
        $this->kontrakkerjaRepo->destroy($model);

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

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