<?php

namespace Community\Src;

use Dever;
use Collection\Lib\Core;

class Api extends Core
{
	public function __construct()
	{
        $this->cate_id = Dever::input('cate_id');
        $this->type = Dever::input('type', 3);
        $this->type_id = Dever::input('type_id', 1);
        parent::__construct();
	}

    # 获取社区下的分类
    public function category()
    {
    	$info_id = $this->id;
    	$page_id = $this->page_id;
        $content_id = Dever::input('content_id');

    	# 之后要通过合集id获取名字
        $cate_data = Dever::db('community/cate')->getAll();

        $where['info_id'] = $info_id;
        $community = Dever::db('collection/community')->one($where);

        $data['cate'] = array();
        if ($community && $community['cate']) {
            $cate = $community['cate'];
            $cate = explode(',', $cate);
            $cate_name = explode(',', $community['cate_name']);
            $i = 0;
            foreach ($cate_data as $k => $v) {
                if (in_array($v['id'], $cate)) {
                    if (isset($cate_name[$i]) && $cate_name[$i]) {
                        $v['name'] = $cate_name[$i];
                    }
                    $data['cate'][$i] = $v;
                    $i++;
                }
            }
        }
        return $data;
    }

    # 获取社区下的帖子
    public function info()
    {
    	$id = Dever::input('info_id');
    	$data = Dever::load('community/lib/info')->getData('getAll', $this->uid, $this->cate_id, $this->type, $this->type_id, false, $id, $this->times, $this->day, $this->id);
        return $data;
    }

    # 获取弹幕、评论
    public function comment()
    {
        $user = Dever::input('user', false);
        $data = Dever::load('community/lib/comment')->getData('getAll', $this->uid, $this->type, $this->type_id, $user, $this->id);
        return $data;
    }

    # 获取聊天泡泡
    public function tips()
    {
        $user = Dever::input('user', false);
        $data = Dever::load('community/lib/tips')->getData('getAll', $this->uid, $this->type, $this->type_id, $user, $this->id);
        return $data;
    }

    # 发布弹幕、评论
    public function addComment()
    {
        $pic = Dever::input('pic');
        $playtime = Dever::input('playtime');
        $content = Dever::input('content');
        if (!$content) {
            Dever::alert('请填写内容');
        }

        $type_id = Dever::input('type_id');
        if (!$type_id) {
            Dever::alert('错误的信息');
        }
        $type = Dever::input('type');
        if (!$type) {
            Dever::alert('错误的信息');
        }

        if ($type == 'content/product') {
            # 对这种评论需要验证是否有权限
            $buy = Dever::load('goods/lib/order')->checkBuy($this->uid, $type_id);
            if (!$buy) {
                Dever::alert('未购买,您没有权限评价');
            }
        }
        
        Dever::load('community/lib/comment')->submit($this->uid, $type_id, $type, $pic, $content, $playtime, 'collection/info', $this->id);

        return 'yes';
    }

    # 发布聊天泡泡
    public function addTips()
    {
        $pic = Dever::input('pic');
        $playtime = Dever::input('playtime');
        $content = Dever::input('content');
        if (!$content) {
            Dever::alert('请填写内容');
        }

        $type_id = Dever::input('type_id');
        if (!$type_id) {
            Dever::alert('错误的信息');
        }
        $type = Dever::input('type');
        if (!$type) {
            Dever::alert('错误的信息');
        }
        $seat = Dever::db('user/seat')->one($type_id);
        if (!$seat || $seat['uid'] != $this->uid) {
            Dever::alert('这不是您的座位');
        }
        
        Dever::load('community/lib/tips')->submit($this->uid, $type_id, $type, $pic, $content, $playtime, 'collection/info', $this->id);

        return 'yes';
    }

    # 发布信息
    public function add()
    {
        $pic = Dever::input('pic');
        $content = Dever::input('content');
        if (!$content) {
            Dever::alert('请填写内容');
        }

        $cate_id = Dever::input('cate_id');
        if (!$cate_id) {
            Dever::alert('错误的信息');
        }
    	$type_id = Dever::input('type_id');
        if (!$type_id) {
            Dever::alert('错误的信息');
        }
        $type = Dever::input('type');
        if (!$type) {
            Dever::alert('错误的信息');
        }

        $to_id = Dever::input('to_id');
        $to_uid = Dever::input('to_uid');
        
        Dever::load('community/lib/info')->submit($this->uid, $cate_id, $type_id, $type, $pic, $content, $to_id, $to_uid, $this->day, 'collection/info', $this->id);

        return 'yes';
    }

    # 点赞
    public function up()
    {
        $id = Dever::input('id');
        if (!$id) {
            Dever::alert('错误的信息');
        }
        $type = Dever::input('type', 20);
        if (!$type) {
            Dever::alert('错误的信息');
        }
        Dever::load('community/lib/up')->submit($this->uid, $id, $type);

        return 'yes';
    }

    # 反对
    public function oppose()
    {
        $id = Dever::input('id');
        if (!$id) {
            Dever::alert('错误的信息');
        }
        $type = Dever::input('type', 20);
        if (!$type) {
            Dever::alert('错误的信息');
        }
        Dever::load('community/lib/oppose')->submit($this->uid, $id, $type);

        return 'yes';
    }
}