<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2023/8/21
 * Time: 10:04
 */
namespace app\api\controller;

use app\api\model\Addr;
use app\api\model\Like;
use app\common\controller\Api;
use \app\api\model\Comment as Arcomment;
use fast\Tree;

class Comment extends Api{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 提交评论
     */
    public function submitComment(){
        $article_id = $this->request->post('article_id');
        $province_id = $this->request->post('province_id');
        $content = xss_clean($this->request->post('content'));
        $addr = Addr::getFind(['ID' =>$province_id],'Name');
        $data = [
            'article_id' =>$article_id,
            'user_id' =>$this->auth->id,
            'content' =>$content,
            'ip' =>$this->request->ip(),
            'createtime' =>time(),
            'province' =>$addr['Name']
        ];
        $res=Arcomment::create($data);
        if(!$res->id){
            $this->error('提交失败');
        }
        $this->success('提交成功',['info' =>$res->toArray()]);
    }

    /**
     * 回复评论
     */
    public function replyComment(){
        $article_id = $this->request->post('article_id',0);
        $reply_id = $this->request->post('reply_id');//回复评论id
        $province_id = $this->request->post('province_id');
        $content = xss_clean($this->request->post('content'));
        $addr = Addr::getFind(['ID' =>$province_id],'Name');
        $data = [
            'article_id' =>$article_id,
            'reply_user_id' =>$this->auth->id,
            'content' =>$content,
            'ip' =>$this->request->ip(),
            'reply_id' =>$reply_id,
            'replytime' =>time(),
            'province' =>$addr['Name']
        ];
        $res=Arcomment::create($data);
        if(!$res->id){
            $this->error('回复失败');
        }
        $this->success('提交成功',['info' =>$res->toArray()]);
    }

    /**
     * 获取评论列表
     */
    public function getCommentList(){
        $article_id = $this->request->post('article_id',0);
        $page = $this->request->post('page',1);

        $list = Arcomment::alias('c')
//            ->join('user u','c.user_id = u.id','left')
            ->join('user u','c.reply_user_id = u.id','left')
            ->where(['c.article_id' =>$article_id,'c.status'=>0])
//            ->page($page)
//            ->limit($this->pageSize)
            ->field('c.*,u.nickname,u.avatar')
            ->select();
        if(!$list){
            $this->error('暂无评论');
        }
        $total_count = Arcomment::alias('c')
            ->where(['c.article_id' =>$article_id,'c.status'=>0])
            ->count();
        Tree::instance()->init($list,'reply_id');
        foreach ($list as $key => $val){
            if(!$val['user_id']){
                unset($list[$key]);
                continue;
            }
            if($val['nickname'] == null){
                $info = \app\common\model\User::where(['id' =>$val['user_id']])->find();
                $val['nickname'] = $info['nickname'];
                $val['avatar'] = $info['avatar'];
            }
            $list[$key]['sub'] = Tree::instance()->getChildren($val['id']);//递归查询
        }
        sort($list);//排序
        $list = array_slice($list, ($page-1)*$this->pageSize, $this->pageSize);//数组进行分页
        $this->success('获取成功',['list' =>$list,'total_count' =>$total_count]);
    }

    /**
     * 删除评论回复
     */
    public function del(){
        $comment_id = $this->request->post('comment_id',0);
        if (empty($comment_id)) {
            $this->error(__('Parameter %s can not be empty', 'comment_id'));
        }
        $where = ['id' =>$comment_id];
        $info = Arcomment::where($where)->find();
        if(!$info){
            $this->error('此评论不存在');
        }
        if($info['status'] == -1){
            $this->error('此评论已被删除');
        }
        $res = Arcomment::update(['status' =>-1],$where);
        $this->success('删除成功');
    }

    /**
     * 点赞
     */
    public function like(){
        $comment_id = $this->request->post('comment_id',0);
        if (empty($comment_id)) {
            $this->error(__('Parameter %s can not be empty', 'comment_id'));
        }
        $where = ['id' =>$comment_id];
        $info = Arcomment::where($where)->find();
        if(!$info){
            $this->error('此评论不存在');
        }
        if($info['status'] == -1){
            $this->error('此评论已被删除');
        }
        $like_where = ['comment_id' =>$comment_id,'user_id'=>$this->auth->id];
        $like= Like::where($like_where)->find();
        if(!$like){
            Arcomment::where($where)->setInc('likes',1);
            $data = [
                'comment_id' =>$comment_id,
                'user_id' =>$this->auth->id,
                'createtime' =>time(),
            ];
            Like::create($data);
            $this->success('点赞成功');
        }
        if($like['status'] == 0){
            Arcomment::where($where)->setDec('likes',1);
            Like::update(['status' =>-1],$like_where);
        }else{
            Arcomment::where($where)->setInc('likes',1);
            Like::update(['status' =>0],$like_where);
        }
        $this->success('操作成功');
    }

}