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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {   
        $gender = null;
        if($request->get('gender')) {
            $gender = implode(', ', $request->get('gender'));
        }
        $job = new Job();
        $job->job_title = $request->get('job_title');
        $job->job_type = $request->get('job_type');
        $job->department = $request->get('department');
        $job->salary = $request->get('salary');
        $job->branch = $request->get('branch');
        $job->day = $request->get('day');
        $job->time = $request->get('time');
        $job->gender = $gender;
        $job->num = $request->get('num');
        $job->degree = $request->get('degree');
        $job->gpax = $request->get('gpax');
        $job->phase = $request->get('phase');
        $job->feature = $request->get('feature');
        $job->role = $request->get('role');
        $job->welfare = $request->get('welfare');
        $job->user_id = $request->get('id');
        $job->save();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {   
        $jobs = Job::all();
        $job = Job::where('user_id', $id)->get();
        return response()->json(['jobs' => $job, 'job_all' => $jobs]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {   
        $jobs = Job::all();
        $job = Job::find($id);
        $job->gender = explode(', ', $job->gender);
        return response()->json(['job' => $job, 'job_all' => $jobs]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $gender = null;
        if($request->get('gender')) {
            $gender = implode(', ', $request->get('gender'));
        }
        $job = Job::find($id);
        $job->job_title = $request->get('job_title');
        $job->job_type = $request->get('job_type');
        $job->department = $request->get('department');
        $job->salary = $request->get('salary');
        $job->branch = $request->get('branch');
        $job->day = $request->get('day');
        $job->time = $request->get('time');
        $job->gender = $gender;
        $job->num = $request->get('num');
        $job->degree = $request->get('degree');
        $job->gpax = $request->get('gpax');
        $job->phase = $request->get('phase');
        $job->feature = $request->get('feature');
        $job->role = $request->get('role');
        $job->welfare = $request->get('welfare');
        $job->save();
    }

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