<?php

namespace App\Http\Controllers\Admin\Core;

use App\Datatables\Admin\Core\CommentsDataTable;
use App\Repositories\Core\Comments;
use Illuminate\Http\Request;

class CommentController extends Controller
{
    public function index($model, $model_id)
    {
        $data['comments'] = Comments::getCommentsByModel($model, $model_id);

        return view('Admin.Core.Comments.partials.list-comments', $data);
    }

    public function list(CommentsDataTable $dataTable, $model, $model_id)
    {
        $data['model'] = $model;
        $data['model_id'] = $model_id;

        return $dataTable->render('Admin.Core.Comment.index', $data);
    }

    public function create($model, $model_id)
    {
        $data['comment']['commentable_type'] = $model;
        $data['comment']['commentable_id'] = $model_id;

        return view('Admin.Core.Comments.partials.modal', $data);
    }

    public function edit(Request $request, $id = false)
    {
        $id = $id ? $id : $request->input('id');
        $data = Comments::get($id);

        return view('Admin.Core.Comments.partials.modal', $data);
    }

    public function store(Request $request)
    {
        $data = $request->all();
        Comments::store($data);

        return response()->json(['error' => 0]);
    }

    public function destroy(Request $request, $id = false)
    {
        $id = $id ? $id : $request->input('id');
        Comments::destroy($id);

        return response()->json(['error' => 0]);
    }
}
