<?php

namespace App\Post;

use App\Core\AbstractController;

class PostsController extends AbstractController
{
  public function __construct(
    PostsRepository $postsRepository,
    CommentsRepository $commentsRepository
  )
  {
      $this->postsRepository = $postsRepository;
      $this->commentsRepository = $commentsRepository;
  }

  public function index()
  {
      $posts = $this->postsRepository->all();
      $this->render("post/index", [
        'posts' => $posts
      ]);
  }

  public function index_c()
  {
    $post = $this->postsRepository->all();  //find($id);
    $comments = $this->commentsRepository->allByPost($id);
    $this->render("post/show", [
      'post' => $post,
      'comments' => $comments
    ]);
  }



  public function comment()
  {
     $id = $_GET['id'];
     if (isset($_POST['content'])) {
         $content = $_POST['content'];
         //$this->commentsRepository->insertForPost($id, $content);
     }
     $post = $this->postsRepository->find($id);
     $comments = $this->commentsRepository->allByPost($id);
     //$this->render("post/show", [
     $this->render("post/comment", [
       'post' => $post,
       'comments' => $comments
     ]);
  }

  public function show()
  {
     $id = $_GET['id'];
     echo "ID= ".$id;
      //die();
     if (isset($_POST['content'])) {
         $content = $_POST['content'];
         var_dump($content);
         //$this->commentsRepository->insertForPost($id, $content);
     }
     $post = $this->postsRepository->find($id);
     $comments = $this->commentsRepository->allByPost($id);
     //die();
     //$this->render("post/show", [
     $this->render("post/show", [
       'post' => $post,
       'comments' => $comments
     ]);
  }

  public function getComment($id)
  {
     $post = $this->postsRepository->find($id);
     $comments = $this->commentsRepository->allByPost($id);
     return $comments;
  }

}

 ?>
