<?php
helper('url');
// Get the URI object
$uri = current_url(true);
?>
<script>
	/*  ADD FORM */
	function addForm(id,title,dialogClass,functionName){ 
	  $('#addTitle').html(title);
	  $('.modal-dialog').addClass(dialogClass);
	  var data = new FormData();
	  data.append('id', id);
	  $.ajax({
					url:"<?php echo base_url().'/'.$ajax_url;?>/"+functionName, 
					method:"POST",
					data:data,
					contentType:false,
					cache:false,
					processData:false,
					success:function(data)
					{
						$("#dataAdd").html(data);
					}
			});
	}
	/*  ADD FORM END */
	/*  ADD ITEM  VALIDATION  AND INSERT PROCESS */
	$(document).ready(function() {
			// validate the aboutus Form  when it is submitted
			$("#formAdd").validate({
					   rules: {
							
					   },
				messages: {
					
						},
			submitHandler: function() {
			var data = new FormData();
			
			var id=$("#id").val();
			//alert(id);
			if(id==""){
			var message="Added";	
			}else{
			var message="Edited";	
			}
			//Form data
			var form_data = $('#formAdd').serializeArray();
			$.each(form_data, function (key, input) {
				data.append(input.name, input.value);
			});
			
			$.ajax({
					url:"<?php echo base_url().'/'.$ajax_url;?>/addDataProcess", 
					method:"POST",
					data:data,
					contentType:false,
					cache:false,
					processData:false,
					beforeSend:function()
					{
					  $('.loading').show();
					},
					success:function(data)
					{ 
						
						if(data == 1){
							
							$('.loading').hide();
							$('#modalAdd').modal('hide');
								toastr.success("<?php echo $heading;?> "+message+" Successfully", "", {
								"timeOut": "100",
								onHidden: function () {
								window.location="<?php echo base_url().$ajax_url;?>";
							}
						});
						
						}
						else{
						$('.loading').hide();
						$('#modalAdd').modal('hide');
								toastr.error("<?php echo $heading;?> could not "+message, "", {
								"timeOut": "100",
								onHidden: function () {
								window.location="<?php echo base_url().$ajax_url;?>";
							}
						});
						}
					}
				   })
		}
	});
	});
/* DELETE ITEMS FORM */

function deleteForm(id){ 
$('#deleteTitle').html('Delete <?php echo $heading;?>');
$('#dataDelete').html('Are you sure to delete this <?php echo $heading;?>?');
$('#deleteId').val(id);
}
/* DELETE ITEMS FORM END */	

/* DELETE ITEM  VALIDATION AND PROCESS */
$("#formDelete").validate({
	submitHandler: function() {
	var data = new FormData();
	//Form data
	var form_data = $('#formDelete').serializeArray();
	$.each(form_data, function (key, input) {
		data.append(input.name, input.value);
	});
	$.ajax({
				url:"<?php echo base_url().'/'.$ajax_url;?>/deleteDataProcess", 
				method:"POST",
				data: data,  
				contentType:false,
				cache:false,
				processData:false,
				beforeSend:function()
				{
				  $('.loading').show();
				},
				success:function(data)
				{
				if(data == 1){
					
					$('.loading').hide();
					$('#modalDelete').modal('hide');
								toastr.success("<?php echo $heading;?> Deleted Successfully", "", {
								"timeOut": "100",
								onHidden: function () {
								window.location="<?php echo base_url().$ajax_url;?>";
							}
						});
					
					}
					else{
					$('.loading').hide();
					$('#modalDelete').modal('hide');
								toastr.error("<?php echo $heading;?> could not Deleted", "", {
								"timeOut": "100",
								onHidden: function () {
								window.location="<?php echo base_url().$ajax_url;?>";
							}
						});
					}
				}
			   })
	} 
});
/* DELETE ITEM  VALIDATION AND PROCESS END */
/* DELETE ALL ITEMS FORM */

