<?php
require_once("config.php");

//require_once("config.php");


$mycon = databaseConnect();

//check if deleting
if(isset($_GET['delete']) && $_GET['delete'] != "")
{
	$myrec = $mycon->prepare("DELETE FROM `pcc_report` WHERE `pcc_id` = :pcc_id");
	$myrec->bindValue(":pcc_id",$_GET['delete']);
	$myrec->execute();
}

//get all pcc
$myrec = $mycon->prepare("SELECT DISTINCT(pccs_area) FROM `pcc_report` ORDER BY `pccs_area`");
$myrec->execute();
$myrecdepartments = $myrec->fetchAll(PDO::FETCH_ASSOC);


//print_r($myrecdepartments);


?>

<!DOCTYPE HTML>
<!-- Website Template by freewebsitetemplates.com -->
<html>
<head>
	<meta charset="UTF-8">
	<title><?php echo pageTitle() ?></title>
	<link rel="stylesheet" href="css/style.css" type="text/css">
	<!--[if lt IE 9]>
		<link rel="stylesheet" type="text/css" href="css/ie.css">
	<![endif]-->
	<link rel="stylesheet" href="jquery/themes/base/jquery.ui.all.css">
	<script src="jquery/ui/jquery.ui.core.js"></script>
	<script src="jquery/ui/jquery.ui.widget.js"></script>
	<script src="jquery/ui/jquery.ui.datepicker.js"></script>
	<link rel="stylesheet" href="jquery/demos.css">
	<script>
	$(function() {
		$( "#from_searchdate" ).datepicker({
			changeMonth: true,
			changeYear: true,
			dateFormat: 'yy-mm-dd',
			duration: 'fast'
			//showTime: true
			//minDate: new Date(2011, 5, 10)
			//maxDate: '+1y +1m +1w +1d'
		});
	});
	</script>
	<script>
		$(function() {
			$( "#to_searchdate" ).datepicker({
				changeMonth: true,
				changeYear: true,
				dateFormat: 'yy-mm-dd',
				duration: 'fast'
				//showTime: true
				//minDate: new Date(2011, 5, 10)
				//maxDate: '+1y +1m +1w +1d'
			});
		});
	</script>

</head>
<body>
	<?php require_once("inc_top.php") ?>
	<div id="body">
<div class="content">
			<div class="section">
				<div class="breadcrumb">
					<span>You are here:</span>
					<ul>
						<li>
							<a href="dashboard.php">home</a>
						</li>
						<li>
							<a href="#">Pastoral Care Centers (PCC)</a>
						</li>
					</ul>
				</div>
				<div class="company">
					<h2>Pastoral Care Centers Reports</h2>
                  <div style="clear:both; height:10px"></div>
					<table width="100%" border="0">
					  <tr>









					    <td align="right"><form id="form1" name="form1" method="post" action="">




Search By Date From:

   <span class="input-group">
                             <input name="from_searchdate" type="text" id="from_searchdate" readonly>
                        </span>&nbsp;To: <span class="input-group">
                             <input name="to_searchdate" type="text" id="to_searchdate" readonly>
                        </span>

&nbsp;
&nbsp;
<input type="submit" name="searchdate" id="searchdate" value="GO!" />&nbsp;&nbsp; &nbsp;





Search By Week:
<select name="week" id="week">
      <option value="">Select Week</option>
    <option value="Week One">Week One</option>
     <option value="Week Two">Week Two</option>
      <option value="Week Three">Week Three</option>
       <option value="Week Four">Week Four</option>
        <option value="Week Five">Week Five</option>

</select>
&nbsp;
&nbsp;
<input type="submit" name="searchweek" id="searchweek" value="GO!" />&nbsp;&nbsp; &nbsp;





Select PCC:
<select name="department" id="department">
    <option value="">All PCCs</option>
<?php
foreach($myrecdepartments as $row)
{
?>
  <option value="<?php echo $row['pccs_area'] ?>" <?php if(isset($_POST['department']) && $_POST['department'] == $row['pccs_area']) echo "selected=\"selected\"" ?>><?php echo $row['pccs_area'] ?></option>
<?php
}
?>
</select>
&nbsp;
&nbsp;&nbsp; &nbsp;
<input type="submit" name="search" id="search" value="GO!" />
  <a href="?nothing">View all</a>
  <input name="command" type="hidden" id="command" value="searchform" />
				        </form></td>
				      </tr>
					  <tr>
					    <td><table width="100%" border="0">
					      <tr>
					       <td width="2%" height="29" bgcolor="#879D91"><strong>S/N</strong></td>
					        <td width="9%" bgcolor="#879D91"><strong>PCC Name</strong></td>
					          <td width="10%" bgcolor="#879D91"><strong>Fellowship Date</strong></td>
					          <td width="10%" bgcolor="#879D91"><strong>Fellowship Week</strong></td>
					         <td width="7%" bgcolor="#879D91"><strong>Total Attendees</strong></td>
					        <td width="5%" bgcolor="#879D91"><strong>Visited By</strong></td>
							<td width="10%" bgcolor="#879D91"><strong>Offering</strong></td>
							<td width="5%" bgcolor="#879D91"><strong>Tithe</strong></td>
							<td width="5%" bgcolor="#879D91"><strong>Expenses</strong></td>
							<td width="9%" bgcolor="#879D91"><strong>Treasury Balance</strong></td>
							<td width="15%" bgcolor="#879D91"><strong>Date Submitted</strong></td>
							<td width="15%" bgcolor="#879D91"><strong>Report Status</strong></td>

				          </tr>
