<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
use Illuminate\Support\Facades\Auth;
use DataTables;

class PostController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if (Auth::user()->hasRole(['admin', 'super-admin'])) {
            if ($request->ajax()) {
                $query = \DB::table('posts')
                    ->leftJoin('users', 'users.id', '=', 'posts.user_id')
                    ->where('posts.is_read_admin', 0)
                    ->select('posts.id', 'posts.title', 'posts.body', 'posts.created_at', 'users.username as student_username')
                    ->orderBy('posts.created_at', 'desc');

                return DataTables::of($query)
                    ->addIndexColumn()
                    ->addColumn('username', function ($row) {
                        return $row->student_username ? $row->student_username : 'N/A';
                    })
                    ->addColumn('title', function ($row) {
                        return $row->title;
                    })
                    ->addColumn('body_snippet', function ($row) {
                        return \Illuminate\Support\Str::limit($row->body, 90);
                    })
                    ->addColumn('action', function ($row) {
                        return '<a href="' . route('posts.show', $row->id) . '" class="btn-view-ticket"><i class="fas fa-eye"></i> View Ticket</a>';
                    })
                    ->rawColumns(['action'])
                    ->make(true);
            }

            $unreadCount = \DB::table('posts')->where('is_read_admin', 0)->count();
            return view('complain.posts.index', compact('unreadCount'));
        }

        $posts = Post::where('user_id', Auth::user()->id)
            ->where('is_read', 0)
            ->latest()
            ->paginate(15);

        return view('complain.posts.index2', compact('posts'));
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('complain.posts.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'title'=>'required',
            'body'=>'required',
        ]);

        if(Auth::user()->hasRole(['admin', 'super-admin']))
            $field = 'is_admin_read';
         else
            $field = 'is_read';

      $post =  Post::create([
            'title'=> $request->title,
            'body'=> $request->body,
            'user_id' => Auth::user()->id
             ]);

             $post->$field = 1;
             $post->save();

        logActivity('post', 'Created new grievance ticket: ' . $post->title, $post);

        return redirect()->route('posts.show', $post->id);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function show($post)
    {
        $id = is_object($post) ? $post->id : $post;
        $post = Post::find($id);
        if(Auth::user()->hasRole(['admin', 'super-admin']))
            return view('complain.posts.show', compact('post'));
        return view('complain.posts.show2', compact('post'));

    }
}
