<?php
	ob_start();
	session_start();			
	include('includes/include_all.php');
	include("includes/paging.php");
	require_once 'php_mailer/PHPMailerAutoload.php';
	$p = new post_pager(20);
	
	$page_title="Projects List || SRISTI-BIRAC"; 
	$page='dashboard';
	$login_id=$_SESSION['registration_id'];   

	if(empty($login_id)){
		header('location:login.php');
	}

	
	$action = $_REQUEST['action'];
	
	if($action == 'approve' && $_REQUEST['prj_id'] != '')
	{
		$sql_upd = "update birac_projects set proposal_status = 'A' where birac_project_id = '".$_REQUEST['prj_id']."'";
		mysql_query($sql_upd) or die(mysql_error());
		
		$sql_user_info = "select email_id,applicant_name,project_title from birac_projects where birac_project_id = '".$_REQUEST['prj_id']."'";
		$res_user_info = mysql_query($sql_user_info);
		
		$row_user_info = mysql_fetch_array($res_user_info);
		
		if($row_user_info['email_id'] != '')
		{
			/* Send Mail Start */
			$send_email = $row_user_info['email_id'];
			$str_subject	=	"ALA Approved";	
			
			$str_message = "<html><body>";
			$str_message .= "Hello ".$row_user_info['applicant_name'].",<br>";
			$str_message .= "<b>Your ALA is Approved</b><br>";
			$str_message .= "<b>Project Title : </b>".$row_user_info['project_title']."<br>";
			$str_message .= "</body></html>";
				
					//mail($str_email_id,$str_subject,$str_message,$headers); 
					
					//Create a new PHPMailer instance
					$mail = new PHPMailer;
					//Set who the message is to be sent from
					$mail->setFrom('pmugyti@sristi.org', 'SRISTI-BIRAC');
					//Set an alternative reply-to address
					$mail->addReplyTo('pmugyti@sristi.org', 'SRISTI-BIRAC');
					//Set who the message is to be sent to
					$mail->addAddress($send_email,'Sristi');
					//Set the subject line
					$mail->Subject = $str_subject;
					//Read an HTML message body from an external file, convert referenced images to embedded,
					//convert HTML into a basic plain-text alternative body
					$mail->msgHTML($str_message);
					//Attach an image file
					/*if($_FILES['proposal_doc']['name'] != '')
					{
						$mail->addAttachment($proposal_doc_upload);
					}*/
					//send the message, check for errors
					//$mail->send();
			/* send Mail End */
		}
	}
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0" />
	<title><?php echo $page_title;?></title>

	<!--=== CSS ===-->

	<!-- Bootstrap -->
	<?php include('cssjs.php');?>

	<!--=== JavaScript ===-->

	<script>
		function formsubmit(pageno,frm)
		{
			var formname	=	eval("document."+frm);
			formname.page3.value=pageno;
			formname.submit();
			//document.frm.submit();
			return false;
		}
	</script>
	<style type="text/css">
		.aligncenter{
			text-align:center;
		}
		.green_circle
		{
			color:#51a351;
		}
		.grey_circle
		{
			color:#dedede;
		}
		.aligncenter a
		{
			text-decoration:none;
		}
		
		.progress {
		    overflow: hidden;
		    height: 18px;
		    margin-top: 20px;
		    background-color: #fff;
		    border-radius: 0px;
		    border:0px solid #ddd;
		   
		}
	</style>
</head>

