<?php

namespace App\Http\Requests;

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

class InventoryRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    private $white_labels;

    public function __construct()
    {
        $this->setWhiteLabel(EF::plotWhiteLabel());


    }

    public function authorize()
    {

        return true;
    }

    public function setWhiteLabel($list = [])
    {
        $this->white_labels = $list;


    }

    public function getWhiteLabel()
    {
        return $this->white_labels;
    }

    /**
     * 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('inventorycommit'))
        {
           $validation = $this->getValidationList();


           return $validation;

           // return $this->inventoryUpdate();
        }
        elseif($current_path === url('inventoryupdate'))
        {
           $validation = $this->getValidationList();

           $labels = $this->getWhiteLabel();
           
           $validation[$labels['inventory']]  = new ExistRule('plots','id');
           $validation[$labels['branch']] = 'nullable';
           


           return $validation;

           // return $this->inventoryUpdate();
        }
        elseif($current_path === url('inventorybulk'))
        {
           $labels = $this->getWhiteLabel();

           $validation[$labels['rate']]   = 'required|Integer|bail|gte:0';


           return $validation;

           // return $this->updateBulkInventory();
        }
    
    }

    // public function inventoryUpdate()
    // {
    //     return [

    //         'inventory'       => new ExistRule('plots','id'),
    //         'rate'            => 'nullable|Integer|gte:0',
    //     ];
    // }

    // public function updateBulkInventory()
    // {
    //     return [

    //         'rate'            => 'nullable|Integer|gte:0',
    //     ];
    // }


    public function getValidationList()
    {
      
        $labels = $this->getWhiteLabel();


        $plot_type    = implode(',', EF::getTypeList());

        $plot_category = implode(',', EF::getCategoryList());

        $plot_floor  = implode(',', EF::getFloorList());


        $validation = [

          $labels['plot_name']              => 'required',

          $labels['dimension_x']            => 'nullable|integer|bail|gte:0',

          $labels['dimension_y']            => 'nullable|integer|bail|gte:0',
      
          $labels['category']               => 'required|in:'.$plot_category,

          $labels['feature_charges']        => 'nullable|integer|bail|gte:0',
          
          $labels['feature_description']    => 'nullable|min:1|max:245',

          // $labels['location']               => 'required|min:1|max:245',

          $labels['size']                   => 'required|min:1|max:50',

          $labels['rate']                   => 'required|integer|bail|gte:0',

          $labels['type']                   => 'required|in:'.$plot_type,

          $labels['floor']                  => 'nullable|in:'.$plot_floor,

          $labels['branch']                 => ['required',new ExistRule('branches','id')],

          
        ];


        return $validation;
    }


   
}
