<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class UseAction extends BaseAction
{
    public function run()
    {
        if (isset($_POST['num']))
        {
            $this->getController()->isLogin();
            $days = 1;
            if ($days != 1)
            {
                throw new iSystemException('只能购买1天');
            }
    
            $iorange = $this->iorange();
            $property = $this->getController()->getProp('cmultiple');
            $price = $iorange['user']['verify'] ? $this->getController()->isVerifyPrice : $this->getController()->price;
            $money = $price * $days;
            $this->getController()->hasMoney($this->uid(),$this->userName(),$money);
            $expireTime = time() + ($days * 24 * 3600);
            iCredits::single($this->uid(), $this->userName())->consume($money);
            $multiple = 2;
            
            if (false !== ($prop = $this->getController()->hasPropMemberLog($this->uid(), 'cmultiple')))
            {
                $option = unserialize($prop['option']);
                $option['expire'] = $expireTime;
                $option['multiple'] = $multiple;
                $this->getController()->updatePropMemberLog($this->uid(), $this->userName(), $property['propertyid'], serialize($option));
            }
            else
            {
                $this->getController()->createPropMemberLog($this->uid(),$this->userName(), $property['propertyid'], serialize(array('class'=>2,'multiple'=>$multiple,'expire'=>$expireTime)));
            }

            $data = array(
                'uid' => $this->uid(),
                'username' => $this->userName(),
                'amount' => $days,
                'price' => $money,
            );
            PropertyLog::single()->create($property['propertyid'],$data);

            $message[] = '购买成功,您现在已经获得'.$multiple.'倍积分功能，未来'.$days.'内你获得的积分都将x'.$multiple;
            $message[] = '返回'.memberLink().'.'.indexLink();
            $this->showMessage($message);
        }
        else
        {
            throw new iSystemException('非法操作');
        }
    }
}