<?php namespace Place\Api;
use Dever;
use Place;
use Place\Lib\Main;
use Place\Lib\Cate;
use Place\Lib\Resource as Core;
class Resource extends Main
{
    protected $entry = true;
    protected $type;

    public function __construct()
    {
        parent::__construct();
        $type = Dever::input('type', 'is_numeric', '资源类型');
        $this->service = new Core($type);
    }

    # 首页、列表页
    public function home()
    {
        $cate = new Cate($this->service->app);
        $data = $cate->getData();
        $data['list'] = $this->service->getList($data['cate_id']);
        return Place::result($data);
    }

    # 详情
    public function view()
    {
        $id = Dever::input('type_id', 'is_numeric', '资源ID');
        $data['info'] = $this->service->getInfo($id);
        # 分类下其他内容
        $data['cate'] = $this->service->getList($data['info']['cate'], $data['info']['id'], 6);
        # 最新内容
        $data['new'] = $this->service->getList(false, $data['info']['id'], 6);
        return Place::result($data);
    }

    # 点击购买按钮
    public function submit()
    {
        $id = Dever::input('type_id', 'is_numeric', '资源ID');
        $info = $this->service->getInfo($id);
        $data['info'] = Dever::load('info', $this->service->app)->submit($info);
        return $data;
    }

    # 拉起支付
    public function pay()
    {
        $id = Dever::input('type_id', 'is_numeric', '资源ID');
        $info = $this->service->getInfo($id);
        $info = Dever::load('info', $this->service->app)->submit($info);
        if ($info['status'] == 2) {
            return $this->service->pay($info);
        } else {
            Dever::error('无权限');
        }
    }

    # 资源评论列表
    public function getReviewList()
    {
        $data['type'] = Dever::input('type', 'is_numeric', '类型');
        $data['type_id'] = Dever::input('type_id', 'is_numeric', '类型ID');
        # 每页10条
        $set['num'] = 10;
        $data = Dever::db('review', 'place')->select($where, $set);
        if ($data) {
            foreach ($data as &$v) {
                $v['cdate_str'] = date('Y-m-d H:i:s', $v['cdate']);
                $v['oper'] = 2;
                if (Place::$uid == $v['uid']) {
                    $v['oper'] = 1;
                }
                $v['user'] = Dever::db('member', 'place')->find($v['uid'], array('col' => 'mobile,name,avatar'));
            }
        }
        return $data;
    }
}