<?php
$message = (isset($_SESSION['message'])) ? $_SESSION['message'] :"";
$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : "insert";

$collapse_area = "collapse";
$add_btn_hide = "";
$cancel_btn_hide = "";
$listing = "block";

$where 	= " WHERE 1=1";


$query_string = "";

$button_value 	= "Add";
$table_name		= "mail";

$next_page_condition 	= "";

if($_REQUEST['hide_top']){
	$next_page_condition.="&hide_top=1";
}
if($_REQUEST['hide_left']){
	$next_page_condition.="&hide_left=1";
}
if($_REQUEST['campaign_id']){
	$campaign_id = $_REQUEST['campaign_id'];
	$where 	.= " AND campaign_id = '$campaign_id' ";
	$next_page_condition.="&campaign_id=".$campaign_id;
}

if(isset($_REQUEST['source'])){
	$source = $_REQUEST['source'];
	$next_page_condition.="&source=$source";
}

$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
	//$next_page_condition.="&start=$start";
}
$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
################################ START EDIT ########################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){
	$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	
	$collapse_area = "";
	$add_btn_hide = "hide";
	$cancel_btn_hide = "hide";
	$listing = "none";
	
	$action 	    = "update";
	$button_value 	= "Update";
	$page_name		= "Edit Mail";	
		
    $id = $_REQUEST['id'];
    $sql= "SELECT * FROM $table_name WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);
	$row_edit = utility::validate_crypt_data($row_edit);
    $id                 = $row_edit['id'];
	$campaign_id	    = $row_edit['campaign_id'];
	$first_name			= $row_edit['first_name'];
	$last_name		    = $row_edit['last_name'];
	$email		        = $row_edit['email'];
	$unsubscribe	    = $row_edit['unsubscribe'];
	$unsubscribe_date	= $row_edit['unsubscribe_date'];
}
######################################### END EDIT ################################# 


############################ START update ############################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string"));
	$id					= $_REQUEST['id'];
	$campaign_id 		= $_REQUEST['campaign_id'];
    $first_name		    = $_REQUEST['first_name'];
	$last_name		    = $_REQUEST['last_name'];
	$unsubscribe		= $_REQUEST['unsubscribe'];
	$email				=  utility::app_crypt($_REQUEST['email'], 'e');
	$unsubscribe_date	= utility::setFormatedDate($_REQUEST['unsubscribe_date']);
    $updated_by			= $_SESSION['user_id'];
	
	$sql = "campaign_id = '".$campaign_id."', first_name = '".$first_name."', last_name = '".$last_name."',email = '".$email."',unsubscribe = '".$unsubscribe."',unsubscribe_date = '".$unsubscribe_date."',updated_by = '".$updated_by."'";
	$sql= "UPDATE $table_name SET ".$sql." WHERE id = '".$id."'";
    mysqli_query($conn,$sql);
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace(array("page=add","action=edit"), array("page=list",""), $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	
	$_SESSION['message']	= ucfirst("Mail updated sucessfully");
	$redirect = "index?page=view_mail$query_string";
	header("Location: $redirect");
	exit();
	
	
}
################################################# END update ################################################# 
//$custome_limit = 2;
###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Mail";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################

	
    
########################################### START INSERT#####################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	    
	utility::addslashesOnAllInput(array("query_string"));
	$campaign_id 	    = $_REQUEST['campaign_id'];
	$first_name     	= $_REQUEST['first_name'];
	$last_name          = $_REQUEST['last_name'];
    $email				= utility::app_crypt($_REQUEST['email'], 'e');
	$unsubscribe_date	= utility::setFormatedDate($_REQUEST['unsubscribe_date']);
	$source	 			= $_REQUEST['source'];
	$unsubscribe	    = $_REQUEST['unsubscribe'];
	$created_on 		= date("Y-m-d H:i:s");
  	$created_by			= $_SESSION['user_id'];
  	$updated_by			= $_SESSION['user_id'];
	
	$sql = "campaign_id = '".$campaign_id."',first_name = '".$first_name."', last_name = '".$last_name."',email = '".$email."',unsubscribe = '".$unsubscribe."',unsubscribe_date = '".$unsubscribe_date."', source = '".$source."',created_on = '".$created_on."', created_by = '".$created_by."', updated_by = '".$updated_by."'";
	$sql= "INSERT INTO $table_name SET ".$sql;
    mysqli_query($conn,$sql);
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace("page=add", "page=view_mail", $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	
	$_SESSION['message']	= ucfirst("Mail added sucessfully");
	$redirect = "index?page=view_mail&name=$name$query_string";
	header("Location: $redirect");
	exit();
}
################################################# END INSERT #################################################
$query = "SELECT * FROM $table_name $where ORDER BY id DESC LIMIT $eu, $limit ";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

