
$(document).ready(function() {
	$('#listamovimenti').DataTable({
		responsive: true,
		columnDefs: [{
			target: 0,
			render: DataTable.render.date(),
		}],
		order: [[0, "desc"]]
	});
	// $('.select2').select2();
});

$(".draggable").draggable();

var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var strDate = d.getFullYear() + '-' + (month < 10 ? '0' : '') + month + '-' + (day < 10 ? '0' : '') + day;

function fillSelect(url, selectName, selectedValue) {
	$(selectName).empty();
	$.getJSON(url, {}, function(items) {
		$.each(items, function(i, item) {
			var label = item.cat_name || item.tag_name || item.nomeConto;
			$(selectName).append(new Option(label, item.id));
		});
		if (selectedValue) {
			$(selectName).val(selectedValue).trigger('change');
		}
	});
}

function resetForm(formSelector, dateValue) {
	$(formSelector).find('input[type="text"], textarea, input[type="number"], input[type="date"], option').val('');
	$(formSelector).find('input[type="date"]').val(dateValue);
}

function openModal(tipo, actionUrl, catUrl, tagUrl, contoUrl, modalTitle) {
	resetForm('#form', strDate);
	$('#myModal').modal('show');
	$('.modal-title').text(modalTitle);
	$('#form').attr('action', actionUrl);
	fillSelect(catUrl, "select[name='mov_fk_categoria']");
	fillSelect(tagUrl, "select[name='mov_fk_tags']");
	fillSelect(contoUrl, "select[name='conto_id_a']");
    fillSelect(contoUrl, "select[name='conto_id_da']");
}


$(document).on('click', '.open_modal_spesa', function() {
	openModal('spesa', '/admin/movimenti/spesa', '/admin/service/catlistSpesa', '/admin/service/taglist', '/admin/service/contolist', 'Nuovo movimento in uscita');
	//$('#importo').attr('name', 'mov_importo');
});

$(document).on('click', '.open_modal_entrata', function() {
	openModal('entrata', '/admin/movimenti/entrata', '/admin/service/catlistEntrata', '/admin/service/taglist', '/admin/service/contolist', 'Nuovo movimento in entrata');
	//$('#importo').attr('name', 'mov_importo');
});


$(document).on('click', '.open_modal_modifica', function() {
	var riga_id = $(this).val();
	$.getJSON('/admin/movimenti/modify/' + riga_id, function(data) {
		resetForm('#form', data[0].mov_data.substring(0, 10));
		fillSelect('/admin/service/catlist', "select[name='mov_fk_categoria']", data[0].cat_name);
		fillSelect('/admin/service/taglist', "select[name='mov_fk_tags']", data[0].tag_name);
		fillSelect('/admin/service/contolist', "select[name='conto_id_da']", data[0].nomeConto);
        fillSelect('/admin/service/contolist', "select[name='conto_id_a']", data[0].nomeConto);
		$('#descrizione').val(data[0].mov_descrizione);

		// Imposta importo e name in base a DARE/AVERE
		if (data[0].mov_importo_dare && parseFloat(data[0].mov_importo_dare) !== 0) {
			$('#importo').val(data[0].mov_importo_dare);
			$('#importo').attr('name', 'mov_importo_dare');
		} else if (data[0].mov_importo_avere && parseFloat(data[0].mov_importo_avere) !== 0) {
			$('#importo').val(data[0].mov_importo_avere);
			$('#importo').attr('name', 'mov_importo_avere');
		} else {
			$('#importo').val('');
			$('#importo').attr('name', 'mov_importo');
		}

		$('.modal-title').text('Modifica movimento');
		$('#form').attr('action', '/admin/movimenti/modify');
		$('#form').append('<input type="hidden" name="id" value="' + riga_id + '">');
		$('#myModal').modal('show');
	});
});
