<?php
/*
 *	www.myucms.com
 *  作者：梦雨
 *  @ QQ50361804
*/
namespace app\bbs\controller;

use think\Controller;
use think\Db;
use app\bbs\model\Comment as CommentModel;
class Comment extends Controller
{
    public function add()
    {
        if (!session('userid') || !session('username')) {
            $this->error('亲！请登录');
        } else {
            $comment = new CommentModel();
            $id = input('id');
            $tid = input('tid');
            $uid = session('userid');
            if (request()->isPost()) {
                $data = input('post.');
                $data['time'] = time();
                $data['fid'] = $id;
                //二级回复
                if ($tid == null) {
                    $data['tid'] = 0;
                } else {
                    $data['tid'] = $tid;
                }
                $data['uid'] = session('userid');
                $data['content'] = remove_xss($data['content']);
                $t = Db::name('forum')->find($id);
                point(config('point.EDIT_POINT'), session('userid'), '回帖：' . $t['title'], $id);
                $forum = Db::name('forum');
                $forum->where('id', $id)->setInc('reply', 1);
                if ($comment->add($data)) {
                    $userid = Db::name('forum')->where('id', $id)->value('uid');
                    $dataa['uid'] = session('userid');
                    $userids = Db::name('comment')->where('id', $tid)->value('uid');
                    if ($tid == null) {
                        $dataa['userid'] = $userid;
                    } else {
                        $dataa['userid'] = $userids;
                    }
                    $dataa['tid'] = $id;
                    $dataa['file'] = 1;
                    $dataa['open'] = 0;
                    $dataa['time'] = time();
                    Db::name('xiaoxi')->insert($dataa);
                    return json(array('code' => 200, 'msg' => '回复成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '回复失败'));
                }
            }
        }
    }
    public function edit()
    {
        if (!session('userid') || !session('username')) {
            $this->error('亲！请登录');
        } else {
            $id = input('id');
            $uid = session('userid');
            $comment = new CommentModel();
            $a = $comment->find($id);
            if (empty($id) || $a == null || $a['uid'] != $uid) {
                $this->error('亲！您迷路了');
            } else {
                if (request()->isPost()) {
                    $data = input('post.');
                    $data['content'] = remove_xss($data['content']);
                    if ($comment->edit($data)) {
                        return json(array('code' => 200, 'msg' => '修改成功'));
                    } else {
                        return json(array('code' => 0, 'msg' => '修改失败'));
                    }
                }
                $tptc = $comment->alias('c')->join('forum f', 'f.id=c.fid')->field('c.*,f.title')->find($id);
                $this->assign('tptc', $tptc);
                return view();
            }
        }
    }
    public function dels()
    {
        $id = input('id');
        //查询是否为版主
        $c = Db::name('comment')->find($id);
        $t = Db::name('forum')->find($c['fid']);
        $uid = session('userid');
        $isbanzhu = 0;
        if (!empty($uid)) {
            $res = banzhu($uid, $t['tid']);
            if ($res) {
                $isbanzhu = 1;
            }
        }
        $this->assign('isbanzhu', $isbanzhu);
        if (session('userid') != $isbanzhu) {
            $this->error('谁给你的权限？');
        } else {
            if (db('comment')->delete(input('id'))) {
                return json(array('code' => 200, 'msg' => '删除成功'));
            } else {
                return json(array('code' => 0, 'msg' => '删除失败'));
            }
        }
    }
    public function delstz()
    {
        $id = input('id');
        //查询是否为版主
        $t = Db::name('forum')->find($id);
        $uid = session('userid');
        $isbanzhu = 0;
        if (!empty($uid)) {
            $res = banzhu($uid, $t['tid']);
            if ($res) {
                $isbanzhu = 1;
            }
        }
        $this->assign('isbanzhu', $isbanzhu);
        if (session('userid') != $isbanzhu) {
            $this->error('谁给你的权限？');
        } else {
            if (db('forum')->delete(input('id'))) {
                return json(array('code' => 200, 'msg' => '删除成功'));
            } else {
                return json(array('code' => 0, 'msg' => '删除失败'));
            }
        }
    }
    public function sc()
    {
        if (!session('userid') || !session('username')) {
            return json(array('code' => 0, 'msg' => '请登录后收藏'));
        } else {
            $id = input('id');
            $data['uid'] = session('userid');
            $data['tid'] = $id;
            $data['file'] = 1;
            $data['time'] = time();
            $t = Db::name('shoucang')->where('uid', session('userid'))->where('tid', $id)->count();
            if ($t > 0) {
                $da = Db::name('shoucang')->where('uid', session('userid'))->where('tid', $id)->value('id');
                if (db('shoucang')->delete($da)) {
                    return json(array('code' => 200, 'msg' => '取消收藏成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '取消收藏失败'));
                }
            } else {
                if (Db::name('shoucang')->insert($data)) {
                    return json(array('code' => 200, 'msg' => '收藏成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '收藏失败'));
                }
            }
        }
    }
    public function ding()
    {
        if (!session('userid') || !session('username')) {
            return json(array('code' => 0, 'msg' => '请登录'));
        } else {
            $id = input('id');
            //查询是否为版主
            $t = Db::name('forum')->find($id);
            $uid = session('userid');
            $isbanzhu = 0;
            if (!empty($uid)) {
                $res = banzhu($uid, $t['tid']);
                if ($res) {
                    $isbanzhu = 1;
                }
            }
            $this->assign('isbanzhu', $isbanzhu);
            if (session('userid') != $isbanzhu) {
                $this->error('谁给你的权限？');
            } else {
                $data['settop'] = 1;
                $datas['settop'] = 0;
                $da = Db::name('forum')->where('id', $id)->value('settop');
                if ($da == 0) {
                    if (Db::name('forum')->where("id = {$id}")->update($data)) {
                        return json(array('code' => 200, 'msg' => '置顶成功'));
                    } else {
                        return json(array('code' => 0, 'msg' => '置顶失败'));
                    }
                } else {
                    if (Db::name('forum')->where("id = {$id}")->update($datas)) {
                        return json(array('code' => 200, 'msg' => '取消置顶成功'));
                    } else {
                        return json(array('code' => 0, 'msg' => '取消置顶失败'));
                    }
                }
            }
        }
    }
    public function jin()
    {
        if (!session('userid') || !session('username')) {
            return json(array('code' => 0, 'msg' => '请登录'));
        } else {
            $id = input('id');
            //查询是否为版主
            $t = Db::name('forum')->find($id);
            $uid = session('userid');
            $isbanzhu = 0;
            if (!empty($uid)) {
                $res = banzhu($uid, $t['tid']);
                if ($res) {
                    $isbanzhu = 1;
                }
            }
            $this->assign('isbanzhu', $isbanzhu);
            if (session('userid') != $isbanzhu) {
                $this->error('谁给你的权限？');
            } else {
                $data['choice'] = 1;
                $datas['choice'] = 0;
                $da = Db::name('forum')->where('id', $id)->value('choice');
                if ($da == 0) {
                    if (Db::name('forum')->where("id = {$id}")->update($data)) {
                        return json(array('code' => 200, 'msg' => '精选成功'));
                    } else {
                        return json(array('code' => 0, 'msg' => '精选失败'));
                    }
                } else {
                    if (Db::name('forum')->where("id = {$id}")->update($datas)) {
                        return json(array('code' => 200, 'msg' => '取消精选成功'));
                    } else {
                        return json(array('code' => 0, 'msg' => '取消精选失败'));
                    }
                }
            }
        }
    }
}