<?php
namespace App\Controller;
use Think\Controller;
class AdverController extends Controller {
  
	 public function shouyexiao(){
		 $uid = $_SESSION['user']['id'];
		 $mod =M("daitu")->where("uid={$uid} and type='首页'")->order("sort ASC")->select();
		 $this->assign("mod",$mod);
		 $this->display("shouyexiao");
	 }
	 public function shouyelun(){
		 $uid = $_SESSION['user']['id'];
		 $mod =M("daitu")->where("uid={$uid} and type='首页轮播'")->order("sort ASC")->select();
		 $this->assign("mod",$mod);
		 
		 $this->display("shouyelun");
	 }
	 public function faxianlun(){
		 $uid = $_SESSION['user']['id'];
		 $mod =M("daitu")->where("uid={$uid} and type='直播轮播'")->order("sort ASC")->select();
		 $this->assign("mod",$mod);
		 
		 $this->display("faxianlun");
	 }
	 public function faxianheng(){
		 $uid = $_SESSION['user']['id'];
		 $mod =M("daitu")->where("uid={$uid} and type='直播图'")->order("sort ASC")->select();
		 $this->assign("mod",$mod);
		 
		 $this->display("faxianheng");
	 }
	  public function faxiantui(){
		 $uid = $_SESSION['user']['id'];
		 $mod =M("daitu")->where("uid={$uid} and type='推荐'")->order("sort ASC")->select();
		 $this->assign("mod",$mod);
		 
		 $this->display("faxiantui");
	 }
	 
	 public function insert() {
		if(!empty($_FILES)) {
			//如果有文件上传 上传附件
			 $this->_upload();
			//$this->forward();
		}
        $daitu = M("daitu"); 
		$datas['name'] = $_POST['name'];
		$datas['picname'] = $_POST['picname'];;
		$datas['link'] = $_POST['link'];
		$datas['sort'] = $_POST['sort'];
		$datas['type'] = $_POST['type'];
		$datas['uid'] = $_SESSION['user']['id'];
		 if($daitu -> create($datas)){

            if($daitu -> add()){
               // $this -> display('400');
			   $this->success("添加成功！",U("Adver/add"));
            }else{
                $this -> error('添加失败！');
            }   
        }else{
            $this -> error('添加失败！');
        }
	}
	 
	 
	public function del(){
		
		$id = $_GET['id'];
		$mod = M("daitu")->delete($id);
      
		if($mod>0){
          $this->success("成功！");
        }else{
           $this -> error('失败！');
        }
		
		
	}
	 public function update(){
		 
		
		// //赋值
		if(!empty($_FILES) && $_FILES["uploadpic"]["error"]==0){
			//如果有文件上传 上传附件
			 $this->_upload();
			 $mod = M('daitu');
			 $datas['name'] = $_POST['name'];
			 $datas['picname'] = $_POST['picname'];;
			 $datas['link'] = $_POST['link'];
			  $datas['sort'] = $_POST['sort'];
			 $datas['type'] = $_POST['type'];
			 $id = $_POST['id'];
			 $mod->create($datas);
			 $mod->where("id={$id}")->save();
			 if($mod>0){
				$this->success("修改成功！");
			}else{
				$this->error("修改失败！");
			 } 
			}else{
				 $mod = M('daitu');
			 $datas['name'] = $_POST['name'];
			 $datas['link'] = $_POST['link'];
			  $datas['sort'] = $_POST['sort'];
			 $datas['type'] = $_POST['type'];
			 $id = $_POST['id'];
			 $mod->create($datas);
			 $mod->where("id={$id}")->save();
			 if($mod>0){
				$this->success("修改成功！");
			}else{
				$this->error("修改失败！");
			 } 
				
			}
		}
		
		
		
		

	 
	 public function edit(){
		$id=$_GET['id'];
		$mod = M('daitu')->where("id={$id}")->find();
		$this->assign("mod",$mod);
		
	   $this->display("edit");
	}
	 // 文件上传
    protected function _upload()
    {	
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize = 5120000 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif','png','jpeg','pjpeg');// 设置附件上传类型 
        $upload->rootPath = './Public/uploads/'; // 设置附件上传目录    
        $upload->autoSub = false; // 设置不创建子目录
		
		$info = $upload->upload();
		if(!$info) {
			$this->error($upload->getError());
        }else {
            //取得成功上传的文件信息
            $uploadList = array_values($info);
			$picname = $uploadList[0]['savename'];
			
			// $image = new \Think\Image();
			// $image->open('./Public/uploads/gl/'.$picname);
			// //按照原图的比例生成一个最大为150*150的缩略图并保存
			// //$image->thumb(150, 150)->save('./Public/uploads/links/s_'.$picname);
			// //$image->thumb(500, 500)->save('./Public/uploads/links/'.$picname);
			// //$image->thumb(220, 220)->save('./Public/uploads/links/m_'.$picname);
			// $image->thumb(1200, 130)->save('./Public/uploads/gl/'.$picname);
			$url = 'http://'.$_SERVER['SERVER_NAME'].'/'.'Public/uploads/'; 
			$_POST['picname']  = $url.$picname;
		}
	}
	
	public function user(){
		$uid = $_SESSION['user']['id'];
		$user = M('user')->where("status=1 and pid={$uid}");
	    $page = new \Think\Page($user->count(),20);
	    $this->assign("user",$user->where("status=1 and pid={$uid}")->order("addtime DESC")->limit($page->firstRow,$page->listRows)->select());
		$this->assign("pageinfo",$page->show());
		
		 $this->display("user");
	}
	
	public function daili(){
		 $user = M('user')->where("status=2 and pid={$uid}");
	     $page = new \Think\Page($user->count(),20);
	     $this->assign("user",$user->where("status=2 and pid={$uid}")->order("addtime DESC")->limit($page->firstRow,$page->listRows)->select());
		 $this->assign("pageinfo",$page->show());
		
		 $this->display("daili");
	}
	
	
	public function del1(){
		
		$id = $_GET['id'];
		$mod = M("user")->delete($id);
      
		if($mod>0){
           die("y");
        }else{
            die("n");
        }
	}
	
	public function ctime(){
		
		$last = $_GET['pass']*60*60*24;

        $id = $_GET['id'];
		$where = array();
		$where['id'] = array('in',$id);
		$mod =  M('user')->where($where)->setInc('viptime',$last);		
		if($mod>0){
           die("y");
        }else{
            die("n");
        }
		
	}
	
	public function shengji(){
        $id = $_GET['id'];
		$data['status'] = '2';
		$mod =  M('user')->where("id={$id}")->save($data);		
		if($mod>0){
           die("y");
        }else{
            die("n");
        }
		
	}
	
	
}