<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class SuperBmAction extends ForumAction
{
    public function run()
    {
        $month = date('Y-m');
        $this->showMessage('超管将不再申请');
        if ($_GET['confirm'])
        {
            $this->getController()->isLogin();
    
            if (IS_SUPER)
            {
                throw new iSystemException('您当前已经是超管，不需要再申请，系统会自动将您列入选举名单');
            }
            
            $check = $this->db()->getValue("SELECT COUNT(*) AS c FROM ".BookManagers::table()." WHERE `uid` = {$this->uid()} AND `status` = 1");
            if ($check)
            {
                throw new iSystemException('只有超管或者书管才能申请选举');
            }
    
            $check = $this->db()->setQueryString("SELECT uid,username FROM ".ApplyBms::table()." WHERE FROM_UNIXTIME(createdate,'%Y-%m') = '$month' AND uid = '{$this->uid()}' AND issuper = 1 LIMIT 1")->find(ture);
            if (!empty($check))
            {
                throw new iSystemException('您已经提交过申请，请耐心等待投票开始');
            }

            try
            {
                Members::single()->hasMoney($this->uid(), 2000, $this->userName());
            }
            catch (MemberNotExistsException $exce)
            {
                throw new iSystemException('你当前的'.setting('creditname').'不足2000，不能申请');
            }
            iCredits::single($this->uid(), $this->userName())->consume(2000);
            $this->db()->insert(ApplyBms::table())->value(array('uid'=>$this->uid(),'username'=>$this->userName(),'createdate'=>CTIME,'issuper'=>1))->q();
    
            $message[] = '恭喜您，申请提交成功！';
            $message[] = '返回'.forumLink($this->id()).'.'.memberLink().'.'.indexLink();
            $this->showMessage($message);
        }
        $this->getController()->render('superbm');
    }
}