<?php

namespace App\Http\Controllers;

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

class JobController extends Controller
{

	public function slugGenerator($slug){
		$lettersNumbersSpacesHypens = '/[^\-\s\pN\pL]+/u';
		$spacesDuplicateHypens = '/[\-\s]+/';
		$slug = preg_replace($lettersNumbersSpacesHypens, '', mb_strtolower($slug, 'UTF-8'));
		$slug = preg_replace($spacesDuplicateHypens, '-', $slug);
		$slug = trim($slug, '-');
		$slug = $slug.'-'.time();
		return $slug;
	}

	public function addJob(Request $request){
		$this->validate($request,[
			'job_title' => 'required',
			'salary' => 'required',
			'company' => 'required',
			'category' => 'required|exists:job_categories,id',
			'expires_at' => 'required'
		]);
		$jobCategory = JobCategory::find($request->category);
		$job = new Job;
		$job->title = $request->job_title;
		$job->vacancy = $request->vacancy;
		$job->salary = $request->salary;
		$job->company_id = $request->company;
		$job->expires_at = $request->expires_at;
		$job->description = nl2br($request->description);
		$job->slug = "";
		$jobCategory->jobs()->save($job);
		$job->slug = $this->slugGenerator($job->title.' '.$job->company->country->name.' '.$job->company->name);
		$job->save();
		flash('You have successfully posted a new job!')->success();
		return redirect()->back();
	}

	public function showJobs(){
		$jobs = Job::orderBy('expires_at','desc')->latest()->paginate(9);
		return view('job.index',compact('jobs'));
	}

	public function showJob($slug){
		$job = Job::where('slug',$slug)->first();
		if(count($job)<1){
			abort(404);
		}
		$jobs = Job::where('id','!=',$job->id)->latest()->limit(4)->get();
		return view('job.show',compact(['job','jobs']));
	}

	public function searchJob(Request $request){
		$categories = JobCategory::latest()->get();
		$jobs = null;
		if($request->has('job_category')){
			if($request->job_category==0){
				$jobs = Job::where('title','like','%'.$request->job_title.'%')->orderBy('expires_at','desc')->paginate(9);
			}
			else{
				$jobs = JobCategory::find($request->job_category)->jobs()->where('title','like','%'.$request->job_title.'%')->orderBy('expires_at','desc')->paginate(9);
			}
		}
		return view('job.search',compact(['jobs','categories']));
	}

	public function showJobsForAdmin(){
		$jobs = Job::latest()->paginate(10);
		$categories = JobCategory::latest()->get();
		$companies = \App\Company::latest()->get();
		return view('admin.job.index',compact(['categories','companies','jobs']));
	}

	public function showAddJob(){
		$categories = JobCategory::latest()->get();
		$companies = \App\Company::latest()->get();
		return view('admin.job.add',compact(['categories','companies']));
	}

	public function deleteJob(Request $request){
		$job = Job::find($request->job_id);
		$jobName = $job->title;
		$job->delete();
		flash('The job '.$jobName.' has been successfully deleted.')->success();
		return redirect()->back();
	}

	public function editJob(Request $request){
		$this->validate($request,[
			'job_title' => 'required',
			'salary' => 'required',
			'company' => 'required|exists:companies,id',
			'category' => 'required|exists:job_categories,id',
			'expires_at' => 'required'
		]);
		$jobCategory = JobCategory::find($request->category);
		$job = Job::find($request->job_id);
		$job->title = $request->job_title;
		$job->vacancy = $request->vacancy;
		$job->salary = $request->salary;
		$job->company_id = $request->company;
		$job->job_category_id = $request->category;
		$job->expires_at = $request->expires_at;
		$job->description = nl2br($request->description);
		$job->slug = $this->slugGenerator($job->title.' '.$job->company->country->name.' '.$job->company->name);
		$job->save();
		flash('You have successfully edited the job!')->success();
		return redirect()->back();
	}
}