<body class="breakpoint-1200" >

	<!-- Header -->
	
    <?php	include('header.php'); ?>
    
    <!-- /.header -->

	<div id="container">
		<div id="sidebar" class="sidebar-fixed">
			<div id="sidebar-content">				

				<!--=== Navigation ===-->
				<?php
                	include('menu.php');
				?>
				
				<!-- /Navigation -->
				
				

				

			</div>
			<div id="divider" class="resizeable"></div>
		</div>
		<!-- /Sidebar -->

		<div id="content">
			<div class="container">
				<!-- Breadcrumbs line -->
				<div class="crumbs">
					<ul id="breadcrumbs" class="breadcrumb">
						<li>
							<i class="icon-home"></i>
							<a href="<?php echo $base_href; ?>">Dashboard</a>
						</li>
                        <li class="current">Projects List</li>
					</ul>

					
				</div>
				<!-- /Breadcrumbs line -->

				<!--=== Page Header ===-->
				<div class="page-header">
					
				</div>
				<!-- /Page Header -->

				<div class="row">

					<!--=== Project Table ===-->
					<div class="col-md-12">
						<div class="widget box">
							<div class="widget-header">
								<h4><i class="icon-reorder"></i>Projects List</h4>
								<div class="toolbar no-padding">
									<div class="btn-group">
										<span class="btn btn-xs widget-collapse"><i class="icon-angle-down"></i></span>
									</div>
								</div>
							</div>
							<div class="widget-content no-padding">
                             <form method="post" name="data_delete">
								<table class="table table-striped table-bordered table-hover table-responsive">
									<thead>
										<tr>
											<th width="3%">No</th>
											<th>Title</th>
                                            <th>Awardee</th>
                                            <th>Type</th>
											<th width="5%">ALA Signing</th>
                                            <?php
                                            if($_SESSION['user_type'] == 'S')
											{ ?>
                                            <th width="5%">Additional Information</th>
                                            <th width="5%">Grant Scheduler</th>
                                            <th >Status of Grant Release</th>
                                            <?php } ?>
                                            <?php
                                            if($_SESSION['user_type'] != 'B')
											{ ?>
                                            <th>Reporting Scheduler</th>
                                            <?php } ?>
                                            <th width="3%">View</th>
                                            
										</tr>
									</thead>
									<tbody>
										<?php

											$sql="select * from birac_projects where 1=1 "; 
											
											if($_SESSION['user_type'] == 'U')
											{
												$sql .= " and registration_id = '".$_SESSION['registration_id']."' ";
											}

											$sql.=" and project_status='Y'";
										$p->query($sql, "birac_project_id", init());
										
										//$result=mysql_query($sql);
										//$no_row=mysql_num_rows($result); 
											
												$count = '0';
												while($row=tep_db_fetch_array($p->recresult))
												{
													$project_title = $row['project_title'];
													$award_type = $row['award_type'];
													$applicant_name = $row['applicant_name'];
													
													$proposal_status = $row['proposal_status'];
													$birac_project_id = $row['birac_project_id'];
													$project_entry = $row['project_entry'];
													
													$total_quaters = get_name("count(birac_quaters_id)","birac_quaters","birac_project_id = '$birac_project_id'");
													$total_only_quaters = get_name("count(birac_quaters_id)","birac_quaters","birac_project_id = '$birac_project_id' and quaters_type = 'Q'");
													$total_release_quaters = get_name("count(birac_quaters_id)","birac_quaters","birac_project_id = '$birac_project_id' and birac_quaters_status = 'Y' and quaters_type = 'Q'");
													$complete_report = get_name("count(b.birac_quarters_release_id)","birac_quaters a,birac_quarters_release b"," a.birac_quaters_id = b.birac_quaters_id and b.report_approve = 'Y' and a.birac_project_id = '$birac_project_id'");
												?>
												<tr>
                                                	<td><?php echo ++$count; ?></td>
                                                    <td><a href="view-birac-prj.php?prj_id=<?php echo $birac_project_id; ?>"><?php echo $project_title;?></a>
													
													<!-- Progress Indicator -->

													<!-- Count Total Percentage for: ALA + Total No. of Quaters -->
													<?php
													// Check ALA Signed OR Not
													$checkALAStatus=mysql_query("SELECT * FROM birac_projects WHERE birac_project_id='$birac_project_id' and proposal_doc!='' ");
													$countALAStatus=mysql_num_rows($checkALAStatus);

													// Count total quarters
													$countQuarters=mysql_query("SELECT birac_quaters_id,birac_project_id FROM birac_quaters WHERE quaters_type='Q' AND birac_project_id='$birac_project_id'");
													$totalQuarters=mysql_num_rows($countQuarters);

													$percentPerQuarter=0;
													if($totalQuarters!=0){
														$percentPerQuarter+=80/$totalQuarters;
													}

													

													// Count Total Percentage per Quarter
													$countPercentage=mysql_query("SELECT * FROM birac_quarters_release a INNER JOIN birac_quaters b ON a.birac_quaters_id = b.birac_quaters_id WHERE b.birac_project_id =  '$birac_project_id' ");
													$resultCountPercentage=mysql_num_rows($countPercentage);
													//echo $resultCountPercentage*$percentPerQuarter;	
													$alaPercentage=20;
													$currentGrantStatus=0;
													if($countALAStatus!=0){
														$currentGrantStatus+=($resultCountPercentage*$percentPerQuarter)+$alaPercentage;
													}
													?>	
														<?php if($currentGrantStatus!=0){?>
														<div class="progress progress-striped active">
															<div class="progress-bar progress-bar-success" style="width: <?php echo $currentGrantStatus;?>%"><?php echo $currentGrantStatus."%";?></div>
														</div>
														<?php } else { ?>
															<p class="text-danger" style="margin-top:10px;">ALA not submitted</p>

														<?php } ?>
                                                    </td>
                                                    <td><?php echo $applicant_name;?></td>
                                                    <td>
														<?php 
															if($project_entry == 'GYTI')
															{
																echo "Award";
															}
															else
															{
																echo "Appreciation";
															}
														?>
                                                    </td>
                                                    <td align="center"><?php 
														if($proposal_status == 'E')
														{
															if($_SESSION['user_type'] == 'S')
															{ 
															?> <a href="submit-proposal.php?prj_id=<?php echo $birac_project_id; ?>"><i class="icon-plus" title="Submit ALA"></i></a><?php
															}
															else
															{
																?>Not Uploaded<?php
															}
														}
														if($proposal_status == 'P')
														{
															?><i class="icon-spinner" title="ALA in Pending"></i><?php
														}
														if($proposal_status == 'A')
														{
															?><a href="submit-proposal.php?prj_id=<?php echo $birac_project_id; ?>"><i class="icon-ok" title="ALA Approved"></i></a><?php
														}
													?></td>
                                                    <?php
                                                    if($_SESSION['user_type'] == 'S')
													{ ?>
                                                    <td align="center"><a href="edit-birac-prj.php?prj_id=<?php echo $birac_project_id; ?>"><i class="icon-edit" title="Add Details"></i></a></td>
                                                    
                                                    
                                                    <td align="center">
                                                    	<?php
															if($proposal_status == 'A')
															{
																?><a href="grant-project.php?prj_id=<?php echo $birac_project_id; ?>"><i class="icon-bar-chart" title="Give Grant"></i></a><?php
															}
														?>
                                                    </td>
													
                                                    <!-- For Status Of Release Grant -->
                                                    <td class="aligncenter">
                                                    <?php
															if($proposal_status == 'A')
															{
																?>
                                                                <a href="release-grant.php?birac-project-id=<?php echo $birac_project_id;?>">
                                                                	<i class="icon-plus" title="Add Report"></i> <br>
                                                                  <?php for($i=1;$i<=$total_quaters;$i++)
																		{  ?>
																			<i class="icon-circle
																				<?php if($i<=$total_release_quaters) { echo "green_circle";} else { echo "grey_circle";} ?>
																			" title="Add Report"></i>
																		<?php }  ?>
                                                                </a>
                                                                <?php
															}
															else
															{
																for($i=1;$i<=$total_quaters;$i++)
																{  ?>
																	<i class="icon-circle
																		<?php if($i<=$total_release_quaters) { echo "green_circle";} else { echo "grey_circle";} ?>
																	" title="Add Report"></i>
																<?php } 
															}
														?>
                                                    </td>
                                                    <?php } ?>
                                                    
                                                    <!-- End Status Of Release Grant -->
                                                    
                                                    <?php
													if($_SESSION['user_type'] != 'B')
													{ ?>
                                                    <td align="center">
                                                    <?php
													$grt_chk = get_name("birac_quaters_status","birac_quaters","birac_project_id = '$birac_project_id'");
														if($grt_chk != '')
														{ ?>
															<a href="list-add-report.php?prj_id=<?php echo $birac_project_id; ?>"><i class="icon-plus" title="Add Report"></i></a><br>
                                                            <!-- display round -->
                                                            <?php 
															for($i=1;$i<=$total_quaters;$i++)
															{  ?>
																<i class="icon-circle
																	<?php if($i<=$complete_report) { echo "green_circle";} else { echo "grey_circle";} ?>
																" title="Add Report"></i>
                                                    		<?php } ?>
                                                            <!-- End display round Status -->
														<?php 
														}
														else
														{
															for($i=1;$i<=$total_quaters;$i++)
															{  ?>
																<i class="icon-circle
																	<?php if($i<=$complete_report) { echo "green_circle";} else { echo "grey_circle";} ?>
																" title="Add Report"></i>
                                                    		<?php } 
														}
														 ?>
                                                    </td>
                                                    <?php } ?>
                                                    
                                                    <td align="center"><a href="view-birac-prj.php?prj_id=<?php echo $birac_project_id; ?>"><i class="icon-zoom-in" title="View"></i></a></td>
												</tr>

												<?php 
												}
											?>

									</tbody>
								</table>
                                </form>
                                <!-- for pagination -->
                                    	<table class="table table-striped table-bordered table-hover table-checkable">
                                        	<tr>
                                            	<td align="center">
                                                	<form method="post" action="<?=$_SERVER['PHP_SELF'];?>" name="frmpagelist">
                                                         <input type='hidden' name="total_pg" value="<?=$p->findPages();?>">
                                                            <input type='hidden' name="curr_pg" value="<?=$p->current_page;?>">
                                                            <input type='hidden' name="page3" value="">
                                                        <?php 
                                                            if($p->page_total!=0) {
                                                                echo $p->pageList(); 
                                                            }
                                                        ?>		
                                                    </form>	
                                                </td>
                                           	</tr>
                                        </table>
                                    <!-- End Pagination -->
							</div> <!-- /.widget-content -->
						</div> <!-- /.widget -->
					</div> <!-- /.col-md-6 -->
					<!-- /.Project Table end -->

					
				</div>
				
			</div>
			<!-- /.container -->

		</div>
	</div>

</body>
</html>