function NuevoTarea() {
    $("#iconobotonguardar").html('<ons-icon spin icon="fa-spinner"></ons-icon>');
    let tarea = $("#pageTareaNueva #tarea").val();
    if (tarea != "") {
        $.ajax({
                method: "POST",
                url: "./includes/nuevatarea.php",
                data: { "tarea": tarea }
            })
            .done(function(msg) {
                $("#iconobotonguardar").html('<ons-icon icon="fa-save"></ons-icon>');
                if (msg >= 0) {
                    $("#listadonuevos").prepend('<ons-list-item><div class="center"><span class="list-item__title">' + $("#tarea").val() + '</span></div><div class="right"><ons-icon icon="fa-trash-alt"></ons-icon></div></ons-list-item>');
                    $("#pageListaCompraNuevo #producto").val("");
                    //$("#form_nuevo_producto").trigger('reset');
                    ons.notification.toast('<ons-icon style="color: green;" icon="fa-check"></ons-icon> Tarea guardada correctamente<button onclick="mitoast.hide()">Cerrar</button>', {
                        id: "mitoast",
                        timeout: 4000
                    });
                } else {
                    ons.notification.toast('<ons-icon style="color: red;" icon="fa-exclamation-triangle"></ons-icon> ERROR, algo ha fallado <button onclick="mitoast.hide()">Cerrar</button>', {
                        id: "mitoast",
                        timeout: 4000
                    });
                }

            });
    }
}

function leerTareas(fn = nohagona) {
    array = [];
    $.each(datos['tareas'], function(key, val) {
        array.push([key, val[0], val[1]]);
    });
    datos['tareas'] = array;
    fn();
}

function listadoTareas() {
    var listado = $("#listadotareas");
    listado.html("");
    listados = [];
    contador = 0;
    if (datos['tareas'].length > 0) {
        $.each(datos['tareas'], function(key, value) {
            var checked = '';
            if (value[2] == 0) {
                checked = 'checked';
            }

            listados[key] = '<ons-list-item class="color' + value[2] + '" id="tarea_' + key + '"><div class="left"><ons-checkbox ' + checked + ' onchange="estadoTarea(' + key + ')" input-id="check-' + key + '" on></ons-checkbox></div><div class="center"><span class="list-item__title">' + value[1] + '</span></div><div class="right" onclick="BorrarTareas(' + key + ')"><ons-icon icon="fa-trash-alt"></ons-icon></div></ons-list-item>';
            contador++;

        });
        if (contador == 0) {
            listados[0] = '<ons-list-item><div class="left"><ons-icon icon="fa-exclamation-circle" style="color:red" class="list-item__icon"></ons-icon></div><div class="center"> No hay tareas Pendientes...</div></ons-list-item>';
        }
        if (listados.length > 0) {
            $.each(listados, function(key, val) {
                listado.append(val);
            });
        }
    } else {
        $.each(listados, function(key, val) {
            listado.append(val);
        });
    }
}

function estadoTarea(key) {
    if (key >= 0) {
        llave = datos['tareas'][key][0];
        var estado = '0';
        if ($('#check-' + key).prop('checked') == false) {
            estado = 1;
        }

        $.ajax({
                method: "POST",
                url: "./includes/estadotarea.php",
                data: { "key": llave, "estado": estado }
            })
            .done(function(msg) {
                if (msg == 1) {
                    $("#tarea_" + key).removeClass().addClass("color" + estado);
                    ons.notification.toast('<ons-icon style="color: green;" icon="fa-check"></ons-icon> Operación guarda correctamente<button onclick="mitoast.hide()">Cerrar</button>', {
                        id: "mitoast",
                        timeout: 1000
                    });
                } else {
                    ons.notification.toast('<ons-icon style="color: red;" icon="fa-exclamation-triangle"></ons-icon> ERROR, algo ha fallado <button onclick="mitoast.hide()">Cerrar</button>', {
                        id: "mitoast",
                        timeout: 4000
                    });
                }

            });
    }
}

function BorrarTareas(key) {
    if (key >= 0) {
        llave = datos['tareas'][key][0];
        $.ajax({
                method: "POST",
                url: "./includes/borrartarea.php",
                data: { "key": llave }
            })
            .done(function(msg) {
                if (msg >= 1) {
                    datos['tareas'].splice(key, 1);
                    $("#tarea_" + key).hide('slow');
                    ons.notification.toast('<ons-icon style="color: green;" icon="fa-check"></ons-icon> Tarea Eliminada correctamente<button onclick="mitoast.hide()">Cerrar</button>', {
                        id: "mitoast",
                        timeout: 1000
                    });
                } else {
                    ons.notification.toast('<ons-icon style="color: red;" icon="fa-exclamation-triangle"></ons-icon> ERROR, algo ha fallado <button onclick="mitoast.hide()">Cerrar</button>', {
                        id: "mitoast",
                        timeout: 4000
                    });
                }

            });
    }
}