<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Mail\UserAppliedForJob;
use App\Job;
use App\JobApplication;
use Mail;

class JobApplicationController extends Controller
{
	public function addApplication(Request $request){
		$this->validate($request,[
			'g-recaptcha-response' => 'required|captcha',
			'name' => 'required',
			'job' => 'required|exists:jobs,id',
			'phone' => 'required',
			'image' => 'required|mimes:pdf,jpg,png,doc,docx,jpeg'
		]);
		$job = Job::find($request->job);
		$fileManager = new \App\Http\Controllers\FileManager;
		$path = '/user_files/cv';
		$application = new JobApplication;
		$application->name = $request->name;
		if($request->has('email')){
			$application->email = $request->email;
		}
		$application->phone = $request->phone;
		$application->cv = $fileManager->moveFile($request,$path);
		$job->applications()->save($application);
		Mail::to('hrd@amgroup.com.np')->cc(['md@amgroup.com.np','ceo@amgroup.com.np'])->send(new UserAppliedForJob($application));
		flash('<p style="font-size:1.2rem" class="text-center text-success">Thank you for you applying for the job. We have recieved your application and will reach back to you as soon as possible!</p>')->success();
		return redirect()->back();
	}
}
