<?php
namespace plugin\test\controller;
use think\Controller;
use think\Db;
use plugin\test\controller\Base;
/**
 * 后台Index控制器
 */
class Index extends Base{
     /* Index查询首页 */
	public function index(){     
	  
        $this->assign('name','前台');
	    return $this->fetch("index/index"); 
	}


    /* 编辑Index */
	public function edit($id){   
	    if($_POST){
		   $Index = new \plugin\chat\model\Index;
           $res=$Index->allowField(true)->validate(true)->save($_POST,['id' => $_POST['id']]);
	       if($res){
			   addUserLog("edit_index",session_uid());
		      $this->success("更新成功！",cookie("__forward__"));
		   }else{
			  $error=$Index->getError()?$Index->getError():"更新失败！";
			  $this->error($error);
		   } 
	   }
	   else{
		 $map['id']=$id;
		 $info=db("Index")->where($map)->find();
	     $this->assign('info', $info);
		
		   cookie("__forward__",input('server.HTTP_REFERER'));
		 $this->meta_title="编辑Index";
		 $this->assign('meta_title', $this->meta_title);
	     return $this->fetch("index/edit");
	   }
	}
	 /* 增加Index */
	public function add($id=""){  
	   if($_POST){
		  $Index = new \plugin\chat\model\Index;
            // 过滤post数组中的非数据表字段数据
          $res=$Index->validate(true)->allowField(true)->save($_POST);
	      if($res){
			  addUserLog("add_index",session_uid());
		      $this->success("新增成功！",cookie("__forward__"));
		  }else{
			  $error=$Index->getError()?$Index->getError():"新增失败！";
			  $this->error($error);
		  } 
	 }
	  else{
		
		  $this->meta_title="新增Index";
		    cookie("__forward__",input('server.HTTP_REFERER'));
		  $this->assign('meta_title', $this->meta_title);
	      return $this->fetch("index/edit");
	  }
	}
	 /* 删除Index*/
	public function del(){   
	    $id=input("id");
	   $map[]=['id',"in",$id];
		if(!$map){
			 $this->error("未选择数据！");
		}
	
		$res=db("Index")->where($map)->delete();
		
		if($res){
		   addUserLog("del_index",session_uid());
		   $this->success("删除成功！");
		}else{
	           $this->error("删除失败！");
		}
	}
	
}
