<?php 
session_start();
// ============
include("../../asset/theme/css/myfunc.php");
include("../../php_crud/my_class_for_web.php");
$obj = new my_class();
$dpt = '';
extract($_GET);


$table_name='_students_admission_form f LEFT JOIN _students_admission_mark_entry e ON f.admission_id = e.admission_id';

$where_cond="f._session='2016' AND f.branch_id=".$branch_id." AND f._class='".$class."' AND (f._approve_status='A' OR f._approve_status='S') ";
//$where_cond=" f._session='".date('Y')."' AND f.branch_id=".$branch_id." AND f._class='".$class."' AND (f._approve_status='A' OR f._approve_status='S') ";
if($dpt){
	 $where_cond= $where_cond." AND f._department='".$dpt."' ";

	}
if($choose_method=='manual'){
	 $where_cond= $where_cond." ORDER BY e.serial_num ASC ";

	}	
// echo 'SELECT * FROM '.$table_name.' WHERE'.$where_cond ;	
$can_list=$obj->View_All_By_Cond($table_name,$where_cond);
$result = array();


$table_name='_students_admission_mark_settings';
$where_cond="branch_id='".$branch_id."' limit 1";
$column = '	total_mark,pass_mark';
$settings=$obj->View_all_data_row_column_By_Cond($table_name,$column, $where_cond);
$total_mark = $settings[0]['total_mark'];
$pass_mark = $settings[0]['pass_mark'];
 $passed_mark = ($pass_mark/100)*$total_mark; ;

$i = 0;
//echo '<pre>';
//print_r($can_list);


foreach ($can_list AS $index1 => $value1) {
	$got_mark = $value1['exam_mark'] ;
	$status = '';
	$status_d = '';
	$entry_status = '';
	
	if($passed_mark<$got_mark ){
		
		$status = '<span class="label label-info arrowed arrowed-righ">Pass</span> ';
		$status_d = 'Pass';
		$entry_status = 'P';
		if($choose_method=='Random'){
			if($got_mark>=$mark_greater){
				$got_mark = $value1['exam_mark'] ;
				$result[$i]['status'] = $status;
		
				$result[$i]['mark_entry_id'] = $value1['mark_entry_id'];
				$result[$i]['_candidate_name'] = $value1['_candidate_name'];
				$result[$i]['admission_form_id'] = $value1['admission_form_id'];
				$result[$i]['_class'] = $value1['_class'];
				$result[$i]['exam_mark'] = $value1['exam_mark'];
				$result[$i]['selection_status'] = $value1['selection_status'] ;
				
				$i++;
			}
		}else{
				$got_mark = $value1['exam_mark'] ;
				$result[$i]['status'] = $status;
				$result[$i]['mark_entry_id'] = $value1['mark_entry_id'];
				$result[$i]['_candidate_name'] = $value1['_candidate_name'];
				$result[$i]['admission_form_id'] = $value1['admission_form_id'];
				$result[$i]['_class'] = $value1['_class'];
				$result[$i]['exam_mark'] = $value1['exam_mark'];
				$result[$i]['selection_status'] = $value1['selection_status'] ;
				$result[$i]['serial_num'] = $value1['serial_num'] ;
				$i++;
		
		}
		
		
	}else{
	 	if($choose_method=='manual'){
			$status = '&nbsp;<span class="label label-important arrowed-in">Fail  </span>&nbsp; ';
	 		$status_d = 'Fail';
	 		$entry_status = 'F';
			
				$got_mark = $value1['exam_mark'] ;
				$result[$i]['status'] = $status;
				$result[$i]['mark_entry_id'] = $value1['mark_entry_id'];
				$result[$i]['_candidate_name'] = $value1['_candidate_name'];
				$result[$i]['admission_form_id'] = $value1['admission_form_id'];
				$result[$i]['_class'] = $value1['_class'];
				$result[$i]['exam_mark'] = $value1['exam_mark'];
				$result[$i]['selection_status'] = $value1['selection_status'] ;
				$result[$i]['serial_num'] = $value1['serial_num'] ;
				$i++;
		}
	}
	
	 
/*	$form_data=array(
				'result_status'=>$entry_status,
		);
	 $up=$obj->Update_Data('_students_admission_mark_entry',$form_data,'mark_entry_id="'.$value1['mark_entry_id'].'"');
*/	
//	if($choose_method=='highest_mark' ){
	
	   
//	}
	
	
	
}
function msort($array, $key, $sort_flags = SORT_REGULAR) {
    if (is_array($array) && count($array) > 0) {
        if (!empty($key)) {
            $mapping = array();
            foreach ($array as $k => $v) {
                $sort_key = '';
                if (!is_array($key)) {
                    $sort_key = $v[$key];
                } else {
                    // @TODO This should be fixed, now it will be sorted as string
                    foreach ($key as $key_key) {
                        $sort_key .= $v[$key_key];
                    }
                    $sort_flags = SORT_STRING;
                }
                $mapping[$k] = $sort_key;
            }
            asort($mapping, $sort_flags);
            $sorted = array();
            foreach ($mapping as $k => $v) {
                $sorted[] = $array[$k];
            }
            return $sorted;
        }
    }
    return $array;
}

