<?php

declare(strict_types=1);

namespace app\index\controller;

use app\model\PostComment as ModelPostComment;
use think\facade\Session;
use think\facade\Validate;
use think\Request;
use think\validate\ValidateRule;

class PostComment extends Common
{
  /**
   * 显示资源列表
   *
   * @return \think\Response
   */
  public function index()
  {
    //
  }

  /**
   * 显示创建资源表单页.
   *
   * @return \think\Response
   */
  public function create()
  {
    //
  }

  /**
   * 保存新建的资源
   *
   * @param  \think\Request  $request
   * @return \think\Response
   */
  public function save(Request $request)
  {
    //

    $user_uid = Session::get('user_uid');

    if (empty($user_uid)) {
      return json_message('请先登录');
    }

    $post_data = $request->post();

    $validate = Validate::rule('post_id', ValidateRule::isRequire())
      ->rule('content|评论内容', ValidateRule::isRequire()->length('5,200', '评论字符长度需要在5-200之间'));

    if (!$validate->check($post_data)) {
      return json_message($validate->getError());
    }

    $post_data['type'] = 3;


    $post_data['user_uid'] = $user_uid;

    ModelPostComment::create($post_data);

    ModelPostComment::clearCountCache();
    

    return json_message();
  }

  /**
   * 显示指定的资源
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function read($id)
  {
    //
  }

  /**
   * 显示编辑资源表单页.
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function edit($id)
  {
    //
  }

  /**
   * 保存更新的资源
   *
   * @param  \think\Request  $request
   * @param  int  $id
   * @return \think\Response
   */
  public function update(Request $request, $id)
  {
    //
  }

  /**
   * 删除指定资源
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function delete($id)
  {
    //

    ModelPostComment::destroy($id);

    ModelPostComment::clearCountCache();

    return json_message();
  }
}
