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

$gtp=mysql_query("select * from admin 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">Farmer Data</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>
								Farmer Data
							</h1>
                            
                            </td>
                            
                            <td style="width:50%; text-align:right; padding-right:15px; font-size:15px;">
                            
                            <i class="fa fa-calendar"></i>&nbsp; <?php echo $financial; ?>
                            
                            </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>

$(".state").live('change',function() {

		  var state=$(this).val();

		  var dataString = 'state='+ state;

		  //alert(dataString);

		  $.ajax

		  ({

		  type: "GET",

		  url: "getdistrict.php",

		  data: dataString,

		  cache: false,

		  success: function(resp)

		  {
		  
		  $('.district').html($('#district',resp).html());

		  }

		  });

});


$(".district").live('change',function() {

		  var district=$(this).val();

		  var dataString = 'district='+ district;

		  //alert(dataString);

		  $.ajax

		  ({

		  type: "GET",

		  url: "gettaluk.php",

		  data: dataString,

		  cache: false,

		  success: function(resp)

		  {
		  
		  $('.taluk').html($('#taluk',resp).html());

		  }

		  });

});


$(".taluk").live('change',function() {

		  var taluk=$(this).val();

		  var dataString = 'taluk='+ taluk;

		  //alert(dataString);

		  $.ajax

		  ({

		  type: "GET",

		  url: "getvillage.php",

		  data: dataString,

		  cache: false,

		  success: function(resp)

		  {
		  
		  $('.village').html($('#village',resp).html());

		  }

		  });

});



$(document).ready(function(){

$('.mobile').blur(function(){
	
		  var mobile=$('.mobile').val();
		  var dataString = 'mobile='+ mobile;
		  //alert(dataString);
		  $.ajax
		  ({
		  type: "GET",
		  url: "getmob.php",
		  data: dataString,
		  cache: false,
		  success: function(html)
		  {
			
			//alert(html);
			var warn=$(html).filter('#warn').text();

			if(warn>0)
			{
				alert("Mobile no already exist.");
			}
			
		  }
		  });

});

});

</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;
			};
		});
		
		var mob=$('.mobile').val().length;
		
		//alert(mob);
		
		if(mob<10)
		{
			alert("Enter Valid Mobile No");
			valid = false;
		}
		
		return valid;
		
		$('button:submit').prop('disabled', true);
		
	});
	
});	

</script>



<?php

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

$id=$_GET['id'];

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

$id=$_POST['id'];
$name=$_POST['name'];
$mobile=$_POST['mobile'];
$address=$_POST['address'];
$state=$_POST['state'];
$district=$_POST['district'];
$taluk=$_POST['taluk'];
$village=$_POST['village'];
$cultivatingcrop=$_POST['cultivatingcrop'];
$nextapply=$_POST['nextapply'];
$executive=$_POST['executive'];
$customertype=$_POST['customertype'];
$product=$_POST['product'];
$howmuchyears=$_POST['howmuchyears'];

$up=mysql_query("update crm_customer set name='$name',mobile='$mobile',address='$address',state='$state',district='$district',taluk='$taluk',village='$village',cultivatingcrop='$cultivatingcrop',nextapply='$nextapply',executive='$executive',customertype='$customertype',product='$product',howmuchyears='$howmuchyears' where ID='$id'") or die(mysql_error());

if($up)
{
	header("location:viewcrmcustomer.php?s=3");
}
else
{
	header("location:viewcrmcustomer.php?s=4");
}

}

