<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use App\ProyectoEP15;

class ProyectoEP15Request extends FormRequest {
    public function authorize(){
        return true;
    }

    
    public function rules(){
        if(isset($this->id)){ // Actualizacion
            return [
                "id_laboratorio" => [
                    "required",
                    "min:1",
                    "max:40"
                ],
                "id_area" => [
                    "required",
                    "min:1",
                    "max:40"
                ],
                'fecha_inicio' => [
                    "required",
                    "date",
                    Rule::unique('corrida_area','fecha_inicio')->where(function ($query) {
                        return $query->where([
                            ["laboratorio_id_laboratorio","=",$this->id_laboratorio],
                            ["area_id_area","=",$this->id_area]
                        ]);
                    })
                ],
                'material_utilizado' => [
                    "required",
                    "min:2",
                    "max:100",
                ],
                'num_niveles' => [
                    "required",
                    "min:1",
                    "max:5",
                    "numeric"
                ]
            ];
        } else { // Cuando es un registro
            return [
                "id_laboratorio" => [
                    "required",
                    "min:1",
                    "max:40"
                ],
                "id_area" => [
                    "required",
                    "min:1",
                    "max:40"
                ],
                'fecha_inicio' => [
                    "required",
                    "date",
                    Rule::unique('corrida_area','fecha_inicio')->where(function ($query) {
                        $laboratorio = (isset($this->id_laboratorio)) ? $this->id_laboratorio : 0;
                        $area = (isset($this->id_area)) ? $this->id_area : 0;

                        return $query->where([
                            ["laboratorio_id_laboratorio","=",$laboratorio],
                            ["area_id_area","=",$area]
                        ]);
                    })
                ],
                'material_utilizado' => [
                    "required",
                    "min:2",
                    "max:100",
                ],
                'num_niveles' => [
                    "required",
                    "min:1",
                    "max:5",
                    "numeric"
                ]
            ];
        }   
    }
    
    
    public function attributes(){
        return [
            'id_laboratorio' => 'laboratorio',
            'id_area' => 'área',
            'fecha_inicio' => 'fecha de inicio',
            'material_utilizado' => 'material utilizado',
            'num_niveles' => 'número de niveles'
        ];
    }
    

    public function messages(){
        return [
            'id_laboratorio.required' => 'El campo es obligatorio',
            'id_area.required' => 'El campo es obligatorio',
            'fecha_inicio.required' => 'El campo es obligatorio',
            'material_utilizado.required' => 'El campo es obligatorio',
            'num_niveles.required' => 'El campo es obligatorio',
            'fecha_inicio.unique' => 'Ya existe un proyecto con las mismas especificaciones'
        ];
    }
}