<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class RewardAction extends BbsAction
{
    public function run()
    {
        $this->getController()->isLogin();
        $tid = $this->getTid();
        $thread = $this->getExistsThread($tid);
        $uid = intval(S::gp('uid','G'));
        $type = intval(S::gp('t','G'));
        $thread['uid'] != $this->uid() && $this->NoPermission();
        !in_array($type, array(1,2)) && $this->NoPermission();
        $reward = $this->db()->select()->from(Treward::table())->where('tid='.$tid)->find(true);
        if (empty($reward))
        {
            throw new iSystemException('不存在的悬赏');
        }
        if ($this->uid() == $uid)
        {
            throw new iSystemException('非法操作，请返回');
        }
        if ($reward['pid'])
        {
            throw new iSystemException('此悬赏已关闭');
        }
        if (1 == $type)
        {
            $pid = intval(S::gp('pid','G'));
            $post = $this->db()->select()->from(Posts::table())->where('id='.$pid)->find(true);
            if (empty($post))
            {
                throw new iSystemException('不存在的回复内容');
            }
            
            if (!$_GET['confirm'])
            {
                $message[] = '设置为最佳答案，将会将剩下的悬赏全部赏给回复作者，同时悬赏将结束！';
                $message[] = ilink('确定',url('system',array('s'=>'bbs','a'=>'reward','tid'=>$tid,'pid'=>$pid,'uid'=>$uid,'t'=>1,'confirm'=>1))).'.'.ilink('取消',url('system',array('s'=>'bbs','a'=>'thread','tid'=>$tid)));
                $this->showMessage($message);
            }
            
            $this->db()->update(Treward::table())->value(array('pid'=>$pid,'answer'=>$this->getController()->filter($post['content']),'answer_uid'=>$uid))->where('tid='.$tid)->q();
            $userName = Members::single()->getNameByUid($uid);
            $money = $reward['caval'] + $reward['cbval']; 
            iCredits::single($uid, $userName)->addMoney($money);
        }
        else if (2 == $type)
        {
            if (!empty($_POST['nums']))
            {
                $nums = intval(S::gp('nums','P'));
                if (empty($nums) || $nums < 1)
                {
                    throw new iSystemException('请输入要发放的'.setting('creditname'));
                }
                if ($nums > $reward['caval'])
                {
                    throw new iSystemException('热心助人'.setting('creditname').'不足'.$nums.'，不能发放');
                }
                $userName = Members::single()->getNameByUid($uid);
                $money = $reward['caval'] + $reward['cbval']; 
                iCredits::single($uid, $userName)->addMoney($nums);
                $this->db()->update(Treward::table())->decrField(array('caval'=>$nums))->where('tid='.$tid)->q();
            }
            else
            {
                if (!$reward['caval'])
                {
                    throw new iSystemException('热心助人'.setting('creditname').'不足，不能发放');
                }
                exit($this->getController()->render('reward',array('num'=>$reward['caval'],'tid'=>$tid,'uid'=>$uid)));
            }
        }
        
        iforward(url('system',array('s'=>'bbs','a'=>'thread','tid'=>$tid)));
    }
}