<?php
// +----------------------------------------------------------------------
// | WindSNS 国内领先企业级社交开源SNS框架系统
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://windsns.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 常州微诺信息科技有限公司 http://www.weinuo.net
// +----------------------------------------------------------------------
// | E-mail: open@weinuo.net
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;
use app\common\controller\Backend;
use fast\Tree;

/**
 * 分类管理
 *
 * @icon fa fa-list
 * @remark 用于统一管理网站的所有分类,分类可进行无限级分类,分类类型请在常规管理->系统配置->字典配置中添加
 */
class Videos extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $videoslist = Db::name('admin_videos')->
        alias('a')->join('ws_admin_videostype w','a.videos_fenleiid = w.video_typeid') ->
        order('videosid', 'desc')->paginate(20); 
        $page = $videoslist->render();
    	$this->assign('videoslist', $videoslist);
        $this->assign('page', $page);
        return $this->view->fetch('index');
    }
    
    public function videostypelist(){ 
    	
    	$urlvideostypeid  =  input('get.videostypesid'); 
    	$videoslist = Db::name('admin_videos')->
        alias('a')->join('ws_admin_videostype w','a.videos_fenleiid = w.video_typeid') ->
        where(['videos_fenleiid'=>$urlvideostypeid])-> 
        order('videosid', 'desc') -> paginate(20,false,['query' => request()->param()]); 
        $page = $videoslist->render();
    	$this->assign('videoslist', $videoslist);
        $this->assign('page', $page);
    	return $this->view->fetch('videos_typeindex');
    	
    }
   

      
    public function videos_tuijianokdo()
    {
    $urlcideostypeid  =  input('get.videosid'); 
    $data['videos_hot'] =  2; 
    $xiugaizhuangtai = Db::name('admin_videos')->where('videosid', $urlcideostypeid)->update($data);// 
    return  $this->fetch('videos_tuijiandook');
    }
    
    

    public function videos_tuijiannodo()
    {
    $urlcideostypeid  =  input('get.videosid'); 
    $data['videos_hot'] =  1; 
    $xiugaizhuangtai = Db::name('admin_videos')->where('videosid', $urlcideostypeid)->update($data);// 
    return  $this->fetch('videos_tuijiandook');
    }
    
    
   
    public function videos_openokdo()
    {
    $urlcideostypeid  =  input('get.videosid'); 
    $data['videos_opentype'] =  2; 
    $xiugaizhuangtai = Db::name('admin_videos')->where('videosid', $urlcideostypeid)->update($data);// 
    return  $this->fetch('videos_tuijiandook');
    }
    
    
    public function videos_opennodo()
    {
    $urlcideostypeid  =  input('get.videosid'); 
    $data['videos_opentype'] =  1; 
    $xiugaizhuangtai = Db::name('admin_videos')->where('videosid', $urlcideostypeid)->update($data);// 
    return  $this->fetch('videos_tuijiandook');
    }   
    
  
    
    
    public function upload()
    {
        $files = request()->file();
        $imags = [];
        $errors = [];
        foreach($files as $file){
          
            $info = $file->move(ROOT_PATH . 'public' . DS . 'imguploads/videos');
          
          
          
            if($info){
              
                $path = '/imguploads/videos/'.$info->getSaveName();
                array_push($imags,$path);
            }else{
                array_push($errors,$file->getError());
            }
        }

      
        if(!$errors){
            $msg['errno'] = 0;
            $msg['data'] = $imags;
            return json($msg);
        }else{
            $msg['errno'] = 1;
            $msg['data'] = $imags;
            $msg['msg'] = "上传出错";
            return json($msg);
        }
    }    
    
    

    
    
      public function videos_add()
    {
    
    $videosype = Db::name('admin_videostype')->order('video_typeid', 'desc')->select(); 
     $this->assign('videosype', $videosype);

    
    return $this->view->fetch('videos_add');
    
    }
    
    
      public function videosdo()
    {

				        $time = time();
						$paytimess = date('Y-m-d H:i:s',$time);
					    $tadytimes = strtotime($paytimess);
				    	$videosadd['videoslittle']=  input('post.videoslittle');
				    	$videosadd['videos_admin']=  input('post.videos_admin'); 
				    	$file=  request()->file('videos_img');
				    	$videosadd['videos_fenleiid']=  input('post.videos_fenleiid');
				    	$videosadd['videos_url']=  input('post.videos_url');
				    	$videosadd['videos_seo']=  input('post.videos_seo'); 
				    	$videosadd['videos_laiyuan']=  input('post.videos_laiyuan');
				    	$videosadd['videos_hot']=  input('post.videos_hot'); 
				    	$videosadd['videos_opentype']=  input('post.videos_opentype'); 
				    	$videosadd['videos_pay']=  input('post.videos_pay'); 
				    	$videosadd['viseos_prices']=  input('post.viseos_prices'); 
				    	$videosadd['videos_infos']=  input('post.videos_infos'); 
				    	$videosadd['videos_addtimes']=  $tadytimes; 
 
  if($file){
			            $info = $file->validate(['size'=>2000000000,'ext'=>'jpg,png'])->move(ROOT_PATH . 'public/static' . DS . 'uploads/videosimg');
			           
			
			            if($info){
			            	
			            	 $imgpath = $info->getSaveName();
			                 $imgnames = $info->getFilename();
			            	
			        	        $videosadd['videos_img']=  $imgpath; 
						        $videosadd['videos_enclosurenames']=  $imgnames; 
						    	$wedid = Db::name('admin_videos')->insert($videosadd);
						    	return $this->view->fetch('videos_adddook');
			    	        }else{
	
			           return $this->view->fetch('videos_adddono');
			        }
    	
    	
            }else{
            	
            	$wedid = Db::name('admin_videos')->insert($videosadd);
            	return $this->view->fetch('videos_adddook');
            	
            	
            	
            }
    	

    	
    	
    }
    
    
    
    

    public function videos_type()//视频分类页面
    {

        $videostypeinfos = Db::name('admin_videostype')->order('video_typeid', 'desc')->paginate(20); //分类列表
		$page = $videostypeinfos->render();
		$this->assign('videostypeinfos', $videostypeinfos);
		$this->assign('page', $page);

        return $this->view->fetch('videos_type');
      
    }
    

   



   //资讯栏目添加
     public function addviseostype()
    {
      $time = time();
	  $paytimess = date('Y-m-d H:i:s',$time);
	  $thistimes = strtotime($paytimess);//获取当前时间
	  
	  $video_typenames = $this->request->request('video_typenames');
      $articleadtypeadd['video_typenames'] =  input('post.video_typenames'); // 获取视频分类名称
      $thisvideosnames = input('post.video_typenames'); // 获取视频分类名称
      $articleadtypeadd['video_opentype'] =  input('post.video_opentype'); // 获取视频分类开启状态1正常2关闭
      $articleadtypeadd['video_addtimes'] =  $thistimes; // 获取视频分类添加的时间

      $sqlnames  = Db::name('admin_videostype')->where(['video_typenames'=>$thisvideosnames])->find();//重复判断！

		if(!$video_typenames){
         	return $this->view->fetch('videos_addtypenoa');//标题为空判断，保存失败！
         	exit;
        }elseif($sqlnames){
			return $this->view->fetch('videos_addtypeno');//标题存在重复，保存失败！
			exit;
		}else{

		 $wedid = Db::name('admin_videostype')->insert($articleadtypeadd);//   保存SQL
		 return $this->view->fetch('videos_addtypeok');//保存成功！
		}  
      

    }  


      //视频分类状态切换 ----开启
    public function vidoes_typeokdo()
    {
    $urlcideostypeid  =  input('get.videostypeid'); //  获取地址栏栏目ID
    $data['video_opentype'] =  1; // 获取视频分类状态: 1、开启 2、隐藏
    $xiugaizhuangtai = Db::name('admin_videostype')->where('video_typeid', $urlcideostypeid)->update($data);// 
    return  $this->fetch('videos_typedook');//操作OK！，更新隐藏状态2
    }
    
    
      //视频分类状态切换 ----隐藏关闭
    public function vidoes_typenodo()
    {
    $urlcideostypeid  =  input('get.videostypeid'); //  获取地址栏栏目ID
    $data['video_opentype'] =  2; // 获取视频分类状态: 1、开启 2、隐藏
    $xiugaizhuangtai = Db::name('admin_videostype')->where('video_typeid', $urlcideostypeid)->update($data);// 
    return  $this->fetch('videos_typedook');//操作OK！，更新隐藏状态2
    }
    
   
   
   
     //栏目更新执行
    public function videos_typeupdata($videostypetypeid)
    {
      $appstid =   Request::instance()->request('videostypetypeid');//获取当前分类的ID 
      $videostypenames['video_typenames'] =  input('post.video_typenames'); // 获取视频分类名称
      $thisvideosnames = input('post.video_typenames'); // 获取资讯栏目名称
      $sqlnames  = Db::name('admin_videostype')->where(['video_typenames'=>$thisvideosnames])->find();//重复判断！
      
      if(!$videostypenames['video_typenames']){
         	return $this->view->fetch('videos_upnob');//名称为空判断，保存失败！
         	exit;
      }elseif($sqlnames){
			return $this->view->fetch('videos_upnoa');//名称存在重复，保存失败！
			exit;
		}else{
      	    $xiugaizhuangtai = Db::name('admin_videostype')->where('video_typeid', $appstid)->update($videostypenames);// 
            return  $this->fetch('videos_typeupok');//更新成功，OK！
      }

    }
   

   
       public function videos_del($videosid) //删除视频
    {
         $urlvideosid  =  input('get.videosid'); //  获取地址栏视频ID
         $shanchu = Db::name('admin_videos')->where('videosid',$urlvideosid)->delete();
         if($shanchu){
            return $this->view->fetch('videos_deldook');//删除成功！
         }else{
          	return $this->view->fetch('article_delno');//删除失败！
			exit;
         }
   }
   
   
   
   
   
   
       public function videos_typedel($videostypeid) //删除视频分类
    {
        $urlvideostypeid =  input('get.videostypeid'); //  获取地址栏栏目ID
        $typewoldsum = Db::name('admin_videos') -> where(['videos_fenleiid' => $urlvideostypeid])->count();//查询该栏目下存在的资讯数量
         
        if($typewoldsum > 1 or $typewoldsum == 1){
            return $this->view->fetch('videos_typedelno');//如果该栏目ID下存在文章数，删除失败！
			exit;
        }else{
         	
         	$shanchu = Db::name('admin_videostype')->where('video_typeid',$urlvideostypeid)->delete();
 	        if($shanchu){
	            return $this->view->fetch('videos_typedelok');//删除成功！
	        }else{
	          	return $this->view->fetch('videos_typedelno');//删除失败！
				exit;
	        }

        }


   }
   
   
   
   
   
   
    //视频编辑页面
   
    public function videos_edit($videosid)
    {
	    $urlarticleid  =  input('get.videosid'); // 获取地址栏ID
    	$videosinfos = Db::name('admin_videos') -> where('videosid',$urlarticleid)-> select();//查询这个资讯所有信息
        $this->assign('videosinfos', $videosinfos);//资讯信息赋值到页面
	    
	    $videosype = Db::name('admin_videostype')->order('video_typeid', 'desc')->select(); //视频分类打印
        $this->assign('videosype', $videosype);


        $domain = $_SERVER['SERVER_NAME']; 
	    $this->assign('domain',$domain);//把当前域名赋值到页面
	    
	    return $this->view->fetch('videos_edit');
    }
   
   
   
   
   
      //资讯编辑执行
    public function videosxiugaido($videosid)
    {
    	$urlid  =  input('get.videosid'); //  获取地址栏栏目ID

     
     
      //========================当前时间======================== //
				        $time = time();
						$paytimess = date('Y-m-d H:i:s',$time);
					    $tadytimes = strtotime($paytimess);//获取当前时间
				        //========================当前时间======================== //
				    	
				    	$videosadd['videoslittle']=  input('post.videoslittle'); // 视频标题
				    	$videosadd['videos_admin']=  input('post.videos_admin'); // 作者
				    	$file=  request()->file('videos_img'); // 获取视频图片路径
				    	$videosadd['videos_fenleiid']=  input('post.videos_fenleiid'); // 视频分类
				    	$videosadd['videos_url']=  input('post.videos_url'); // 视频地址
				    	$videosadd['videos_seo']=  input('post.videos_seo'); // SEO关键字
				    	$videosadd['videos_laiyuan']=  input('post.videos_laiyuan'); // 视频来源
				    	$videosadd['videos_hot']=  input('post.videos_hot'); // 是否推荐
				    	$videosadd['videos_opentype']=  input('post.videos_opentype'); // 发布状态
				    	$videosadd['videos_pay']=  input('post.videos_pay'); // 是否收费：1免费 2收费'
				    	$videosadd['viseos_prices']=  input('post.viseos_prices'); // 售价
				    	$videosadd['videos_infos']=  input('post.videos_infos'); // 内容
				    	$videosadd['videos_addtimes']=  $tadytimes; // 发布时间
 
 
  if($file){
			            $info = $file->validate(['size'=>2000000000,'ext'=>'jpg,png'])->move(ROOT_PATH . 'public/static' . DS . 'uploads/videosimg');

			            if($info){
			            	
			            	 $imgpath = $info->getSaveName();//文件路径
			                 $imgnames = $info->getFilename();//文件名
			            	
			        	        $videosadd['videos_img']=  $imgpath; // 获取视频图片路径
						        $videosadd['videos_enclosurenames']=  $imgnames; // 附件：图片名称
						    	$wedid = Db::name('admin_videos')-> where('videosid', $urlid) -> update($videosadd);//   保存SQL
						    	return $this->view->fetch('videos_updatadook');
			    	        }else{
			            // 上传失败获取错误信息
			           return $this->view->fetch('videos_adddono');//操作失败！判断文件类型，大小是否OK
			        }
    	
            }else{
            	
            	$wedid = Db::name('admin_videos')-> where('videosid', $urlid) ->update($videosadd);//   保存SQL
            	return $this->view->fetch('videos_updatadook');
            	
            }
     

    }  
   
   
   
   
   
   