if(($choose_method !='Random') && ($choose_method !='manual')){
	$resu = msort($result, array('exam_mark'));
	 krsort($resu);
	
}else{
	$resu = $result; 
	}
//echo '<pre>';
//print_r($resu);

if($choose_method=='highest_mark'){
	if($set_number && count($resu)>$set_number)
		//$result = array_slice($result, $num-$set_number); 
		$resu = array_slice($resu, 0, $set_number);
}
$t = array();

function array_random($arr, $num = 1) {
    shuffle($arr);
   
    $r = array();
    for ($i = 0; $i < $num; $i++) {
        $r[] = $arr[$i];
    }
    return $num == 1 ? $r[0] : $r;
}


if($choose_method=='Random'){
	 if($set_number && count($resu)>$set_number)
	 	{	  	
			$rand_keys = array_random($resu, $set_number); 
		}
		else if($set_number)
		{ 
			$rand_keys = array_random($resu, count($resu)); 
			
			
		}
	
	
	$resu = $rand_keys;
	
}
 
?>

<div class="row-fluid">
			<form id="mark-info" >
			<?php if($choose_method=='manual') echo '<label> Approved Candidate = <span class="approved_num"> </span> </label>'; ?>  
            <table id="sample-table-22" class="table table-striped table-bordered table-hover">
                <thead>
                <th>Sl</th>
                <th>Name</th>
                <th>Form No</th>
                <th>Class</th>
                <th>Mark</th>
                <th>Status</th>
               <?php if($choose_method=='manual') echo '<th>Serial</th> <th>Action</th>'; ?> 
                 
             
                </thead>
                <tbody>
                    <?php
                    $sl = 1;
					$i = 0;
					foreach ($resu AS $index1 => $value1) {
                        echo '<tr class="every">';
						if($choose_method=='manual'){
							$serial = '';	
						}else{
							$serial = $i;
						}
						
						
                     ?>  
                       <td> <?php echo $sl ; ?>  </td>
                       <td><?php echo $value1['_candidate_name'] ; ?> </td>
                       <td><?php echo $value1['admission_form_id'] ?> </td>
                       <td><?php echo $value1['_class'];   ?></td>
					   <td> <?php echo $value1['exam_mark']; ?> </td>
                       <td> 
					   		<?php echo $value1['status'];  
					   			
					   		?> 
                       </td> 
                      <?php if($choose_method=='manual') 
							echo '<td><input type="text" name="serial" value="'.$value1['serial_num'].'" class="serial"  /></td>';
							else
							echo '<input type="hidden" name="serial" value="'.$serial.'" class="serial"  />';
						?>
                        
                       <?php 
					      
					   		if($choose_method=='manual'){
									 $selection_status = $value1['selection_status'];
									if($selection_status==0){
										$appro_status = 'Approve';
										$approve_class = 'btn-info tooltip-success btn-single-approve';
										$data_title = 'Click to Approve';
									}else{
										$appro_status = 'Approveed';
										$approve_class = 'btn-success tooltip-info  btn-single-unapprove';
										$data_title = 'Click to Unapprove';
										}
									echo ' 
										<td class="approve-button"> <span data-original-title="'.$data_title.'" class="btn  btn-small  icon-ok '.$approve_class.'" data-rel="tooltip" data-placement="top" title="">'.$appro_status.'</span></td>
										';
									
									}	
					   		
						
						?> 
                        
                         
                        
                          	
                          
                          <input type="hidden" name="mark" value="<?php echo $value1['mark_entry_id']; ?>" class="mark"  /> 
           <?php   $sl++;  $i++; } ?>
                </tbody>	

            </table>
            
             
         <input type="hidden" value="<?php echo $branch_id; ?>" id="branch_id" />     
             
            </form> 
             
        </div>
