<?php

namespace Forum\Src;

use Dever;

class Review
{
    public function __construct()
    {
        $this->post = Dever::input('post');
    }

    public function get()
    {
        $param['option_posts_id'] = $this->post;
        $param['page'] = array(50, 'list');

        $data = Dever::load('forum/review-list', $param);

        return $data;
    }

    public function _token()
    {
        $token['post'] = Dever::load("forum/posts.info#id");

        return http_build_query(Dever::token($token));
    }

    public function _url()
    {
        return Dever::proxy('forum/review.save', $this->_token());
    }

    public function save()
    {
        # 检测是否登录
        $comment = Dever::load('main/config-one#comment', 1);

        $user = Dever::load('passport/user.data');

        if (!$user) {
            if ($comment == 1) {
                Dever::alert('请先登录');
            }
            $user['id'] = -1;
        }

        $param['option_posts_id'] = $this->post;
        $param['option_content'] = Dever::input('content');
        $param['option_uid'] = $user['id'];

        if (!$param['option_posts_id']) {
            Dever::alert('请选择要评论的主题');
        }

        if (!$param['option_content']) {
            Dever::alert('请填写内容');
        }

        $post = Dever::load('forum/posts-one', $param['option_posts_id']);
        if (!$post) {
            Dever::alert('请选择要评论的主题');
        }

        $cate = Dever::load('forum/cate-one', $post['cate_id']);
        if (!$cate) {
            Dever::alert('请选择要评论的主题');
        }
        if ($cate['comment'] == 2) {
            Dever::alert('您还不能发布评论');
        }

        //$info = Dever::load('forum/review-one', $param);
        $info = false;

        if ($info) {
            Dever::alert('你要发布的内容已经存在，请不要重复发布');
        } else {
            $param['add_posts_id'] = $param['option_posts_id'];
            $param['add_content'] = $param['option_content'];
            $param['add_uid'] = $user['id'];
            $param['add_floor'] = $this->floor();
            $param['add_ip'] = Dever::ip();

            $id = Dever::load('forum/review-insert', $param);

            if ($id > 0) {
                Dever::load('forum/posts-addReview', array('where_id' => $param['option_posts_id']));
            }

            Dever::out($id);
        }
    }

    private function floor()
    {
        $info = Dever::load('forum/review-new', array('where_posts_id' => $this->post));
        if ($info) {
            $floor = $info[0]['floor'];
        } else {
            $floor = 0;
        }

        $floor += 1;

        return $floor;
    }

    public function info($data = array())
    {
        if (!$data) {
            Dever::alert(404);
        }

        $data['content'] = Dever::markdown($data['content']);

        $data['time'] = Dever::mdate($data['cdate'], 2);
        $data['cdate'] = date('Y-m-d H:i:s', $data['cdate']);

        $data['user_name'] = $data['uid'] > 0 ? Dever::load('passport/user-one#username', $data['uid']) : Dever::load('main/config-one#noname', 1);
        $data['user_link'] = Dever::url('forum/list?uid=' . $data['uid'], 'main');
        $data['user_avatar'] = Dever::load('passport/user-one#avatar', $data['uid']);

        //$data['floor']    = '#' . $data['floor'];

        return $data;
    }
}
