<?php namespace Place_interface\Api;
use Dever;
use Place;
use Place_interface\Lib\Core;
class Role extends Core
{
    //protected $login = true;
    protected $entry = true;

    # 我的信息
    public function info()
    {
        $role_id = Dever::input('role_id', 'is_numeric', '角色ID');
        $data['role'] = Dever::db('role', 'place_user')->find(array('id' => $role_id, 'status' => 1), array('col' => 'id,name,score_id'));
        if (!$data['role']) {
            Dever::error('角色信息错误');
        }
        $score = Dever::db('info', 'place_score')->find($data['role']['score_id']);
        $data['role']['score_name'] = $score['name'];
        # 获取当前等级
        $user = array();
        if (Place::$uid) {
            $user = Dever::db('user_role', 'place_user')->find(array('role_id' => $role_id, 'uid' => Place::$uid));
            if ($user) {
                if ($user['edate']) {
                    $user['edate_str'] = date('Y-m-d H:i:s', $user['edate']); 
                } else {
                    $user['edate_str'] = '永久'; 
                }
            }
        }
        
        $data['level'] = Dever::db('role_level', 'place_user')->select(array('role_id' => $role_id, 'status' => 1), array('col' => 'id,name,level,icon,price,day_desc,day,content'));
        if ($data['level']) {
            foreach ($data['level'] as &$v) {
                # 获取权益
                $v['profit'] = Dever::load('role', 'place_user')->profit($v['id']);
                $v['content'] = htmlspecialchars_decode($v['content']);
                if ($user && $v['id'] == $user['level_id']) {
                    $data['user'] = $v;
                    $data['user']['edate_str'] = $user['edate_str'];
                }
            }
        }
        
        return $data;
    }

    # 获取价格
    public function price()
    {
        return Dever::load('role', 'place_user')->getPrice();
    }

    # 购买
    public function buy()
    {
        if (!Place::$uid) {
            $this->showLogin();
        }
        return Dever::load('role', 'place_user')->pay();
    }
}