function getDeleteAll(){
$('#deleteAllTitle').html('Delete <?php echo $heading;?>');
$('#dataDeleteAll').html('Are you sure to delete this <?php echo $heading;?>(s)');
}
/* DELETE ALL ITEMS FORM END */	

/* DELETE ALL ITEM  VALIDATION AND PROCESS */
$("#formDeleteAll").validate({
	submitHandler: function() {
	//Form data
	ids_array = [];
    var ids_array = [];  
	var e = document.getElementsByName("chk_child");
	for (i = 0; i < e.length; i++) 1 == e[i].checked && ids_array.push(e[i].value);
	var selected_values = ids_array.join(","); 
	//alert(selected_values);
	$.ajax({
				url:"<?php echo base_url().'/'.$ajax_url;?>/deleteAll", 
				method:"POST",
				data: 'id='+selected_values,  
				cache:false,
				processData:false,
				beforeSend:function()
				{
				  //$('.loading').show();
				},
				success:function(data)
				{
				if(data == 1){
					$('.loading').hide();
					$('#modalDeleteAll').modal('hide');
					$("#modalTitle").html("<?php echo $heading;?> Delete Success");
					$("#modalData").html("<p style='color:#0DA700;'><?php echo $heading;?>(s)  Deleted Successfully</p>");
					$('#modalGeneral').modal('show');
					}
					else{
					$('.loading').hide();
					$('#modalDeleteAll').modal('hide');
					$("#modalTitle").html("<?php echo $heading;?> Delete Failed");
					$('#modalData').html("<p style='color:#FF0000;'><?php echo $heading;?>(s) could not Deleted</p>");
					$('#modalGeneral').modal('show');
					}
				}
			   })
	} 
});
/* DELETE ALL ITEM  VALIDATION AND PROCESS END */
/* VIEW ITEM  */
function viewForm(id,dialogClass){ 
	$('#viewTitle').html('View <?php echo $heading;?>');
	$('.modal-dialog').addClass(dialogClass);
	  var data = new FormData();
	  data.append('id', id);
	  $.ajax({
					url:"<?php echo base_url().'/'.$ajax_url;?>/viewData", 
					method:"POST",
					data:data,
					contentType:false,
					cache:false,
					processData:false,
					success:function(data)
					{
						$("#dataView").html(data);
					}
				   });
	 }
/* VIEW ITEM END */

	/* CHECK DUPLICATE PAGE NAME */
	jQuery.validator.addMethod("checkDuplicatePagename", function (value, element) {  
    var value=$("#page_name").val(); 
    var id=$("#id").val(); 
	var tmp = null;
	var data = new FormData();
	data.append("name",value);
	data.append("id",id);
	$.ajax({
				url:"<?php echo base_url($uri->getSegment(1).'/'.$uri->getSegment(2).'/checkDuplicatePagename');?>", 
				method:"POST",
				data:data,
				contentType:false,
				cache:false,
				processData:false,
				async:false,
				beforeSend:function()
				{
				  $('.loading').show();
				},
				success:function(data)
				{	
				  $('.loading').hide();
				if(data == 1){ tmp = false;}
				else{tmp = true;}
				}
			}); 
			 return tmp;
			}, "This Page name already in use"); 
	/* CHECK DUPLICATE PAGE NAME END */
	
	function deleteJS(id){
		var data = new FormData();
		data.append("id",id);
		$.ajax({
				url:"<?php echo base_url().'/'.$ajax_url;?>/deleteJS", 
				method:"POST",
				data: data,  
				contentType:false,
				cache:false,
				processData:false,
				beforeSend:function()
				{
				  $('.loading').show();
				},
				success:function(data)
				{
				if(data == 1){
					$('.loading').hide();
					$('#js_'+id).hide();
					}
					else{
					$('.loading').hide();
					//$('#css_'+id).hide();
					}
				}
			   })
	}


function copyLink(id) {
  navigator.clipboard.writeText(id);
  alert("Link Copied: " + id);
}
</script>

	