<?PHP

ob_start();

session_start();

include 'connect.php';

if(isset($_SESSION['adminemail']))

{

$user=$_SESSION['adminemail'];



$gtp=mysqli_query($conn,"select * from admin where username='$user'");

$gtp1=mysqli_fetch_assoc($gtp);



include 'top.php';

?>





<div id="page-wrapper" >
          <div id="page-inner">

			<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=mysqli_query($conn,"select * from financial where user='$user' order by ID desc");

$aca1=mysqli_fetch_assoc($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>

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
  <script>
    $(function() {
      // Initialize the datepicker
      $("#datepicker").datepicker({
        format: "mm/dd/yyyy",
        autoclose: true,
        todayHighlight: true
      });

      // Adjust the positioning of the datepicker dropdown
      $("#datepicker").on("show", function(e) {
        var inputOffset = $(this).offset();
        var inputHeight = $(this).outerHeight();
        var datepickerDropdown = $(".datepicker-dropdown");

        datepickerDropdown.css({
          top: inputOffset.top + inputHeight + 10,
          left: inputOffset.left
        });
      });
    });
  </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(){
		console.log('Blur event occurred');
        var mobile = $('.mobile').val();
        var dataString = 'mobile=' + mobile;

        // Send AJAX request to getmob.php
        $.ajax({
            type: "GET",
            url: "getmob.php",
            data: dataString,
            cache: false,
            success: function(html) {
                // Log the HTML response received from the server
                console.log("Response from getmob.php:", html);
                
                // Extract the warning count from the response
                var warn = $(html).filter('#warn').text();

                // Check if the warning count is greater than 0
                if (warn > 0) {
                    // Display alert if mobile number already exists
                    alert("Mobile number already exists.");
                    $('.warn').val(1);
                } else {
                    // Clear the warning field
                    $('.warn').val('');
                }
            },
            error: function(xhr, status, error) {
                // Log any errors that occur during the AJAX request
                console.error("Error during AJAX request:", status, error);
            }
        });
    });
});





</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;


		var warn=$('.warn').val();
		

		//alert(mob);

		

		if(mob<10)

		{

			alert("Enter Valid Mobile No.");

			valid = false;

		}
		else if(warn==1)
		{
			
			alert("Mobile No already exist.");

			valid = false;
			
		}

		

		return valid;

		

		$('button:submit').prop('disabled', true);

		

	});

	

});	



</script>







<?php        



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

{



$name=$_POST['name'];

$address=$_POST['address'];

$mobile=$_POST['mobile'];

$whatsapp=$_POST['whatsapp'];

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

$followdate=$_POST['followdate'];

$remarks=$_POST['remarks'];

$othercrop=$_POST['othercrop'];

$howmuchyears=$_POST['howmuchyears'];


if($cultivatingcrop=='Others')
{
	$cultivatingcrop=$othercrop;
}


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



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



$mxx=mysqli_query($conn,"select MAX(ID) as id from crm_customer");

$mxx1=mysqli_fetch_assoc($mxx);



$nmx=$mxx1['id']+1;



$ins=mysqli_query($conn,"insert into crm_customer(ID,name,address,mobile,whatsapp,state,district,taluk,village,cultivatingcrop,nextapply,executive,customertype,product,date,howmuchyears) values('$nmx','$name','$address','$mobile','$whatsapp','$state','$district','$taluk','$village','$cultivatingcrop','$nextapply','$executive','$customertype','$product','$date','$howmuchyears')") or die(mysqli_error($conn));



$fol=mysqli_query($conn,"insert into followup(date,customer,executive,remarks) values('$followdate','$nmx','$executive','$remarks')");



if($ins)

{

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

}

else

{

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

}



}



else 