<?php

$totaloffering = 0;
$totaltithe = 0;
$totalexpenses = 0;
$totaltreasury = 0;


	$sql = "SELECT * FROM `pcc_report` WHERE `pcc_id` > -1 ";

	$param = array();

	if(isset($_POST['search']))
	{
		$searchtext = $_POST['search'];
		$department = $_POST['department'];


		if(strlen(trim($department)) > 0)
		{
			$sql .= " AND (`pccs_area` = :pccs_area)";
			$param[":pccs_area"] = $department;
		}
	}



	if(isset($_POST['searchdate']))
	{
		$searchtext = $_POST['search'];
		//$department = $_POST['department'];
		$start =$_POST['from_searchdate'];
		$end = $_POST['to_searchdate'];


		if(strlen(trim($start)) > 0)
		{
			$sql .= " AND (`fellowship_date` >= :start and fellowship_date <= :end)";
			$param[":start"] = $start;
			$param[":end"] = $end;
		}
	}


	if(isset($_POST['searchweek']))
	{
		$searchtext = $_POST['search'];
		//$department = $_POST['department'];
		$week = $_POST['week'];

		if(strlen(trim($week)) > 0)
		{
			$sql .= " AND (`fellowship_week` = :week)";
			$param[":week"] = $week;
		}
	}











	$myrec = $mycon->prepare($sql);
	$myrec->execute($param);

	$pos = 0;
	while($row = $myrec->fetch(PDO::FETCH_ASSOC))
	{
		 $fd = $row['fellowship_date'];
					$ds = date("Y-m-d", $row['date_submitted']);

					//echo $ds;

					$date1 = date_create(date("d-m-Y", strtotime($fd)));
				$date2 = date_create(date("d-m-Y", strtotime($ds)));

					$interval=date_diff($date1,$date2);
		            $diff = $interval->format('%R%a');


							$time_submitted = date('H', $row['date_submitted']);
							//echo $time_submitted."<br>";

							if($diff <= 1)
							{

							$flag = "On time"; //green flag

							}
							else
							{
								$flag = "Late"; // red flag
		            }


?>
					      <tr bgcolor="<?php echo changeRowColor(++$pos) ?>" class="tableRowHover">
					       <td height="29"><?php echo $pos ?></td>
					           <td><?php echo $row['pccs_area'] ?></td>
					            <td><?php echo date('d-m-Y', strtotime($row['fellowship_date'])) ?></td>
                                <td><?php echo $row['fellowship_week'] ?></td>


					        <td><?php echo $row['total_attendance'] ?></td>
					        <td><?php echo $row['visitors_names'] ?></td>

					            <td>N<?php echo  number_format($row['offering'],2);?></td>

					              <td>N<?php echo  number_format($row['tithe'],2);?></td>
					              <td>N<?php echo  number_format($row['expenses'],2);?></td>
					           		<td>N<?php echo  number_format($row['treasury'],2);?></td>


					                    <td><?php echo date('d-m-Y H:i:s',$row['date_submitted']);?></td>
					                     <td><?php echo $diff; //$row['report_status'] ?></td>
				          </tr>
<?php

$totaloffering = $totaloffering + $row['offering'];
$totaltithe = $totaltithe + $row['tithe'];
$totalexpenses = $totalexpenses + $row['expenses'];
$totaltreasury = $row['treasury'];
	}
?>
<tr>
					       <td height="40"></td>
					           <td></td>



					        <td></td>
					        <td></td>
					        <td></td>
					        <td></td>

					        <td style="background-color:#3B4F36; color:#fff; font-weight:bold;">N<?php echo  number_format($totaloffering,2);?></td>
					        <td style="background-color:#3B4F36; color:#fff; font-weight:bold;">N<?php echo  number_format($totaltithe,2);?></td>
					        <td style="background-color:#3B4F36; color:#fff; font-weight:bold;">N<?php echo  number_format($totalexpenses,2);?></td>
					        <td style="background-color:#3B4F36; color:#fff; font-weight:bold;">N<?php echo  number_format($totaltreasury,2);?></td>


					        <td></td>
				          </tr>
				        </table></td>
				      </tr>
			      </table>
					<p>&nbsp;</p>
				</div>
			</div>
		</div>
        </div>
	<?php require_once("inc_footer.php") ?>
</body>
</html>