<?php

namespace App\Http\Requests\Projets;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;

class UpdateProjetSuivi extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return $this->user()->can('edit_projet');
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'prochaine_echeance' => 'nullable|date_format:d/m/Y',
            'etat_avancement' => 'nullable|string',
            'prochaine_etape' => 'nullable|string',
            'risque' => 'required|in:maitrise,retard,retard_important',
            'niveau_priorite' => 'nullable|integer|between:0,99',
        ];
    }

    public function failedValidation(Validator $validator)
    {
        return redirect()->route('projets.edit', $validator->valid()['projet_id'])->with([
            'errors' => $validator->errors(),
            'active' => 'suivi'
        ]);
    }
}
