<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');
################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)==0 or $start=="")?$start=0:$start;
$pagesize = intval($pagesize)==0?$pagesize=100:$pagesize;
if($parentID=="") {
	$parentID=0;
}
$heading="Manage Whatsapp Enquiry";
$record_type="Enquiry";
$script_name="manage_whatsappenquiry.php";
$table_name="whatsapp_enquiry";
#################### Add Record ##################
if($_POST['multiple_delete']){
	// print_r($_POST['enquiry_id']); die;
	$ids = $_POST['enquiry_id'];
	foreach($ids as $id){
		$sql=db_query("DELETE from $table_name where en_id=$id");
	}
}
if($_REQUEST[action]=="add_record" && $catName!="") {
	$sql=db_query("select * from $table_name where locationName='$catName' and parentID='$parentID'");
	$num=NumRows($sql);
	if($num>0) {
		$_SESSION[session_message]=$record_type.' already exist';
	}
	else{
		if($_FILES[image][name]!="") {
			$file_extension=strtolower(getExtension($_FILES[image][name]));
			if($file_extension!="jpg" && $file_extension!="gif" && $file_extension!="jpeg" && $file_extension!="png") {
				$_SESSION[session_message]='Please upload image (.jpg,.gif,.png Format Only)';
				//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize);
				$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
				exit;
			}
			if($_FILES[image][name]!="") {
				$img_name=date("d_m_Y_H_i_s");
				$img=upload_file($img_name,"image","uploaded_files/location_images");
				$width="50";
				$height="50";
				$source_folder=SITE_DIR_PATH."/uploaded_files/location_images/";
				$destination_folder=SITE_DIR_PATH."/uploaded_files/location_images/thumbs/";
				saveResizedImage($img, $width, $height, $source_folder, $destination_folder);
			}
		}
		$maxRes=db_fetch_array(db_query("select max(locationOrder) from $table_name where parentID='$parentID'"));
		$catOrder=$maxRes[0]+1;
		$sql=db_query("insert into $table_name set parentID='$parentID', locationName='$catName', locationtDesc='$catDesc', locationImage='$img', locationOrder='$catOrder'");
		$_SESSION[session_message]=$record_type.' has been added successfully';
		//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize);
		$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
		exit;
	}
}
####################################################
if($_POST[action]=="Top"){
	if(sizeof($_POST[cid])==0) {
		$_SESSION[session_message]='Please select a record first.';
		//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=$start&pagesize=$pagesize");
		$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=$start&pagesize=$pagesize";
        echo "<script>window.location.href='$redirect_url';</script>";
		exit;
	}
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$dataRes=$_POST[cid];
		updateTable($table_name," locationPosition='1' where en_id='$dataRes[$x]'");
	}
	$_SESSION[session_message]='Selected record(s) Set Top successfully.';
	//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
	exit;
}
if($_POST[action]=="Middle"){
	if(sizeof($_POST[cid])==0) {
		$_SESSION[session_message]='Please select a record first.';
		//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=$start&pagesize=$pagesize");
		$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=$start&pagesize=$pagesize";
        echo "<script>window.location.href='$redirect_url';</script>";
		exit;
	}
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$dataRes=$_POST[cid];
		updateTable($table_name," locationPosition='2' where en_id='$dataRes[$x]'");
	}
	$_SESSION[session_message]='Selected record(s) Set Middle successfully.';
	//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
	exit;
}
if($_POST[action]=="Bottom"){
	if(sizeof($_POST[cid])==0) {
		$_SESSION[session_message]='Please select a record first.';
		//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=$start&pagesize=$pagesize");
		$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=$start&pagesize=$pagesize";
        echo "<script>window.location.href='$redirect_url';</script>";
		exit;
	}
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$dataRes=$_POST[cid];
		updateTable($table_name," locationPosition='3' where en_id='$dataRes[$x]'");
	}
	$_SESSION[session_message]='Selected record(s) Set Bottom successfully.';
	//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
	exit;
}
if($action=="Update") {
	while (list($key,$val)=each($_POST)) {
		if (substr($key,0,6)=="order_") {
			$key1 = substr($key,6,strlen($key)-6);
			$qry1=db_query("update $table_name set locationOrder='$val' where en_id='$key1'");
		}
	}
	$_SESSION[session_message]=$record_type.' order has been updated successfully';
	//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize);
	$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
	exit;
}
if($id=="delete_record") {
	/*if(!anyreference($catID)) {*/
		$categoryRes=getResult($table_name," where en_id='$catID'");
		$filename=$categoryRes['locationImage'];
		if($filename!="") {
			$file=SITE_DIR_PATH."/uploaded_files/location_images/".$filename;
			if(file_exists($file)) {
				unlink($file);
			}
			$file_thumb=SITE_DIR_PATH."/uploaded_files/location_images/thumbs/".$filename;
			if(file_exists($file_thumb)) {
				unlink($file_thumb);
			}
		}
		db_query("delete from $table_name where en_id='$catID'");
		$_SESSION[session_message]=$record_type.' has been deleted successfully';
	/*}
	else{
		$_SESSION[session_message]=$record_type.' can not be deleted as any reference of this '.$record_type.' already exist. You need to delete those references first.';
	}*/
	//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
	exit;	 
}
if($id=="deactivate_record") {
	updateTable($table_name," status='N' where en_id='$catID' and status='Y' ");
	$_SESSION[session_message]=$record_type.' has been deactivated successfully.';
	//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize);
	$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
	exit;
}
if($id=="activate_record") {
	updateTable($table_name," status='Y' where en_id='$catID' and status='N' ");
	$_SESSION[session_message]=$record_type.' has been activated successfully.';
	//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize);
	$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
	exit;
}
if($id=="set_top_business_category") {
	updateTable($table_name," topBusinessCategory='Y', status='Y' where en_id='$recordID'");
	$_SESSION[session_message]=$record_type.' has been set as top business category successfully.';
	//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize);
	$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
	exit;
}
if($id=="unset_top_business_category") {
	$homeCnt=getCount($table_name," where topBusinessCategory='Y' and parentID='0'");
	if ($homeCnt < 7) {
		$_SESSION[session_message]=$record_type.' is required hence can not be unset as top business category.';
		//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize);
		$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
		exit;
	}
	updateTable($table_name," topBusinessCategory='N' where en_id='$recordID'");
	$_SESSION[session_message]=$record_type.' has been unset as top business category successfully.';
	//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize);
	$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&en_id=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
	exit;	
}
########################################################
admin_header("Manage $record_type");	
?>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
 <tr>
	<td align="left" width="50%" class="myTitle"><?=$heading;?></td>
	<td align="right">
	 <form name="pagesize_form" action="<?=$PHP_SELF?>" method="post" style="margin:0px;padding:0px;">
	 <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
	  <tr>
	   <td align="right">Records Per Page:
	    <select name="pagesize" onchange="this.form.submit();">
	     <option value="0">Select</option>
	     <?php
	     foreach($_GLOBALS[pgsize_arr] as $val) {
		     if(trim($pagesize)==trim($val)) {
			     echo '<option value="'.$val.'" selected>'.$val.'</option>';
		     }
		     else{
			     echo '<option value="'.$val.'">'.$val.'</option>';
		     }
	     }
	     ?>
	    </select>
	    <input type="hidden" name="catID" value="<?=$en_id;?>">
	    <input type="hidden" name="parentID" value="<?=$parentID;?>">
		 </td>
		</tr>
	 </table>
	 </form>
	</td>
 </tr>