<?php 
if($choose_method !='manual'){
?>
<div class="row-fluid">
	<button class="btn btn-small btn-success" type="button" id="btn_approve">
                                    <i class="icon-ok bigger-100"></i>
                                    Approve
                                </button>
</div>
<?php } ?>
<script>
		$('[data-rel=tooltip]').tooltip();
            var oTable2 = $('#sample-table-22').dataTable({
                "aoColumns": [
                    {"bSortable": true},
                    null, null, null, null,  <?php if($choose_method=='manual') echo 'null,null,'; ?>  
                    {"bSortable": true}
                ]});
  	function classCount(name){
		  var num = $('.'+name).size();
		  return num;
	}
	var num = classCount('btn-single-unapprove') ;
	 $(".approved_num").text(num);
  
   $( "#btn_approve" ).click(function() {
		 $this = $(this);
		 var mark = '';
		 var i = 1;
		 branch_id = $( "#branch_id" ).val();
		 var alldat = {"branch_id":branch_id,data:[]};
		 var sing ;
		 $( ".every" ).each(function() {
		    //if(i==1) mark = $( this ).find( ".mark" ).val();
			//else mark = mark+','+$( this ).find( ".mark" ).val();
			mark = $( this ).find( ".mark" ).val();
			serial = $( this ).find( ".serial" ).val();
			sing = {"mark":mark, "serial":serial}
			//i++;
			alldat.data.push(sing);
		});
		
		// var dataStr = "mark=" + data+"&branch_id=" + branch_id; 
		$.ajax({
            type: 'post',
            url: "module/admission/update_chosen_candidat_by_exam.php",
            data: alldat,
			dataType:"json",
            success: function (response)
            {
              
				//setTimeout('$("#msg").html("")', 5000);
               alert('Success');
            }
        });
		return false;
	});		
//	$( ".btn-single-approve" ).click(function() {
// $("").on("click", function(event){	



$("table").on("click", ".btn-single-approve", function(event){		
		 $this = $(this);
		 $paent = $this.parent();
		 var mark = '';
		 var i = 1;
		 mark = $( this ).parent().parent().find( ".mark" ).val();
		 branch_id = $( "#branch_id" ).val();
		 var dataStr = "mark=" + mark+"&branch_id=" + branch_id; 
		
		$.ajax({
            type: 'post',
            url: "module/admission/manual_approve_selected_candidat_by_exam.php",
            data: dataStr,
            success: function (response)
            {
               $this.parent().empty();
			$paent.html('<span data-original-title="Click to Unapprove" class="btn  btn-small  icon-ok btn-success tooltip-info  btn-single-unapprove" data-rel="tooltip" data-placement="top" title="">Approveed</span>'); 
			  // alert(response);
			  var num = classCount('btn-single-unapprove') ;
			  $(".approved_num").text(num);
            }
        });

		return false;
	});
	
//	$( ".btn-single-unapprove" ).click(function() {
//	$(".btn-single-unapprove").on("click", function(event){
$("table").on("click", ".btn-single-unapprove", function(event){		
		 $this = $(this);
		 $paent = $this.parent();
		 var mark = '';
		 
		 var i = 1;
		 mark = $( this ).parent().parent().find( ".mark" ).val();
		 var dataStr = "mark=" + mark; 
		$.ajax({
            type: 'post',
            url: "module/admission/manual_unapprove_selected_candidat_by_exam.php",
            data: dataStr,
            success: function (response)
            {
               $this.parent().empty();
			   $paent.html('<span data-original-title="Click to Approve" class="btn  btn-small  icon-ok btn-info tooltip-success btn-single-approve" data-rel="tooltip" data-placement="top" title="">Approve</span>'); 
			   // alert(response);
			   var num = classCount('btn-single-unapprove') ;
			  $(".approved_num").text(num);
            }
        });

		return false;
	});
	
$("table").on("change", ".serial", function(event){	
	var serial = $( this ).val();
	mark = $( this ).parent().parent().find( ".mark" ).val();
	branch_id = $( "#branch_id" ).val();
	var dataStr = "mark=" + mark+"&branch_id=" + branch_id+"&serial=" + serial; 
	$.ajax({
            type: 'post',
            url: "module/admission/update_serial_chosen_candidat.php",
            data: dataStr,
            success: function (response)
            {
              // alert
            }
        });
	
	
});				
</script>				

