
<?php
$abso = str_replace("\\","/", FCPATH);
$virt = str_replace("\\","/", base_url());
$image = $virt."dist/img/_sys/user.png";
?>
<script type="text/javascript">
    var valuedatatable;
	
	$(document).ready(function() {
		
		$('#datatable_candidateList').DataTable( {
			"pageLength": 25,
			initComplete: function () {	
				var $table = $(this);			
				var $filter = $table.parents('.dataTables_wrapper').find('.dataTables_filter');
			
				var $row = $filter.parents('.row').eq(0);
						$row.find('.col-sm-6').eq(0).removeClass('col-sm-6').addClass('col-sm-3');
						$row.find('.col-sm-6').eq(0).removeClass('col-sm-6').addClass('col-sm-9');
				
				$filter.find('input[type="search"]').attr('id','search_input');
				
				var companyList = JSON.parse('<?php echo ($companyList) ? json_encode($companyList) : NULL; ?>');
				this.api().columns(3).every( function () {				
					var column = this;
					var wrap = $('<label />',{ 'class': 'company_select' })
											.html('<span>Entity:   </span>')
											.css('margin','0 20px 0 0')
											.prependTo( $filter );
					var select = $('<select id="select_company"><option value="">ALL</option></select>');
						if(companyList) {
							$.each(companyList, function(k,v) {
								select.append( '<option value="'+v['companyName']+'">'+v['companyName']+'</option>' )
							});
						}
						
						select.addClass('form-control input-sm');
						select.appendTo( wrap );
						select.on( 'change', function () {
								var val = $.fn.dataTable.util.escapeRegex( $(this).val() );
								column.search( val ? '^'+val+'$' : '', true, false ).draw();
						});
					
				});
				
				var postList = JSON.parse('<?php echo ($positionList) ? json_encode($positionList) : NULL; ?>');				
				this.api().columns(2).every( function () {				
					var column = this;
					var wrap = $('<label />',{ 'class': 'position_select' })
											.html('<span>Designation:   </span>')
											.css('margin','0 20px 0 0')
											.prependTo( $filter );
					var select = $('<select id="select_position"><option value="">ALL</option></select>');
						if(postList) {
							$.each(postList, function(k,v) {
								select.append( '<option value="'+v['positionName']+'">'+v['positionName']+'</option>' )
							});
						}
						
						select.addClass('form-control input-sm');
						select.appendTo( wrap );
						select.on( 'change', function () {
								var val = $.fn.dataTable.util.escapeRegex( $(this).val() );
								column.search( val ? '^'+val+'$' : '', true, false ).draw();
						});
					
				});
				
			}
		});

		$('.uploader input[type="file"]').off('change').on('change', function() {
			var $img = $(this).siblings('img').eq(0);
			var input = this;
			
			if (input.files && input.files[0]) {
				$img.attr('src', '');
				var reader = new FileReader();			
				reader.onload = function (e) {
					$img.attr('src', e.target.result);
				}			
				reader.readAsDataURL(input.files[0]);
			}	
			$(this).siblings('input[type="hidden"]').val('0');
			closeLoader();
		});
		
		$('.uploader input[type="file"]').off('click').on('click', function() {
			openLoader();
		});
	

  });
			
	function clearImage(me) {
		var $u = $(me).parents('.form-group').eq(0).find('.uploader').eq(0);	
		var $i = $u.find('img').eq(0);
		var def = '<?php echo $image; ?>';
		
		if(	$.trim(	$i.attr('src')	) == def ) return;
		
		if(confirm('Are you sure to reset the candidate image?')) {
			$i.attr('src',def);
			$i.siblings('input[type="hidden"]').val('1');
		}
	}
    
	function addCandidate(me) {
		openLoader();
		$('#saveCandidate').removeAttr('disabled').prop('disabled',0).removeClass('disabled');
		$('#candidateID').val(0);
		$('#candidateName').val("");
		$('#positionID').val(0);
		$('#companyID').val(0);
		$('#userfile').val('');		
		$('#userfileimage').attr('src','<?php echo $image; ?>');
		$('#candidate_modal').modal('show');
		closeLoader();
	}
	
	function showDetail(me) {
		openLoader();
		$('#saveCandidate').removeAttr('disabled').prop('disabled',0).removeClass('disabled');
		
		var c = Number(	$(me).parents('.item').eq(0).find('.item_candidateID').eq(0).val()	);
		var n = $.trim(	$(me).parents('.item').eq(0).find('.item_candidateName').eq(0).val()	);
		var p = Number(	$(me).parents('.item').eq(0).find('.item_positionID').eq(0).val()	);
		var e = Number(	$(me).parents('.item').eq(0).find('.item_companyID').eq(0).val()	);
		var i = $.trim(	$(me).parents('.item').eq(0).find('.item_candidate_picture').eq(0).val()	);
		
		var img = (i) ? '<?php echo $virt; ?>img/candidate/'+i : '<?php echo $image; ?>';
		
		$('#candidateID').val(c);
		$('#candidateName').val(n);
		$('#positionID').val(p);
		$('#companyID').val(e);
		$('#userfile').val("");		
		$('#userfileimage').attr('src',img);
						
		$('#candidate_modal').modal('show');
		closeLoader();		
	}
	
	/**
	* NEW POSITION
	*/
	function newPosition() {
		openLoader();
		$('#add_position_name').val('');
		$('#add_position_elected').val('');
		$('#add_position_ordering').val('');
		$('#saveCandidate').attr('disabled','disabled').prop('disabled',1).addClass('disabled');
		$('#new_position_wrapper').slideDown();
		closeLoader();
	}
	
	function saveNewPosition() {		
		var data = {
			positionID : 0,
			positionName : $('#add_position_name').val(),
			elected : $('#add_position_elected').val(),
			ordering : $('#add_position_ordering').val(),
		};
		
		openLoader();
		$.post('<?php echo base_url(); ?>position/savepositionajax', data, function(r) {
			var obj = ($.trim(r)) ? JSON.parse(r) : null;
			if(obj) {
				if(obj.status == 'SUCCESS') {
					$('#positionID').append('<option value="'+obj.positionID+'">'+data.positionName+'</option>');
					$('#positionID').val(obj.positionID);
				}
			}
			
			closeLoader();
		});
		
		$('#saveCandidate').removeAttr('disabled').prop('disabled',0).removeClass('disabled');
		$('#new_position_wrapper').slideUp();
	}
	
	function cancelAddPosition() {
		openLoader();
		$('#add_position_name').val('');
		$('#add_position_elected').val('');
		$('#add_position_ordering').val('');
		$('#saveCandidate').removeAttr('disabled').prop('disabled',0).removeClass('disabled');
		$('#new_position_wrapper').slideUp();
		closeLoader();
	}
	
	/**
	* NEW COMPANY
	*/
	function newCompany() {
		openLoader();
		$('#add_company_name').val('');
		$('#saveCandidate').attr('disabled','disabled').prop('disabled',1).addClass('disabled');
		$('#new_company_wrapper').slideDown();
		closeLoader();
	}
	
	function saveNewCompany() {
		var data = {
			companyID : 0,
			companyName : $('#add_company_name').val(),
			companyAddress : ''
		};
		
		openLoader();
		$.post('<?php echo base_url(); ?>company/savecompanyajax', data, function(r) {
			var obj = ($.trim(r)) ? JSON.parse(r) : null;
			if(obj) {
				if(obj.status == 'SUCCESS') {
					$('#companyID').append('<option value="'+obj.companyID+'">'+data.companyName+'</option>');
					$('#companyID').val(obj.companyID);
				}
			}
			
			closeLoader();
		});
		
		$('#saveCandidate').removeAttr('disabled').prop('disabled',0).removeClass('disabled');
		$('#new_company_wrapper').slideUp();
	}
	
	function cancelAddCompany() {
		openLoader();
		$('#add_company_name').val('');
		$('#saveCandidate').removeAttr('disabled').prop('disabled',0).removeClass('disabled');
		$('#new_company_wrapper').slideUp();
		closeLoader();
	}
	
	
	
	
	function deleteConfirm(candidateID)
	{
		$('#deleteID').val(candidateID);
		$('#delete_modal').modal('show');
		
	}
	
	function deleteCandidate(){
	
		var candidateID = $('#deleteID').val();
		//if (confirm("Are you sure?")) {
        
			$.ajax({
				type: "POST",
				url: "<?php echo base_url(); ?>candidate/deleteCandidate",
				data: {
					'candidateID' : candidateID
				},
				dataType:'json',
				success: function(data){
					if(data.status == 'success'){
						
						location.reload();
						
					}
				}
			 });
		
		 //}
    	//return false;
	
	}
	
	function printcandidate(){			
		$('#print_btn').attr('href','javascript:void(0);');
		
		var position = $.trim($('#select_position').val()).toUpperCase();
		var postList = JSON.parse('<?php echo ($positionList) ? json_encode($positionList) : NULL; ?>');
		if(postList) {
			$.each(postList, function(k,v) {
				if(v['positionName'].toUpperCase() == position) {
					position = Number(v['positionID']);
					return;
				}
			});
		} else {
			position = 0;
		}
		
		var company = $.trim($('#select_company').val()).toUpperCase();
		var companyList = JSON.parse('<?php echo ($companyList) ? json_encode($companyList) : NULL; ?>');
		if(companyList) {
			$.each(companyList, function(k,v) {
				if(v['companyName'].toUpperCase() == company) {
					company = Number(v['companyID']);
					return;
				}
			});
		} else {
			company = 0;
		}
		
		var search = $.trim($('#search_input').val());				
				search = (search !== "") ? search.replace(' ','%20') : 'all';
		
		var url = "<?php echo base_url() ?>candidate/printCandidate/"+search+"/"+position+"/"+company;
		
		$('#print_btn').attr('target','_blank');
		$('#print_btn').attr('href',url);
		return true;
		
	}
	
	function isNumberKey(evt){
    	var charCode = (evt.which) ? evt.which : event.keyCode
    	if (charCode > 31 && (charCode < 48 || charCode > 57)){
			return false;
		}
		else{
			return true;
		}
	}
	
	
</script>

