<?php
session_start();
$where 					= " WHERE 1=1 ";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
} 
$campaign_id = utility::app_crypt($_REQUEST['campaign_id'],'d');
if($_REQUEST['id'] != $campaign_id){
	die("Invaild URL");
}
####################################### ARCHIVING/UNARCHIVING SAMEER #######################################
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'archive') {
	foreach ($_REQUEST["campaign_ids"] as $cam_id) {
		mysqli_query($conn, "UPDATE campaign_management SET is_archive = '1' WHERE id = '$cam_id' ");
	}
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
	$query_string = str_ireplace("action=archive", "", $query_string);
	$query_string = "&" . $query_string;

	$_SESSION['info_message'] = "Campaign(s) archived sucessfully";
	header("Location: index?page=view$query_string");
	exit();
}
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'unarchive') {
	foreach ($_REQUEST["campaign_ids"] as $cam_id) {
		mysqli_query($conn, "UPDATE campaign_management SET is_archive = '0' WHERE id = '$cam_id' ");
	}
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
	$query_string = str_ireplace("action=unarchive", "", $query_string);
	$query_string = "&" . $query_string;

	$_SESSION['info_message'] = "Campaign(s) unarchived sucessfully";
	header("Location: index?page=view$query_string");
	exit();
}
################################################## END ARCHIVING/UNARCHIVING ORGANISATION ###################################
###########################################  STRAT FILTER CONDITIONS    #####################################
$campaign_name 	= isset($_REQUEST['campaign_name']) ? $_REQUEST['campaign_name'] : "";
if ($campaign_name) {
	$where .= " AND campaign_name LIKE '%$campaign_name%' ";
	$next_page_condition .= "&campaign_name=$campaign_name";
}


$for_end_date 	= isset($_REQUEST['for_end_date']) ? $_REQUEST['for_end_date'] : "";
$from_date = isset($_REQUEST['from_date']) ? utility::setFormatedDate($_REQUEST['from_date']) : "";
$to_date = isset($_REQUEST['to_date']) ? utility::setFormatedDate($_REQUEST['to_date']) : "";
if($for_end_date == 1){
	if($from_date && $to_date){
		$where .= " AND end_date BETWEEN '$from_date 00:00:00' and '$to_date 23:59:59' ";
		$next_page_condition.="&from_date=".$_REQUEST['from_date']."&to_date=".$_REQUEST['to_date']."";
	}else if($from_date){
		$where.= " AND end_date >= '$from_date' ";
		$next_page_condition.="&from_date=".$_REQUEST['from_date'];
	}else if($to_date){
		$where.= " AND end_date <= '$to_date' ";
		$next_page_condition.="&to_date=".$_REQUEST['to_date'];
	}
	$next_page_condition.="&for_end_date=".$for_end_date;
}else{
	if($from_date && $to_date){
		$where .= " AND start_date BETWEEN '$from_date 00:00:00' and '$to_date 23:59:59' ";
		$next_page_condition.="&from_date=".$_REQUEST['from_date']."&to_date=".$_REQUEST['to_date']."";
	}else if($from_date){
		$where.= " AND start_date >= '$from_date' ";
		$next_page_condition.="&from_date=".$_REQUEST['from_date'];
	}else if($to_date){
		$where.= " AND start_date <= '$to_date' ";
		$next_page_condition.="&to_date=".$_REQUEST['to_date'];
	}
}


$is_archive = isset($_REQUEST['is_archive']) ? $_REQUEST['is_archive'] : "";
if ($is_archive) {
	$where .= " AND is_archive = '$is_archive' ";
	$next_page_condition .= "&is_archive=$is_archive";
} else {
	$where .= " AND is_archive = '0' ";
}

###########################################  	END  FILTER CONITIONS 	#####################################
###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "campaign_management";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Campaign";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ORDER BY id DESC LIMIT $eu, $limit";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

$total_no_of_records = mysqli_num_rows(mysqli_query($conn,$join_query_for_paging));
#################################   DELETE    #########################################
// when we click on delete this area of php code will execute
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete')
{
	mysqli_query($conn,"DELETE FROM $table_name WHERE id = '".$_REQUEST['id']."' ");
	
	$_SESSION['info_message'] = "Campaign deleted sucessfully";
	header("location: $file_name?page=view".$next_page_condition.$paging);
	exit();
}
#################################  END DELETE    #########################################
?>

