<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
/**
 * 后台历史记录控制器
 */
class History extends Home{
     /* 历史记录查询首页 */
	public function index(){     
      if($_POST){
			foreach ($_POST as $key=>$value){
				if($value){
					$map[]  = [$key,'like', '%'.$value.'%'];
				 }
			}
        }
        $map=isset($map)?$map:'';
        $res=getLists('History',$map,10,'id desc',"");
	    $this->assign('res', $res);
		$this->meta_title="历史记录管理";
		$this->assign('meta_title', $this->meta_title);
	    return $this->fetch("history/index"); 
	}


    /* 编辑历史记录 */
	public function edit($id){   
	    if($_REQUEST){
		   $History = new \app\admin\model\History;
           $res=$History->allowField(true)->validate(true)->save($_REQUEST,['id' => $_REQUEST['id']]);
	       if($res){
			   addUserLog("edit_history",session_uid());
		      $this->success("更新成功！",cookie("__forward__"));
		   }else{
			  $error=$History->getError()?$History->getError():"更新失败！";
			  $this->error($error);
		   } 
	   }
	   else{
		 $map['id']=$id;
		 $info=db("History")->where($map)->find();
	     $this->assign('info', $info);
		
		   cookie("__forward__",input('server.HTTP_REFERER'));
		 $this->meta_title="编辑历史记录";
		 $this->assign('meta_title', $this->meta_title);
	     return $this->fetch("history/edit");
	   }
	}
	 /* 增加历史记录 */
	public function add($id=""){  
	   if($_REQUEST){
		  $History = new \app\admin\model\History;
            // 过滤post数组中的非数据表字段数据
          $res=$History->validate(true)->allowField(true)->save($_REQUEST);
	      if($res){
			  addUserLog("add_history",session_uid());
		      $this->success("新增成功！",cookie("__forward__"));
		  }else{
			  $error=$History->getError()?$History->getError():"新增失败！";
			  $this->error($error);
		  } 
	 }
	  else{
		
		  $this->meta_title="新增历史记录";
		    cookie("__forward__",input('server.HTTP_REFERER'));
		  $this->assign('meta_title', $this->meta_title);
	      return $this->fetch("history/edit");
	  }
	}
	 /* 删除历史记录*/
	public function del(){   
	    $id=input("id");
	    $map[]=['id',"in",$id];
		if(!$map){
			 $this->error("未选择数据！");
		}
		$model= request()->controller();
		$res=db($model)->where($map)->delete();
		
		if($res){
		   addUserLog("del_history",session_uid());
		   $this->success("删除成功！");
		}else{
	           $this->error("删除失败！");
		}
	}
	
}