// 视频支付订单列表
   public function videospaylist()
    {
   
   $videospaylist = Db::name('admin_videospaylist')->alias('a') 
   -> join('user w','a.user_id = w.id') -> 
   join('admin_videos e','a.videospay_videosid = e.videosid') -> 
   
   order('videospayid', 'desc')->paginate(20); //视频订单列表
   $page = $videospaylist->render();
   $this->assign('videospaylist', $videospaylist);
   $this->assign('page', $page);
    return $this->view->fetch('videos_paylist');
 
    } 
    
  // 视频支付订单列表----------用户筛选分类列表
   public function videospayuserlist()
    {
   
   $urlvideospayuserlist  =  input('get.videospayuserid'); //  获取地址栏订单用户Id
   
   $videospaylist = Db::name('admin_videospaylist')->alias('a') 
   -> join('user w','a.user_id = w.id') -> 
   join('admin_videos e','a.videospay_videosid = e.videosid') -> 
   where(['user_id' => $urlvideospayuserlist])-> 
   order('videospayid', 'desc')-> paginate(20,false,['query' => request()->param()]); //订单列表
   $page = $videospaylist->render();
   $this->assign('videospaylist', $videospaylist);
   $this->assign('page', $page);
    return $this->view->fetch('videos_userpaylist');
 
    }   
   
   
 // 视频支付订单彻底删除 
        public function videos_paylistdel($videospayid) //删除视频
    {
         $urlvideospayid  =  input('get.videospayid'); //  获取地址栏视频订单ID
         $shanchu = Db::name('admin_videospaylist')->where('videospayid',$urlvideospayid)->delete();
         if($shanchu){
            return $this->view->fetch('videos_delpaylistdook');//删除成功！
         }else{
          	return $this->view->fetch('article_delno');//删除失败！
			exit;
         }
   }  
   
   
   
  
   
   
   
   
   
   
}