<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0"><?=$page_header;?></h1>
            </div>
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</div>
<section class="content">
	<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12">
				<div class="card">
					<!----------------------- Message ------------------------------->
					<? if(isset($_SESSION['info_message'])){ ?>
					  <div class="alert alert-info alert-dismissable">
						<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
						<? echo $_SESSION['info_message']; unset($_SESSION['info_message']);?>
					  </div>
					  <? } ?>
					<!----------------------- End Message ------------------------------->
					<div class="card">
						<div class="card-body">
						<!-- search form -->
						<?php include_once('campaign_search_options.inc.php');?>
						<!-- /.search form -->
						Total No. of Campaign: <?=$total_no_of_records;?>
						
							<form action="index?page=add" method="post" name="form1" id="form1">
							<div class="card-footer">
							   <button type="submit" name="add" class="btn btn-primary">Add Campaign</button>
							   <? if(!$is_archive){ ?>
								<button type="button" name="archive_campaign" id="archive_campaign" class="btn btn-warning pull-right" style="float:right;">Archive</button>
								<? }else{ ?>
								<button type="button" name="unarchive_campaign" id="unarchive_campaign" class="btn btn-warning pull-right" style="float:right;">Unarchive</button>
								<? } ?>
							</div>
							<? if($result>0){?>
							<div class="table-responsive">
							 <table id="example1" class="table table-bordered table-striped">
								<thead>
								  <tr>
									<!--<th>#</th>-->
									<th>Campaign Name</th>
									<th>Start Date</th>
									<th>End Date</th>
									<th>Mail Subject</th>
									<th>Mail Body</th>
									<th>Edit</th>
									<th>Delete</th>
									<th class="no-sort" style="text-align:center;">
										<input type="checkbox" name="check_all" id="check_all">
									</th>
								  </tr>
								</thead>
								<tbody>
									<?
										while($row =mysqli_fetch_array($sql)){ 
										$row=utility::validate_crypt_data($row);
									?>
								  <tr>
								    <td nowrap>
									<a href="index?page=view_campaign_management_details&from=view_campaign&campaign_id=<?=urlencode(utility::app_crypt($row['id'],'e'));?>&id=<?=$row['id'].$next_page_condition.$paging;;?>">
									<?php echo ucfirst($row["campaign_name"]); ?>
									</td>
									<td><span class="hide"><?php echo date("YYYY/mm/dd",strtotime($row['start_date']));?></span><?php echo utility::getFormatedDate($row['start_date']); ?></td>
									<td><span class="hide"><?php echo date("YYYY/mm/dd",strtotime($row['end_date']));?></span><?php echo utility::getFormatedDate($row['end_date']); ?></td>
									<td><?php echo ucfirst($row["mail_subject"]);?></td>
									<td><?php echo '<span title="'.ucfirst($row['mail_body']).'">'.utility::ellipsis(ucfirst($row['mail_body']),100).'</span>'; ?></td>
									<td><a href="index?page=add&action=edit&campaign_id=<?=urlencode(utility::app_crypt($row['id'],'e'));?>&id=<?=$row['id'].$next_page_condition.$paging;?>"> <i class="fa fa-fw fa-edit"></i> </a></td>
									<td><a href="index?page=view&action=delete&campaign_id=<?=urlencode(utility::app_crypt($row['id'],'e'));?>&id=<?=$row['id'].$next_page_condition.$paging; ?>"onclick=" javascript:return confirm('Are you sure! You want to delete');"><i class="fa fa-fw fa-trash"></i> </a></td>
									<td align="center">
										<input class="checkbox" type="checkbox" name="campaign_ids[<?=$row['id']?>]" value="<?=$row['id']?>" id="primary">
							        </td>
									
								  </tr>
									<? } ?>
									
								</tbody>
							 </table>
							 </div>
							 <? }else{ ?>
								No campaign found...
							 <? } ?>
							<div class="box-footer clearfix">
								<!-- Paging starts here  -->
								<? 
								$next_page_condition .="&page=view";
								include("paging/paging_row.inc.php") 
								?>
								<!-- Paging ends starts here  -->
							</div>
						</div>
						<div class="card-footer">
						   <button type="submit" name="add" class="btn btn-primary">Add Campaign</button>
						</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>

<script type="text/javascript">
$(function () {
	$(".close_button").click(function(){
		$(".modal_iframe").attr("src","");
	});
	///////////////////////////// START ARCHIVING/UNARCHIVING Campaign///////////////////////////////////////
	$("#archive_campaign").click(function(){
		if ($(".checkbox:checked").length == 0){
			alert("Please select Campaign");
			return false;
		}
		if(confirm("Are you sure you want archive the selected Campaign(s).")){
			$('#form1').attr("action","index?page=view&action=archive<?=$next_page_condition.$paging;?>");
			$('#form1').submit();
		}
	});
	$("#unarchive_campaign").click(function(){
		if ($(".checkbox:checked").length == 0){
			alert("Please select Campaign");
			return false;
		}
		if(confirm("Are you sure you want unarchive the selected Campaign(s).")){
			$('#form1').attr("action","index?page=view&action=unarchive<?=$next_page_condition.$paging;?>");
			$('#form1').submit();
		}
	});
	///////////////////////////// START ARCHIVING/UNARCHIVING Campaign///////////////////////////////////////
	$('#check_all').click(function(){
        if($("#check_all:checked").length == 1){
            $('.checkbox').prop( "checked", true );
        }else{
            $('.checkbox').prop( "checked", false );
        }
    });
});
</script>