<?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">Customer Reallocate</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>
								Customer Reallocate
							</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>
	
jQuery(function($){

	// simple jQuery validation script
	$('#customer').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 r=0
		$('.check').each(function(i){
		
		if($(this).attr('checked'))
		{
		
		var row = $(this).parents('.item-row');
		
		r++;
		}
		
		});
		//alert(r);
		
		if(r==0)
		{
		alert("Select atleast one checkbox");
		valid = false;
		}
		
		
		return valid;
		
		$('button:submit').prop('disabled', true);
		
	});
	
});	

</script>

<script type="text/javascript">

$(document).ready(function(){

$('#checkall:checkbox').change(function () {

//alert("text");	
    
if($(this).prop("checked") == true)	
{		
$('input:checkbox').prop( "checked", true );	
}    
else if($(this).prop("checked") == false)	
{		
$('input:checkbox').prop( "checked", false);	
}	

});

});

</script>

<?php

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

$executive=$_POST['executive'];
$customer=$_POST['customer'];
$check=$_POST['checkbx'];

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

echo count($check);

//echo"$executive & $customer & $check";

foreach($check as $chk)
{
	
	$cust=$customer[$chk];
	
	$sel=mysql_query("update crm_customer set executive='$executive' where ID='$cust'");
	
	$selc=mysql_query("update customer set executive='$executive' where salescustid='$cust'");
	
	$upd=mysql_query("update followup set executive='$executive' where customer='$cust' and date>='$date'");
	
}

if($sel)
{
	header("location:viewemployee.php?s=11");
}
else
{
	header("location:viewemployee.php?s=12");
}

}

else
{
	
	$empid=$_GET['id'];
	
	$emp=mysql_query("select * from employee where ID='$empid'");
	$emp1=mysql_fetch_array($emp);
	
	echo"<form action='customerreallocate.php' method='post'>
	
	<center><br /><input type='hidden' name='employee' value='".$empid."' />
	
	<table>
	
	<tr><td>Executive </td><td style='padding:0 7px;'> : <b>".$emp1['name']."</b></td></tr>
	
	<tr><td>Select Executive </td><td style='padding:0 7px;'> : <select name='executive' required><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))
	{

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

	}
	
	echo"</select></td></tr>
	
	</table></center><br />
	
	<table id='dataTables-example' class='table table-striped table-bordered table-hover'>
	<thead><th width='5%'><input type='checkbox' id='checkall' /></th>		<th width='15%'>Name</th>
	<th width='11%'>Mobile</th>
	<th width='15%'>Taluk</th>
	<th width='15%'>District</th>
	<th width='15%'>State</th>
	<th width='15%'>Crops</th></thead>";

	$getsite3="select * from crm_customer where executive='$empid' order by name asc";
	$getsite4=mysql_query($getsite3) or die(mysql_error());
	$sno=0;
	while($getsite5=mysql_fetch_array($getsite4))
	{
		$sno=$sno+1;
		
		$ste=mysql_query("select * from statecode where ID='$getsite5[state]'");
		$ste1=mysql_fetch_array($ste);
		
		$dis=mysql_query("select * from districts where ID='$getsite5[district]'");
		$dis1=mysql_fetch_array($dis);
		
		$tlk=mysql_query("select * from taluk where ID='$getsite5[taluk]'");
		$tlk1=mysql_fetch_array($tlk);
		
		echo"<tr><td><input type='checkbox' name='checkbx[".$sno."]' value='".$sno."' /></td>
		<td><input type='hidden' name='customer[".$sno."]' value='".$getsite5['ID']."' /> ".$getsite5['name']."</td>
		<td>".$getsite5['mobile']."</td>
		<td>".$tlk1['taluk']."</td>
		<td>".$dis1['district']."</td>
		<td>".$ste1['state']."</td>
		<td>".$getsite5['cultivatingcrop']."</td></tr>";

	}

	echo"</table>

	<center><button type='submit' name='submit' value='submit'>Submit</button></center>

	</form>";

}

?>




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



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

    <script>
    $(document).ready(function() {
        $('#dataTables-example').dataTable({ 
		
		"aoColumnDefs": [
      
	      { 'bSortable': false, 'aTargets': [0,-1] }
       ],
	   
	   "aaSorting": [1, "asc"]
	   
	  });
		
	  
    });
	
	
    </script>


<?php

include 'bottom.php';
}
else
{

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

?>