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


    /* 编辑流量 */
	public function edit($id){   
	    if($_POST){
		   $Flow = new \app\admin\model\Flow;
           $res=$Flow->allowField(true)->validate(true)->save($_POST,['id' => $_POST['id']]);
	       if($res){
			   addUserLog("edit_flow",session_uid());
		      $this->success("更新成功！",cookie("__forward__"));
		   }else{
			  $error=$Flow->getError()?$Flow->getError():"更新失败！";
			  $this->error($error);
		   } 
	   }
	   else{
		 $map['id']=$id;
		 $info=db("Flow")->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("flow/edit");
	   }
	}
	 /* 增加流量 */
	public function add($id=""){  
	   if($_POST){
		  $Flow = new \app\admin\model\Flow;
            // 过滤post数组中的非数据表字段数据
          $res=$Flow->validate(true)->allowField(true)->save($_POST);
	      if($res){
			  addUserLog("add_flow",session_uid());
		      $this->success("新增成功！",cookie("__forward__"));
		  }else{
			  $error=$Flow->getError()?$Flow->getError():"新增失败！";
			  $this->error($error);
		  } 
	 }
	  else{
		
		  $this->meta_title="新增流量";
		    cookie("__forward__",input('server.HTTP_REFERER'));
		  $this->assign('meta_title', $this->meta_title);
	      return $this->fetch("flow/edit");
	  }
	}
	 /* 删除流量*/
	public function del(){   
	    $id=input("id");
	    $map['id']=["in",$id];
		if(!$map["id"]){
			 $this->error("未选择数据！");
		}
		$model= request()->controller();
		$res=db($model)->where($map)->delete();
		
		if($res){
		   addUserLog("del_flow",session_uid());
		   $this->success("删除成功！");
		}else{
	           $this->error("删除失败！");
		}
	}
	
}
