<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class UseAction extends BaseAction
{
    public function run()
    {
        if ($_GET['do'] == 1)
        {
            $this->getController()->isLogin();

            $bookid = S::gp('bid', 'G', 2);
            $book = fetchBookInfo($bookid);
            if (empty($book))
            {
                throw new iSystemException('不存在的小说');
            }

            if (false === ($prop = $this->getController()->hasPropMemberLog($this->uid(), 'brick')))
            {
                throw new iSystemException('您没有板砖，是否需要' . ilink('购买', url('system',array('s'=>'props','c'=>'brick'))) . '？');
            }
            $option = unserialize($prop['option']);
            if (1 > $option['amount'])
            {
                throw new iSystemException('您的板砖已经用完，是否需要' . ilink('购买', url('system',array('s'=>'props','c'=>'brick'))) . '？');
            }
            $option['amount'] = $num = $option['amount'] - 1;
            $this->getController()->updatePropMemberLog($this->uid(), $this->userName(), $prop['property_id'], serialize($option));
            $this->db()->query('UPDATE `'.Book::table()."` SET `egg` = `egg` + 1 WHERE `bookid` = '$bookid'");

            $feed = '{{'.$this->uid().'}}砸砖给[url='.staticUrl('bookshow',array('bid'=>$bookid)).']《'.$book['bookname'].'》[/url]';
            iFeed::single()->create($feed, $this->uid(), 'book');
            
            $message[] = '砸砖成功成功<br/>您还剩下' . $num . '块砖<br/>返回'.bookLink($bookid).'.'.memberLink().'.'.indexLink();
            $this->showMessage($message);
        }
        else
        {
            throw new iSystemException('非法操作');
        }
    }
}
