<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class DoneAction extends MemberAction
{
    public function run()
    {
        $id = S::gp('id', 'G', 2);
        $task = Task::model()->findByPk($id);
        if (empty($task))
        {
            throw new iSystemException('不存在的任务');
        }

        $tasker = Tasker::model()->find(array('condition'=>'task_id='.$id.' AND uid='.$this->uid()));
        if (empty($tasker))
        {
            throw new iSystemException('你没有接受过此任务，不能放弃');
        }

        $check = TaskRespone::model()->find(array('condition'=>'uid='.$this->uid().' AND tasker_id='.$tasker->id.' AND status = 0'));
        if ($check)
        {
            throw new iSystemException('请不要重复提交任务');
        }

        if (isset($_POST['content']))
        {
            $content = S::gp('content', 'P');
            if (empty($content))
            {
                throw new iSystemException('请收入帐号信息');
            }
            $respone = new TaskRespone();
            $respone->content = $content;
            $respone->uid = $this->uid();
            $respone->username = $this->userName();
            $respone->task_id = $id;
            $respone->tasker_id = $tasker->id;
            $respone->save();

            $tasker->status = 2;
            $tasker->save();

            $message[] = '任务提交成功，我们会尽快审核';
            $message[] = ilink('继续做任务', url('system', array('s'=>'member','c'=>'task'))).'|'.memberLink('去看小说');
            $this->showMessage($message);
        }

        $this->getController()->render('done', array(
                'task' => $task,
                'tasker' => $tasker,
            )
        );
    }
}