<?php

namespace App\Http\Controllers;

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

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

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

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

        $rules = [
          'name' => 'required|string',
          'percentage' => 'nullable|numeric',
          'amount' => 'nullable|numeric',
          'limit' => 'nullable|numeric',
        ];

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

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

        $coupon = new Coupon();
        $coupon->name = strip_tags($request->name);
        $coupon->percentage = $request->percentage;
        $coupon->amount = $request->amount;
        $coupon->limit = $request->limit;
        $coupon->save();

        return redirect('coupons');
    }

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

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

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

        $rules = [
          'name' => 'required|string',
          'percentage' => 'nullable|numeric',
          'amount' => 'nullable|numeric',
          'limit' => 'nullable|numeric',
        ];

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

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

        $coupon = Coupon::find($id);
        $coupon->name = strip_tags($request->name);
        $coupon->percentage = $request->percentage;
        $coupon->amount = $request->amount;
        $coupon->limit = $request->limit;
        $coupon->save();

        return redirect('coupons');
    }

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