<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Categories;
use Illuminate\Support\Facades\Validator;

class CategoriesController 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;

        $categories = new Categories();
        $categories = $categories->where('id', 'LIKE', '%' . strip_tags($request->session()->get('search')) . '%')
                       ->orwhere('name', '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('categories.index', compact('categories'));
          } else {
            return view('categories.ajax', compact('categories'));
          }

    }

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

        $rules = [
          'name' => 'required|string',
          'en_name' => 'required|string',
        ];

        $messages = [
          'name.required' => 'Se requiere el nombre de la categoría',
          'en_name.required' => 'Se requiere el nombre de la categoría (en ingles)',
        ];

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

        $category = new Categories();
        $category->name = strip_tags($request->name);
        $category->en_name = strip_tags($request->en_name);
        $category->save();

        return redirect('categories');
    }

    /**
     * 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  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

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

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

          $rules = [
            'name' => 'required|string',
            'en_name' => 'required|string',
          ];

          $messages = [
            'name.required' => 'Se requiere el nombre de la categoría',
            'en_name.required' => 'Se requiere el nombre de la categoría (en ingles)',
          ];

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

          $category = Categories::find($id);
          $category->name = strip_tags($request->name);
          $category->en_name = strip_tags($request->en_name);
          $category->save();

          return redirect('categories');
    }

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