<?PHP
ob_start();
session_start();
include 'connect.php';
if(isset($_SESSION['adminemail']))
{
$user=$_SESSION['adminemail'];

$gtp=mysql_query("select * from employee where username='$user'");
$gtp1=mysql_fetch_array($gtp);

include 'top.php';
?>



			<div class="main-content">
				<div class="main-content-inner">
					<div class="breadcrumbs ace-save-state" id="breadcrumbs">
						<ul class="breadcrumb">
							<li>
								<i class="ace-icon fa fa-home home-icon"></i>
								<a href="#">Home</a>
							</li>
							<li class="active">Customers</li>
						</ul><!-- /.breadcrumb -->
					</div>

					<div class="page-content">
			

						<div class="page-header">
                        
<?php

$aca=mysql_query("select * from financial where user='$user' order by ID desc");
$aca1=mysql_fetch_array($aca);

$financial=$aca1['financial'];

?>
                        
                        <table style="width:100%;">
                        
                        <tr><td style="width:50%;">
                        
							<h1>
								Customers
							</h1>
                            
                            </td>
                            
                            <td style="width:50%; text-align:right; padding-right:15px; font-size:15px;">
                            
                            Executive : <?php echo $gtp1['name']; ?>
                            
                            </td></tr></table>
                            
						</div><!-- /.page-header -->

						<div class="row">
							<div class="col-xs-12">
                            
								<!-- PAGE CONTENT BEGINS -->


<script src="assets/js/jquery.js"></script>

<script>
	
jQuery(function($){
		   
	// simple jQuery validation script
	$('#course').submit(function(){
		
		var valid = true;
		var errormsg = 'This field is required!';
		var errorcn = 'error';
		
		$('.' + errorcn, this).remove();			
		
		$('#required', this).each(function(){
			var parent = $(this).parent();
			if( $(this).val() == '' ){
				var msg = $(this).attr('title');
				msg = (msg != '') ? msg : errormsg;
				$('<span style="font-size:13px;color:#FF0000; padding-left:3px;" class="'+ errorcn +'">'+ msg +'</span>')
					.appendTo(parent)
					.fadeIn('fast')
					.click(function(){ $(this).remove(); });
				valid = false;
			};
		});
		
		return valid;
	});
	
});	

</script>   
								
										
                                        
<?php

if(isset($_GET['id']))
{

$id=$_GET['id'];

if(isset($_POST['submit']))
{

$id=$_POST['id'];

$name = $_POST['name'];

$address = $_POST['address'];

$phone = $_POST['phone'];

$email=$_POST['email'];

$state=$_POST['state'];

$code=$_POST['code'];

$irregationsystem=$_POST['irregationsystem'];

$typeofsoil=$_POST['typeofsoil'];

$totalland=$_POST['totalland'];

$cultivatingcrop=$_POST['cultivatingcrop'];

$cropstage=$_POST['cropstage'];

$lastapplied=$_POST['lastapplied'];

$nextapply=$_POST['nextapply'];

$wantedqty=$_POST['wantedqty'];

$executive=$_POST['executive'];

// now we insert it into the database

$date=date('Y-m-d',strtotime($date));

if($name)
{ 

$in= "update customer set name='$name',address='$address',phone='$phone',email='$email',state='$state',code='$code',irregationsystem='$irregationsystem',typeofsoil='$typeofsoil',totalland='$totalland',cultivatingcrop='$cultivatingcrop',cropstage='$cropstage',lastapplied='$lastapplied',nextapply='$nextapply',wantedqty='$wantedqty',executive='$executive' where ID='$id'";

$in1 = mysql_query($in) or die(mysql_error());

if($in1)
{

 header("location:viewcustomer.php?s=1");

}

}

else
{

 header("location:viewcustomer.php?s=2");

}

}

else
{

$ex=mysql_query("select * from customer where ID='".$id."'");
$ex1=mysql_fetch_array($ex);

$desig=mysql_query("select * from employee_designation where designation='Executive'");
$desig1=mysql_fetch_array($desig);

$design=mysql_query("select * from employee_designation where designation='Team Leader'");
$design1=mysql_fetch_array($design);

if($ex1['lastapplied']!='0000-00-00' && $ex1['lastapplied']!='1970-01-01')
{
	$lastapplied=date('d-m-Y',strtotime($ex1['lastapplied']));
}

if($ex1['nextapply']!='0000-00-00' && $ex1['nextapply']!='1970-01-01')
{
	$nextapply=date('d-m-Y',strtotime($ex1['nextapply']));
}

echo'<form id="course" action="viewcustomer.php?id='.$id.'" method="post">

<input type="hidden" name="id" value="'.$id.'" />

<table width="70%" style="margin:auto; line-height:35px;">

<tr><td width="32%">Name <span class="red">*</span></td><td width="11">:</td><td><input type="text" name="name" id="required" title="*" value="'.$ex1['name'].'" /></td></tr>

<tr><td>Address <span class="red">*</span></td><td>:</td><td><textarea name="address" id="required" title="*" style="width:220px; height:75px;">'.$ex1['address'].'</textarea></td></tr>

<tr><td>Contact </td><td>:</td><td><input autocomplete="off" type="text" name="contact" value="'.$ex1['phone'].'" /></td></tr>

<tr><td>Email </td><td>:</td><td><input autocomplete="off" type="text" name="email" value="'.$ex1['email'].'" /></td></tr>

<tr><td>State </td><td>:</td><td><input autocomplete="off" type="text" name="state" value="'.$ex1['state'].'" /></td></tr>

<tr><td>State Code </td><td>:</td><td><input autocomplete="off" type="text" name="code" value="'.$ex1['code'].'" /></td></tr>

<tr><td>Irregation System </td><td>:</td><td><input autocomplete="off" type="text" name="irregationsystem" value="'.$ex1['irregationsystem'].'" /></td></tr>

<tr><td>Type of Soil </td><td>:</td><td><input autocomplete="off" type="text" name="typeofsoil" value="'.$ex1['typeofsoil'].'" /></td></tr>

<tr><td>Total Land </td><td>:</td><td><input autocomplete="off" type="text" name="totalland" value="'.$ex1['totalland'].'" /></td></tr>

<tr><td>Cultivating Crop </td><td>:</td><td><input autocomplete="off" type="text" name="cultivatingcrop" value="'.$ex1['cultivatingcrop'].'" /></td></tr>

<tr><td>Crop Stage </td><td>:</td><td><input autocomplete="off" type="text" name="cropstage" value="'.$ex1['cropstage'].'" /></td></tr>

<tr><td>Last Applied </td><td>:</td><td><input autocomplete="off" type="text" name="lastapplied" class="datepicker" value="'.$lastapplied.'" /></td></tr>

<tr><td>Next Apply </td><td>:</td><td><input autocomplete="off" type="text" name="nextapply" class="popupdatepicker" value="'.$nextapply.'" /></td></tr>

<tr><td>Wanted Qty </td><td>:</td><td><input autocomplete="off" type="text" name="wantedqty" value="'.$ex1['wantedqty'].'" /></td></tr>

<tr><td>Executive <span class="red">*</span></td><td>:</td><td><select name="executive" id="required" title="*" style="width:220px;"><option value="">Select Executive</option>';

$exec=mysql_query("select * from employee where (designation='$desig1[ID]' OR designation='$design1[ID]')");
while($exec1=mysql_fetch_array($exec))
{
	$executive="<option value='".$exec1['ID']."'>".$exec1['name']."</option>";
	$executive=str_replace("<option value='".$ex1['executive']."'","<option value='".$ex1['executive']."' selected",$executive);
	echo $executive;
}

echo'</select></td></tr>

<tr><td colspan="3" style="text-align:center; padding-top:7px;"><button type="submit" name="submit" class="btn btn-sm btn-primary">Submit</button></td></tr>

</table>

</form>';

}

}

else
	{
		
	
		
		
	if (isset($_GET['s'])) 	
	{
	 $status=$_GET['s'];
		
		 if($status=='1')
		 {
			 echo'<div class="alert alert-block alert-success">
									<button type="button" class="close" data-dismiss="alert">
										<i class="ace-icon fa fa-times"></i>
									</button>
						Values Edited Successfully.
				  </div>';				  
		 }
		 if($status=='7')
		 {
			 echo'<div class="alert alert-block alert-success">
									<button type="button" class="close" data-dismiss="alert">
										<i class="ace-icon fa fa-times"></i>
									</button>
						Values Deleted Successfully.
				  </div>';				  
		 }
	}		


echo'<table style="width:99%; margin:11px auto 10px auto;">
                                        
                                        <tr><td style="width:20%;">
                                        <a href="customer.php"><button class="btn btn-minier btn-info">+ Add Customer</button></a>
                                        </td>
                                        
                                        <td style="width:50%;"></td>
                                        
                                        <td style="width:30%;">
                                        
                                        </td>
                                        
                                        </tr>
                                        
                                        </table>';

	echo"<table id='dynamic-table' class='table table-striped table-bordered table-hover'>

	<thead><th width='7%'>S.No</th><th width='15%'>Name</th><th width='15%'>Address</th><th width='15%'>Contact</th><th width='15%'>Executive</th><th style='width:10%;'>Edit</th></thead>";

	if($gtp1['designation']==2)
	{
		
	$r='';	
	$tl=mysql_query("select * from team_allocation where team_leader='$gtp1[ID]'");
	while($tl1=mysql_fetch_array($tl))
	{
	$r.=$tl1['ID'].',';
	}
	$r=rtrim(",",$r);
		
	$getsite3="select * from customer where executive IN ($r) order by ID asc";
	}
	else
	{
	$getsite3="select * from customer where executive='$gtp1[ID]' order by ID asc";
	}
	
	
	$getsite4=mysql_query($getsite3) or die("Could not get");
	$sno=0;
	while($getsite5=mysql_fetch_array($getsite4))
	{
		$sno=$sno+1;
		
		$emp=mysql_query("select * from employee where ID='$getsite5[executive]'");
		$emp1=mysql_fetch_array($emp);
		
		echo"<tr><td>".$sno."</td><td>".$getsite5['name']."</td><td>".$getsite5['address']."</td>
		<td>".$getsite5['phone']."</td><td>".$emp1['name']."</td><td>
		
														<div class='hidden-sm hidden-xs btn-group'>
														
														<a href='viewcustomerhistory.php?customer=".$getsite5['ID']."' title='View'>
																<i class='ace-icon fa fa-search-plus bigger-100 blue'></i></a> &nbsp;";
																
															if($gtp1['type']!='staff')
															{
															echo"<a href='viewcustomer.php?id=".$getsite5['ID']."' title='Edit'>
																<i class='ace-icon fa fa-pencil bigger-100 green'></i></a>";
															}
															
														 echo"</div>					  
		</td></tr>";

		}

		echo "</table>";

	}

?>

                                            
                                            							

										<!-- PAGE CONTENT ENDS -->
									</div><!-- /.col -->
								</div><!-- /.row -->
							</div><!-- /.page-content -->
						</div>
					</div><!-- /.main-content -->


<?php
include 'bottom.php';
}
else
{

 header("location:login.php");
}
ob_flush();
?>