<?php

namespace App\Http\Requests;

use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class ProjectRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
       public function rules()
    {
       
        $rules = [
            'package_id'=>'required',
            'project_name'=>'required',
            'pay_type'=>'required',
            'credit_number'=>'required',
            'month'=>'required',
            'year'=>'required',
            'code'=>'required',
            'team_work'=>'required',
            'license'=>'required',
            'description'=>'required',
            ];
        return $rules;
    }
    public function attributes()
    {
        return [
            'package_id.required'=>__('site.package'),
            'project_name.required'=>__('site.project_name'),
            'pay_type.required'=>__('site.pay_type'),
            'credit_number.required'=>__('site.credit_number'),
            'month.required'=>__('site.month'),
            'year.required'=>__('site.year'),
            'code.required'=>__('site.code'),
            'team_work.required'=>__('site.team_work'),
            'license.required'=>__('site.license'),
            'description.required'=>__('site.description'),
           
        ];
    }
    /**
     * Handle a failed validation attempt.
     *
     * Override the parent method action if the request is for AJAX
     *
     * @param Validator $validator
     * @return void
     *
     * @throws ValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        // Change response attitude if the request done via Ajax requests like API requests
        if(Request::wantsJson()){
            $errors = (new ValidationException($validator))->errors();
            throw new HttpResponseException(response()->json(['errors' => $errors], 422));
        } else {
            // Do the original action of the Form request class
            parent::failedValidation($validator);
        }
    }
}

