<div class="container">
	<div class="row">
		<div class="panel panel-default">
			<div class="panel-heading">
				<h2 class="text-center">TA数据录入</h2>
			</div>
			<div class="panel-body">
				<div class="method_select">
					  <ul class="nav nav-tabs" role="tablist">
						<li role="presentation">
							<a href="#handinput" aria-controls="handinput" role="tab" data-toggle="tab">手动录入</a>
						</li>
						<li role="presentation">
							<a href="#listinput" aria-controls="listinput" role="tab" data-toggle="tab">列表抓取</a>
						</li>
						<li role="presentation">
							<a href="#excelinput" aria-controls="excelinput" role="tab" data-toggle="tab">excel导入</a>
						</li>
						<li role="presentation">
							<a href="#list_view" aria-controls="list_view" role="tab" data-toggle="tab">数据预览</a>
						</li>
						<li role="presentation" class="active">
							<a href="#config_view" aria-controls="list_view" role="tab" data-toggle="tab">配置中心</a>
						</li>
					  </ul>
					  <div class="tab-content">
						<div role="tabpanel" class="tab-pane" id="handinput">
							<div class="row" style="margin-top:10px;">
								<div class="col-md-18">
									<input class="form-control" id="ta_url" type="text" autocomplete="off" />
								</div>
								<div class="col-md-6">
									<a href="#" id="contentbyurl" class="btn btn-info">抓取页面</a>
								</div>
							</div>
							<div class="result_view">
								<div class="row">
									<h3 class="text-center">评论抓取预览</h3>
									<div class="review_content">
										<div class="col-md-4">
											<p class="review_name"></p>
											<p class="user_loc"></p>
											<p class="review_stars"></p>
										</div>
										<div class="col-md-20">
											<p class="ta_title"></p>
											<p class="rating_date"></p>
											<p class="ta_content"></p>
											<p class="review_pic"></p>
											<p class="experience_date"></p>
										</div>
									</div>
								</div>
							</div>
						</div>
						
						<div role="tabpanel" class="tab-pane" id="excelinput">
							<div class="row" style="margin-top:10px;">
								<div class="col-md-18">
									<input class="form-control" id="file_excel" type="file" />
								</div>
								<div class="col-md-6">
									<a href="#" id="contentbyexcel" class="btn btn-info">导出信息</a>
								</div>
							</div>
							<div class="excel_view">
								<h3 class="text-center">excel数据导出预览</h3>
								<div class="excel_content">
									
								</div>
							</div>
						</div>
						
						<div role="tabpanel" class="tab-pane" id="listinput">
							<div class="row" style="margin-top:10px;">
								<div class="col-md-7">
									<select name="destination" class="form-control" id="destination">
										<option value="">选择站点</option>
										<option value="tp_Beijing">tp_Beijing</option>
										<option value="tp_Shanghai">tp_Shanghai</option>
										<option value="tp_Guilin">tp_Guilin</option>
										<option value="tp_Xian">tp_Xian</option>
										<option value="tp_Zhangjiajie">tp_Zhangjiajie</option>
										<option value="tp_Hangzhou">tp_Hangzhou</option>
										<option value="tp_Hangzhou">tp_Chengdu</option>
										<option value="train">train</option>
									</select>
								</div>
								<div class="col-md-7">
									<select name="production" class="form-control" id="production">
										<option value="">选择产品</option>
									</select>
								</div>
								<div class="col-md-7">
									<select name="nums" class="form-control" id="pagenums">
										<option value="">选择抓取条数</option>
										<option value="">1~5</option>
										<option value="5">6~10</option>
										<option value="10">11~15</option>
									</select>
								</div>
								<div class="col-md-3">
									<a href="#" id="batchcontents" class="btn btn-info">批量抓取</a>
								</div>
								<div class="col-md-24" style="margin-top:30px" id="console"> 
									
								</div>
							</div>
						</div>
						
						<div role="tabpanel" class="tab-pane" id="list_view">
							<div class="row" style="margin-top:10px;">
								<div class="col-md-9">
									<select name="destination" class="form-control" id="view_destination">
										<option value="">选择站点</option>
										<option value="tp_Beijing">tp_Beijing</option>
										<option value="tp_Shanghai">tp_Shanghai</option>
										<option value="tp_Guilin">tp_Guilin</option>
										<option value="tp_Xian">tp_Xian</option>
										<option value="tp_Zhangjiajie">tp_Zhangjiajie</option>
										<option value="tp_Hangzhou">tp_Hangzhou</option>
										<option value="tp_Hangzhou">tp_Chengdu</option>
										<option value="train">train</option>
									</select>
								</div>
								<div class="col-md-9">
									<a href="#" id="view_list" class="btn btn-info">查看数据</a>
								</div>
							</div>
							<div id="list_view_content" style="margin-top:20px;">
								
							</div>
						</div>
						
						<div role="tabpanel" class="tab-pane active" id="config_view">
							<div class="row" style="margin-top:10px;">
								<div class="col-md-5">
									<select name="confg_destination" class="form-control" id="confg_destination">
										<option value="">选择站点</option>
										<option value="tp_Beijing">tp_Beijing</option>
										<option value="tp_Shanghai">tp_Shanghai</option>
										<option value="tp_Guilin">tp_Guilin</option>
										<option value="train">train</option>
									</select>
								</div>
								<div class="col-md-5">
									<input name="production_code" type="text" class="form-control" placeholder="请输入产品代号" />
								</div>
								<div class="col-md-11">
									<input name="production_link" type="text" class="form-control" placeholder="请输入产品链接" />
								</div>
								<div class="col-md-3">
									<a href="#" id="addproduction" class="btn btn-info">添加产品</a>
								</div>
							</div>
							<hr>
							<h3 style="text-align:center">配置查询</h3>
							<div class="row">
								<div class="col-md-5">
									<select name="search_confg_destination" class="form-control" id="search_confg_destination">
										<option value="">选择站点</option>
										<option value="tp_Beijing">tp_Beijing</option>
										<option value="tp_Shanghai">tp_Shanghai</option>
										<option value="tp_Guilin">tp_Guilin</option>
										<option value="tp_Xian">tp_Xian</option>
										<option value="tp_Zhangjiajie">tp_Zhangjiajie</option>
										<option value="tp_Hangzhou">tp_Hangzhou</option>
										<option value="tp_Hangzhou">tp_Chengdu</option>
									</select>
								</div>
								<div class="col-md-3">
									<a href="###" id="searchconfig" class="btn btn-info">查询配置</a>
								</div>
							</div>
							<hr>
							<div id="config_result">
							</div>
						</div>
						
					  </div>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