</table>
<hr size="2" width="99%" color="#DA251C">
<?php
##################Product Search Option###################
if ($_POST['srch']) {
	echo "hello"; die;
}
?>
<form name="srch" method="post" action="" style="display:inline" enctype="multipart/form-data">
<table width="100%"  border="0" cellpadding="0" cellspacing="2">
 <tr>
  <td align="right"><b>Search Location </b> [by name etc.] :-
   <input type="text" name="keyword" value="" size="40">
   <input type="submit" class="button" value="GO">   
  </td>
 </tr>   
</table>
</form>
<?php
$cond="";
if (strlen($keyword)) {
	?>
	<br/>
	<div style="width:99%;">
	 <div style="float:left;"><strong>Search For :</strong> <?=$keyword;?></div>
	 <div style="float:right;"><a href="javascript:history.go(-1);"><strong>Go Back</strong></a></div>
	</div> 
	<?php
	$cond=" WHERE (en_country like '%$keyword%' or en_name like '%$keyword%')";
}
$qry1= db_query("select * from $table_name ");
$qry= db_query("select * from $table_name $cond order by en_id desc limit $start, $pagesize");
$reccnt = NumRows($qry1);
// echo $reccnt; die;
echo pageNavigation($reccnt,$pagesize,$start);
print_message();
?>
<table width="100%" border="0" cellpadding="0" cellspacing="2">
 <tr>
  <td>
   <?php
   if($parentID!="" && $parentID!=0) {
   ?>
   <a href="whatsapp_enquiry.php">ManageEnquiry</a> 
	<?php
   }
   ?>
  </td>
 </tr>
