<?php

namespace App\Http\Requests\InventoryRequests;

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

class ItemRequest 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('productcommit'))
        {
           return $this->getValidationList();
        }
        elseif($current_path === url('productupdate'))
        {
          $validation = $this->getValidationList();

          $validation['product'] = ['required', new ExistRule('items','id')];
          //$validation['product_code']   = ['required','min:3','max:50', new UpdateUniqueRule('items','code',$this->product)];

           return $validation;
        }
        elseif($current_path === url('servicecommit'))
        {
           return $this->getServiceValidationList();
        }
        elseif($current_path === url('serviceupdate'))
        {
          $validation = $this->getServiceValidationList();

          $validation['service'] = ['required', new ExistRule('items','id')];
          //$validation['service_code']   = ['required','min:3','max:50', new UpdateUniqueRule('items','code',$this->service)];

           return $validation;
        }

    }

    public function getValidationList()
    {




        $validation = [

         //  'project'              =>  ['required', new ExistRule('branches','id')],

           'product_unit'         =>  ['required', new ExistRule('inventory_units','id')],

           'product_category'     =>  ['required', new ExistRule('inventory_categories','id')],

           //'product_code'         => 'required|unique:items,code|min:0|max:50',

           'product_name'          => 'required|min:3|max:50',

           'product_description'   =>  'nullable|max:500',

           'product_minimum'       =>  'nullable|integer|bail|gt:0',

           'product_maximum'       =>  'nullable|integer|bail|gt:0',


        ];


        return $validation;
    }

    public function getServiceValidationList()
    {


        $unit_list = implode(',', EF::getServiceUnitList());

        $category_list = implode(',', EF::getServiceCategoryList());


        $validation = [

          // 'project'              =>  ['required', new ExistRule('branches','id')],

            'service_unit'         =>  ['required', new ExistRule('inventory_units','id')],

           'service_category'     =>  ['required', new ExistRule('inventory_categories','id')],

           //'service_code'         => 'required|unique:items,code|min:0|max:50',

           'service_name'         => 'required|min:3|max:50',

           'service_description'   =>  'nullable|max:500',

           'service_minimum'       =>  'nullable|integer|bail|gt:0',

           'service_maximum'       =>  'nullable|integer|bail|gt:0',
        ];


        return $validation;
    }
}
