//window.backend_serverUrl="http://localhost:20521";
window.backend_serverUrl="";
$("#fastNav").on("click",".nav-link",function(){
	var newsCategory=$(this).data("newscategory");
	getNewsByCategory(newsCategory);
});

function getNewsByCategory(newsCategory) {
	var newsContainer;
	var newsContainerId;
	if (newsCategory == 1) {
		newsContainerId = "#companyNews";
	} else if (newsCategory == 5) {
		newsContainerId = "#industryNews";
	} else if (newsCategory == 2) {
		newsContainerId = "#noticeNews";
	}else if(newsCategory==6){
		newsContainerId="#partyBuildingNews";
	}else if(newsCategory==7){
		newsContainerId="#themeEducationNews";
	}else if(newsCategory==3){
		newsContainerId="#theneweraNews";
	}
	newsContainer = $(newsContainerId);
	$.ajax({
		url: window.backend_serverUrl+"/suncd/published/news/category/" + newsCategory + "?currentPage=1&pageSize=6",
		success: function(response) {
			//console.log(response)
			var news = response.data;
			if(!news){
				return;
			}
			newsContainer.empty();
			for (let i = 0; i < news.length; i++) {
				let newsitem = `<li>
										  <div style="height:24px;">
										    <a class="news-item" data-bs-toggle="collapse" href="#collapseExample${newsCategory+''+i}" role="button" aria-expanded="false" aria-controls="collapseExample">
										      ${news[i].title}
										    </a>
										  </div>
										  <div data-bs-parent="${newsContainerId}" class="collapse" id="collapseExample${newsCategory+''+i}"><a class="news-detail" href="newsdetail.html?id=${news[i].id}&ctg=${newsCategory}" target="_blank">
											${news[i].intro}
										  </a></div>
									  </li>`;
				newsContainer.append(newsitem);
			}
			setTimeout(function(){
				$(`#collapseExample${newsCategory}0`).collapse('show');
			},150);
			return;
			
			if (newsCategory == 1) {
				$("#blockNewsList").empty();
				let newsLen = news.length > 4 ? 4 : news.length;
				for (let i = 0; i < newsLen; i++) {
					let blockItem = `<div class="col-12 col-sm-12 col-md-3">
										<div data-newsid="${news[i].id}" class="product-item">
											<div class="product-item-img-box">
												<img class="img" src="img/u/n/${news[i].cover}" alt="suncd" onerror="this.style.display='none'" />
											</div>
											<div class="product-title">${news[i].title}</div>
											<div class="product-title-decorate"></div>
											<div class="product-desc">${news[i].intro}</div>
											<div class="publish-date">${news[i].createTime}</div>
										</div>
									</div>`;
					$("#blockNewsList").append(blockItem);
				}
			}

			if (newsCategory == 5) {
				$("#blockIndustryNewsList").empty();
				let industryNewLen = news.length > 4 ? 4 : news.length;
				for (let i = 0; i < industryNewLen; i++) {
					let blockItem = `<div class="col-12 col-sm-12 col-md-3">
										<div class="product-item">
											<div class="product-item-img-box">
												<img class="img" src="img/u33.png" alt="太阳高科" onerror="this.style.display='none'" />
											</div>
											<div class="product-title">${news[i].title}</div>
											<div class="product-title-decorate"></div>
											<div class="product-desc">${news[i].intro}</div>
											<div class="publish-date">${news[i].createTime}</div>
										</div>
									</div>`;
					$("#blockIndustryNewsList").append(blockItem);
				}
			}
			
			if(newsCategory==7){
				$("#blockThemeNewsList").empty();
				let themeNewLen = news.length > 4 ? 4 : news.length;
				for (let i = 0; i < themeNewLen; i++) {
					let blockItem = `<div class="col-12 col-sm-12 col-md-3">
										<div class="product-item">
											<div class="product-item-img-box">
												<img class="img" src="img/u33.png" />
											</div>
											<div class="product-title">${news[i].title}</div>
											<div class="product-title-decorate"></div>
											<div class="product-desc">${news[i].intro}</div>
											<div class="publish-date">${news[i].createTime}</div>
										</div>
									</div>`;
					$("#blockThemeNewsList").append(blockItem);
				}
			}



		},
		error:function(xhr,textStatus,error){
			newsContainer.html("<div style='font-family:fangsong;font-style:oblique;height:100%;' class='d-flex justify-content-center align-items-center'>加载失败！</div>");
		}
	});
}

