<?php

namespace App\Http\Controllers;

use App\Job;
use Illuminate\Http\Request;

class JobController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $jobs = Job::all();

        $jobs_sorted = $jobs->sortBy('name');

        $jobs = $jobs_sorted->values();

        return view('admin.job.index', ['jobs' => $jobs]);
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $job = new Job;

        $job->name = request('name');
        $job->acces_level = request('acces_level');
        $job->desc = request('desc');
        
        if (null !==(request('schedule_see'))) {
            $job->schedule_see = request('schedule_see');
        } else {
            $job->schedule_see = false;
        }

        if (null !==(request('schedule_edit'))) {
            $job->schedule_edit = 1;
        } else {
            $job->schedule_edit = false;
        }
        if (null !==(request('schedule_notify'))) {
            $job->schedule_notify = 1;
        } else {
            $job->schedule_notify = false;
        }
        if (null !==(request('message_see'))) {
            $job->message_see = 1;
        } else {
            $job->message_see = false;
        }
        if (null !==(request('message_edit'))) {
            $job->message_edit = 1;
        } else {
            $job->message_edit = false;
        }
        if (null !==(request('message_notify'))) {
            $job->message_notify = 1;
        } else {
            $job->message_notify = false;
        }
        if (null !==(request('paper_edit'))) {
            $job->paper_edit = 1;
        } else {
            $job->paper_edit = false;
        }
        if (null !==(request('paper_publish'))) {
            $job->paper_publish = 1;
        } else {
            $job->paper_publish = false;
        }
        if (null !==(request('paper_notify'))) {
            $job->paper_notify = 1;
        } else {
            $job->paper_notify = false;
        }
        if (null !==(request('inventory_see'))) {
            $job->inventory_see = 1;
        } else {
            $job->inventory_see = false;
        }
        if (null !==(request('inventory_edit'))) {
            $job->inventory_edit = 1;
        } else {
            $job->inventory_edit = false;
        }
        if (null !==(request('inventory_notify'))) {
            $job->inventory_notify = 1;
        } else {
            $job->inventory_notify = false;
        }
        if (null !==(request('user_see'))) {
            $job->user_see = 1;
        } else {
            $job->user_see = false;
        }
        if (null !==(request('user_edit'))) {
            $job->user_edit = 1;
        } else {
            $job->user_edit = false;
        }
        if (null !==(request('user_notify'))) {
            $job->user_notify = 1;
        } else {
            $job->user_notify = false;
        }

        $job->save();

        return redirect('/admin/config/job/'.$job->id);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Job  $job
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $job = Job::find($id);

        return view('admin.job.show', ['job' => $job]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Job  $job
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $job = Job::find($id);

        return view('admin.job.edit', ['job' => $job]);
    }

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Job  $job
     * @return \Illuminate\Http\Response
     */
    public function destroy(Job $job)
    {
        //
    }
}
