<?php

// 根据课程id获取课程详细信息
require_once ("../user_include.inc");

ErrorLogger::doOutput("Compass...ajax_get_course_detail.php....Start.", 0);

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$mediaId = ParamUtil::getRequestNumber("mediaId", 0);

//参数检查
if(empty($unionId) || empty($mediaId)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

//判断用户是否存在
$param = array();
$param['unionid'] = $unionId;
$param['delete_flg'] = false;
$userList = UserMst::getList($param,'id','desc', 0, 1);
if(empty($userList)) {
	$result["message"] = "用户不存在！";
	responseNG($result);
}

$userMst = $userList[0];

//判断用户的观看权限


$result = array();

//判断视频是否存在
$courseMediaDat = CourseMediaDat::getById($mediaId);
if(empty($courseMediaDat)) {
	$result["message"] = "视频不存在！";
	responseNG($result);
}

//如果为合集视频获取对应的媒体列表
$courseTitle = "";
$teacherName = "";
$teacherProfile = "";
$frontImage = "";
$courseMediaList = array();
//单个视频
if($courseMediaDat->course_id == 0) {
	$courseMediaList[] = $courseMediaDat;
	$courseTitle = $courseMediaDat->title;
	$teacherName = $courseMediaDat->teacher;
	$teacherProfile = $courseMediaDat->teacher_profile;
	$frontImage = $courseMediaDat->front_image;
} else {
	//合集视频
	$courseMst = CourseMst::getById($courseMediaDat->course_id);
	$courseTitle = $courseMst->title;
	$teacherName = $courseMst->teacher;
	$teacherProfile = $courseMst->teacher_profile;
	$frontImage = $courseMst->front_image;
	
	$param = array();
	$param['course_id'] = $courseMediaDat->course_id;
	$param['delete_flg'] = false;
	$tmpList = CourseMediaDat::getList($param,'id','asc');
	if(!empty($tmpList)) {
		foreach($tmpList as $tmp) {
			$tmp->haveBuy = false;
			if(!$tmp->is_free) {
				//收费课程的情况下，判断用户是否已经购买本课程
				$param = array();
				$param['user_id'] = $$userMst->id;
				$param['course_id'] = $courseId;
				$param['media_id'] = $tmp->id;
				$param['is_free'] = false;
				$param['status'] = 'SUCCESS';
				$param['delete_flg'] = false;
				$userBuyList = UserMediaDat::getList($param, "id", "desc", 0, 1);
				if(!empty($userBuyList)) {
					$tmp->haveBuy = true;
				}
			}
			$courseMediaList[] = $tmp;
		}
	}
}

//接口返回数据
$result["courseTitle"] = $courseTitle;
$result["teacherName"] = $teacherName;
$result["teacherProfile"] = $teacherProfile;
$result["frontImage"] = $teacherProfile;
$result["mediaCount"] = count($courseMediaList);
$result["mediaList"] = $courseMediaList;

ErrorLogger::doOutput("Compass...ajax_get_course_detail.php....End.", 0);


//返回结果
responseOK($result);

function responseNG($result) {
	$result = array("status"=>"NG", "result"=>$result);
	print json_encode($result);
	exit;
}
function responseOK($result) {
	$result = array("status"=>"OK", "result"=>$result);
	print json_encode($result);
	exit;
}
?>