{



$sel=mysqli_query($conn,"select * from admin where username='$user'");

$nmrw=mysqli_num_rows($sel);

$sel1=mysqli_fetch_assoc($sel);



?>



<form id="course" action="crmcustomer.php" method="post">

<input type="hidden" class="warn" />

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



<tr><td>Name <span style="color:red;">*</span></td><td></td><td><input class="form-control" style="width: 30ch; margin-bottom:10px" type="text" name="name" required autocomplete="off" /></td></tr>



<tr><td>Mobile <span style="color:red;">*</span></td><td></td><td><input class="form-control mobile" style="width: 30ch; margin-bottom:10px" type="text" name="mobile"  required maxlength="10" autocomplete="off" /></td></tr>


<tr><td>Whatsapp </td><td></td><td><input class="form-control" style="width: 30ch; margin-bottom:10px" type="text" name="whatsapp" maxlength="10" autocomplete="off" /></td></tr>


<tr><td>Address</td><td></td><td><textarea class="form-control" style="width: 30ch; margin-bottom:10px" name="address" style="width:220px;"></textarea></td></tr>



<tr><td>State <span style="color:red;">*</span></td><td></td><td><select style="width: 30ch; margin-bottom:10px; padding-left:10px; height:34px;border-radius: 5px;border: 1px solid #c0c0c0;" name="state" class="state" style="width:220px;" required><option value="">-- Select --</option>



<?php



$ste=mysqli_query($conn,"select * from statecode order by ID asc");

while($ste1=mysqli_fetch_assoc($ste))

{

	echo"<option value='".$ste1['ID']."'>".$ste1['state']."</option>";

}



?>



</select></td></tr>



<tr><td>District <span style="color:red;">*</span></td><td></td><td><select style="width: 30ch; margin-bottom:10px;padding-left:10px; height:34px;border-radius: 5px;border: 1px solid #c0c0c0;" name="district" class="district" style="width:220px;" required><option value="" selected="selected">-- Select --</option></select></td></tr>





<tr><td>Taluk <span style="color:red;">*</span></td><td></td><td><select style="width: 30ch; margin-bottom:10px; padding-left:10px;height:34px;border-radius: 5px;border: 1px solid #c0c0c0;" name="taluk" class="taluk" style="width:220px;" required><option value="" selected="selected">-- Select --</option></select></td></tr>





<tr><td>Village</td><td></td><td><select style="width: 30ch; margin-bottom:10px; padding-left:10px; height:34px;border-radius: 5px;border: 1px solid #c0c0c0;" name="village" class="village" style="width:220px;"><option value="" selected="selected">-- Select --</option></select></td></tr>





<tr><td>Customer Type</td><td></td><td><select class="form-control" style="width: 30ch; margin-bottom:10px" name="customertype" style="width:220px;"><option value="Farmer">Farmer</option><option value="Non-Farmer">Non-Farmer</option></select></td></tr>



<tr><td>Product</td><td></td><td><select class="form-control" style="width: 30ch; margin-bottom:10px" name="product" style="width:220px;"><option value="">-- Product --</option>



<?php



$prd=mysqli_query($conn,"select * from product where (category='1' or category='2' or category='3' or category='4') order by ID asc");

while($prd1=mysqli_fetch_assoc($prd))

{

	echo"<option value='".$prd1['ID']."'>".$prd1['product']."</option>";

}



?>



</select></td></tr>



<script type="text/javascript">


function showstuff(element){



    document.getElementById("other").style.display = element=="Others"?"block":"none";


}


</script>



<tr><td>Cultivating Crop</td><td></td><td><select class="form-control" style="width: 30ch; margin-bottom:10px" name="cultivatingcrop" style="width:220px;" onchange="showstuff(this.value)"><option value="">-- Select --</option>

<option value="Paddy">Paddy</option>
<option value="Maize">Maize</option>
<option value="Sorghum">Sorghum</option>
<option value="Black gram">Black gram</option>
<option value="Green gram">Green gram</option>
<option value="Groundnut">Groundnut</option>
<option value="Gingelly">Gingelly</option>
<option value="Cotton">Cotton</option>
<option value="Mango">Mango</option>
<option value="Banana">Banana</option>
<option value="Sapota">Sapota</option>
<option value="Papaya">Papaya</option>
<option value="Guava">Guava</option>
<option value="Acid Lime">Acid Lime</option>
<option value="Grapes">Grapes</option>
<option value="Tomato">Tomato</option>
<option value="Bhendi">Bhendi</option>
<option value="Brinjal">Brinjal</option>
<option value="Chilli">Chilli</option>
<option value="Paprika">Paprika</option>
<option value="Pumpkin">Pumpkin</option>
<option value="Bitter Gourd">Bitter Gourd</option>
<option value="Cucumber">Cucumber</option>
<option value="watermelon">watermelon</option>
<option value="Carrot">Carrot</option>
<option value="Beetroot">Beetroot</option>
<option value="Radish">Radish</option>
<option value="Onion">Onion</option>
<option value="Jasmine">Jasmine</option>
<option value="Coconut">Coconut</option>
<option value="Tea">Tea</option>
<option value="Cashewnut">Cashewnut</option>
<option value="Betelvine">Betelvine</option>
<option value="Arecanut">Arecanut</option>
<option value="Turmeric">Turmeric</option>
<option value="Cardamom">Cardamom</option>
<option value="Cassava">Cassava</option>
<option value="Sweet potato">Sweet potato</option>
<option value="Cluster bean">Cluster bean</option>
<option value="Cabbage">Cabbage</option>
<option value="Cauliflower">Cauliflower</option>
<option value="Curry leaf">Curry leaf</option>
<option value="Rose">Rose</option>
<option value="Marigold">Marigold</option>
<option value="Rupper">Rupper</option>
<option value="Cumbu">Cumbu</option>
<option value="Ragi">Ragi</option>
<option value="Panivaragu">Panivaragu</option>
<option value="Samai">Samai</option>
<option value="Kudiraivali">Kudiraivali</option>
<option value="Sugar cane">Sugar cane</option>
<option value="Drumstick tree">Drumstick tree</option>
<option value="Others">Others</option>

</select></td></tr>

<tr><td colspan="3">
<DIV ID="other" style="display:none; width:100%;">
<table>
<tr><td style="width:290px;">Crop</td><td style="padding:0 5px;"></td><td><input class="form-control" style="width: 30ch; margin-bottom:10px" type="text" name="othercrop" /></td></tr>
</table>
</DIV>
</td></tr>

<tr><td>Next Apply</td><td></td><td><input class="form-control" style="width: 30ch; margin-bottom:10px" type="text" name="nextapply" autocomplete="off" /></td></tr>



<?php



if($nmrw>0)

{

	

echo'<tr><td>Executive</td><td></td><td><select class="form-control" style="width: 30ch; margin-bottom:10px" name="executive" style="width:220px;"><option value="">-- Select --</option>';



$exec=mysqli_query($conn,"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=mysqli_fetch_assoc($exec))

{

	echo"<option value='".$exec1['ID']."'>".$exec1['name']."</option>";

}



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



}

else

{



$empd=mysqli_query($conn,"select * from employee where username='$user'");

$empd1=mysqli_fetch_assoc($empd);



$usr=$empd1['ID'];



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

	

}



?>



<tr><td>Next Follow Up Date <span style="color:red;">*</span></td><td></td><td><input class="form-control" style="width: 30ch; margin-bottom:10px" type="text" name="followdate" autocomplete="off" class="popupdatepicker" id="datepicker" required /></td></tr>


<tr><td>How Much Years </td><td></td><td><input class="form-control" style="width: 30ch; margin-bottom:10px" type="text" name="howmuchyears" /></td></tr>


<tr><td>Remarks </td><td></td><td><textarea class="form-control" style="width: 30ch; margin-bottom:10px" name="remarks"></textarea></td></tr>



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



</table>



</form>



<?php



}

	

?>

                                                                

        

								



										<!-- PAGE CONTENT ENDS -->

									</div><!-- /.col -->

								</div><!-- /.row -->

							</div><!-- /.page-content -->

						</div>

					</div><!-- /.main-content -->
					</div><!-- /.main-content -->
					</div><!-- /.main-content -->







<?php

include 'bottom.php';

}

else

{



 header("location:login.php");

}

ob_flush();



?>