<?php
$msg='';
include("include/settings.php");
include("checksession.php");

if(!isset($_GET['leadid']) || !isset($_GET['is_p']))
{
	header("location:home.php");
}

$memberdetailmaster=$mysqli->prepare("select count(*) from rishta_leadmaster where lead_id=?");
$memberdetailmaster->bind_param("i",$_GET['leadid']);
$memberdetailmaster->execute();
$memberdetailmaster->bind_result($the_lead_count);
$memberdetailmaster->fetch();
$memberdetailmaster->close();

if($the_lead_count!=0)
{
	$lead_id=$_GET['leadid'];
	$is_p =$_GET['is_p'];
}
else
{
	header("location:home.php");
}

$usermaster=$mysqli->prepare("select cdi_followups.*, cdi_usermaster.user_full_name,cdi_usermaster.user_email_id from cdi_followups inner join cdi_usermaster on cdi_followups.sales_executive_id=cdi_usermaster.user_id where lead_id=? order by followup_created_on desc");
$usermaster->bind_param('i',$lead_id);
$usermaster->execute();
$userresult=$usermaster->get_result();

if(isset($_POST['btnSaveFollowUp']))
{
	if(trim($_POST["txtFollowup"])!=null && trim($_POST["txtFollowupTakenOn"])!=null)
	{		
		$followupmaster=$mysqli->prepare("INSERT INTO cdi_followups(lead_id,sales_executive_id,followup_description,followup_created_on,entry_created_on) VALUES (?,?,?,?,?)");
		$followupmaster->bind_param("iisss",$lead_id,$_SESSION['userId'],$_POST['txtFollowup'],$_POST['txtFollowupTakenOn'],$thisdate);
		$followupmaster->execute();
		$followupmaster->close();
		
		$msg="Follow Up is added successfully.";
		header( "refresh:2;url=followlead.php?leadid=".$lead_id."&is_p=".$is_p);
	}
	else
	{
		$msg="Please enter all the fields correctly.";
	}
}


?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Manage FollowUps</title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
	<link rel="Shortcut Icon" href="../img/favicon.ico" />
	<meta name="author" content="admin">
	<link href="../css/bootstrap.min.css" rel="stylesheet" type="text/css" media="all"/>
	<link href="../css/adminStyles.css" rel="stylesheet" type="text/css" />
    <link href="../css/loginStyles.css" rel="stylesheet" type="text/css" />
	<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700' rel='stylesheet' type='text/css' />
	<link href="../css/demo.min.css" rel="stylesheet" type="text/css" media="all"/>
	<link href="../css/animate.css" rel="stylesheet" type="text/css" media="all"/>
	<style>
		.pagelink.active
		{
			background: #cb5151;
			color: #fff;
		}
		
	</style>
</head>
<body>
	<div id="u-app-wrapper">
		<!-- Enter navigation here-->
		<?php include('../include/admin-topbar.php') ?>
		<div id="u-left-panel" class="u-left-panel19">
		<!-- Enter navigation here-->
		<?php include('../include/admin-navigation.php') ?>
		</div>
		<div class="content-wrapper">
			<div class="content-panel">
				<div class="col-sm-12 col-xs-12">
					<?php
					if(trim($msg)!=null)
					{
						?>
							<div class='row'>
								<div class='col-md-12'>
									<div class="alert alert-success errormessage" id="errormessage">
										<strong><?php echo $msg ?></strong>
									</div>
								</div>
							</div>
						<?php
					}
					?>
					<panel>
						<h3>Manage FollowUps</h3>
					</panel>
					
					<form action='#' method='post'>
						<div class="row">
							<div class="col-md-4">
								<textarea placeholder="FollowUp Description"  id="txtFollowup" name="txtFollowup" class="form-control-custom" ></textarea>
							</div>
							<div class="col-md-2">
								<input placeholder="Taken On" type="text" id="txtFollowupTakenOn" name="txtFollowupTakenOn" class="form-control-custom" autocomplete='off' readonly='readonly' value="" />
							</div>
							<div class="col-md-2">
								<input style="background-color: #13c2cf;" type="submit" id="btnSaveFollowUp" name="btnSaveFollowUp" value="Add Follow Up" class="btnCustom" onclick="return saveuser()"/> &nbsp;&nbsp;
							</div>
							<div class="col-md-2 text-right">
								<?php
								if($is_p==1)  //is_project_lead
								{
									?>
										<a href="edit-rishta-lead.php?leadid=<?php echo $lead_id ?>" id="" name="" class="btnCustom">View Lead Detail</a>
									<?php										
								}
								else
								{
									?>	
										<a href="edit-rishta-lead.php?leadid=<?php echo $lead_id ?>" id="" name="" class="btnCustom">View Lead Detail</a>
									<?php
								}
								?>
							</div>
							
						</div>
					</form>
					
					
					  <div id="ajaxcoursedata">
						   
							 
							<div class="row mtop10">
								<div class="col-md-12">
									<?php
										if(mysqli_num_rows($userresult)>0)
										{
											?>
											<table class="table table-responsive table-stripped table-bordered">
												<thead>
													<tr class='alert alert-success'>
														<th>Sr.No</th>
														<th>Sales Exec. Name </th>
														<th>Sales Exec. Email</th>
														<th>Follow Up</th>
														<th>Created On</th>
													</tr>
												</thead>
												<tbody>
											        <?php
													$i=0;
													while($fuserrow=mysqli_fetch_object($userresult))
													{
														$i++;
														?>
															<tr class="text-center">
																<td><?php echo $i; ?></td>
																<td class="text-left"><?php echo $fuserrow->user_full_name ?></td>
																<td class="text-left"><?php echo $fuserrow->user_email_id ?></td>
																<td class="text-left"><?php echo nl2br($fuserrow->followup_description) ?></td>
																<td><?php echo $fuserrow->followup_created_on ?></td>
															</tr>
														<?php
													}
													?>
												</tbody>
											</table>
											<?php
										}
										else
										{
											?>
												<div class="alert alert-danger">
													<strong>Warning!</strong> You have no follow up for this lead.
												</div>
											<?php
										}
									?>
								</div>
							</div>
					  </div>
				</div>
			</div>
		</div>
		
	</div><!--Global-->

	

	<!--COMBINED FILE-->
	<script src="../js/jquery.js" type="text/javascript"></script>
	<script src="../js/jquery-ui.js" type="text/javascript"></script>
	<script>
		
		function saveuser()
		{
			var txtFollowup=$('#txtFollowup').val();
			var txtFollowupTakenOn=$('#txtFollowupTakenOn').val();
			
			if($.trim(txtFollowupTakenOn).length==0 || $.trim(txtFollowup).length==0)
			{
				alert("Please fill all the required fields.");
				return false;
			}
			else
			{
				return confirm('Are You Sure?');
			}
		}
		
		function back() 
		{
			var c=confirm('Are You Sure');
			if(c==true)
			{
				window.location="manage-leads.php";
			}
		}  	
		  
		$(document).ready(function() 
		{
			$("#txtFollowupTakenOn").datepicker({
				dateFormat: 'yy-mm-dd',
				// minDate: 0, // 0 days offset = today	
			});
			
		
		});	
	</script>
	
</body>
</html>