<?php
class IndexModule extends CommonModule{

	public function index(){
		$list=M('addslide')->order('id asc')->select();
		$this->assign('list', $list);
		$this->display();
	}

	public function edit(){
		$id=Q('get.id','','intval');
		if(!empty($id)){
			$slide=M('addslide')->where('id='.$id)->find();
			$this->assign('slide',$slide);
		}
		$this->display();
	}

	public function saveadd(){
		if(IS_POST){
			$data=array();
			$data =$_POST;
			$msg=Check::rule(
				array(check::must($data['title']),'标题不能为空'),
				array(check::must($data['url']),'链接地址不能为空')
			);
			if($msg!==true){
				$this->error($msg);
			}else{
				$info=M('addslide')->data($data); //更新信息
				if ($info->where("title='".$data['title']."'")->find()) {$this->error('该幻灯片已存在，无需重复添加');}
				if ($info->count()>20) $this->error('幻灯片最多可添加20条');
				if ($info->add()){
					$this->success('添加成功','?r=slide');
				}else{
					 $this->error('添加失败');
				}
			};
		}else{
			$this->error('非法请求');
		};
	}


	public function savedit(){
		$id=Q('get.id','','intval');
		if(IS_POST){
			$data = Q('post.');
			$db   = M('addslide');
			$db->validate = array(
					array('title', 'nonull', '标题不能为空',2,3),
					array('url', 'nonull', '链接地址不能为空',2,3)
				);

			if (!$db->create($data)){$this->error($db->error);}
			$db->replace($data);
			$this->success('保存成功','?r=slide');
		}else{
			$this->error('非法请求');
		};
	}

	function del(){
		$id=Q('get.id','','intval');
		if(empty($id)) $this->error('参数错误');
		$info=M('addslide')->where('id='.$id)->delete();
		if($info){
			$this->success('删除成功','?r=slide');
		}else{
			$this->error('该幻灯片不存在');
		}
	}
}