$(function(){
	var searchVal=window.location.search;
    if(searchVal && searchVal!=''){
        var tag=searchVal.indexOf("id=");
        if(tag>=0){
            var idArr=searchVal.split("=");
            var active_id=idArr[1];
            preLoadingInit();
            getCourseDetailData(active_id);
        }
    }
})

function preLoadingInit(){
	var clientHeight=$(window).height();
	var loadingheight=clientHeight-100;
	var text='<div style="height:'+loadingheight+'px;">'
		+'<div class="col-sm-2">'
		+'<div class="sp sp-circle"></div>'
		+'<h5 class="preloading">努力加载中...</h5>'
		+'</div>'
		+'</div>';
	$("#root").html(text);
}

function getCourseDetailData(target){
    var totalURL=gobalData.link;
    var requestURL=totalURL.course_detail;
    $.ajax({
        url:requestURL,
        type:'POST',
        dataType:'json',
        data:{
            spec_id:target
        },
        success:function(res){
            if(res.code=='1'){
                var _data=res.data;
                courseDetailInit(target,_data);
            }
        }
    });
}



function courseDetailInit(target,data){
        $("#root").empty();
        var deatilBrief='';
        if(data.brief!=''){
            deatilBrief=data.brief;
        }else{
            deatilBrief='<div style="width:100%;text-align:center;font-size:14px;color:#606266;">暂无简介</div>';
        }
        //console.log(data);
        
        var text='<div style="padding-bottom:10px;border-bottom:solid #DCDFE6 1px;">'
            +'<div style="margin-bottom:5px;width:100%;height:160px;border:solid #EBEEF5 1px;">'
            +'<img src="'+data.course_img+'" alt="" style="width:100%;height:100%;"/>'
            +'</div>'
            +'<div class="courseInfoItem">课程名称：'+data.course_name+'</div>'
            +'<div class="courseInfoItem">责任教师：'+data.teacher_name+'</div>'
            +'<div class="courseInfoItem">单位机构：'+data.teacher_organization+'</div>'
            +'</div>'
            //团队
            +'<div style="padding-bottom:10px;border-bottom:solid #DCDFE6 1px;">'
            +'<div style="font-size:14px;color:#303133;padding-top:10px;">课程安排</div>'
            +'<div style="width:100%;font-size:14px;color:#303133;padding:10px 0px 5px 0px;">'
            +'<table id="companyModel" border style="width:100%;">'
            +'<tr style="width:100%;">'
            +'<td style="width:15%;text-align:center;">时间</td>'
            +'<td style="width:60%;text-align:center;">授课内容</td>'
            +'<td style="width:25%;text-align:center;">授课教师</td>'
            +'</tr>'
            +'</table>'
            +'</div>'
            +'</div>'
            //内容简介
            +'<div style="padding:10px 0px 10px 0px;border-bottom:solid #DCDFE6 1px;font-size:14px;color:#606266;">'
            +deatilBrief
            +'</div>'
            //章节显示
            +'<div id="chapterModel" style="padding-top:5px;">'
            //+chapterText
            +'<div style="clear:both;"></div>'
            +'</div>'
            +'<div style="clear:both;"></div>';
        $("#root").html(text);
        getCompanyData(target);
        getChapterData(target);
        
        $("#closeVideo").on("tap",function(){
        		$("#videoModel").empty();
        		$("#closeVideo").hide();
        		$("#videoModelWrapper").css({
				height:'0px'
			});
        });
    }

