<?php
	session_start(); 
	include("../include/config.php");
	include("../include/simpleimage.php");
	include("../include/functions.php"); 
	$BASE_URL = 'https://www.dev.goquicklly.com';
	//$BASE_URL ='https://www.quicklly.com';
	validate_admin();
	function isValidImage($file)
{
    if (!isset($file['tmp_name']) || !is_uploaded_file($file['tmp_name'])) {
        return false;
    }

    $allowedMime = ['image/jpeg', 'image/png', 'image/webp'];

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime  = finfo_file($finfo, $file['tmp_name']);
    finfo_close($finfo);

    if (!in_array($mime, $allowedMime)) {
        return false;
    }

    if (getimagesize($file['tmp_name']) === false) {
        return false;
    }

    $head = file_get_contents($file['tmp_name'], false, null, 0, 512);
    if (preg_match('/<\?php|<script|<html|<!DOCTYPE/i', $head)) {
        return false;
    }

    return true;
}
	if($_REQUEST['submitForm']=='yes'){
	    
	    $title=mysqli_real_escape_string($GLOBALS['conn'],$_POST['title']);
	    $subtitle=mysqli_real_escape_string($GLOBALS['conn'],$_POST['subtitle']);
	    $botton_name=$_POST['botton_name'];
	    $section=$_POST['section'];
	    $button_color=$_POST['button_color'];
	    $status=$_POST['status'];
		require_once __DIR__ . '/../include/S3Helper.php';
    	$s3 = new S3Helper();
		if (isset($_FILES['fileImg']) && $_FILES['fileImg']['error'] === 0 && isValidImage($_FILES['fileImg'])) {

                $Image= new SimpleImage();
                $filename = $_FILES['fileImg']['name'];
                $ext = pathinfo($filename, PATHINFO_EXTENSION);
                
                $img=time()."-".$filename;

				$path = $s3->upload($_FILES['fileImg']['tmp_name'], 'images/popup/',$img);
				if (!$path) {
					die('Website icon upload failed');
				}

                // Upload to main website FTP
                // $url = SITE_URL;

                // if($url ==MAINSITE_URL || $url==DEVSITE_URL ||
                //     $url==UATSITE_URL){

                //     if($url ==MAINSITE_URL){
                //         $conn_id = ftp_connect("3.133.214.148") or die("Error in FTP connection - Live");
                //         $login_status = ftp_login($conn_id, "mainuser@quicklly.com", "n3At8hv3LZKCiF6") or die("Error in FTP login - Live");
						
				//    }
                //     else {
                //         $conn_id = ftp_connect("92.204.135.155") or die("Error in FTP connection");
                //         ftp_pasv($conn_id, true);
                        
                //         if($url==UATSITE_URL)
                //             $login_status = ftp_login($conn_id, "devgoquicklly", "7ZcS58nmn_MH31") or die("Error in FTP login");
                //         else
                //             $login_status = ftp_login($conn_id, "devpuneet@dev.goquicklly.com", "1}PG%zK}Ymvi") or die("Error in FTP login");
                //     }
                //      ftp_pasv($conn_id, true); 
                //     $destination_file = "images/popup/".$img;
                //     $upload_status = ftp_put($conn_id, $destination_file, $_FILES['fileImg']['tmp_name'], FTP_BINARY);
                        
                //    if (!$upload_status)
                //         echo "Something went wrong, Contact to site administrator.";
                        
                //     ftp_close($conn_id);
                // }
                
                // // Upload locally to admin
                // move_uploaded_file($_FILES['fileImg']['tmp_name'],"../images/popup/".$img);
                
            }
           
	    $obj->query("insert into tbl_popup set title='".$title."',subtitle='".$subtitle."',botton_name='".$botton_name."'
	    ,image='$img',section='".$section."',status='".$status."',button_color='".$button_color."',created_at=now(),updated_at=now()");
            $_SESSION['sess_msg']='Data added sucessfully';
            
            
	 
                      header("location:manage-app-popup.php");
		              exit();	
  }  
  if(isset($_GET['delid']))
  {
	  $obj->query("delete from tbl_popup where pop_id='".$_GET['delid']."'");
	   $_SESSION['sess_msg']='Data deleted sucessfully';
	  header("location:manage-app-popup.php");
		              exit();
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo SITE_TITLE; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="css/admin.css" rel="stylesheet" type="text/css" />
<script>
  
	function checkall(objForm)
    {
	len = objForm.elements.length;
	var i=0;
	for( i=0 ; i<len ; i++){
		if (objForm.elements[i].type=='checkbox') 
		objForm.elements[i].checked=objForm.check_all.checked;
	}
   }
	function del_prompt(frmobj,comb)
		{
		//alert(comb);
			if(comb=='Delete'){
				if(confirm ("Are you sure you want to delete record(s)"))
				{
					frmobj.action = "home-page-del.php";
					frmobj.what.value="Delete";
					frmobj.submit();
					
				}
				else{ 
				return false;
				}
		}
		else if(comb=='Deactivate'){
			frmobj.action = "home-page-del.php";
			frmobj.what.value="Deactivate";
			frmobj.submit();
		}
		else if(comb=='Activate'){
			frmobj.action = "home-page-del.php";
			frmobj.what.value="Activate";
			frmobj.submit();
		}
		
		
	}

</script>

</head>
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<?php include("header.php"); 

?>
<tr>
		<td align="right" class="paddRtLt70" valign="top">
			<table width="99%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						
						<td align="right" valign="top">
							<table width="100%" border="0" cellspacing="0" cellpadding="0">
                               <!------------ Search Section ----------------->
                            <tr>
									<td align="left" valign="middle" class="bodr" style="padding-bottom:20px;">
									<table width="100%" border="0" cellspacing="0" cellpadding="3">
  
  <tr><td height="10"></td></tr>
  <tr>
    <td>
    <td height="100" align="left" valign="top" bgcolor="#f3f4f6" class="bodr">
                            <form name="frm" method="POST" enctype="multipart/form-data" action="">
						<input type="hidden" name="submitForm" value="yes" />
						<input type="hidden" name="display_page" value="2">
						
							<table width="100%" cellpadding="0" cellspacing="0">
									<tr>
										<td align="center" colspan="2" class="paddRt14 paddBot11">
										<font color="#FF0000"><strong><?php echo $_SESSION['sess_msg']; $_SESSION['sess_msg']='';?></strong></font></td>
									</tr>
										<tr >
										<td width="18%" align="right" class="paddBot11 paddRt14"> Title</td>
										<td width="82%" align="left" class="paddBot11">
									<input type="text" name="title" required>
										</td> 
									</tr>
										<tr >
										<td width="18%" align="right" class="paddBot11 paddRt14"> Sub Title</td>
										<td width="82%" align="left" class="paddBot11">
									<input type="text" name="subtitle" required>
										</td> 
									</tr>
									<tr >
										<td width="18%" align="right" class="paddBot11 paddRt14"> Button Text</td>
										<td width="82%" align="left" class="paddBot11">
									<input type="text" name="botton_name" required>
										</td> 
									</tr>
										
									
									
										
									
										<tr >
										<td width="18%" align="right" class="paddBot11 paddRt14"> Section</td>
										<td width="82%" align="left" class="paddBot11">
									<select name="section" id="sections_id" style="width: 14%;" required onchange="getStores(this.value);">
                                      <option value="">Select Section</option>
                                     
                                     <?php
                                     $sql2=$obj->query("select * from tbl_section where status=1");
                                     while($line=$obj->fetchNextObject($sql))
																{
                                     ?>
                                     
                                     <option value="<?=$line->section_slug?>" ><?=$line->section_name?></option>
                                     <?php
																}
                                     ?>
                                      </select>
										</td> 
									</tr>
									
									
									
									
										<tr >
										<td width="18%" align="right" class="paddBot11 paddRt14"> Photo</td>
										<td width="82%" align="left" class="paddBot11">
									<input type="file" name="fileImg" accept="image/png" required>
									
								
										</td> 
									</tr>
									<?php
									if(@$cardrow->photo!='')
									{
									?>
									<tr>
									    <td></td>
									    <td>	<img src="<?= SITE_URL_CDN ?>/<?= @$cardrow->photo ?>" style="width:100px; height:100px;float:left"></td>
									</tr>
									<?php
									}
									?>
									
									<tr >
										<td width="18%" align="right" class="paddBot11 paddRt14"> Button Color</td>
										<td width="82%" align="left" class="paddBot11">
									<input type="color" name="button_color"  required>
										</td> 
									</tr>
									
								<tr >
										<td width="18%" align="right" class="paddBot11 paddRt14"> Status</td>
										<td width="82%" align="left" class="paddBot11">
										<select name="status" required>
                                      <option value="">Select Status</option>
                                      <option value="1" >Active</option>
                                      <option value="0" >Deactive</option>
                                      </select>
										</td> 
									</tr>
										
							  
								
									
									
						
									<tr>
										<td width="18%" align="right" class="paddRt14 paddBot11">&nbsp;</td>
										<td width="82%" align="left" class="paddBot11">
											<input type="submit" name="submit" value="Submit"  class="submit" border="0"/> 	                  		 &nbsp;&nbsp;
											<input name="Reset" type="reset" id="Reset" value="Reset" class="submit" border="0" /></td>
									</tr>
								</table></form>
							</td>
</td>
  </tr>
</table>

									</td>
								</tr>
                                <tr><td height="10"></td></tr>
                                
                                <!----- Search End --------->
								<tr>
									<td align="left" valign="middle" class="headingbg bodr text14">
									<em><img src="images/arrow2.gif" width="21" height="21" hspace="10" align="absmiddle" /></em>
									<span  style="float:right; padding-right:10px;">
					                </span>
									</td>
								</tr>
								<form name="frm" method="post" action="#" enctype="multipart/form-data">
									<tr>
										<td height="100" align="left" valign="top" bgcolor="#FFFFFF" class="bodr">
											<table width="100%" cellpadding="0" cellspacing="0">
											<?php if($_SESSION['sess_msg']){ ?>
											<tr><td  align="center"><font color="#FF0000"><strong><?php echo $_SESSION['sess_msg'];$_SESSION['sess_msg']='';?></strong></font></td></tr>
											
											<?php }?>
												<tr>
														<td align="left">
															<?php 

$sql2=$obj->query("select * from tbl_popup");
$reccnt=$obj->numRows($sql2);

															if($reccnt==0)
															{
															?>
																<table width="100%" border="0" cellspacing="0" cellpadding="0">
																	<tr>
																		<td align="center" valign="middle"><font face="Arial, Helvetica, sans-serif"   color="#FF0000" size="+1">No Record</font></td>
																	</tr>
																</table>
															<?php 
															}
															else
															{
															?>
															<table width="100%" border="0" cellspacing="0" cellpadding="0">
                                                            <tr><td align="center" colspan="15"><strong><?php echo $reccnt; ?> Record Found.</strong></td></tr>
																<tr>
																	<td width="2%" align="left" class="padd5" bgcolor="#f3f4f6"><strong>S No.</strong></td>
																	
																		<td width="2%" align="left" class="padd5" bgcolor="#f3f4f6"><strong>Title</strong></td>
																	
                                               <td width="5%" align="center" bgcolor="#f3f4f6" class="padd5"><strong>Sub Title</strong></td>
                                                <td width="10%" align="center" bgcolor="#f3f4f6" class="padd5"><strong>Button Text</strong></td>
												<td width="10%" align="center" bgcolor="#f3f4f6" class="padd5"><strong>Section</strong></td>
												<td width="8%" align="left" bgcolor="#f3f4f6" class="padd5"><strong>Photo</strong></td>
                                               	<td width="9%" align="center" bgcolor="#f3f4f6" class="padd5"><strong>Status</strong></td>
												<td width="4%" align="center" class="padd5" bgcolor="#f3f4f6"><strong>Action</strong></td>
																  
																</tr>
																<?php
																$i=0;
																while($line=$obj->fetchNextObject($sql2))
																{
																$i++;
															
																
																if($i%2==0)
																{
																$bgcolor = "#f3f4f6";
																}
																else
																{
																$bgcolor = "";
																}
																?>
																	<tr bgcolor="<?php echo $bgcolor;?>">
																		<td class="padd5"><strong><?php echo $i; ?>.</strong></td>
																		
                                                                     
									
                                        <td class="padd5"><?php echo $line->title; ?></td>
                                         <td class="padd5"><?php echo $line->subtitle; ?></td>
										  <td class="padd5"><?php echo $line->botton_name; ?></td>
										  <td class="padd5"><?php echo $line->section; ?></td>
                                          <td class="padd5">
                                              <?php
									if($line->image!='')
									{
									?>
                                              <img src="<?= SITE_URL_CDN ?>/images/popup/<?= $line->image ?>" style="width:100px; height:100px;">
                                   <?php
									}
                                   ?>              
                                              </td>
                                          
                                         <td class="padd5"><?php if($line->status==1){ echo "<span style='color:green;'>Active</span>"; }else{ echo "<span style='color:red;'>Deactive</span>"; } ?></td>	
<td class="padd5"><a href="?delid=<?=$line->pop_id?>" style="color: red;" onclick="return confirm('Are you sure?')">Delete</a></td>										 
																	</tr>
																<?php
																
																}
																
																?>
															
	
	

															</table>
															<?php }?>
														</td>
												</tr>
												
											</table>
										</td>
									</tr>
								</form>
							</table>
						</td>
					</tr>
			</table>
		</td>
</tr>
<tr><td height="100"></td></tr>
<?php include('footer.php'); ?>
<script src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
    function get_type(a)
    {
       
        if(a=='predefined'){
            $("#type").show();
            $("#sections").prop('required',true);
            $("#section_id").hide();
            $("#store_id").hide();
        }else
        {
            $("#sections").prop('required',false);
            $("#type").hide();
            $("#section_id").show();
            $("#store_id").show();
            
        }
        
    }
     function getStores(section_id)
     {
       
      $.ajax({
        url: 'ajax-get-store.php',
        type: 'post',
        data: { "sectionid": section_id},
        success: function(response) { 
            
            $("#stores_id").html(response);
        }
    });
     }
</script>

</table>
</body>
</html>