<?php
namespace app\mainapp\controller;

use app\mainapp\BaseController;
use think\facade\Session;

use app\common\model\Worker as WorkerModel;
use app\common\model\Video as VideoModel;

class Video extends BaseController
{
	
	
	
	public function getVideo() 
	{
		$videoid = input('videoid/d', 0);
		$video = VideoModel::with(['worker'])->findOrEmpty($videoid);  
		if ($video->isEmpty()){
			page_result(1, "视频信息不存在。");
		}
		page_result(0, "", array(
			'video'		=> $video
			));
	}
	
	
	public function getMore()
	{
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map = array();
		$map[] = ['status','=',3];
		$plist = VideoModel::with(['worker'])->where($map)->order(['createtime'=>'desc', 'id'=>'desc'])->page($ppage)->limit($psize)->append(['status_text'])->select();  
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}
	
	
	
	
	
	
	
	// 管理
	
	public function delVideo()
	{
		$workerid = input('workerid/d', 0);
		$worker = WorkerModel::findOrEmpty($workerid);
		if ($worker->isEmpty()){
			page_result(0, "", array(
				'plist' => array(),
				'pstatus' => 'noMore'
				));
		}
		$videoid = input('videoid/d', 0);
		VideoModel::where(['workerid'=>$workerid])->delete($videoid); 
		page_result(0, "", array( ));
	}
	
	
	
	public function listVideo()
	{
		$workerid = input('workerid/d', 0);
		$worker = WorkerModel::findOrEmpty($workerid);
		if ($worker->isEmpty()){
			page_result(0, "", array(
				'plist' => array(),
				'pstatus' => 'noMore'
				));
		}
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map = array();
		$map[] = ['workerid', '=', $workerid]; 
		$plist = VideoModel::with(['worker'])->where($map)->order(['createtime'=>'desc', 'id'=>'desc'])->page($ppage)->limit($psize)->append(['status_text'])->select();  
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}
	
	
	
	public function setVideo()
	{
		$workerid = input('workerid/d', 0);
		$worker = WorkerModel::findOrEmpty($workerid);
		if ($worker->isEmpty()){
			page_result(1, "企业信息不存在");
		}
		$title = input('title/s', "");
		$videourl = input('videourl/s', "");
		if ( empty($title) || empty($videourl) ){
			page_result(1, "标题、视频信息均不能为空。");
		}
		$form = new VideoModel;
		$form->save([
			'workerid'			=> input('workerid/d', 0),
			'title'				=> $title,
			'imageurl'			=> "",
			'videourl'			=> $videourl,
			'createtime'		=> time()
		]);
		page_result(0, "", array());
	}
	  
}
	