// Como Billing jQuery
jQuery(document).ready(function($) {
    "use strict"; // Start of use strict
	
	$('body').on('click', '.datepicker', function() {
		$(this).datepicker({
			showButtonPanel: true,
			dateFormat: 'yy-mm-dd'
		});
		$(this).datepicker('show');
	});
	$('#tabs').responsiveTabs({
		startCollapsed: 'accordion',
		animation: 'slide',
		duration: 500,
		scrollToAccordion: true
	});
	
	/* ********** Dashboard ********** */
	// Update Panel Content
	$(document).on('click', '.timechange', function(e) {
		var admintype = $(this).data('admintype');
		var filter = $(this).data('filter');
		var page = $(this).data('page');
		var section = $(this).data('section');
		var postData = 'admintype=' + admintype + '&filter=' + filter + '&page=' + page + '&section=' + section;
		var ajaxUrl = 'pages/dashboard/ajax/' + section +'.php';
		$.ajax({
			type: 'POST',
			url: ajaxUrl,
			data: postData,
			dataType: 'json',
			async: true,
			cache: false,
			timeout: 10000
		})
		.done(function(data){ 
			if ((data.output.updateOP === 'update') && (data.output.updateString)) {
				if (data.output.updateTitle) {
					$('#' + data.output.section + ' .title-dynamic').html(data.output.updateTitle);	
				}
				$('#' + data.output.section + ' .content-dynamic').html(data.output.updateString);	
			}
		})
		.fail(function(xhr, status, error) { 
			var err = JSON.parse(xhr.responseText);
			console.log(err.Message);
		});
		e.preventDefault();
	});
			
	// To-Do Updates
	$('#todoForm').submit(function(e) {
		var ajaxUrl = '/admin/process/todo.php';
		var postData = $('#todoForm').serialize();
		var deleteItems = []; 
		if(confirm('Are you sure these items are complete?  They will be removed from your to-do list.')) {
			$.ajax({
				type: 'POST',
				url: ajaxUrl,
				data: postData,
				dataType: 'json',
				async: true,
				cache: false,
				timeout: 10000
			})
			.done(function(data){ 
				if ($.isArray(data.output.update)) {
					$.each(data.output.update, function(key, value){
						deleteItems.push('todo-' + value);
					});
				} else {
					deleteItems.push('todo-' + value);
				}
				$.each(deleteItems, function(key, value){
					var deleteme = '#' + value;
					$(deleteme).animate({ backgroundColor: '#fbc7c7' }, 'fast').animate({ opacity: 'hide' }, 'slow');
				});
			})
			.fail(function(xhr, status, error) { 
				var err = JSON.parse(xhr.responseText);
				console.log(err.Message);
			});
		}
		e.preventDefault();
	});
	
	// Data Tables
	$('.dataTable').DataTable({
		responsive: true,
		stateSave: true
	});
	$('#tabs').tabs( {
		'show': function(event, ui) {
			var table = $.fn.dataTable.fnTables(true);
			if ( table.length > 0 ) {
				$(table).dataTable().fnAdjustColumnSizing();
			}
		}
	});
	
	// Ongoing Fee Report
	$('#dateRange').hide();
	$('select#disp').change(function () {
		if ($('select#disp').val() == 'dateRange') {
			$('#dateRange').show();
		} else {
			$('#dateRange').hide();
		}
	});
	
	// Admin Scripts
	$(document).on('click', '.admin', function (e) {
		var modalID, adminType, adminAct, editID, page, section, infoLink, processLink;
		modalID = '#adminModal';
		adminType = $(this).data('admintype');
		adminAct = $(this).data('adminact');
		editID = $(this).data('editid');
		page = $(this).data('page');
		section = $(this).data('section');
		infoLink = 'admin/admin.modal.ajax.php';
		processLink = 'admin/process/' + adminType + '.php';
			
		//console.log('adminAct: ' + adminAct);
		//console.log('editID: ' + editID);
		//console.log('page: ' + page);
		//console.log('section: ' + section);
		
		if (adminAct === 'delete') {
			var ajaxUrl = 'admin/process/'+ adminType +'.php';
			var postData = 'adminType=' + adminType + '&op=' + adminAct + '&page=' + page + '&section=' + section + '&editID=' + editID;
			if(confirm('Are you would like to delete this ' + adminType + '?')) {
				$.ajax({
					type: 'POST',
					url: ajaxUrl,
					data: postData,
					dataType: 'json',
					async: true,
					cache: false,
					timeout: 10000
				})
				.done(function(data){ 
					if (data.output.updateOP === 'delete') {
						var updateItem = '';
						updateItem = updateItem  + '#' + data.output.update;
						//console.log(updateItem);
						$(updateItem).animate({ backgroundColor: '#fbc7c7' }, 'fast').animate({ opacity: 'hide' }, 'slow');
					}
				})
				.fail(function(xhr, status, error) { 
					var err = JSON.parse(xhr.responseText);
					console.log(err.Message);
				});
				e.preventDefault();
			}
			//return false;
		} else {
			// Populate the Modal Form
			postData = 'adminType=' + adminType + '&adminAct=' + adminAct + '&editID=' + editID + '&page=' + page + '&section=' + section;
			$.ajax({
				type: 'POST',
				url: infoLink,
				data: postData,
				dataType: 'json',
				async: true,
				cache: false,
				timeout: 10000
			})
			.done(function(content){ 
				$(modalID).show();
				$('#adminModal .modal-title').show().html(content.content.title);
				$('#adminModal #formContent').show().html(content.content.content);
			})
			.fail(function(xhr, status, error) { 
				var err = JSON.parse(xhr.responseText);
				console.log(err.Message);
			});
			
			// Slider Scripts
			$('.slider').each(function(i){
				$(this).slider({
					min: parseInt($(this).attr('min')),
					max: parseInt($(this).attr('max')),
					value: parseInt($(this).closest('td').find('input').val()),
					step: parseInt($(this).attr('inc')),
					slide: function( event, ui ) {
						$(this).closest('td').find('input').val(ui.value);
					}
				});
			});
				
			// Validate & Process the Modal Form
			$('#adminForm').validate({
				errorClass: 'formError',
				submitHandler: function() {
					//console.log('SUBMIT');
					adminType = $('input[name=adminType]').val();
					postData = $('#adminForm').serialize();
					processLink = 'admin/process/' + adminType + '.php';
					//console.log(processLink);
					//console.log(postData);
					$.ajax({
						type: 'POST',
						url: processLink,
						data: postData,
						dataType: 'json',
						async: true,
						cache: false,
						timeout: 10000
					})
					.done(function(data){ 
						//console.log('SUCCESS');
						//reload_notifications();
						$('.modal').modal('hide');
							if (data.output.update){
							var updateItem = '#' + data.output.update;
							var updateSection = '#' + data.output.section;
							if (data.output.updateOP === 'delete') {
								$(updateItem).animate({ backgroundColor: '#fbc7c7' }, 'fast').animate({ opacity: 'hide' }, 'slow');
							} else if (data.output.updateOP === 'add') {
								if (data.output.updateString) {
									$(updateSection).append(data.output.updateString).hide().fadeIn('slow');
								}
							} else if (data.output.updateOP === 'edit') {
								if (data.output.updateString) {
									$(updateItem).fadeOut('slow', function(){
										$(this).replaceWith(data.output.updateString);
										$(this).fadeIn('slow');
									});
								}
							}
						} else {
							//console.log('Not Finding It');
						}
					})
					.fail(function(xhr, status, error) { // if fail then getting message
						var err = JSON.parse(xhr.responseText);
						console.log(err.Message);
						$('.modal').modal('hide');
					});
					e.preventDefault();
				}
			});
		}
	});
	
	// Modal Scripts
	$(function () {
		$("#svcProvider").change(function() {
			var val = $(this).val();
			if(val === "NEW") {
				$("#newProvider").show();
			}
			else {
				$("#newProvider").hide();
			}
		});
	});

});