<?php
namespace plugin\test\controller;
use think\Controller;
use think\Db;
use plugin\test\controller\Admin;
class Lists extends Admin{

   public function index(){ 
		
        if($_POST){
			foreach ($_POST as $key=>$value){
				if($value){
					$map[]  = [$key,'like', '%'.$value.'%'];
				 }
			}
        }
		$map=isset($map)?$map:'';
        $res=getLists('Ad',$map,10,'id desc',"");
	    $this->assign('res', $res);
		$this->meta_title="广告管理";
		$this->assign('meta_title', $this->meta_title);
	    return $this->fetch('lists/index'); 
	}

     /* 编辑广告 */
	public function edit($id){  
		
	    if($_POST){
		   $Ad = new \plugin\test\model\Ad;
           $res=$Ad->validate(true)->allowField(true)->save($_POST,['id' => $_POST['id']]);
	       if($res){
			    addUserLog("edit_Ad",session_uid());
		        $this->success("更新成功！",cookie("__forward__"));
		   }else{
			    $error=$Ad->getError();
			    $this->error($error?$error:"更新失败！");
		   } 
	  }
	  else{
		 $map['id']=input("id");
		 $info=db("Ad")->where($map)->find();
	     $this->assign('info', $info);
		 unset($map);
		 $map['pid']=0;
	     $list =db('category')->where($map)->select(); 
            /* 获取模块信息 */
         $this->assign('list', $list); 
		 cookie("__forward__",input('server.HTTP_REFERER'));
		 $this->meta_title="编辑广告-".$info["title"];
		 $this->assign('meta_title', $this->meta_title);
	     return $this->fetch('lists/edit');
	   }
	}

     /* 增加广告 */
	public function add($id=""){ 
		
	  if($_POST){
		  $Ad = new \plugin\test\model\Ad;
            // 过滤post数组中的非数据表字段数据
          $res=$Ad->validate(true)->allowField(true)->save($_POST);
	      if($res){
			   addUserLog("add_Ad",session_uid());
		      $this->success("新增成功！",cookie("__forward__"));
		  }else{
			  $error=$Ad->getError();
			  $this->error($error?$error:"新增失败！");
		  } 
	}
	  else{
		  $map['pid']=0;
	      $list =db('category')->where($map)->select(); 
	      $this->assign('list', $list);
		  $this->meta_title="新增广告"; 
		  cookie("__forward__",input('server.HTTP_REFERER'));
		  $this->assign('meta_title', $this->meta_title);
	      return $this->fetch('lists/edit');
	  }
	}

    /* 删除广告 */
	public function del(){   
	    $id=input("id");
	    $map[]=['id',"in",$id];
		if(!$map){
			 $this->error("未选择数据！");
		}
		$res=db("Ad")->where($map)->delete();
		if($res){
		   addUserLog("del_Ad",session_uid());
		   $this->success("删除成功！");
		}else{
			 $this->error("删除失败！");
		}
	}

}