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

extract($_GET);

$department_id;
$section_id;
$session;

if($operation=='insert'){
// ======================
	foreach($obj->View_Student_List("sc._id='$section_id' AND pf_std._status!='D'") as $value_s){
	extract($value_s);

	$std_id = $value_s['_id'];
	// ====================

	$form_data = array(
		'_section_id'=>$section_id,
		'_session'=>$session,
		'_std_id'=>$std_id,
		'_combined_sub_ids'=>'',
		'_main_sub_ids'=>'',
		'_3rd_sub_id'=>'',
		'_4th_sub_id'=>'',
		'_status'=>'A',
		'_entry_by'=>$_SESSION['LOGIN_USERID'],
		'_entry_date'=>date("Y:m:d h:i:s")
		);

	$obj->Insert_Data_By_Cond("_int_subject_select", $form_data,"_section_id='$section_id' AND _session='$session' AND _std_id='$std_id'");

	}

}
?>

<style type="text/css">
	.input-append, .input-prepend {
		margin-bottom: 0px !important;
	}
	.table td {
		padding: 3px !important;
	}
	.select_in{
		width:170px;
	}
</style>

<?php
$det_in = $obj->Details_By_Cond("_int_institute_setup_vw","section_id='$section_id'");

$tch_id = $obj->Details_By_Cond("_r_class_routine","_section_id='$section_id' AND _teacher_type='CT' AND _teacher_id!='0' GROUP BY _teacher_type");
$teacher_id =  $tch_id['_teacher_id'];

$tch_det = $obj->Details_By_Cond("_pf_emp_personal_info","_id='$teacher_id'");

$total_row = $obj->subject_select_student_rows("sl._section_id='$section_id' AND sl._session='$session'");
?>

<div class="row-fluid">
<legend>
	Subject Select for (<?php echo 'Class: '.$det_in['class'].', Department: '.$det_in['department'].', Student Type: '.$det_in['student_type'].', Section: '.$det_in['section']; ?>).
</legend>
</div>


<!-- ==================================== -->
<form class="form-vertical" id="attendance_sheet">

