<?php

namespace App\Http\Controllers;

use App\iChambaParameter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class IChambaParameterController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
     public function index(Request $request)
     {
         //
         $request->session()->put('search', $request
               ->has('search') ? strip_tags($request->get('search')) : ($request->session()
               ->has('search') ? strip_tags($request->session()->get('search')) : ''));

         $request->session()->put('field', $request
                 ->has('field') ? strip_tags($request->get('field')) : ($request->session()
                 ->has('field') ? strip_tags($request->session()->get('field')) : 'id'));

         $request->session()->put('sort', $request
                 ->has('sort') ? strip_tags($request->get('sort')) : ($request->session()
                 ->has('sort') ? strip_tags($request->session()->get('sort')) : 'asc'));

         //$headers = $request->get('header') != '' ? $request->get('header') : -1;

         $ichambaparameters = new iChambaParameter();
         $ichambaparameters = $ichambaparameters->where('id', 'LIKE', '%' . strip_tags($request->session()->get('search')) . '%')
                        ->orwhere('parameter', 'LIKE', '%' . strip_tags($request->session()->get('search')) . '%')
                        ->orwhere('num_value', 'LIKE', '%' . strip_tags($request->session()->get('search')) . '%')
                        ->orwhere('string_value', 'LIKE', '%' . strip_tags($request->session()->get('search')) . '%')
                         ->orderBy(strip_tags($request->session()->get('field')), strip_tags($request->session()->get('sort')))
                         ->paginate(10);

         if ($request->ajax()) {
             return view('parameters.index', compact('ichambaparameters'));
           } else {
             return view('parameters.ajax', compact('ichambaparameters'));
           }

     }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
     public function create(Request $request)
     {
         //
         if ($request->isMethod('get'))
         return view('parameters.form');

         $rules = [
           'parameter' => 'required|string',
           'num_value' => 'nullable|numeric',
           'string_value' => 'nullable|string',
         ];

         $messages = [
           'parameter.required' => 'Se requiere un nombre de parametro',
         ];

         $validator = Validator::make($request->all(), $rules, $messages);
         if ($validator->fails()) {
           return redirect()->back()->withInput($request->all())->withErrors($validator);
         }

         $ichambaparameter = new iChambaParameter();
         $ichambaparameter->parameter = strip_tags($request->parameter);
         $ichambaparameter->num_value = $request->num_value;
         $ichambaparameter->string_value = strip_tags($request->string_value);
         $ichambaparameter->save();

         return redirect('parameters');
     }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\iChambaParameter  $iChambaParameter
     * @return \Illuminate\Http\Response
     */
    public function show(iChambaParameter $iChambaParameter)
    {
        //
    }

    public function parameters()
    {
        //
         $ichambaparameters = new iChambaParameter();
         $min_time = $ichambaparameters->where('parameter', 'min_time')->first()->num_value;
         $max_time = $ichambaparameters->where('parameter', 'max_time')->first()->num_value;

         return response()->json([
             'min_time' => $min_time,
             'max_time' => $max_time
         ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\iChambaParameter  $iChambaParameter
     * @return \Illuminate\Http\Response
     */
    public function edit(iChambaParameter $iChambaParameter)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\iChambaParameter  $iChambaParameter
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        if ($request->isMethod('get'))
        return view('parameters.form', ['ichambaparameter' => iChambaParameter::find($id)]);

        $rules = [
          'parameter' => 'required|string',
          'num_value' => 'nullable|numeric',
          'string_value' => 'nullable|string',
        ];

        $messages = [
          'parameter.required' => 'Se requiere un nombre de parametro',
        ];

        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
          return redirect()->back()->withInput($request->all())->withErrors($validator);
        }

        $ichambaparameter = iChambaParameter::find($id);
        $ichambaparameter->parameter = strip_tags($request->parameter);
        $ichambaparameter->num_value = $request->num_value;
        $ichambaparameter->string_value = strip_tags($request->string_value);
        $ichambaparameter->save();

        return redirect('parameters');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\iChambaParameter  $iChambaParameter
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        iChambaParameter::destroy($id);
        return redirect('parameters');
    }
}