function getChapterData(target){
        var totalURL=gobalData.link;
        var requestURL=totalURL.course_chapter;
        $.ajax({
            url:requestURL,
            type:'POST',
            dataType:'json',
            data:{
                spec_id:target,
                page:1,
                limit:10000
            },
            success:function(res){
                if(res.code=='1'){
                    var _data=res.data;
                    var text='';
                    var len=_data.length;
                    gobalData.chapterData=_data;
                    if(len>0){
                        for(var i=0;i<len;i++){
                        		var num=i+1;
                            var chapterItem=_data[i];
                            var chapterTitle=chapterItem.zj_name;
                            text+='<div style="width:50%;float:left;padding:5px 0px 5px 0px;">'
                                +'<div id="chapterItem_'+chapterItem.zj_id+'" '
                                +'onclick="handleChapterItem(chapterItem_'+chapterItem.zj_id+')"'
                                +'style="width:96%;margin:0 auto;">'
                                +'<div style="width:100%;height:100px;border:solid #DCDFE6 1px;">'
                                +'<img src="'+chapterItem.zj_img+'" alt="" '
                                +'style="width:100%;height:100px;"/>'
                                +'</div>'
                                +'<div style="padding-top:5px;font-size:13px;color:#606266;">'
                                +chapterTitle
                                +'</div>'
                                +'</div>'
                                +'</div>';
                            if(num%2==0){
                            		text+='<div style="clear:both;"></div>';
                            }
                            
                            if(i==len-1){
                                $("#chapterModel").append(text);
                            }
                        }
                    }else{
                        gobalData.chapterLoading = false;
                        text='<div style="width:100%;text-align:center;font-size:14px;color:#606266;padding-top:10px;">'
                            +'暂无章节'
                            +'</div>';
                        $("#chapterModel").append(text);
                    }
                    
                }
            }
        });
    }

function getCompanyData(target){
        var totalURL=gobalData.link;
        var requestURL=totalURL.course_company;
        $.ajax({
            url:requestURL,
            type:'POST',
            dataType:'json',
            data:{
                spec_id:target,
                data_type:"json",
                page:1,
                limit:10000
            },
            success:function(res){
                if(res.code=='1'){
                    var _data=res.data;
                    var len=_data.length;
                    var text='';
                    if(len>0){
                        for(var i=0;i<len;i++){
                            var item=_data[i];
                            text+='<tr style="width:100%;font-size:13px;color:#606266;">'
                                +'<td style="width:15%;text-align:center;">'+item.name+'</td>'
                                +'<td style="width:60%;text-align:center;">'+item.organization+'</td>'
                                +'<td style="width:25%;text-align:center;">'+item.subject+'</td>'
                                +'</tr>';
                            if(i==len-1){
                                $("#companyModel").append(text);
                            }
                        }
                    }else{
                        text='<tr style="width:100%;font-size:13px;color:#606266;">'
                        		+'<td colspan=3 align="center">'
                            	+'暂无数据'
                            	+'</td>'
                            	+'</tr>';
                        $("#companyModel").append(text);
                    }
                }
            }
        });
    }

function handleChapterItem(target){
        var _id=target.id;
        var tag=_id.indexOf("_");
        if(tag>=0){
            var _idArr=_id.split("_");
            var activeId=_idArr[1];
            var _chapterData=gobalData.chapterData;
            for(var i in _chapterData){
                var item=_chapterData[i];
                if(item.zj_id==activeId){
                		//console.log(item);
                		$("#videoModel").empty();
                		var videoLink=item.zj_video;
                		var _videoLink=videoLink.trim();
                		if(_videoLink!=''){
                        var text='<video controls="controls" controlsList="nodownload" '
                        		+'x5-video-player-type="h5" x5-video-orientation="landscape"'
                        		+'poster="'+item.zj_img+'"'
                        		+'style="width:100%;height:100%;">'
							+'<source src="'+_videoLink+'" type="video/mp4">'
							+'您的浏览器不支持Video'
							+'</video>';
						
						var clientHeight=$(window).height();
						$("#videoModelWrapper").css({
							height:clientHeight+'px'
						});
                        $("#videoModel").append(text);
                        $("#closeVideo").show();
                			//mui('#popover').popover('show');
                		}else{
                			mui.toast('此章节暂无视频',{ duration:'short', type:'div' });
                		}
                		
                }
            }
        }
}