<div class="row-fluid">
	<input type="hidden" name="total_row" value="<?php echo $total_row; ?>">	
	<input type="hidden" name="section_id" value="<?php echo $section_id; ?>">
	<input type="hidden" name="session" value="<?php echo $session; ?>">
	<input type="hidden" name="department_id" value="<?php echo $department_id; ?>">					
	
	<table id="sample-table-273" class="table table-striped table-bordered table-hover">
		<thead>
			<th>Sl</th>
			<th>Student ID</th>
			<th>Name</th>
			<th>Roll</th>
			<th>Combined Subjects</th>			
			<th>Main Subjects</th>
			<th style="text-align: center;">
				<span>3rd Subject</span></br>
				<span>
					<select class="subject_master_3rd select_in" id="subject_master_3rd" onchange="call_3rd_subject_4th_master(<?php echo $department_id; ?>)" style="margin-bottom: 0;">
			   			<option value="">- - - -</option>
			   			<?php
			   			foreach($obj->View_colmn_By_Cond("_int_subject","_id,_subject_full_name,_subject_short_name,_subject_code","_dept_id='$department_id' AND _subject_type='B'") as $value){
						extract($value);
						?>
			   			<option value="<?php echo $value['_id']; ?>"><?php echo $value['_subject_full_name']; if(!empty($value['_subject_code'])){echo ' ('.$value['_subject_code'].')';} ?></option>
			   			<?php } ?>
			   		</select>
		   		</span>
			</th>
			<th style="text-align: center;">
				<span>4th Subject</span></br>
				<span>
					<select class="subject_master_4th select_in" id="subject_master_4th" onchange="call_4th_subject(<?php echo $department_id; ?>)" style="margin-bottom: 0;">
			   			<option value="">- - - -</option>
			   			<?php
			   			foreach($obj->View_colmn_By_Cond("_int_subject","_id,_subject_full_name,_subject_short_name,_subject_code","_dept_id='$department_id' AND _subject_type='O'") as $value){
						extract($value);
						?>
			   			<option value="<?php echo $value['_id']; ?>"><?php echo $value['_subject_full_name']; if(!empty($value['_subject_code'])){echo ' ('.$value['_subject_code'].')';} ?></option>
			   			<?php } ?>
			   		</select>
		   		</span>
			</th>

			<th>Contact No</th>

		</thead>
		<tbody>
			<?php
			$i=0;
			foreach($obj->subject_select_student("sl._section_id='$section_id' AND sl._session='$session' ORDER BY _section_roll ASC") as $value_s){
			extract($value_s);
			$i++;	
			?>	
			<tr>
				<td class="td_pad">
					<input type="hidden" name="sub_select_id_<?php echo $i; ?>" value="<?php echo $value_s['_id']; ?>">
					<?php echo $i; ?>
				</td>
				<td class="td_pad"><?php echo $value_s['_uniq_id']; ?></td>
				<td class="td_pad"><?php echo $value_s['_full_name']; if(!empty($value_s['_nick_name'])){echo ' ('.$value_s['_nick_name'].')'; } ?></td>
				<td class="td_pad"><?php echo $value_s['_section_roll']; ?></td>
				
				<td class="td_pad">
				<?php
				$t=0;
				$cids=''; $cname='';
				foreach($obj->View_colmn_By_Cond("_int_subject","_id,_subject_full_name,_subject_short_name,_subject_code","_dept_id='$department_id' AND _subject_type='C'") as $l=>$c){
				if($t==0){ $cids.=trim($c['_id']); $cname.= trim($c['_subject_short_name']); }
				else{ $cids.=",".trim($c['_id']); $cname.= ",".trim($c['_subject_short_name']); }
				$t++;
				}
				$cids.="";$cname.="";
				echo $cname;
				?>	
				<input type="hidden" name="comb_sub_ids_<?php echo $i; ?>" value="<?php echo $cids; ?>">
				</td>

				<td class="td_pad">
				<?php
				$x=0;
				$ids=''; $name='';
				foreach($obj->View_colmn_By_Cond("_int_subject","_id,_subject_full_name,_subject_short_name,_subject_code","_dept_id='$department_id' AND _subject_type='M'") as $m=>$n){
				if($x==0){ $ids.=trim($n['_id']); $name.= trim($n['_subject_short_name']); }
				else{ $ids.=",".trim($n['_id']); $name.= ",".trim($n['_subject_short_name']); }
				$x++;
				}
				$ids.="";$name.="";
				echo $name;
				?>	
				<input type="hidden" name="main_sub_ids_<?php echo $i; ?>" value="<?php echo $ids; ?>">
				</td>

				<td class="td_pad" style="text-align: center;">
					<select class="subject_3rd select_in" name="subject_3rd_<?php echo $i; ?>" style="margin-bottom: 0;">
			   			<option value="0">- - - -</option>
			   			<?php
			   			foreach($obj->View_colmn_By_Cond("_int_subject","_id,_subject_full_name,_subject_short_name,_subject_code","_dept_id='$department_id' AND _subject_type='B'") as $value){
						extract($value);
						?>
			   			<option <?php if($value['_id']==$value_s['_3rd_sub_id']) echo 'selected'; ?> value="<?php echo $value['_id']; ?>"><?php echo $value['_subject_full_name']; if(!empty($value['_subject_code'])){echo ' ('.$value['_subject_code'].')';} ?></option>
			   			<?php } ?>
			   		</select>
				</td>
				<td class="td_pad" style="text-align: center;">
					<select class="subject_4th select_in" name="subject_4th_<?php echo $i; ?>" style="margin-bottom: 0;">
			   			<option value="0">- - - -</option>
			   			<?php
			   			foreach($obj->View_colmn_By_Cond("_int_subject","_id,_subject_full_name,_subject_short_name,_subject_code","_dept_id='$department_id' AND _subject_type!='M'") as $value){
						extract($value);
						?>
			   			<option <?php if($value['_id']==$value_s['_4th_sub_id']) echo 'selected'; ?> value="<?php echo $value['_id']; ?>"><?php echo $value['_subject_full_name']; if(!empty($value['_subject_code'])){echo ' ('.$value['_subject_code'].')';} ?></option>
			   			<?php } ?>
			   		</select>
				</td>

				<td class="td_pad"><?php echo $value_s['_contact_mobile']; ?></td>				
				
			</tr>	
			<?php } ?>
			
			
		</tbody>	

	</table>