</table><br />
<form name="formn" method="post" action="" enctype="multipart/form-data">
<table width="100%"  border="0" cellpadding="2" cellspacing="2" class="lightBorder table table-bordered table-hover">
 <?php if($reccnt>0) { ?>
 <tr bgcolor="#EAEAEA">
<!--  <td width="3%"><input type="checkbox" name="chk" onclick="CheckAll()" class="NoStyle"></td>-->
  <td width="10%" height="20" align="left">
      <label class="container1"><input type="checkbox" name="enquiry_id" value="0" id="select_all_enquiry"><span class="checkmark"></span></label>
  </td>
  <td width="10%" height="20" align="left"><strong>S.No.</strong></td>
  <td width="40%" align="left"><strong>Sender Detail </strong></td>
  <td width="40%" align="center"><strong>Enquiry Details</strong></td>
  <td width="10%" align="center"><strong>Receive Date</strong></td>
  <td width="15%" align="left"><strong>Options</strong></td>
 </tr>
 <?php
 $sl=$start+1;
 while($arr=mysqli_fetch_array($qry)) {
 $arr=ms_htmlentities_decode($arr);
	 if($sl%2!=0) {
		 $color="#f9f9f9";
	 }
	 else{
		 $color="#f0f0f0";
	 }
	 $catgType1=$arr['locationType'];
	  if($catgType1=='0') {
		 $catgType2="Products";
	 }
	 	  if($catgType1=='1') {
		 $catgType2="Services";
	 }
	 	 	  if($catgType1=='2') {
		 $catgType2="Courses";
	 }
?>
	 <tr  bgcolor="<?=$color;?>">
<!--	 <td class="<?=$class?>" valign="top"><input type=checkbox name="cid[]" value="<?=$arr['en_id'];?>" class="NoStyle"></td>-->
	<td align="left"> 
        <label class="container1"><input type="checkbox" name="enquiry_id[]" value="<?=$arr['en_id'];?>"><span class="checkmark"></span></label>
	</td>
	 <td align="left"><?=$sl;?>.</td>
	 <td align="left" class="<?=$class?>">
 Name: <b><?=$arr['en_name'];?></b> <br /> 
 Mobile: <b><?=$arr['en_mobile'];?></b>  <br />
       <?php if($arr['booking_date']!='' || $arr['en_time']!=''){ ?>  
 Booking Date: <b><?=$arr['booking_date'];?>, <?=$arr['en_time'];?></b> 
         <?php } else { ?>
         <?php } ?>
 </td>
	 <td align="left" class="<?=$class?>">
Sender Ip Address: <b><?=$arr['en_ip'];?></b>  <br /> 
URl: <b><?=$arr['en_url'];?></b>  <br /> 
<!--    <a href="mailto:<?=$arr['en_email'];?>"><img src="mail.png" style="width:10%"></a>-->
<a href="tel:<?=$arr['en_mobile'];?>"><img src="call.png" style="width:10%"></a>
	  </td>
	   <td align="left" class="<?=$class?>">
 <b><?=$arr['en_date'];?></b>  
	  </td>
	  <td class="<?=$class?>" align="left">
	   <?php if($arr[status]=='Y' && $parentID!=0) { ?>
	   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=deactivate_record&start=<?=$start;?>&catID=<?=$arr[en_id];?>&parentID=<?=$parentID;?>&pagesize=<?=$pagesize;?>"><img src="<?=SITE_ADMIN_URL?>/images/active.gif" alt="Deactivate This <?=$record_type;?>" title="Deactivate This <?=$record_type;?>" border="0"></a>
	   <?php }
	   if($arr[status]=='N' && $parentID!=0) { ?>
	   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=activate_record&start=<?=$start;?>&catID=<?=$arr[en_id];?>&parentID=<?=$parentID;?>&pagesize=<?=$pagesize;?>"><img src="<?=SITE_ADMIN_URL?>/images/deactive.gif" alt="Activate This <?=$record_type;?>" title="Activate This <?=$record_type;?>" border="0"></a>
	   <?php } ?>
	   <?php /*if(!anyreference($arr[catID]) && $parentID!=0) { */ ?>
	   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=delete_record&start=<?=$start;?>&catID=<?=$arr[en_id];?>&parentID=<?=$parentID;?>&pagesize=<?=$pagesize;?>" onClick="return confirm('Are you sure to delete this <?=$record_type;?>...');"><i class="fa fa-trash"></i></a>
	   <?php /*} */?>
	   <!--&nbsp;<a href="<?=SITE_ADMIN_URL?>/manage_meta_tags_category.php?start=<?=$start;?>&catID=<?=$arr[catID];?>&parentID=<?=$parentID;?>&pagesize=<?=$pagesize;?>"><img src="<?=SITE_ADMIN_URL?>/images/preview.gif" width="16" height="16" alt="Manage Meta Details" title="Manage Meta Details" border="0"></a>-->
	  </td>
	 </tr>
	 <?php
	 $sl++;
 }
 ?>
  <tr>
	 <!-- <tr bgcolor="EAEAEA">
      <td colspan="7">
        <input type="submit" name="action" value="Top"   class="button">
        <input type="submit" name="action" value="Middle"   class="button">
        <input type="submit" name="action" value="Bottom"   class="button">
      </td>
    </tr>-->
 <tr bgcolor="<?=$color;?>">
 	<td colspan="6"><input type="submit" name="multiple_delete" value="Delete" class="button"></td>
 </tr>
 <?php } else{ ?>
 <tr><td><b>No Record Found........</b></td></tr>
 <?php } ?>
