<?php
/*
 *		Powered by YiCms 
 *
 *		mailto:yixiao@yicms.cn
 *
 */
class BuyAction extends Action{
	public function checkUser($level){
		if(empty($_SESSION['aid'])) $this->redirect('/Admin/Index/login');
		if(!empty($level) && !in_array($level,$_SESSION['levels'])) $this->error('您没有此权限!.');
	}
	public function index(){
		$this->checkUser('2_3_1');
		$data = M('Buy');
		import("@.ORG.Page");
		$count = $data->count('id');
		$p     = new Page ($count, 20);	
		$list = $data->limit($p->firstRow.','.$p->listRows)->order('time DESC')->select();
		$product = M('Product');
		foreach($list as $key=>$value){
			$productname = $product->field('title')->where("id = $value[pid]")->find();
			$list[$key] = array_merge($list[$key],$productname);
		}
		$this->assign('list',$list);
		//分页设置
		$p->setConfig('header','条记录');
		$p->setConfig('prev','<<上一页');
		$p->setConfig('next','下一页>>');
		$p->setConfig('first','<<第一页'); 
        $p->setConfig('last','最后一页>>'); 
		$page = $p->show();
		$this->assign( "page", $page);
		$this->display();
	}
	public function edit(){
		$this->checkUser('2_3_2');
		$id = (int)$_GET['id'];
		$data = M('Buy');
		$list = $data->where("id = $id")->find();
		$product = M('Product');
		$productname = $product->field('title')->where("id = $list[pid]")->find();
		$list = array_merge($list,$productname);
		$this->assign('list',$list);
		$this->display();
	}
	public function stop(){
		$this->checkUser('2_3_2');
		$list['id'] = (int)$_GET['id'];
		$list['state'] = (int)$_GET['state'];
		$data = M('Buy');
		if(!$data->save($list)) $this->error('操作失败!请联系网络管理员!');
		$this->success('操作成功!');
	}
	public function del(){
		$this->checkUser('2_3_2');
		$id = (int)$_GET['id'];
		$data = M('Buy');
		if(!$data->where("id = $id")->delete()) $this->error('删除失败!请联系网络管理员!');
		$this->success('删除成功!');
	}
}
?>