</div>

<!-- ========================== -->

<div class="row-fluid">	
	<div class="span5"><b>Class Teacher name:</b> <?php echo $tch_det['_full_name']; if(!empty($tch_det['_nick_name'])){ echo ' ('.$tch_det['_nick_name'].')';} ?></div>

	<div class="span3"><b>Mobile No:</b> <?php echo $tch_det['_contact_mobile_no']; ?></div>
</div>

<!-- =========================== -->

<div class="row-fluid">									
	<div class="span12">			
		<div class="control-group">
			<label class="control-label" for="form-field-1">&nbsp;</label>

			<?php if($_SESSION['edit']=='Y' OR $_SESSION['add']=='Y'){ ?>
			<div class="controls">
				<?php if($total_row){ ?>
				<button class="btn btn-small btn-success" type="button" id="btn_save_sub">
					<i class="icon-ok bigger-100"></i>Save
				</button>
				<?php
				}
				else{
				?>
				<button class="btn btn-small btn-success" type="button" id="btn_save_sub">
					<i class="icon-ok bigger-100"></i>Save
				</button>
				<?php } ?>

				&nbsp; &nbsp; &nbsp;
				<button class="btn btn-small btn-danger" type="reset" id="btn_reset">
					<i class="icon-undo bigger-100"></i>
					Reset
				</button>
			</div>
			<?php } ?>
		</div>
	</div>
	
</div>


</form>


<script type="text/javascript">
$('.spinner1').ace_spinner({value:0,min:0,max:80,step:1, btn_up_class:'btn-info' , btn_down_class:'btn-info'})
.on('change', function(){
});



$('.timepicker1').timepicker({
	minuteStep: 1,
	showSeconds: false,
	showMeridian: false
})

// ======================================


$("#btn_save_sub").click(function(){

	var department_id = $('input[name="department_id"]').val();
	var section_id = $('input[name="section_id"]').val();
	var session = $('input[name="session"]').val();

	var total_row = $('input[name="total_row"]').val();
	var operation = 'update_sub';

	//var rs = false;
	var datastr_sc1="department_id="+department_id+"&section_id="+section_id+"&session="+session;

	for (i = 1; i <= total_row; i++) {

		var sub_select_id = $('input[name="sub_select_id_'+i+'"]').val();
		var comb_sub_ids = $('input[name="comb_sub_ids_'+i+'"]').val();
		var main_sub_ids = $('input[name="main_sub_ids_'+i+'"]').val();
		var subject_3rd = $('select[name="subject_3rd_'+i+'"]').val();
		var subject_4th = $('select[name="subject_4th_'+i+'"]').val();

		var dataStr="operation="+operation+"&sub_select_id="+sub_select_id+"&main_sub_ids="+main_sub_ids+"&comb_sub_ids="+comb_sub_ids+"&subject_3rd="+subject_3rd+"&subject_4th="+subject_4th;
		
		//alert(subject_3rd);

		$.ajax({
			type:"post",
			url:"module/exam/save_subject_select.php",
			data:dataStr ,
			success:function(result){
				// alert(result);
			}
		});		
	}
	
	alert('Save Successful');
	
	$(".data_student_list").load("module/exam/get_sub_std_list.php",datastr_sc1);

	$('#first_part').remove();
	$('#second_part').css("display","block");


});




</script>