<?php
session_start();
require_once ('../config/config.inc.php');
require_once ('../config/mysql.inc.php');
require_once ('includes/functions.php');



if(isset($_GET['task_id']) && trim($_GET['task_id']) >0) {
	$taskId = trim($_GET['task_id']);			
} else {
	$error = 'Task id should not be blank';
}

if(isset($_POST['update_commission'])) {		
	
	if(isset($_POST['message']) && strlen($_POST['message']) > 0) {
		$message = trim($_POST['message']);			
	} else {
		//$error = 'Please enter your comments';
		$message = '';		
	}	

	
	if(empty($error)) {
		
		
		
     }
}
?>
<script src="assets/plugins/jquery/jquery.min.js" ></script>
<script>      
function validateCommission(id){			
	  var sum = 0;        
	  $(".com_value").each(function() {          
	    if (!isNaN(this.value) && this.value.length != 0) {
	      sum += parseFloat(this.value);
	    }    
	  });
	 alert(sum);
	  /*$("#expenses_sum").val(sum.toFixed(2));
		var mainVal = 100-(sum*1);
		$('#main_per-'+sel_tech_id).val(mainVal);
		var maunTechVal = $('#main_per-'+sel_tech_id).val();
		var totCommission = parseFloat(maunTechVal)+(sum*1);
		
		//alert(totCommission);
		if(mainVal<1){
			alert('The total commission % for all technicians should not exceed 100');			
		}*/
}
</script>
        <div class="widget-header">
          <h3>Change Commission</h3>
        </div>
        <div class="widget-content pad20f">
          <?php if(isset($error)) {	echo '<div class="error" style="color:red;">' .$error. '</div>';	}  else if(isset($success)) {
          		echo '<div class="error" style="color: green;font-size: 24px; margin-top: 10%;text-align: center;">' .$success. '</div>';	
          		}?>
          	
          <?php if(isset($success) && $success != ''){	/*Dont show form*/ } else { ?>	
          <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
            <div class="col-12">
            	<?php 
															      	 	$tt = 0; $cc = 0;
																		$totalCommissionPer = 100;
															      	 	//if($showExtraTechs){															      	 		
																			$chkExtraRs = mysqli_query($con, "SELECT id, technician_id, commission_percent FROM tbl_job_extra_tech WHERE job_id = '".$taskId."'");
															      	 		while($extraTechAddedRec = mysqli_fetch_array($chkExtraRs)){ ?>	    		
															      	 		<div class="form-group row">
																			  <label for="email" class="col-6 col-form-label"><?php echo getTechnicianName($extraTechAddedRec['technician_id']); ?></label>
																			  <div class="col-6 pt-2">																			    
																			    <input class="form-control col-6 com_value comm_per-<?php echo $extraTechAddedRec['id'];?>" onblur="validateCommission(this.id)" name="extra_per-<?php echo $extraTechAddedRec['technician_id'];?>" id="extra_per-<?php echo $extraTechAddedRec['technician_id'];?>" type="text" value="<?php echo $extraTechAddedRec['commission_percent']; ?>">
																			  </div>
																			</div>
															      	 	<?php $tt+= $extraTechAddedRec['commission_percent']; $cc++;} ?>																		
																		
																		<?php //} ?>	
																		
																		<div class="form-group row">
																			  <label for="main_per" class="col-6 col-form-label">For Main Tech (%)</label>
																			  <div class="col-6 pt-2">
																			    <input class="form-control col-3 com_value" readonly="readonly" name="main_per" id="main_per-<?php echo $taskId;?>" type="text" value="<?php echo $totalCommissionPer-$tt;  ?>">
																			  </div>
																		</div>																																		
																		<div class="form-group">
																		 
																		</div>																						
																		<div class="form-group row justify-content-md-center mt-4 mb-2">			
																			 <div class="col-12">				 	
																				  <button id="btn" type="button" name="update_commission" data-backdrop="static" class="btn btn-primary submit-pop">Update</button>					    
																			  </div>
																	    </div>													
                             
            </div>
          </form>
          <?php } ?>
        </div>
