<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Asset;
use App\Rules\ExistRule;
use EF;

class AssetRequest 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()
    {
          EF::errorMessage('Required form validation.');
        
        $current_path = url()->current();

        if($current_path === url('assetcommit'))
        {
           return $this->storeAsset();
        }

        elseif($current_path === url('assetupdate'))
        {
            return $this->updateAsset();
        }
    }

     public function storeAsset()
    {

        $this->type = ucfirst($this->input('type'));

        $asset = new Asset;

        $types = $asset->getDistinctAsset('type',['type']);

        $types = array_map('current', $types->toArray());

        $types = implode(',',$types);



        $common_validation = [

             'asset_type'                       =>  'required|In:'.$types,
             'name'                             =>  'required|min:3|max:50',
             'description'                      =>  'required|min:3|max:500',
             'code'                             =>  'required|min:0|max:50',
         
             
        ];

        if($this->input('asset_type') === "Other")
        {
            $common_validation['asset_type']              = 'nullable';
            $common_validation                           += ['type' => 'required|NotIn:'.$types];
        }

        return $common_validation;
    }

     public function updateAsset()
    {
      
        return [

             'asset'                              =>  ['required', new ExistRule('assets','id')],
             'name'                               =>  'required|min:3|max:50',
             'description'                        =>  'required|min:3|max:500',
             'code'
           
            
             
             
          
        ];
    }

}
