<?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() {
			$( ".datepicker" ).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="datefrom" type="text" id="datefrom" class="datepicker" value="<?php if(isset($_POST['datefrom'])) echo $_POST['datefrom'] ?>" >
                        </span>&nbsp;To: <span class="input-group">
                             <input name="dateto" type="text" id="dateto" class="datepicker" value="<?php if(isset($_POST['dateto'])) echo $_POST['dateto'] ?>" >
                        </span>

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

  <input name="command" type="hidden" id="command" value="searchform" />




Search By Week:
<select name="week" id="week">
      <option value="">Select Week</option>
    <option value="Week One"<?php if(isset($_POST['week']) && $_POST['week'] == "Week One") echo "selected=\"selected\"" ?>>Week One</option>
     <option value="Week Two"<?php if(isset($_POST['week']) && $_POST['week'] == "Week Two") echo "selected=\"selected\"" ?>>Week Two</option>
      <option value="Week Three"<?php if(isset($_POST['week']) && $_POST['week'] == "Week Three") echo "selected=\"selected\"" ?>>Week Three</option>
       <option value="Week Four"<?php if(isset($_POST['week']) && $_POST['week'] == "Week Four") echo "selected=\"selected\"" ?>>Week Four</option>
        <option value="Week Five"<?php if(isset($_POST['week']) && $_POST['week'] == "Week Five") echo "selected=\"selected\"" ?>>Week Five</option>
</select>
&nbsp;
&nbsp;
<input type="submit" name="search" id="search" value="GO!" />&nbsp;&nbsp; &nbsp;

  <input name="command" type="hidden" id="command" value="searchform" />



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="5%" bgcolor="#879D91"><strong>Fellowship Week</strong></td>
					          <td width="7%" bgcolor="#879D91"><strong>Start Time</strong></td>
					          <td width="7%" bgcolor="#879D91"><strong>End Time</strong></td>
					         <td width="7%" bgcolor="#879D91"><strong>Total Attendees</strong></td>
					         <td width="5%" bgcolor="#879D91"><strong>Total Workers</strong></td>
                             <td width="4%" bgcolor="#879D91"><strong>Total 1st Timers</strong></td>
					        <td width="5%" bgcolor="#879D91"><strong>Visited By</strong></td>
							<td width="7%" bgcolor="#879D91"><strong>Offering</strong></td>
							<td width="7%" bgcolor="#879D91"><strong>Full 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="10%" bgcolor="#879D91"><strong>Date Submitted</strong></td>
							<td width="15%" bgcolor="#879D91"><strong>Report Status</strong></td>

				          </tr>
<?php
$first_timers = 0;
$totaloffering = 0;
$totalfulloffering = 0;
$totaltithe = 0;
$totalexpenses = 0;
$totaltreasury = 0;

	$currentYear = date("Y");
	$date_of_current_year = ($currentYear-1)."-12-31";
	//$sql = "SELECT * FROM `pcc_report` WHERE `pcc_id` > -1 AND `fellowship_date`>'2015-12-31'";
    // $sql = "SELECT * FROM `pcc_report` WHERE `pcc_id` > -1 AND `fellowship_date`>'".$date_of_current_year."'";
     $sql = "SELECT * FROM `pcc_report` WHERE `pcc_id` > -1 AND year(`fellowship_date`) = '".$currentYear."'";

	$param = array();

	if(isset($_POST['search']))
	{
		$sql = "SELECT * FROM `pcc_report` WHERE `pcc_id` > -1";

        $searchtext = $_POST['search'];
		$department = $_POST['department'];
		$dateto = $_POST['dateto'];
		$datefrom = $_POST['datefrom'];
		$week = $_POST['week'];

		if(strlen(trim($department)) > 0)
		{
   	 		//$sql = "SELECT * FROM `pcc_report` WHERE `pcc_id` > -1 AND `fellowship_date` >'".$date_of_current_year."'";
			//$sql .= " AND (`pccs_area` = :pccs_area)";
			//$sql = "SELECT * FROM `pcc_report` WHERE `pcc_id` > -1 AND (`pccs_area` = :pccs_area)";
			//$sql = "SELECT * FROM `pcc_report` WHERE `pcc_id` > -1 AND year(`fellowship_date`) ='".$currentYear."'";
			$sql .= " AND (`pccs_area` = :pccs_area)";
			$param[":pccs_area"] = $department;
		}

		if(strlen(trim($week)) > 0)
		{
	        //$sql = "SELECT * FROM `pcc_report` WHERE `pcc_id` > -1 AND `fellowship_date` >'".$date_of_current_year."'";
			//$sql .= " AND ( `fellowship_week` = :week)";
			// $sql = "SELECT * FROM `pcc_report` WHERE `pcc_id` > -1 AND year(`fellowship_date`) = '".$currentYear."'";
			 $sql .= " AND ( `fellowship_week` = :week)";
			$param[":week"] = $week;
          }

		      if(strlen(trim($datefrom)) > 0)
				{
					$sql .= " AND (`fellowship_date` >= :datefrom)";
					// $sql = "SELECT * FROM `pcc_report` WHERE `pcc_id` > -1 AND (`fellowship_date` >= :datefrom)";
					$param[":datefrom"] = $datefrom;
				}
				if(strlen(trim($dateto)) > 0)
				{
					$sql .= " AND (`fellowship_date` <= :dateto)";
					$param[":dateto"] = $dateto;
		        }


	}

	$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']);

 	 $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 $row['fellowship_date'] ?></td>
                                <td><?php echo $row['fellowship_week'] ?></td>
                                <td><?php echo $row['start_time'] ?>pm</td>
                                <td><?php echo $row['end_time'] ?>pm</td>


					        <td><?php echo $row['total_attendance'] ?></td>
					        <td><?php echo $row['workers_attend'] ?></td>
                             <td><?php echo $row['first_timers'] ?></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['fulloffering'],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('m/d/Y H:i:s',$row['date_submitted']);?></td>
					                     <td><?php echo $flag?></td>
				          </tr>
<?php

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

					        <td></td>
					        <td></td>
					        <td></td>
					        <td></td>
					        <td></td>
					        <td></td>
					        <td></td>
                            <td style="background-color:#3B4F36; color:#fff; font-weight:bold;"><?php echo  number_format($first_timers);?></td>
                            <td style="background-color:#3B4F36; color:#fff; font-weight:bold;"><?php echo  "";?></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($totalfulloffering,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>
			      <tr>
				  					    <td>
				  					      <form action="reports_pcc_export.php" method="post" name="form2" target="actionframe" id="form3">
				  					        <input type="submit" name="button3" id="button3" value="Export to Excel" onclick="javscript: exportReport(this.form,'csv') " />
                                             <input name="search" type="hidden" id="search" value="<?php if(isset($_POST['search'])) echo $_POST['search'] ?>" />
											 <input name="datefrom" type="hidden" id="datefrom" value="<?php if(isset($_POST['datefrom'])) echo $_POST['datefrom'] ?>" />
					        <input name="dateto" type="hidden" id="dateto" value="<?php if(isset($_POST['dateto'])) echo $_POST['dateto'] ?>" />
                             <input name="department" type="hidden" id="department" value="<?php if(isset($_POST['department'])) echo $_POST['department'] ?>" />

				  					      </form>
				  					    </td>

				      </tr>

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