<?php

namespace App\Http\Controllers;

use App\User;
use App\Report;
use App\ReportComment;
use App\Models\FinishedContracts;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

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

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Report  $report
     * @return \Illuminate\Http\Response
     */
    public function veredict(Request $request, $id)
    {
        //
        if ($request->isMethod('get'))
        return view('reports.veredict', ['report' => Report::find($id)]);

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

        $messages = [
          'veredict.required' => 'Se requiere un veredicto',
        ];

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

        $report = Report::find($id);
        $report->veredict = strip_tags($request->veredict);
        $coupon->save();

        return redirect('reports');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Report  $report
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Report $report)
    {
        //
    }

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

    public function report(Request $request) {

      $rules = [
        'contract_id' => 'required|numeric',
        'comment' => 'required|string',
      ];

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

        $user = Auth::user();
        $contract = FinishedContracts::where('id', $request->contract_id)->first();

        if ($user->id == $contract->user_id) {
          $report = new Report();
          $report->contract_id = $contract->id;

          $moderator = User::where('role_id', 5)->get();
          if ($moderator != '[]') {
            $report->moderator_id = $moderator->random()->id;
          } else {
            $report->moderator_id = User::where('role_id', 7)->first()->id;
          }
          $report->save();

          $reportcomment = new ReportComment();
          $reportcomment->report_id = $report->id;
          $reportcomment->user_id = $user->id;
          $reportcomment->comment = strip_tags($request->comment);
          $reportcomment->save();

          $contract->status_id = 6;
          $contract->save();

          return response()->json([
              'message' => 'Reporte registrado con éxito, nos comunicaremos con usted por correo electrónico a la brevedad para continuar con el caso'
          ]);
        }
      }
    }
}
