<?php
	include_once("includes/paginator.php");
	include_once("includes/paginator_html.php");   
?>
<?php
  if($action == '0'){
?>
	<?php 
	  if($search == '0'){
		$srv=mysql_query("Select * From tbl_cate_service");
		$srv_title= 'សេវាកម្មអាពាហ៍ពិពាហ៍';	  
	  }else{
	    $srv_title= 'សេវាកម្មអាពាហ៍ពិពាហ៍';
		if(isset($_POST['txtSearch'])){
		   $txtSearch = $_POST['txtSearch'];		   
		}
		$srv=mysql_query("Select * From tbl_cate_service WHERE title LIKE '%".trim($txtSearch)."%'");
	  }
	  
	?>
	<div class="cover_con">
		 <div id="hot_news" style="background: url(images/current.png) no-repeat bottom center; text-align: center; float: left; width: 214px;"><?php echo $srv_title; ?></div>
		 <div id="box_search">
		     <div class="search">
			    <script type="text/javascript" >
					function isValid() {          
					 var search=document.getElementById("txtSearch");           
					 if(search.value==''){
						alert ('Please Input Service Name!');
						document.getElementById('txtSearch').focus();
						return false;
					 }
					}
				</script>
				<form name="fn" id="imageform" method="POST" enctype="multipart/form-data" action="?p=service&search=search_srv#txt" onSubmit="return isValid();">
					<input type="hidden" name="p" value="service">
					<input type="text" name="txtSearch" id="txtSearch" class="formText required" placeholder="Search ..." />
					<input type="submit" id="s_btn" value="&nbsp;"/>
				</form>
            </div>
		 </div>
		 <div class="clear" style="height: 12px;"></div>
		 <?php
		 // Banner Service Slideshow
		 $srv_banner_q = mysql_query("SELECT * FROM tbl_service_banner WHERE status=1 ORDER BY id DESC");
		 $srv_banners = array();
		 if ($srv_banner_q) {
			 while ($rb = mysql_fetch_array($srv_banner_q)) {
				 if (!empty($rb['image'])) {
					 $srv_banners[] = $rb;
				 }
			 }
		 }
		 ?>
		 <?php if (!empty($srv_banners)): ?>
		 <div id="service_banner_slideshow" style="position: relative; width: 100%; max-width: 930px; height: 460px; margin: 0 0 16px 0; overflow: hidden; border-radius: 6px; box-shadow: 0 3px 12px rgba(252,94,165,0.2); border: 2px solid #fc5ea5; background: #fff;">
			 <?php foreach ($srv_banners as $idx => $s_banner): ?>
			 <div class="srv-banner-slide" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: <?php echo ($idx === 0) ? '1' : '0'; ?>; transition: opacity 0.8s ease-in-out; pointer-events: <?php echo ($idx === 0) ? 'auto' : 'none'; ?>;">
				 <?php if (!empty($s_banner['link'])): ?>
					 <a href="<?php echo htmlspecialchars($s_banner['link']); ?>" target="_blank" style="display: block; width: 100%; height: 100%;">
						 <img src="images/ads/<?php echo $s_banner['image']; ?>" style="width: 100%; height: 100%; object-fit: cover; border: 0;" alt="<?php echo htmlspecialchars($s_banner['title']); ?>" />
					 </a>
				 <?php else: ?>
					 <img src="images/ads/<?php echo $s_banner['image']; ?>" style="width: 100%; height: 100%; object-fit: cover; border: 0;" alt="<?php echo htmlspecialchars($s_banner['title']); ?>" />
				 <?php endif; ?>
				 <?php if (!empty(trim($s_banner['title']))): ?>
				 <div style="position: absolute; bottom: 0; left: 0; right: 0; padding: 7px 14px; background: rgba(0,0,0,0.55); color: #fff; font-family: 'Khmer OS', Arial, sans-serif; font-size: 13px; text-shadow: 1px 1px 2px #000; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
					 <?php echo htmlspecialchars(mb_substr(trim($s_banner['title']), 0, 100, 'utf-8')); ?>
				 </div>
			 <?php endif; ?>
			 </div>
			 <?php endforeach; ?>

			 <?php if (count($srv_banners) > 1): ?>
			 <div class="srv-banner-dots" style="position: absolute; bottom: 12px; right: 15px; z-index: 20; display: flex; gap: 6px;">
				 <?php foreach ($srv_banners as $idx => $s_banner): ?>
					 <span class="srv-dot" onclick="goSrvSlide(<?php echo $idx; ?>)" style="width: 11px; height: 11px; border-radius: 50%; background: <?php echo ($idx === 0) ? '#fc5ea5' : 'rgba(255,255,255,0.7)'; ?>; border: 1px solid #fff; cursor: pointer; transition: background 0.3s; display: inline-block;"></span>
				 <?php endforeach; ?>
			 </div>
			 <script type="text/javascript">
			 (function() {
				 var slides = document.querySelectorAll('#service_banner_slideshow .srv-banner-slide');
				 var dots = document.querySelectorAll('.srv-banner-dots .srv-dot');
				 if (!slides || slides.length <= 1) return;
				 var cur = 0;
				 window.goSrvSlide = function(n) {
					 cur = n;
					 for (var i = 0; i < slides.length; i++) {
						 slides[i].style.opacity = (i === cur) ? '1' : '0';
						 slides[i].style.pointerEvents = (i === cur) ? 'auto' : 'none';
						 if (dots[i]) {
							 dots[i].style.background = (i === cur) ? '#fc5ea5' : 'rgba(255,255,255,0.7)';
						 }
					 }
				 };
				 setInterval(function() {
					 cur = (cur + 1) % slides.length;
					 window.goSrvSlide(cur);
				 }, 4000);
			 })();
			 </script>
			 <?php endif; ?>
		 </div>
		 <?php endif; ?>
	 <?php while($row_srv=mysql_fetch_array($srv)){	
	    if(!empty($row_srv['image'])){
			echo'<a href="?p=service&cs_id='.$row_srv['id'].'&action=cate_mnu#txt" style="text-decoration: none;"><div id="srv_cover"><div id="bg_srv_title">'.$row_srv['title'].'</div><div id="img_srv" style="background: url(\'images/service/icon/'.$row_srv['image'].'\') no-repeat center center;"></div></div></a>';
	    }else{
			echo'<a href="?p=service&cs_id='.$row_srv['id'].'&action=cate_mnu#txt" style="text-decoration: none;"><div id="srv_no_cover"><div id="bg_srv_title">'.$row_srv['title'].'</div></div></a>';
		}
	 }
	?>	 
		 <div class="clear" style="height: 0px;"></div>
	</div>
<?php }else{ ?>
     <?php 
	  $srv=mysql_query("Select * From tbl_cate_service"); 
	 ?>
    <div class="cover_con">
		 <div id="hot_news" style="background: url(images/current.png) no-repeat bottom center; text-align: center; float: left; width: 214px;">សេវាកម្មអាពាហ៍ពិពាហ៍</div>
		 <div id="box_search">
		     <div class="search">
			    <script type="text/javascript" >
					function isValid() {          
					 var search=document.getElementById("txtSearch");           
					 if(search.value==''){
						alert ('Please Input Name!');
						document.getElementById('txtSearch').focus();
						return false;
					 }
					}
				</script>
				<form name="fn" id="imageform" method="POST" enctype="multipart/form-data" action="?p=service&action=cate_mnu&search=search_srv#txt" onSubmit="return isValid();">
					<input type="hidden" name="p" value="service">
					<input type="text" name="txtSearch"  id="txtSearch" class="formText required" placeholder="Search ..." />
					<input type="submit" name="search"  id="s_btn" value="&nbsp;"/>
				</form>
            </div>
		 </div>
		 <div class="clear" style="height: 4px;"></div>		 

    <div id="srv_cate">
	    <div id="shadow_m"></div>
	    <div id="mt"></div>
	    <div id="mc">
		<?php while($row_srv=mysql_fetch_array($srv)){ ?>
		   <div id="cc_srv">
		   <ul class="sub"><a href="?p=service&cs_id=<?php echo $row_srv['id']; ?>&action=show#txt" style="text-decoration: none;"><li class="subani" <?php if(isset($_GET['cs_id']) && $_GET['cs_id'] == $row_srv['id']){ ?> style="color: #faeb6b; font-weight: bold;" <?php } ?>>
		   <?php echo $row_srv['title']; ?>
		   </li></a>
		   </ul>
		   </div>
		   
		   <div class="clear" style="height: 4px;"></div>		   
		<?php } ?>   
		</div>
	    <div id="mb"></div>
	</div>
	
	<div id="srv_detail" style="margin-top: 0px;">
		<div id="st"></div>
		<div id="sc">
		    <?php
				if($action != 'show_detail'){
			?>
							<?php 
								if($search == '0'){
									  $achs=mysql_query("SELECT * FROM tbl_service WHERE cid='".mysql_real_escape_string($_GET['cs_id'])."' ORDER BY id DESC");
									  $num_rows = mysql_num_rows($achs);
										if(isset($_GET['page']))
											$s= $_GET['page'];
										else
									  $s="1";
									  $param ="p=service&cs_id=".$_GET['cs_id']."&action=show";
									  $a = new Paginator_html($s,$num_rows, $param);
									  $a->set_Limit(5);
									  $a->set_Links(2);
									  $limit1 = $a->getRange1();
									  $limit2 = $a->getRange2();
									  //End Select Paging by each categories												  
									  $srv_detail=mysql_query("Select * From tbl_service WHERE cid='".mysql_real_escape_string($_GET['cs_id'])."' ORDER BY id DESC LIMIT $limit1, $limit2"); 						  
									  $srv_title_detail1=mysql_fetch_array(mysql_query("Select * From tbl_cate_service WHERE id='".mysql_real_escape_string($_GET['cs_id'])."'"));
									  $srv_title_detail = $srv_title_detail1['title'];
							   }else{							        
									    if(isset($_POST['txtSearch'])){
										   $txtSearch = $_POST['txtSearch'];		   
									    }
										if(isset($_GET['s'])){
										   $txtSearch = $_GET['s'];	
										}
										if(isset($_GET['page'])){
										   $page = '&page='.$_GET['page'];	
										}
										$serverPath = getBaseUrl();
										//echo '<script>alert("'.$serverPath.'");</script>';
										$proUrl = str_replace("/Site/", "/Site/?p=service&action=cate_mnu&search=search_srv&s=".$txtSearch.$page."#txt", $serverPath);
									 	
							 ?>
							 <script type="text/javascript">		
								var url = '<?php echo $proUrl; ?>';	
								//alert(url);									
								window.history.pushState({path:url},'',url);                               								
							 </script>

							<?php							 
							         									 
							         $achs=mysql_query("SELECT * FROM tbl_service WHERE name LIKE '%".trim($txtSearch)."%' OR description Like '%".trim($txtSearch)."%' OR detail Like '%".trim($txtSearch)."%' OR s_s Like '%".trim($txtSearch)."%' ORDER BY id DESC");
									  $num_rows = mysql_num_rows($achs);
										if(isset($_GET['page']))
											$s= $_GET['page'];
										else
									  $s="1";
									  $param ="p=service&action=cate_mnu&search=search_srv&s=".$txtSearch."";
									  $a = new Paginator_html($s,$num_rows, $param);
									  $a->set_Limit(5);
									  $a->set_Links(2);
									  $limit1 = $a->getRange1();
									  $limit2 = $a->getRange2();
									  //End Select Paging by each categories												  
									  $srv_detail=mysql_query("Select * From tbl_service WHERE name LIKE '%".trim($txtSearch)."%' OR description Like '%".trim($txtSearch)."%' OR detail Like '%".trim($txtSearch)."%' OR s_s Like '%".trim($txtSearch)."%' ORDER BY id DESC LIMIT $limit1, $limit2"); 						  
									  $srv_title_detail= 'Result of search for: '.$txtSearch; 				   
							   }
							   ?>
							 							   
							   <?php
							   
							  echo '<div id="hot_news" style="position: relative; margin-top: -8px; background: none; float: left; width: 676px; padding-left: 15px; border-bottom: 1px solid #fc18c7;">»​ '.$srv_title_detail.'</div>';
							?>

							<div class="clear" style="height: 6px;"></div>
							<?php while($row_srv_detail=mysql_fetch_array($srv_detail)){   
							   $con = preg_replace("/<img[^>]+\>/i", "", $row_srv_detail['description']);
							?>
								<div id="box_service">
								   <a href="?p=service&cs_id=<?php echo $row_srv_detail['cid']; ?>&s_id=<?php echo $row_srv_detail['id']; ?>&action=show_detail#txt" style="text-decoration: none;"><div id="read_more">ព័តមានលំអិត</div></a>
								   <div id="box_service_title"><?php echo $row_srv_detail['name']; ?></div>
								   <div class="clear" style="height: 6px;"></div>
								   <?php if(!empty($row_srv_detail['image'])){ ?>
									   <?php echo'<div id="box_service_img" style="background: url(\'images/service/'.$row_srv_detail['image'].'\');"></div>'; ?>
									   <div id="box_service_desc"><?php echo preg_replace("@^(<br\\b[^>]*/?>)+@i", "",$con); ?></div>
								   <?php }else{ ?>
										 <div id="box_service_desc_no_img"><?php echo preg_replace("@^(<br\\b[^>]*/?>)+@i", "",$con); ?></div>
								   <?php } ?>		 
								   <div class="clear" style="height: 6px;"></div> 
								   
								</div>
							<?php } ?>

							<?php if($num_rows > 5){?>  
							<div class="clear" style="height: 25px;"></div> 							
								<table><tr><td>
								  <span class="as" style="color: #ff0000; font-weight: bold; font-family: Arial; font-size: 12px;"><?php $a->first(); ?><?php $a->previousNext(); ?><?php $a->Last(); ?></span>                         
							   </td></tr></table>
							<div class="clear" style="height: 15px;"></div>
							<?php } ?>
							<div class="clear" style="height: 5px;"></div> 
							
			<?php }else{ ?>
						<?php 						
						  $srv_detail=mysql_fetch_array(mysql_query("Select * From tbl_service WHERE id='".mysql_real_escape_string($_GET['s_id'])."'")); 
						  echo '<div id="hot_news" style="position: relative; margin-top: -8px; background: none; float: left; width: 676px; padding-left: 15px; border-bottom: 1px solid #fc18c7;">»​ '.$srv_detail['name'].'</div>';
						?>
						<div class="clear" style="height: 6px;"></div>							
						<div id="show_service_detail">
						<!-- Facebook Share Button -->
						<?php 
						$is_https_share = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
						    || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https')
						    || (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443);
						$current_share_url = ($is_https_share ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
						?>
						<div class="fb-share-container" style="margin: 6px 0 14px 0; display: flex; align-items: center; gap: 10px; flex-wrap: wrap;">
							<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode($current_share_url); ?>" target="_blank" onclick="window.open(this.href, 'facebook-share-dialog', 'width=626,height=436'); return false;" style="display: inline-flex; align-items: center; gap: 8px; background-color: #1877F2; color: #ffffff; padding: 7px 16px; border-radius: 6px; font-family: Arial, sans-serif; font-size: 13px; font-weight: bold; text-decoration: none; box-shadow: 0 2px 6px rgba(24,119,242,0.3);">
								<svg width="16" height="16" fill="#ffffff" viewBox="0 0 24 24"><path d="M24 12.073c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.99 4.388 10.954 10.125 11.854v-8.385H7.078v-3.47h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.47h-2.796v8.385C19.612 23.027 24 18.062 24 12.073z"/></svg>
								<span>Share on Facebook</span>
							</a>
							<div class="fb-share-button" data-href="<?php echo htmlspecialchars($current_share_url); ?>" data-layout="button_count" data-size="large"></div>
						</div>
						<div class="clear" style="height: 0px;"></div>	
                           <?php //echo'<div id="box_service_img" style="background: url(\'images/service/'.$srv_detail['image'].'\');"></div>'; ?>						
						   <?php echo $srv_detail['detail']; ?>
						</div>

            <?php } ?>			
	
		</div>
		<div id="sb"></div>
	</div>
			
	
    <div class="clear" style="height: 0px;"></div>
	</div>
<?php } ?>	