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

if($_POST){

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

	$_SESSION['form']=$_POST;

	if(isset($_FILES["file"]["name"]) && !empty($_FILES["file"]["name"])){
		$lfilename = basename($_FILES['file']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		$lnewfile=md5(microtime()).".".$lext;
		if(move_uploaded_file($_FILES['file']['tmp_name'],"uploads/jobs/".$lnewfile))
		{
			$_POST["file"]=$lnewfile;
		}
	}

	if($flgEr!=TRUE && $pgAct=="add")
	{
        $_POST["added_by"]=$_SESSION["admin"]["uid"];
		$flgIn=$db->insertAry("jobs",$_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"=>"HR","page_id"=>$pgMod)));
			}
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$flgUp=$db->updateAry("jobs",$_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"=>"HR","page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		
		}
	} 
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
    $flgUp=$db->delete("jobs","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"=>"HR","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 jobs 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 Job Posting</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">Job Title</label>
    							<div class="controls">
    								<input type="text" name="title" class="input-block-level" value="<?php echo $aryForm["title"]; ?>" required>
    							</div>
    						</div>
    
    	 					<div class="control-group">
    							<label class="control-label">Department</label>
    							<div class="controls">
    							    <select class="input-block-level" name="department">
    							        <option value="">--Select Department--</--Select--></option>
    							        <?php $department = $db->getRows("select * from job_department");
    							                foreach($department as $d){ ?>
    							                <option value="<?php echo $d['id']; ?>" <?php if($aryForm['department']== $d['id'])echo "selected"; ?>><?php echo $d["name"]; ?></option>
    							        <?php } ?>
    							    </select>
    							</div>
    						</div>
    
                            <div class="control-group">
    							<label class="control-label" for="mname">Designation</label>
    							<div class="controls">
    								<select class="input-block-level" name="designation">
    							        <option value="">--Select Designation--</--Select--></option>
    							        <?php $designation = $db->getRows("select * from job_designation");
    							                foreach($designation as $d){ ?>
    							                <option value="<?php echo $d['id']; ?>" <?php if($aryForm['designation']== $d['id'])echo "selected"; ?>><?php echo $d["name"]; ?></option>
    							        <?php } ?>
    							    </select>
    							</div>
    						</div>

                            <div class="control-group">
    							<label class="control-label">JobType</label>
    							<div class="controls">
                                    <select class="input-block-level" name="jobtype">
    							        <option value="">--Select Job Type--</--Select--></option>
    							        <?php $jobtype = $db->getRows("select * from job_type");
    							                foreach($jobtype as $d){ ?>
    							                <option value="<?php echo $d['id']; ?>" <?php if($aryForm['jobtype']== $d['id'])echo "selected"; ?>><?php echo $d["name"]; ?></option>
    							        <?php } ?>
    							    </select>
    							</div>
    						</div>

                            <div class="control-group">
    							<label class="control-label">Process Name</label>
    							<div class="controls">
    								<select class="input-block-level" name="process">
    							        <option value="">--Select Process--</--Select--></option>
    							        <?php $jobtype = $db->getRows("select * from job_process");
    							                foreach($jobtype as $d){ ?>
    							                <option value="<?php echo $d['id']; ?>" <?php if($aryForm['process']== $d['id'])echo "selected"; ?>><?php echo $d["name"]; ?></option>
    							        <?php } ?>
    							    </select>
    							</div>
    						</div>

                            <div class="control-group">
    							<label class="control-label">Job Description</label>
    							<div class="controls">
    							<textarea rows="5" class="span12" name="description" style="background:white;color:black;"><?php echo $aryForm["description"]; ?></textarea>
    							</div>
    						</div>

    				</div>

                </div>

    			<div class="span6">

    				<div class="well no-padding">

                        <div class="control-group">
							<label class="control-label">Work Experience</label>
							<div class="controls">
								<input type="number" name="experience" class="input-block-level" min="0" value="<?php echo $aryForm["experience"]; ?>" >
							</div>
						</div>

						<div class="control-group">
							<label class="control-label">Opening Date</label>
							<div class="controls">
								<input type="date" name="opening_date" class="input-block-level" value="<?php echo $aryForm["opening_date"]; ?>" >
							</div>
						</div>

						<div class="control-group">
							<label class="control-label">Valid Till</label>
							<div class="controls">
								<input type="date" name="valid_till" class="input-block-level" value="<?php echo $aryForm["valid_till"]; ?>" >
							</div>
						</div>

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

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

						<div class="control-group">
							<label class="control-label">Status</label>
							<div class="controls">
    							<input type="radio" name="status" value="1" <?php if($aryForm["status"]===1)echo "checked"; ?> checked> <span style="color:green;">Active</span> &emsp;&emsp;
    							<input type="radio" name="status" value="0" <?php if($aryForm["status"]===0)echo "checked"; ?>> <span style="color:red;">Inactive</span>
							</div>
						</div>


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

                        <div class="control-group">
							<label class="control-label">Upload Attachment</label>
							<div class="controls"> 
								<input type="file" name="file" />
								<?php if($aryForm["file"]!=""){ 
								    if(strpos($aryForm["file"],"pdf")){
								        echo "<div><a href='uploads/jobs/$aryForm[file]' download style='color:green;'>Download File</a></div>";
								    }else {
        								echo "<div style='width:40px;height:40px;border:solid 2px red;display:block;margin:5px;'><a href='uploads/jobs/$aryForm[file]' download><img src='uploads/jobs/$aryForm[file]' style='width:40px;height:40px;'></a></div>";
								    }
								 } ?>
							</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=HR&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"=>"HR","page_id"=>$pgMod,"action"=>"add");

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

	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <div style="position: absolute;width:300px;z-index: 99999;margin: 30px;display:none;">
        	<select onchange="fetchByClass()" class="span6" name="class" id="class">
                <option value="">Class</option>
                <?php
                    $class=$db->getRows("select * from class where school_id='".$_SESSION["admin"]["school_id"]."'");
                    foreach($class as $cla){
                        ?>
                        <option value="<?php echo $cla["class"] ?>">
                            <?php echo $cla["class"] ?>
                        </option>
                        <?php
                    }
                ?>
            </select>
        </div>
        <!--<input type="hidden" id="tabletype" value="enquiry_data" />-->
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>S.no</th>
                    <th>Title</th>
                    <th>Department</th>
                    <th>Designation</th>
                    <th>Experience</th>
                    <th>Salary</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['title']; ?></td>
                    <td><?php echo $db->getVal("select name from job_department where id='".$job['department']."' "); ?></td>
                    <td><?php echo $db->getVal("select name from job_designation where id='".$job['designation']."' "); ?></td>
                    <td><?php echo $job['experience']; ?> years</td>
                    <td><?php echo $job['min_salary']."-".$job["max_salary"]; ?> Rs.</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=HR&page_id=make_admission&action=add';
				}else{
					window.location='?src=HR&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);
	});
}

function deletefile(filename){
    alert(filename);
}

</script>