function getNewsBlockByCategory(newsCategory) {
	var newsContainer;
	var newsContainerId;
	if (newsCategory == 1) {
		newsContainerId = "#companyNews";
	} else if (newsCategory == 5) {
		newsContainerId = "#industryNews";
	} else if (newsCategory == 2) {
		newsContainerId = "#noticeNews";
	}else if(newsCategory==6){
		newsContainerId="#partyBuildingNews";
	}else if(newsCategory==7){
		newsContainerId="#themeEducationNews";
	}else if(newsCategory==11){
		newsContainerId="#customerCases";
	}
	newsContainer = $(newsContainerId);
	$.ajax({
		url: window.backend_serverUrl+"/suncd/published/news/category/" + newsCategory + "?currentPage=1&pageSize=6",
		success: function(response) {
			var news = response.data;
			if(!news){
				return;
			}
			if(newsCategory==1){
				$("#blockNewsList").empty();
				let newsLen = news.length > 4 ? 4 : news.length;
				for (let i = 0; i < newsLen; i++) {
					let blockItem = `<div class="col-12 col-sm-12 col-md-3">
										<a href='newsdetail.html?id=${news[i].id}&ctg=${newsCategory}' data-newsid="${news[i].id}" class="product-item">
											<div class="product-item-img-box">
												<img class="img" src="img/u/n/${news[i].cover}" alt="suncd" onerror="this.style.display='none'" />
											</div>
											<div class="product-title">${news[i].title}</div>
											<div class="product-title-decorate"></div>
											<div class="product-desc">${news[i].intro}</div>
											<div class="publish-date">${news[i].createTime}</div>
										</a>
									</div>`;
					$("#blockNewsList").append(blockItem);
				}
			}else if (newsCategory == 6) {
				$("#blockNewsList").empty();
				let newsLen = news.length > 4 ? 4 : news.length;
				for (let i = 0; i < newsLen; i++) {
					let blockItem = `<div class="col-12 col-sm-12 col-md-3">
										<a href='newsdetail.html?id=${news[i].id}&ctg=${newsCategory}' data-newsid="${news[i].id}" class="product-item">
											<div class="product-item-img-box">
												<img class="img" src="img/u/n/${news[i].cover}" alt="suncd" onerror="this.style.display='none'" />
											</div>
											<div class="product-title">${news[i].title}</div>
											<div class="product-title-decorate"></div>
											<div class="product-desc">${news[i].intro}</div>
											<div class="publish-date">${news[i].createTime}</div>
										</a>
									</div>`;
					$("#blockNewsList").append(blockItem);
				}
			}else if (newsCategory == 5) {
				$("#blockIndustryNewsList").empty();
				let industryNewLen = news.length > 4 ? 4 : news.length;
				for (let i = 0; i < industryNewLen; i++) {
					let blockItem = `<div class="col-12 col-sm-12 col-md-3">
										<a target='_blank' href='newsdetail.html?id=${news[i].id}&ctg=${newsCategory}' class="product-item">
											<div class="product-item-img-box">
												<img class="img" src="img/u/n/${news[i].cover}" alt="suncd" onerror="this.style.display='none'" />
											</div>
											<div class="product-title">${news[i].title}</div>
											<div class="product-title-decorate"></div>
											<div class="product-desc">${news[i].intro}</div>
											<div class="publish-date">${news[i].createTime}</div>
										</a>
									</div>`;
					$("#blockIndustryNewsList").append(blockItem);
				}
			}else if(newsCategory==7){
				$("#blockThemeNewsList").empty();
				let themeNewLen = news.length > 4 ? 4 : news.length;
				for (let i = 0; i < themeNewLen; i++) {
					let blockItem = `<div class="col-12 col-sm-12 col-md-3">
										<a target='_blank' href='newsdetail.html?id=${news[i].id}&ctg=${newsCategory}' data-newsid="${news[i].id}" class="product-item">
											<div class="product-item-img-box">
												<img class="img" src="img/u/n/${news[i].cover}" alt="suncd" onerror="this.style.display='none'"/>
											</div>
											<div class="product-title">${news[i].title}</div>
											<div class="product-title-decorate"></div>
											<div class="product-desc">${news[i].intro}</div>
											<div class="publish-date">${news[i].createTime}</div>
										</a>
									</div>`;
					$("#blockThemeNewsList").append(blockItem);
				}
			}else if(newsCategory==11){
				$("#customerCases").empty();
				let themeNewLen = news.length > 4 ? 4 : news.length;
				for (let i = 0; i < themeNewLen; i++) {
					let blockItem = `<div class="col-12 col-sm-12 col-md-3">
										<a target='_blank' href='newsdetail.html?id=${news[i].id}&ctg=${newsCategory}' data-newsid="${news[i].id}" class="product-item">
											<div class="product-item-img-box">
												<img class="img" src="img/u/n/${news[i].cover}" alt="suncd" onerror="this.style.display='none'"/>
											</div>
											<div class="product-title">${news[i].title}</div>
											<div class="product-title-decorate"></div>
											<div class="product-desc">${news[i].intro}</div>
											<div class="publish-date">${news[i].createTime}</div>
										</a>
									</div>`;
					$("#customerCases").append(blockItem);
				}
			}



		}
	});
}

