<?php

namespace App\Http\Controllers;

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

class StatusController extends Controller
{
    //
    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;

        $status = new Status();
        $status = $status->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('status.index', compact('status'));
          } else {
            return view('status.ajax', compact('status'));
          }

    }

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

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

        $messages = [
          'name.required' => 'Se requiere el nombre del status',
          'en_name.required' => 'Se requiere el nombre del status en inglés',
        ];

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

        $status = new Status();
        $status->name = strip_tags($request->name);
        $status->en_name = strip_tags($request->en_name);
        $status->save();

        return redirect('status');
    }

    /**
     * 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('status.form',['status' => Status::find($id)]);

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

        $messages = [
          'name.required' => 'Se requiere el nombre del status',
          'en_name.required' => 'Se requiere el nombre del status en inglés',
        ];

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

        $status = Status::find($id);
        $status->name = strip_tags($request->name);
        $status->en_name = strip_tags($request->en_name);
        $status->save();

        return redirect('status');
    }

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