<?php

namespace App\Http\Controllers;

use App\ReportComment;
use App\Models\FinishedContracts;
use App\NoHome;
use Illuminate\Http\Request;

class ReportCommentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, $id, $contract_id)
    {
        //
        $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;

        $contract = FinishedContracts::where('id', $contract_id)->first();
        $nohome = NoHome::where('contract_id', $contract_id)->first();
        $comments = new ReportComment();
        $comments = $comments->where('report_id', $id)
                        ->orderBy(strip_tags($request->session()->get('field')), strip_tags($request->session()->get('sort')))
                        ->paginate(10);

        if ($request->isMethod('get'))
        return view('reports.comments', compact('comments', 'contract', 'nohome'));
    }

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

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

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

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