<?php
$pgMod="visitor";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);

if($_POST){

    // print_r($_POST);
    // die();

	$_SESSION['form']=$_POST;

	if($flgEr!=TRUE && $pgAct=="add")
	{
        $_POST["added_by"]=$_SESSION["admin"]["uid"];
		$flgIn=$db->insertAry("visitor",$_POST);
		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			if($db->getErMsg()!=""){
				array_push($alert_err,$db->getErMsg());
			}else{
				redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Reception","page_id"=>$pgMod)));
			}
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$flgUp=$db->updateAry("visitor",$_POST,"where id='".$_GET['id']."' ");
		//echo $db->getLastQuery();exit;
		if(!is_null($flgUp))
		{
			$_SESSION['msg']='update Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Reception","page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		
		}
	} 
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
    $flgUp=$db->delete("visitor","where id='".$_GET['id']."' ");

	if(!is_null($flgUp)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Reception","page_id"=>$pgMod)));
}

if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
	?>
	<div class="alert alert-success">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-thumbs-up-alt"></i> <b>Notice</b><?php echo $_SESSION['msg'] ?></i>
	</div>
	<?php
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
	?>
	<div class="alert alert-error">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b>Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
if($pgAct=="edit" && $_GET["id"]!=""){   
	$aryForm=$db->getRow("SELECT * FROM visitor WHERE id='".$_GET['id']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !='')){
	 
	if(isset($_SESSION['form']))
	{
		$aryForm=$_SESSION['form'];
		unset($_SESSION['form']);
	}
?>
<!-- Forms --> 
<style>.top-bar-minimize{display:none;}
thead, tfoot {display: table-header-group;}
.top-bar+.well,.accordion .accordion-heading+.accordion-body{display: inline-block !important;width: 96.7% !important;overflow: inherit !important;} 
</style>

<style>
.recepit {
	width: 750px !important;
	height: 400px !important;
}
.outer, .inner{ 
  max-height: 800px;
  min-height: 405px;
 }
 .outer .undefined{
  background-color: #DADADA !important;
  border: 5px inset #181616 !important;
  min-height: 440px;
  min-width:675px;
}

</style>

<form method="post" action="" id="printableDiv" enctype="multipart/form-data">
     <div class="row-fluid">
        <div class="span12">
            <div class="top-bar">
                <h3><i class="icon-cog"></i>Add visitor</h3>
                <div style="float:right; margin-top:5px">
                    <button type="button" onclick="checkvalid(this)" class="btn btn-primary" id="saveformbutton"> Save</button>
                    <button type="submit" id="saveme" style="display:none" class="btn btn-primary"> Save</button>
                   
                </div>
            </div>

            <div class="well">

    			<div class="span6">
    
    				<div class="well no-padding">

						<div class="control-group">
							<label class="control-label">Mobile No.</label>
							<div class="controls">
								<input type="text" name="mobile" class="input-block-level" value="<?php echo $aryForm["mobile"]; ?>" >
							</div>
						</div>

						<div class="control-group">
							<label class="control-label">Visitor Name</label>
							<div class="controls">
								<input type="text" name="name" class="input-block-level" value="<?php echo $aryForm["name"]; ?>" >
							</div>
						</div>

						<div class="control-group">
							<label class="control-label">Address</label>
							<div class="controls">
								<input type="text" name="address" class="input-block-level" value="<?php echo $aryForm["address"]; ?>" >
							</div>
						</div>

    				</div>

                </div>

    			<div class="span6">

    				<div class="well no-padding">

						<div class="control-group">
							<label class="control-label">Whom to Meet</label>
							<div class="controls">
								<input type="text" name="to_meet" class="input-block-level" value="<?php echo $aryForm["to_meet"]; ?>" >
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label">Purpose</label>
							<div class="controls">
								<input type="text" name="purpose" class="input-block-level" value="<?php echo $aryForm["purpose"]; ?>" >
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label">Remarks</label>
							<div class="controls">
								<input type="text" name="remarks" class="input-block-level" value="<?php echo $aryForm["remarks"]; ?>" >
							</div>
						</div>


    				</div>

                </div>

            </div>
        </div>
        
	</div>
</form>

<?php
}
else{
	?>
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
    <button style="float:right; margin-top:5px" onclick="window.location='./?src=Reception&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add <?php echo $pgMod; ?></button>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"Reception","page_id"=>$pgMod,"action"=>"add");

	$aryList = $db->getRows("select * from visitor ");

	if(is_array($aryList) && count($aryList)>0)
	{
		?>

        <table class="table" id="example">
            <thead>
                <tr>
                    <th>S.no</th>
                    <th>Name</th>
                    <th>Mobile</th>
                    <th>Address</th>
                    <th>To Meet</th>
                    <th>Purpose</th>
                    <th>Remarks</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tfoot>
            </tfoot>
            <tbody>
            <?php foreach($aryList as $job){ $i++;
            $aryPgAct["id"]=$job['id']; $aryPgAct['page_id']=$pgMod;?>
                <tr>
                    <td><?php echo $i; ?></td>
                    <td><?php echo $job['name']; ?></td>
                    <td><?php echo $job['mobile']; ?></td>
                    <td><?php echo $job['address']; ?></td>
                    <td><?php echo $job['to_meet']; ?></td>
                    <td><?php echo $job['purpose']; ?></td>
                    <td><?php echo $job['remarks']; ?></td>
                    <td>
                        <?php $aryPgAct['action']="edit"; ?>
                            <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Edit</button></a>
                        <?php $aryPgAct['action']="delete"; ?>
                            <a href="javascript:;" onclick="del('<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>')" class="confirmation"><button type="button" class="btn btn-danger">Delete</button></a>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
        </div>
        <?php
	}
	?>
	</div></div></div>
	<?php
}
?>
<!-- / Forms -->
<script type="text/javascript">
$(document).ready(function(e) {
    var oTable = $('#ajaxtable').dataTable( {
		"bProcessing": true,
		'sPaginationType': 'full_numbers',
		"sAjaxSource": 'Enquiry/ajax.php?type='+$('#tabletype').val(),
		'iDisplayLength': 5,
		"oLanguage": {
			"sSearch": "Search by any columns:"
		}
	});
	
	getAdmissionNo();
    getenrollment();	
});
function fetchByClass(){
	var name=$("#class").val();
	var section=$("#section").val();
	if(name!=""){
		var url='ajax.php?class='+name+'&type='+$('#tabletype').val();
		if(section!=""){
			url+='&section='+section
		}
		 oTable = $('#ajaxtable').dataTable( {
			"bProcessing": true,
			'sPaginationType': 'full_numbers',
			"bServerSide": false,
			"bDestroy":true,
			"sAjaxSource": url,
			'iDisplayLength': 5,
			"oLanguage": {
				"sSearch": "Search by any columns:"
			}
		});
	}else{
		var oTable = $('#ajaxtable').dataTable( {
			"bProcessing": true,
			'sPaginationType': 'full_numbers',
			"bServerSide": false,
			"bDestroy":true,
			"sAjaxSource": 'ajax.php?type='+$('#tabletype').val(),
			'iDisplayLength': 5,
			"oLanguage": {
				"sSearch": "Search by any columns:"
			}
		});
	}
}
function checkform(){
	var data="name="+$("#name").val()+"&dob="+$("#dob").val()+"&fname="+$("#fname").val()+"&mname="+$("#mname").val()+"&cast="+$("#cast").val()+"&mobile="+$("#mobile").val();
	$.post( "ajax.php?"+data, { type: "PreCheckExist"} , function( data ) {
		//alert(data);
	  	if(data=="0"){
			$("#saveme").click();
		}else{
			alert(data);
			if(confirm("are you sureyou want to register a this student?")){
				$("#saveme").click();
			}else{
				if(confirm("You want to add new student?")){
					window.location='?src=Reception&page_id=make_admission&action=add';
				}else{
					window.location='?src=Reception&page_id=make_admission';
				}
			}
		}
	});
	return false;
}
function changeIns(){
	var cla=$("#class").val();
	$.post( "ajax.php", { type: "classinstallment" ,class: cla } , function( data ) {
	  	$("#ins").html(data);
		getTotal();
	});
}
function buschangeIns(){
	var bus=$("#bus").val();
	if(bus==""){
		$("#busins").html('');
	}else{
		$.post( "ajax.php", { type: "businstallment", bus: bus } , function( data ) {
			$("#busins").html(data);
			getbusTotal();
		});
	}
}
function hostelchangeIns(){
	var hostel=$("#hostel").val();
	if(hostel==""){
		$("#hostelins").html('');
	}else{
		$.post( "ajax.php", { type: "hostelinstallment", hostel: hostel } , function( data ) {
			$("#hostelins").html(data);
			gethostelTotal();
		});
	}
}
function getTotal(){
	var sum=0;var classfee=0;var busfee=0;
	var cost = document.getElementsByName('fee[]');
	classfee=$('#classfee').val();
	for(var i=0;i<cost.length;i++)
	{
		sum = parseFloat(sum) + parseFloat(cost[i].value);
	}
	$('#feetotal').val(parseInt(sum));
	$('#oldtotal').val(parseInt(sum));
}
function getbusTotal(){
	var sum=0;var classfee=0;var busfee=0;
	var cost = document.getElementsByName('busfee[]');
	classfee=$('#busclassfee').val();
	for(var i=0;i<cost.length;i++)
	{
		sum = parseFloat(sum) + parseFloat(cost[i].value);
	}
	$('#busfeetotal').val(parseInt(sum));
	$('#busoldtotal').val(parseInt(sum));
}
function gethostelTotal(){
	var sum=0;var hostelfee=0;
	var cost = document.getElementsByName('hostelfee[]');
	hostelfee=$('#hostelclassfee').val();
	for(var i=0;i<cost.length;i++)
	{
		sum = parseFloat(sum) + parseFloat(cost[i].value);
	}
	$('#hostelfeetotal').val(parseInt(sum));
	$('#hosteloldtotal').val(parseInt(sum));
}
function updateIns(){
	var sum=0;
	 var discountAmt=$("#discount").val();
	 var TotalAmt=$('#oldtotal').val();
	 var RestedAmt=TotalAmt-discountAmt;
	 var cost = document.getElementsByName('fee[]');
	 if(RestedAmt<=0){
		alert("Discount Cannot Greater Then Total Fees"+TotalAmt);
		$("#discount").val(0);
		var DevidedAmt = (TotalAmt/(cost.length));
		$('.feeInsCharge').val(DevidedAmt);
	}else{
		var DevidedAmt = (RestedAmt/(cost.length));
		$('.feeInsCharge').val(DevidedAmt);
	}
	for(var i=0;i<cost.length;i++)
	{
		sum = parseFloat(sum) + parseFloat(cost[i].value);
	}
	$('#feetotal').val(parseInt(sum));
}
function busupdateIns(){
	var sum=0;
	 var discountAmt=$("#busdiscount").val();
	 var TotalAmt=$('#busoldtotal').val();
	 var RestedAmt=TotalAmt-discountAmt;
	 var cost = document.getElementsByName('busfee[]');
	 if(RestedAmt<=0){
		alert("Discount Cannot Greater Then Total Fees"+TotalAmt);
		$("#busdiscount").val(0);
		var DevidedAmt = (TotalAmt/(cost.length));
		$('.busfeeInsCharge').val(DevidedAmt);
	}else{
		var DevidedAmt = (RestedAmt/(cost.length));
		$('.busfeeInsCharge').val(DevidedAmt);
	}
	for(var i=0;i<cost.length;i++)
	{
		sum = parseFloat(sum) + parseFloat(cost[i].value);
	}
	$('#busfeetotal').val(parseInt(sum));
}
function hostelupdateIns(){
	var sum=0;
	var discountAmt=$("#hosteldiscount").val();
	var TotalAmt=$('#hosteloldtotal').val();
	var RestedAmt=TotalAmt-discountAmt;
	var cost = document.getElementsByName('hostelfee[]');
	if(RestedAmt<=0){
		alert("Discount Cannot Greater Then Total Fees"+TotalAmt);
		$("#hosteldiscount").val(0);
		var DevidedAmt = (TotalAmt/(cost.length));
		$('.hostelfeeInsCharge').val(DevidedAmt);
	}else{
		var DevidedAmt = (RestedAmt/(cost.length));
		$('.hostelfeeInsCharge').val(DevidedAmt);
	}
	for(var i=0;i<cost.length;i++)
	{
		sum = parseFloat(sum) + parseFloat(cost[i].value);
	}
	$('#hostelfeetotal').val(parseInt(sum));
}
function getsibling(id){
	$.post( "ajax.php", { type: "getsibling", id: id } , function( data ) {
	  	alert(data);
	});
}
function checkvalid(id){
	if($("#name").val()==""){
		alert("student name cannot blank");
	}
	else if($("#fname").val()==""){
		alert("student fathers name cannot blank")
	}
	else if($("#apply_class").val()==""){
		alert("Please select apply class")
	}
	else if($("#batch").val()==""){
		alert("Please select batch")
	}
	else if($("#mobile").val()==""){
		alert("Please enter mobile no.")
	}
	else
	{
		$("#saveformbutton").hide();
		$("#saveme").click();
	}
}

function getRollNo(cls){
	$.post( "Enquiry/ajax.php", { type: "getrollno", cls: cls } , function( data ) {
	  	$("#roll_no").val(data);
	});
} 

function getAdmissionNo(){
	$.post( "Enquiry/ajax.php", { type: "getadmissionno" } , function( data ) {
	  	$("#admissionno").val(data);
	});
}

function getregno(val){
    if(val=="Purchased Application"){
        $("#feediv").show(); 
    } else {
        $("#feediv").hide();
    }
}


function getenrollment(){
    var session = $('#batch').val();
	$.post( "Enquiry/ajax.php", { type: "getenrollment", session: session } , function(data) {
	  	$("#enrollment").val(data);
	});
	getapplication();
}

function getapplication(){
    var session = $('#batch').val();
	$.post( "Enquiry/ajax.php", { type: "getapplication", session: session } , function(data) {
	  	$("#application").val(data);
	});
}


$(document).ready(function(){
    $("#staffdiv").hide();
    $("#classdiv").hide();
    $("#sectiondiv").hide();
    $("#studentdiv").hide();
});

function changetype(type){
    // alert(type); 
    
    if(type=="Staff"){
        $("#staffdiv").show();
        $("#classdiv").hide();
        $("#sectiondiv").hide();
        $("#studentdiv").hide();
    }
    else if(type=="Parent"){
        $("#staffdiv").hide();
        $("#classdiv").show();
        $("#sectiondiv").show();
        $("#studentdiv").show();
    }
    else {
        $("#staffdiv").hide();
        $("#classdiv").hide();
        $("#sectiondiv").hide();
        $("#studentdiv").hide();
    }
    
}


function getStudents(){
    var cls = $("#class").val();
    var section = $("#section").val();

	$.post( "Reception/ajax.php", { type: "get_student_list", cls: cls, section: section } , function(data) {
	  	$("#student_list").html(data);
	});

}

</script>