#################################   DELETE    #########################################
// when we click on delete this area of php notes will execute
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete'){
	$id	= $_REQUEST['id'];
	mysqli_query($conn,"DELETE FROM $table_name WHERE id = $id ");
	
	$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	$query_string = str_ireplace(array("action=delete"), array(""), $query_string);
	$query_string = "&".$query_string;
	
	$_SESSION['message']	= ucfirst("Mail deleted sucessfully");
	$redirect = "index?page=view_mail";
	header("Location: $redirect$query_string");
	exit();
}
#################################  END DELETE    #########################################
?>
<style type="text/css">
.content-wrapper{
	margin-left: 0 !important;
}
</style>
<section class="content-header hide">
  <h1>
	<? echo str_replace(array("_"),array(" "),$page_header); ?>
  </h1>		
</section>
<!----------------------- Message ------------------------------->
	<? if(isset($_SESSION['message'])){ ?>
	<section class="content-header">
	  <div class="alert alert-info alert-dismissable" style="margin-bottom:0px;">
		<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
		<? echo $_SESSION['message'];
			unset($_SESSION['message']);
		?>
	  </div>
	</section>
	<? } ?>
<!----------------------- End Message ------------------------------->
<section class="content">
  <div class="container-fluid">
	<div class="row">
		 <div class="col-lg-12">
			<div class="card">
			<div class="card-body">
			<div class="" style="">
				<?php include_once('add_mail.inc.php');?>
			</div>
			<!--<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">-->					
			<form action="?page=view_mail&name=<?=$name?>" method="post" name="form1" id="form1" class="hide">
			<div class="table-responsive" id="listing" style="display:<?=$listing;?>;">
			<? if($result>0){
			?>
				<!-- Start  records header -->
			
			<table id="example1" class="table table-bordered table-striped">
				<thead>
					<tr>
						<th>First Name</th>
						<th>Last Name</th>
						<th>Email</th>
						<th>Unsubscribe</th>
						<th>Unsubscribe Date</th>
						<th>Edit</th>
						<th>Delete</th>
						
					</tr>
				</thead>
				<tbody>
				<!-- End records header -->
				<?
				while($row = mysqli_fetch_array($sql)){
						$row=utility::validate_crypt_data($row);
				?>
				<!-- Start data -->
				<tr>
					<td><?php echo  $row['first_name']; ?></td>
					<td><?php echo  $row['last_name']; ?></td>
					<td><?php echo  utility::app_crypt($row['email'],'d'); ?></td>
					<td><?php echo ($row["unsubscribe"] ==1) ? 'Yes':'No';?></td>
					<td><span class="hide"><?php echo date("YYYY/mm/dd",strtotime($row['unsubscribe_date']));?></span><?php echo utility::getFormatedDate($row['unsubscribe_date']); ?></td>
					<td><a href="index?page=view_mail&action=edit&id=<?=$row['id'].$next_page_condition.$paging;?>"><img src="../images/edit.gif" border="0" /> </a></td>
					<td>
						<a href="index?page=view_mail&action=delete&id=<?=$row['id'].$next_page_condition.$paging; ?>" onclick=" javascript:return confirm('Are you sure! You want to delete');"><img src="../images/del.gif" border="0" /> </a>
					</td>
				</tr>
				<!-- End data -->
				<? 
				}?>
				
				<?}else{?>
				<tr>
					<td colspan="6">No mail found...</td>
				</tr>
				<?php }?>
				</tbody>
				
			</table>
			</div>
				<!-- Paging starts here  -->
				<? 
				$next_page_condition .="&page=view_mail";
				include("paging/paging_row.inc.php") ?>
				<!-- Paging ends starts here  -->
					
				<!--</table>-->
				</form>
				</div>
				</div>
			</div>
		</div>
	</div>
</section>
		