</table>
</form><br/>
<?php
if ($id == "add_new" || $id == "edit_record") {
	?>
	<a name="bot"></a>
	<br/><br/> 
	<form name="formn" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this);">
	<?php echo $msg;?>
	<table width="100%"  border="0" cellpadding="0" cellspacing="2" class="lightBorder">
	 <tr>
	  <td class="brdrbtm" width="125"><b>Category Name</b>*</td>
	  <td class="brdrbtm">
	   <input name="catName" class="txtfieldadmin" size="70" value="<?=$locationName?>" id="NOBLANK~Please enter category name~DM~">
	  </td>
	 </tr>
	 <!--
	 <tr>
	  <td class="brdrbtm"><b>Category Description</b></td>
	  <td class="brdrbtm"><textarea name="catDesc" rows="10" cols="70" class="txtfieldadmin" ><?=$catDesc?></textarea></td>
	 </tr>
	 !-->
	 <tr>
	  <td class="brdrbtm" width="150"><b>Category Image</b></td>
	  <td class="brdrbtm" >
	   <input name="image" type="file" class="button">
	   <?php
	   if($res['catImage']!="") {
		   $file1=SITE_DIR_PATH."/uploaded_files/location_images/".$res['locationImage'];
		   if(file_exists($file1)) {
			   $dimens=GetImageSize($file1);
			   if($dimens[0]>130) {
				   $dimens[0]=$dimens[0]+30;
				   $dimens[1]=$dimens[1]+80;
			   }
			   else{
				   $dimens[0]=200;
				   $dimens[1]=200;
			   }
			   ?>
			   <a href="javascript:void(0);" onClick="openwin('<?=SITE_ADMIN_URL;?>/popup.php?img=location_images/<?=$res['locationImage'];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong>
			   <?php
		   }
	   }
	   ?>
	   <br/><strong>Image Size: Please upload square image for better view.</strong>
	  </td>
	 </tr>
	  <td></td>
	  <td class="brdrbtm">
	   <?php
	   if($res[catID]=="") {
		   ?>
		   <input type="submit" name="add_record" class="button" value="Add <?=$record_type;?>"><input type="hidden" name="action" value="add_record">
		   <?php
	   }
	   else{
		   ?>
		   <input type="submit" name="update_record" class="button" value="Update <?=$record_type;?>"><input type="hidden" name="action" value="update_record">
		   <?php
	   }
	   ?>
	   <input type="hidden" name="start" value="<?=$start;?>">
	   <input type="hidden" name="pagesize" value="<?=$pagesize;?>">
	   &nbsp;&nbsp;<input type="button" name="cancel" class="button" value="Cancel" onclick="javascript: window.location.href='<?=SITE_ADMIN_URL;?>/<?=$script_name;?>?parentID=<?=$parentID;?>&catID=<?=$en_id;?>&start=<?=$start;?>&pagesize=<?=$pagesize;?>'">
	  </td>
	 </tr>
	</table>
	</form>
	<?php
}
admin_footer();?>
<script type="text/javascript">
		$(function(){
			console.log('Hello');
			$('#select_all_enquiry').change(function(){
				let checked = $('#select_all_enquiry').is(":checked");
				console.log(checked);
				if(checked){
					$('input:checkbox').attr('checked', 'checked');
				}else{
					$('input:checkbox').attr('checked', '');
				}
			});
		})
	</script>
<?php
exit;
?>