<?php
	include "db_connection/db_connection.php";
	include "config/config.php";
	$page   = isset($_GET['page'])     ? intval($_GET['page']) : 1;
	$limit  = isset($_GET['limit'])    ? intval($_GET['limit']) : 100;
	$search = isset($_GET['search'])   ? mysqli_real_escape_string($connect, $_GET['search']) : "";
	$sortBy = isset($_GET['sortBy'])   ? $_GET['sortBy'] : "party_det_id";
	$sortDir= isset($_GET['sortDir'])  ? $_GET['sortDir'] : "DESC";

	$offset = ($page - 1) * $limit;

	$allowedCols = ['party_det_id','party_code','cmp_name','con_pers_1','con_pers_2','con_pers_3','con_pers_des_1','con_pers_des_2','con_pers_des_3','con_no_1','con_no_2','con_no_3','email_id_1','email_id_2','email_id_3'];
	if(!in_array($sortBy, $allowedCols)) $sortBy = 'party_det_id';
	$sortDir = ($sortDir == "ASC") ? "ASC" : "DESC";


	$where = "";

	if ($search != "") {

    $search = mysqli_real_escape_string($connect, $search);

    $where = "WHERE 
        party_code LIKE '%$search%' 
        OR cmp_name LIKE '%$search%'
        OR con_pers_1 LIKE '%$search%'
		OR con_pers_2 LIKE '%$search%'
		OR con_pers_3 LIKE '%$search%'
		OR con_pers_des_1 LIKE '%$search%'
		OR con_pers_des_2 LIKE '%$search%'
		OR con_pers_des_3 LIKE '%$search%'
		OR con_no_1 LIKE '%$search%'
		OR con_no_2 LIKE '%$search%'
		OR con_no_3 LIKE '%$search%'
		OR email_id_1 LIKE '%$search%'
		OR email_id_2 LIKE '%$search%'
		OR email_id_3 LIKE '%$search%'
		";
}


	$totalQuery = mysqli_query($connect, "SELECT COUNT(*) AS cnt FROM party_det $where");
	$total = mysqli_fetch_assoc($totalQuery)['cnt'];
	$pages = ceil($total / $limit);

	$sql = "SELECT * FROM party_det $where 
			ORDER BY $sortBy $sortDir 
			LIMIT $limit OFFSET $offset";

	$result = mysqli_query($connect, $sql);

	$data = "";
	$loop=1;
	if(mysqli_num_rows($result)>0){
	while($row = mysqli_fetch_assoc($result)){
		$party_id	=	base64_encode(base64_encode(base64_encode($row['party_det_id'])));

				$cmp_count = strlen($row['cmp_name']);
				$cmp_name='';
				if($cmp_count>11){
					$cmp_name = substr($row['cmp_name'],0,11)."...";
				} else {
					$cmp_name = $row['cmp_name'];
				}
				$pc_title='';
				$pc_title .= $row['party_code']; 
			if($row['direct_costing']=='1'){
				$sub_sql = mysqli_query($connect,"select * from subscription_type where subscription_id='".$row['subscription_id']."'");

				$res_sub = mysqli_fetch_object($sub_sql);
				$subs_type= substr($res_sub->subscription_name,0,3);

				$pc_title .='   (D/'.$subs_type.')';
			}

		if($row['statu_s'] == 'active'){ $sts_s = 1; }else{ $sts_s = 2; }
					if($loop%2 != "0")
						$bg = "#ffffff";
					else
						$bg = "#f2f2f2";

					$con_pers1='';
				$con_pers_tip1='';
				$con_pers2='';
				$con_pers_tip2='';
				$con_pers3='';
				$con_pers_tip3='';
				$cmp_count1 = strlen($row['con_pers_1']);
				$cmp_count2 = strlen($row['con_pers_2']);
				$cmp_count3 = strlen($row['con_pers_3']);

				if($row['con_pers_1']!=""){
					if(strlen($row['con_pers_1'])>15){
					  $con_pers1 = substr($row['con_pers_1'],0,15)."...";
					} else {
					  $con_pers1 =$row['con_pers_1'];
					}
					$con_pers_tip1 = $row['con_pers_1'];
				}

				if($row['con_pers_2']!=""){
					if(strlen($row['con_pers_2'])>15){
					  $con_pers2 = substr($row['con_pers_2'],0,15)."...";
					} else {
					  $con_pers2 = $row['con_pers_2'];
					}
					$con_pers_tip2 = $row['con_pers_2'];
				}

				if($row['con_pers_3']!=""){
					if(strlen($row['con_pers_3'])>15){
						$con_pers3 = substr($row['con_pers_3'],0,15)."...";
					} else {
						$con_pers3 =$row['con_pers_3'];
					}
					$con_pers_tip3 = $row['con_pers_3'];
				}
			$comp_html ='';
			  if($cmp_count>11){ 
				$comp_html .='<div class="tooltip1">'.$cmp_name.'
				<span class="tooltiptext1">'.$row['cmp_name'].'</span>
			</div>';
			  } else {  
			$comp_html .=$cmp_name;
			 }
				
			$con_html='';	
			 if($cmp_count1>15){
			$con_html.=' <div class="tooltip1">'.$con_pers1.'
				<span class="tooltiptext1">'.$row['con_pers_1'].'</span>
			</div>';
			} else {
			$con_html.=$con_pers1.'<br>';
			  }  
			 if($cmp_count2>15){ 
			$con_html.='<div class="tooltip1">'.$con_pers2.'
				<span class="tooltiptext1">'.$row['con_pers_2'].'</span>
			</div>';
			 } else {  
			$con_html.=$con_pers2.'<br>';
			}  
			if($cmp_count3>15){ 
			$con_html.='<div class="tooltip1">'.$con_pers3.'
				<span class="tooltiptext1">'.$row['con_pers_3'].'</span>
			</div>';
			 } else {  
			 $con_html.=$con_pers3;
			 } 
			 
			 
			 $des_string = '';
			if($row['con_pers_des_1']!=""){
				$des_string .=$row['con_pers_des_1']."<br>";
			}
			if($row['con_pers_des_2']!=""){
				$des_string .= $row['con_pers_des_2']."<br>";
			}
			if($row['con_pers_des_3']!=""){
				$des_string .=$row['con_pers_des_3'];
			}
			$mob_string = '';
			if($row['con_no_1']!=""){
				$mob_string .=$row['con_no_1']."<br>";
			}
			if($row['con_no_2']!=""){
				$mob_string .=$row['con_no_2']."<br>";
			}
			if($row['con_no_3']!=""){
				$mob_string .=$row['con_no_3'];
			}
			
			$mail_string = '';
			if($row['email_id_1']!=""){
				$mail_string .=$row['email_id_1']."<br>";
			}
			if($row['email_id_2']!=""){
				$mail_string .=$row['email_id_2']."<br>";
			}
			if($row['email_id_3']!=""){
				$mail_string .=$row['email_id_3'];
			}
			$status_html='';
			$party_id	=	base64_encode(base64_encode(base64_encode($row['party_det_id'])));
			$page_id= base64_encode(base64_encode(base64_encode(2))); //THIS IS FOR PAGE IDENTIFICATION IN BL FILE for active inactive

			$page_id1= base64_encode(base64_encode(base64_encode(3))); //THIS IS FOR PAGE IDENTIFICATION IN BL FILE for active inactive

		   if($row['statu_s']=="active"){  
		   $status_html .='<a href="bl/party_BL.php?party_id='.$party_id.'&page_id1='.$page_id1.'&statu_s=active">Active</a>';
		    }else{ 
			 $status_html .='<a href="bl/party_BL.php?party_id='.$party_id.'&page_id1='.$page_id1.'&statu_s=inactive">Inactive</a>';
			} 
	   $data .= '<tr style="background-color:'.$bg.'">
		<td style="text-align:center;">'.$loop.'</td>
		<td>'.$pc_title.'</td>
		<td>'.$comp_html.'</td>
		<td>'.$con_html.'</td>
		<td>'.$des_string.'</td>
		<td>'.$mob_string.'</td>
		<td>'.$mail_string.'</td>
		<td><a href="party_update.php?party_id='.$party_id.'" id="updlink'.$loop.'" onfocus="foc(this.id)" onblur="stnon(this.id)">View / Update</a><br>'.$status_html.'
		</td>
	</tr>';
	  $loop ++;
	}
	} else {
		$data .= "<tr><td colspan='7' style='text-align:center;'>There are no records in the category</td></tr>";
	}
	$response = [
		"rows"   => $data,
		"pages"  => $pages,
		"total"  => $total
	];

	echo json_encode($response);