function browseMore(ctgId){
	location.href="newsmore.html?categoryId="+ctgId;
}

$("#blockNewsList,#blockThemeNewsList,#blockIndustryNewsList").on("click",".product-item",function(){
	var id=$(this).data("newsid");
	//alert(id);
	window.location.href="newsdetail.html?id="+id;
});


function getProductByCategory(ctg) {
	//alert(ctg)
	var productContainer = $("#product-container");
	$.ajax({
		url: window.backend_serverUrl+"/suncd/product/category/" + ctg + "?currentPage=1&pageSize=5",
		success: function(response) {
			var productList = response.data;
			productContainer.empty();

			let productListLen = productList.length;
			for (let i = 0; i < productListLen; i++) {
				let coverList=productList[i].coverList;
				let cover1,cover2;
				for(let j=0;j<coverList.length;j++){
					if(coverList[j].category=="cover1"){
						cover1=coverList[j];
					}else{
						cover2=coverList[j];
					}
				}
				if(!cover1){
					cover1={path:"default.png"}
				}
				if(!cover2){
					cover2={path:"default.png"}
				}
				let blockItem = `<div class="col-12 col-sm-12 col-md-3">
									<div data-productid="${productList[i].id}" class="product-item">
										<div class="product-item-img-box">
											<img class="img" src="img/u/p/${cover1.path}" />
										</div>
										<div class="product-title">${productList[i].name}</div>
										<div class="product-title-decorate"></div>
										<div class="product-desc">${productList[i].desc}</div>
										<div class="publish-date">${productList[i].createTime}</div>
									</div>
								</div>`;
				productContainer.append(blockItem);
			}
			
			
		}
	});
}

function getProductByNewsCategory(ctg) {
	//alert(ctg)
	var productContainer = $("#product-container");
	$.ajax({
		//url: window.backend_serverUrl+"/suncd/product/category/" + ctg + "?currentPage=1&pageSize=5",
		url: window.backend_serverUrl+"/suncd/published/news/category/" + ctg + "?currentPage=1&pageSize=50",
		success: function(response) {
			var productList = response.data;
			productContainer.empty();

			let productListLen = productList.length;
			for (let i = 0; i < productListLen; i++) {
				
				let cover;
				if(!productList[i].cover){
					cover="default.png";
				}else{
					cover=productList[i].cover;
				}
				
				let blockItem = `<div class="col-12 col-sm-12 col-md-3">
									<a target='_blank' href='newsdetail.html?id=${productList[i].id}&ctg=${ctg}' data-newsid="${productList[i].id}" data-productid="${productList[i].id}" class="product-item">
										<div class="product-item-img-box">
											<img class="img" src="img/u/n/${cover}"  alt="suncd" onerror="this.style.display='none'" />
										</div>
										<div class="product-title">${productList[i].title}</div>
										<div class="product-title-decorate"></div>
										<div class="product-desc">${productList[i].intro}</div>
										<div class="publish-date">${productList[i].createTime}</div>
									</a>
								</div>`;
				productContainer.append(blockItem);
			}
			
			
		}
	});
}