<?php
class CaseController extends Easy_Controller_Action {
	public $_User;
	protected $_Case;
	public function init(){
		$this->_User = new UsersModel();
		$this->_Case = new CaseModel();
	}
	public function setAction(){
		if ($this->_Request->isPost()){
			if ( $this->_Case->addShop($this->_Request->getPost())===false){
				$this->_View->message = $this->_Case->error;
			}else{
				$this->_View->message = _('添加成功!');
			}
			exit($this->_View->render('message'));
		}else{
			$this->_View->usergroup = $this->_User->groupOption();
			$this->_View->listRow = $this->_Case->getAll();
		}
	}
	/*public function delshopAction(){
		if (count($this->_Request->getParams('id'))==0){
			$this->_View->message = _('您没有选择记录!');
		}else{
			$this->_Case->delshop($this->_Request->getParams('id'));
			$this->_View->message = _('删除成功!');
		}
		exit($this->_View->render('message'));
	}*/
	public function modifyAction(){
		if ($this->_Request->isPost()){
			$this->_Case->modify($this->_Request->getPost());
			$this->_View->message = _('修改成功!');
			
			exit($this->_View->render('message'));
		}else{
			$id = $this->_Request->getParam('id');
			$this->_View->modify = $this->_Case->getRow($id);
			$this->_View->usergroup = $this->_User->groupOption($this->_View->modify['usergroup']);
		}
	}
	public function indexAction(){
		$state = (int)$this->_Request->getParams('state');
		$this->_View->rows = $this->_Case->getList('a.state='.$state);
		$this->_View->printpage = $this->_Case->printPage;
	}
	public function delshopAction(){
		if (count($this->_Request->getParams('id'))==0){
			$this->_View->message = _('您没有选择记录!');
		}else{
			$this->_Case->delshop($this->_Request->getParams('id'));
			$this->_View->message = _('删除成功!');
		}
		exit($this->_View->render('message'));
	}
	public function deleteAction(){
		if (count($this->_Request->getParams('id'))==0){
			$this->_View->message = _('您没有选择记录!');
		}else{
			$this->_Case->delete($this->_Request->getParams('id'));
			$this->_View->message = _('删除成功!');
		}
		exit($this->_View->render('message'));
	}
	/*审核一条订单记录*/
	public function stateAction(){
		$license = $GLOBALS['license'];
		if (strtolower($license['version']) == 'free' || $_SERVER['HTTP_HOST'] == '127.0.0.1'){
			if ($this->_Request->getPost('num')>10){
				$this->_View->message = _('免费版与测试版无法使用收费项目功能!');
				exit($this->_View->render('message'));
			}
		}
		$id = $this->_Request->getParam('id');
		if (!$id){
			$this->_View->message = _('您没有选择记录!');
		}else{
			$this->_Case->changeState($id);
			$this->_View->message = _('订单审核成功!');
		}
		exit($this->_View->render('message'));
	}
	public function findAction(){
		$this->_View->usergroup = $this->_User->groupOption((int)$this->_Request->getParams('usergroup'));
		if ($this->_Request->getParams('keyword')){
			$stime = strtotime($this->_Request->getParams('stime'));
			$etime = strtotime($this->_Request->getParams('etime'));
			$where = ' 1=1';
			if ($this->_Request->getParams('usergroup')!= 'all'){
				$where.= ' AND a.groupid='.(int)$this->_Request->getParams('usergroup');
			}
			if ($this->_Request->getParams('usertype') != 'all'){
				$where.= ' AND a.groupid='.(int)$this->_Request->getParams('usertype');
			}
			if (urldecode($this->_Request->getParams('keyword')) != '*'){
				$where.= ' AND a.'.$this->_Request->getParams('type').' LIKE \'%'.urldecode(trim($this->_Request->getParams('keyword'))).'%\'';
			}
			if ($stime && $etime){
				$where.= 'a.addtime>'.$stime.' AND a.addtime<'.$etime;
			}
			if ($this->_Request->getParams('state') != 'all'){
				$where.= ' AND a.state='.(int)$this->_Request->getParams('state');
			}
			//echo $where;
			$this->_View->search = $this->_Case->getList($where);
			$this->_View->printpage = $this->_Case->printPage;
		}
	}
	public function  reportAction(){
		$this->_View->reports = $this->_Case->report();	//订单报告
		$this->_View->printpage = $this->_Case->printPage;
	}
}