else
{

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

$sel=mysql_query("select * from admin where username='$user'");
$nmrw=mysql_num_rows($sel);
$sel1=mysql_fetch_array($sel);

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

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

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

<tr><td>Name</td><td>:</td><td><input type="text" name="name" value="'.$ex1['name'].'" /></td></tr>

<tr><td>Mobile</td><td>:</td><td><input type="text" name="mobile" value="'.$ex1['mobile'].'" /></td></tr>

<tr><td>Address</td><td>:</td><td><textarea name="address" style="width:220px;">'.$ex1['address'].'</textarea></td></tr>

<tr><td>State</td><td>:</td><td><select name="state" class="state" style="width:220px;"><option value="">-- Select --</option>';

$ste=mysql_query("select * from statecode order by ID asc");
while($ste1=mysql_fetch_array($ste))
{
	$sst="<option value='".$ste1['ID']."'>".$ste1['state']."</option>";
	$sst=str_replace("<option value='".$ex1['state']."'","<option value='".$ex1['state']."' selected",$sst);
	echo $sst;
}

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

<tr><td>District</td><td>:</td><td><select name="district" class="district" style="width:220px;"><option value="" selected="selected">-- Select --</option>';

$dis=mysql_query("select * from districts where state='$ex1[state]'");
while($dis1=mysql_fetch_array($dis))
{
	$sdis="<option value='".$dis1['ID']."'>".$dis1['district']."</option>";
	$sdis=str_replace("<option value='".$ex1['district']."'","<option value='".$ex1['district']."' selected",$sdis);
	echo $sdis;
}

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


<tr><td>Taluk</td><td>:</td><td><select name="taluk" class="taluk" style="width:220px;"><option value="" selected="selected">-- Select --</option>';

$tlk=mysql_query("select * from taluk where district='$ex1[district]'");
while($tlk1=mysql_fetch_array($tlk))
{
	$stlk="<option value='".$tlk1['ID']."'>".$tlk1['taluk']."</option>";
	$stlk=str_replace("<option value='".$ex1['taluk']."'","<option value='".$ex1['taluk']."' selected",$stlk);
	echo $stlk;
}

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


<tr><td>Village</td><td>:</td><td><select name="village" class="village" style="width:220px;"><option value="" selected="selected">-- Select --</option>';

$vil=mysql_query("select * from villages where taluk='$ex1[taluk]'");
while($vil1=mysql_fetch_array($vil))
{
	$svil="<option value='".$vil1['ID']."'>".$vil1['village']."</option>";
	$svil=str_replace("<option value='".$ex1['village']."'","<option value='".$ex1['village']."' selected",$svil);
	echo $svil;
}

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


<tr><td>Customer Type</td><td>:</td><td><select name="customertype" style="width:220px;">';

?>

<option value="Farmer" <?php if($ex1['customertype']=='Farmer'){ echo'selected="selected"'; } ?>>Farmer</option>
<option value="Non-Farmer" <?php if($ex1['customertype']=='Non-Farmer'){ echo'selected="selected"'; } ?>>Non-Farmer</option>

<?php

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

<tr><td>Product</td><td>:</td><td><select name="product" style="width:220px;"><option value="">-- Product --</option>';

$prd=mysql_query("select * from product where (category='1' or category='2' or category='3' or category='4') order by ID asc");
while($prd1=mysql_fetch_array($prd))
{
	$sprd="<option value='".$prd1['ID']."'>".$prd1['product']."</option>";
	$sprd=str_replace("<option value='".$ex1['product']."'","<option value='".$ex1['product']."' selected",$sprd);
	echo $sprd;
}

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

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

<tr><td>Next Apply</td><td>:</td><td><input type="text" name="nextapply" value="'.$ex1['nextapply'].'" /></td></tr>

<tr><td>How Much Years</td><td>:</td><td><input type="text" name="howmuchyears" value="'.$ex1['howmuchyears'].'" /></td></tr>';

if($nmrw>0)
{

echo'<tr><td>Executive</td><td>:</td><td><select name="executive" style="width:220px;"><option value="">-- Select --</option>';

$exec=mysql_query("select employee.ID as ID, employee.name as name from employee left join department on employee.department=department.ID where department.crm='1' order by employee.ID asc");
while($exec1=mysql_fetch_array($exec))
{
	$exs="<option value='".$exec1['ID']."'>".$exec1['name']."</option>";
	$exs=str_replace("<option value='".$ex1['executive']."'","<option value='".$ex1['executive']."' selected",$exs);
	echo $exs;
}

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

}
else
{

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

$usr=$empd1['ID'];

echo"<input type='hidden' name='executive' value='".$usr."' />";
	
}

echo'<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=='3')
		 {
			 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>';
		 }
		 elseif($status=='4')
		 {
			 echo'<div class="alert alert-block alert-danger">
									<button type="button" class="close" data-dismiss="alert">
										<i class="ace-icon fa fa-times"></i>
									</button>
						Values not edited. please try again.
				  </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>';
		 }
	}

$sel=mysql_query("select * from admin where username='$user'");
$nmrw=mysql_num_rows($sel);
$sel1=mysql_fetch_array($sel);

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

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

	<thead><th></th>
    <th width='15%'>Name</th>
	<th width='15%'>Address</th>
	<th width='11%'>Mobile</th>
	<th width='15%'>State</th>
	<th width='15%'>District</th>
	<th width='15%'>Crops</th>
	<th style='width:10%;'>Edit</th></thead>";

if($nmrw>0)
{	
	$getsite3="select * from crm_customer order by name asc";
	$exct=0;
}
else
{
	$empd=mysql_query("select * from employee where username='$user'");
	$empd1=mysql_fetch_array($empd);

	$exct=$empd1['ID'];
}

	echo"</table>";
		
	$nmrw=$nmrw;
	$exct=$exct;

	}

?>


<script type="text/javascript" language="javascript" class="init">

		var nmrw=<?php echo $nmrw; ?>;
		var exct=<?php echo $exct; ?>;
		
		//alert(nmrw);

		$(document).ready(function() {			
			$('#example').dataTable( {
				"processing": true,
				"serverSide": true,
				"ajax": "server.php?nmrw="+ nmrw +"&exct="+ exct,
				"language": {
                    "processing": '<i class="fa fa-spinner fa-spin" style="font-size:24px;color:rgb(75, 183, 245);"></i>'
                 },
				 'columnDefs' : [
					//hide the second & fourth column
					{ 'visible': false, 'targets': [0] }
				 ]
			} );
		} );

</script>


										<!-- 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();

?>