<?php
class CarController extends Easy_Controller_Action {
	public $_Car;
	public function init(){
		$this->_User = new UsersModel();
		$this->_Car = new CarModel();
		
	}
	public function addAction(){
		$this->_View->usergroup = $this->_User->groupOption();
		if ($this->_Request->isPost()){
			$license = $GLOBALS['license'];
			if (strtolower($license['version']) == 'free' || $_SERVER['HTTP_HOST'] == '127.0.0.1'){
				if ($this->_Request->getPost('num')>10){
					$this->_View->message = _('免费版与测试版一次性生成卡片不能超过10张!');
					exit($this->_View->render('message'));
				}
			}
			if ( $this->_Car->add($this->_Request->getPost())===false){
				$this->_View->message = $this->_Car->error;
			}else{
				$this->_View->message = _('添加成功!');
			}
			exit($this->_View->render('message'));
		}
	}
	public function indexAction(){
		//echo $this->_Request->getPost('usertype');
		$this->_View->usergroup = $this->_User->groupOption($this->_Request->getPost('usergroup'));
		if (  $this->_Request->getParams('do') == 'search' ){
			$this->_View->car = $this->_Car->Search($this->_Request->getParams());
		}else{
			$this->_View->car = $this->_Car->getAll();
		}
		$this->_View->sql = base64_encode(urlencode($this->_Car->_DB->sql));
		$this->_View->printPage = $this->_Car->printPage;
	}
	public function deleteAction(){
		if ($this->_Request->isPost()){
			$this->_Car->delete($this->_Request->getPost('id'));
		}else{
			$this->_Car->delete($this->_Request->getParam('id'));
		}
		$this->_View->message = _('删除成功!');
		exit($this->_View->render('message'));
	}
	public function usedAction(){
		$this->_View->usergroup = $this->_User->groupOption($this->_Request->getPost('usergroup'));
		if ( $this->_Request->getParams('do') == 'search' ){
			$this->_View->car = $this->_Car->Search($this->_Request->getParams());
		}else{
			$this->_View->car = $this->_Car->getAll(array('locked'=> 1));
		}
		//print_r($this->_Car);
		$this->_View->sql = base64_encode(urlencode($this->_Car->_DB->sql));
		$this->_View->printPage = $this->_Car->printPage;
	}
	public function statAction(){
		$this->_View->stat = $this->_Car->stat();
		$this->_View->printPage = $this->_Car->printPage;
	}
}