$(function(){
	//手动提交获取TA评论
	$('#contentbyurl').click(function(){
		//获取填写的url
		var ta_url = $('#ta_url').val();
		var stars = '';
		var pic_htm = '';
		if(ta_url == ''){
			alert('请填写需要采集的TA地址');
		}else{
			$.ajax({
				url:'/info.php/apps/tripadvisor_spider/index/get_reviews_detail',
				data:{url:ta_url},
				type:'POST',
				success:function(json,status){
					var data = $.parseJSON(json);
					console.log(data);
					$('.ta_content').html(data.content);
					$('.experience_date').html('Date of experience: '+data.experience_date);
					$('.review_name').html(data.review_name);
					$('.user_loc').html(data.user_loc);
					$('.rating_date').html('Reviewed:'+data.rating_date);
					for(var i=0;i<data.pic.length;i++){
						pic_htm += '<p><a href="'+data.pic[i]+'" target="_blank"><img src="'+data.pic[i]+'"/></a></p>';
					}
					$('.review_pic').html(pic_htm);
					$('.ta_title').html('<strong>'+data.title+'</strong>');
					if(data.star_nums){
						for(var i=0;i<data.star_nums;i++){
							stars += '<span class="glyphicon glyphicon-star"></span>';
						}
						$('.review_stars').html(stars);
					}
				}
			});
		}
	});
	
	//批量抓取
	$('#batchcontents').click(function(){
		var destination = $('#destination').val();
		var pagenums = $('#pagenums').val();
		var product_links = $('#production').val();
		var product_code = $('#production option:selected').text();
		$(this).addClass('disabled');
		$('#console').html('');
		
		if(destination != ''){
			$.ajax({
				url:'/info.php/apps/tripadvisor_spider/index/get_destination_reviews/',
				data : {
					destination : destination,
					pagenum : pagenums,
					product_links : product_links
				},
				type:'POST',
				success:function(json,status){
					var data = $.parseJSON(json);
					$('#batchcontents').removeClass('disabled');
					$('#console').html('<p>5条评论链接获取成功，下面开始抓取具体评论！</p>');
					
					for(var x=0;x<data.urls.length;x++){
						$.ajax({
							url:'/info.php/apps/tripadvisor_spider/index/get_reviews_detail',
							type:'POST',
							data:{
								url : data.urls[x],
								html_num : 'list_'+x,
								destination : destination,
								product_links : product_links,
								product_code : product_code
							},
							success:function(content,status){
								var data = $.parseJSON(content);
								console.log(content);
								var console_html = $('#console').html();
								console_html += '<p>标题：'+data.title+' 评论抓取成功！</p>';
								$('#console').html(console_html);
							}
						});		
					}
				},
				error:function (){
					alert('获取失败，请重试！');
					$('#batchcontents').removeClass('disabled');
				},
				timeout:function(){
					alert('请求超时，请重试！');
					$('#batchcontents').removeClass('disabled');
				}
				
			});
		}
	});
	
	//修改目的地获取产品
	$('#destination').on("change",function (){
		var destination = $(this).val();
		if(destination != ''){
			$.ajax({
				url : '/info.php/apps/tripadvisor_spider/index/get_production_code',
				data : {destination : destination},
				type:'POST',
				success : function (json,status){
					var jsondata = $.parseJSON(json);
					var options_html = '<option value="">选择产品</option>';
					for (var i=0;i<jsondata.length;i++){
						options_html += '<option value="'+jsondata[i].trc_links+'">'+jsondata[i].trc_code+'</option>';
					}
					$('#production').html(options_html);
				}
			});
		}
	});
	
	//查询配置信息
	$('#searchconfig').on('click',function(){
		var destination = $('#search_confg_destination').val();
		if(destination != ''){
			$.ajax({
				url : '/info.php/apps/tripadvisor_spider/index/get_production_code',
				data : {destination : destination},
				type:'POST',
				success : function (json,status){
					var jsondata = $.parseJSON(json);
					var config_html = '';
					for (var i=0;i<jsondata.length;i++){
						var num = i+1;
						config_html += '<div class="row"><div class="col-md-1">'+num+'</div>';
						config_html += '<div class="col-md-2">'+jsondata[i].trc_code+'</div>';
						config_html += '<div class="col-md-18">'+jsondata[i].trc_links+'</div>';
						config_html += '<div class="col-md-3"><a href="###" id="deleteconfig" class="btn btn-danger" onclick="deleteconfig('+jsondata[i].trc_id+')">删除配置</a></div></div><hr>';
					}
					$('#config_result').html(config_html);
				}
			});
		}
	});
	
	
	//excel导出抓取
	$('#contentbyexcel').click(function(){
		var fileArray = document.getElementById("file_excel").files;
		var formData = new FormData();
		for(var i=0; i<fileArray.length; i++){
            formData.append("fileArray", fileArray[i]);
        }
		
		$.ajax({
			 url:'/info.php/apps/tripadvisor_spider/index/analysis_excel',
			 data: formData,
			 type: 'POST',
			 cache: false,
			 contentType: false,
			 processData: false,
			 success:function(json,status){
				 var jsondata = $.parseJSON(json);
				 var html = '';
				 var j,ta_url;
				 var num = 0;
				 	
				 for (var i=0;i<jsondata.length;i++){
					 for (j in jsondata[i].list_data){
						 html += '<div class="row"><div class="col-md-4">';
						 html += '<p>'+jsondata[i].list_data[j][0]+'</p>';
						 html += '<p>团名:'+jsondata[i].list_data[j][1]+'</p>';
						 html += '<p>导游:'+jsondata[i].list_data[j][2]+'</p>';
						 html += '<p id="reviews_user_'+num+'"></p>';
						 html += '<p id="reviews_loc_'+num+'"></p>';
						 html += '<p id="reviews_stars_'+num+'"></p>';
						 html += '</div>';
						 html += '<div class="col-md-20">';
						 html += '<p id="excel_title_'+num+'"></p>';
						 html += '<p id="excel_reviewed_'+num+'"></p>';
						 html += '<p id="excel_content_'+num+'"></p>';
						 html += '<p id="excel_pic_'+num+'"></p>';
						 html += '<p id="excel_vistiedtime_'+num+'"></p>';
						 ta_url = jsondata[i].list_data[j][3];
						 //console.log(ta_url);
						 if(ta_url.indexOf('ShowUserReviews') != -1){
							 $.ajax({
								 url:'/info.php/apps/tripadvisor_spider/index/get_reviews_detail',
								 data:{url:ta_url,html_num:num,destination:jsondata[i].list_data[j][0],group_name:jsondata[i].list_data[j][1],guidename:jsondata[i].list_data[j][2]},
								 type:'POST',
								 success:function(json_detail,status){
									 var data = $.parseJSON(json_detail);
									 $('#excel_title_'+data.html_id).html(data.title);
									 $('#excel_content_'+data.html_id).html(data.content);
									 $('#reviews_user_'+data.html_id).html('评论用户:'+data.review_name);
									 $('#reviews_loc_'+data.html_id).html('评论用户地区:'+data.user_loc);
									 $('#excel_reviewed_'+data.html_id).html('Reviewed:'+data.rating_date);
									 $('#excel_vistiedtime_'+data.html_id).html('Date of experience:'+data.experience_date);
									 var stars_html = '';
									 for(var i=0;i<data.star_nums;i++){
										stars_html += '<span class="glyphicon glyphicon-star"></span>';
									 }
									 $('#reviews_stars_'+data.html_id).html('星级:'+stars_html);
									 if(data.pic.length > 0){
										 var pic_html = '';
										 for(var j=0;j<data.pic.length;j++){
											pic_html += '<p><a href="'+data.pic[j]+'" target="_blank"><img src="'+data.pic[j]+'"/></a></p>';
										 }
									 }
									 $('#excel_pic_'+data.html_id).html(pic_html);
									 console.log(html);
								 }
							 });
							 
						 }else{
							 html += '<p>'+ta_url+'</p>';
						 }
						 html += '</div></div><hr>';
						 num++;
					 }
					 
				 }
				 //console.log(html);
				 $('.excel_content').html(html);
			 }
		});
	});
	
	//预览数据
	$('#view_list').click(function(){
		var view_destination = $('#view_destination').val();
		if(view_destination != ''){
			$.ajax({
				url:'/info.php/apps/tripadvisor_spider/index/get_all_reviews/'+view_destination,
				type:'POST',
				success:function(json,status){
					var jsondata = $.parseJSON(json);
					var html = '';
					for(var y=0;y<jsondata.list.length;y++){
						html += '<div class="row"><div class="col-md-4">';
						html += '<p class="review_name">'+jsondata.list[y].tr_member_name+'</p><p class="user_loc">'+jsondata.list[y].tr_member_loc+'</p><p class="review_stars">';
						for(var i=0;i<jsondata.list[y].tr_member_starts;i++){
							html += '<span class="glyphicon glyphicon-star"></span>';
						}				
						html += '</p></div><div class="col-md-20"><p class="ta_title">'+jsondata.list[y].tr_review_title+'</p><p class="rating_date">Reviewed: '+jsondata.list[y].tr_review_date+'</p><p class="ta_content">'+jsondata.list[y].tr_content+'</p>';
						html += '<p class="review_pic">';
						
						if($.parseJSON(jsondata.list[y].tr_review_pics).length > 0){
							for(var j=0;j<$.parseJSON(jsondata.list[y].tr_review_pics).length;j++){
								html += '<p><a href="'+$.parseJSON(jsondata.list[y].tr_review_pics)[j]+'" target="_blank"><img src="'+$.parseJSON(jsondata.list[y].tr_review_pics)[j]+'"/></a></p>';
							}
						}
						
						html += '</p><p class="experience_date">Date of experience: '+jsondata.list[y].tr_visited_date+'</p><p class="product_code">product code: '+jsondata.list[y].tr_product_code+'</p></div></div><hr>';
					}
					html += '<p class="pull-right">total nums : '+jsondata.list.length+'</p>'
					$('#list_view_content').html(html);
				}
			});
		}
	});
	
	//添加数据
	$('#addproduction').click(function (){
		var config_destination = $('#confg_destination').val();
		var production_code = $('input[name="production_code"]').val();
		var production_link = $('input[name="production_link"]').val();
		$(this).addClass('disabled');
		
		if(config_destination && production_code && production_link){
			$.ajax({
				url : '/info.php/apps/tripadvisor_spider/index/add_production/',
				data : {
					config_destination : config_destination,
					production_code : production_code,
					production_link : production_link
				},
				type:'POST',
				success : function (json,status){
					var jsondata = $.parseJSON(json);
					$('#confg_destination').val('');
					$('input[name="production_code"]').val('');
					$('input[name="production_link"]').val('');
					$('#addproduction').removeClass('disabled');
					alert(jsondata.reason);
				}
			});
		}else{
			alert('请输入完整参数');
		}
		
	});
});

function deleteconfig(id){
	if(id != ''){
		$.ajax({
			url : '/info.php/apps/tripadvisor_spider/index/deleteconfig/',
			data : {
					config_id : id
				},
				type:'POST',
				success:function(){
					alert('配置已删除');
					window.location.reload();
				}
		});
	}
}


</script>