startComments();

//----------------------------------------------------------------


var comments = $('.mt-comments');

$('#btn-add').on('click',function(){
    $("#add-comments").modal("show");
});
$('#form-comment-add').submit(function(e) {
    e.preventDefault();
    $.ajax({
        url: url_comentario_post,
        type: "POST",
        data: $('#form-comment-add').serialize(),
        beforeSend: function () {
            App.blockUI({
                target: "#portlet-comments",
                message: "Cargando..."
            })
        },
        success: function (data) {
            swal("Listo", data.success, "success");
            startComments();
        },
        error: function () {
            swal("Error","No se actualizo la informacion", "error");
        },
        complete: function () {
            $("#add-comments").modal("hide");
            App.unblockUI("#portlet-comments");
        }
    });
});
$('#form-comment-edit').submit(function(e) {
    e.preventDefault();
    $.ajax({
        url: url_comentario_editar_post,
        type: "POST",
        data: $('#form-comment-edit').serialize(),
        beforeSend: function () {
            App.blockUI({
                target: "#portlet-comments",
                message: "Cargando..."
            })
        },
        success: function (data) {
            swal("Listo", data.success, "success");
            startComments();
        },
        error: function () {
            swal("Error","No se actualizo la informacion", "error");
        },
        complete: function () {
            $("#edit-comments").modal("hide");
            App.unblockUI("#portlet-comments");
        }
    });
});
$('.scroller').slimScroll({
    height: '500px'
});

//-----------------------------------------------------------------

function editComment(commentid){
    $.ajax({
        url: url_comentario_editar_get+"/"+commentid,
        type: "GET",
        beforeSend: function () {
            App.blockUI({
                target: "#portlet-comments",
                message: "Cargando..."
            })
        },
        success: function (data) {

            $("#txt-edit-id").val(data.result.id);
            $("#txt-comment").val(data.result.descripcion);
            $("#edit-comments").modal("show");

        },
        error: function () {
            swal("Error","No se encontró el mensaje", "error");
        },
        complete: function () {
            App.unblockUI("#portlet-comments");
        }
    });
};

function deleteComment(commentid){
    $.ajax({
        url: url_comentario_eliminar_get+"/"+commentid,
        type: "GET",
        beforeSend: function () {
            App.blockUI({
                target: "#portlet-comments",
                message: "Cargando..."
            })
        },
        success: function (data) {
            swal("Listo", data.success, "success");
            startComments();
        },
        error: function () {
            swal("Error","Ocurrió un error en el servidor", "error");
        },
        complete: function () {
            App.unblockUI("#portlet-comments");
        }
    });
};

function startComments(){
    $.ajax({
        url: url_comentario_get,
        type: "GET",
        data: {
            'id': id
        },
        beforeSend: function () {
            App.blockUI({
                target: "#portlet-comments",
                message: "Cargando..."
            })
        },
        success: function (data) {
            comments.empty();
            $.each(data.result,function(key,value){
                comments.append("<div class=\"mt-comment\">" +
                    "<div class=\"mt-comment-body\">" +
                    "   <div class=\"mt-comment-info\">" +
                    "       <span class=\"mt-comment-author\">"+value.nombre+"</span>" +
                    "       <span class=\"mt-comment-date\">"+value.fecha+"</span>" +
                    "   </div>" +
                    "   <div class=\"mt-comment-text\">"+ value.texto+"</div>" +
                    "   <div class=\"mt-comment-details\">" +
                    "       <ul class=\"mt-comment-actions\">" +
                    "           <li>" +
                    "               <button id=\"edit-comment\" onclick=\"editComment("+value.id+")\">Editar</button>" +
                    "           </li>" +
                    "           <li>" +
                    "               <button id=\"delete-comment\" onclick=\"deleteComment("+value.id+")\">Eliminar</button href=\"#\">" +
                    "           </li>" +
                    "       </ul>" +
                    "   </div>" +
                    "</div>" +
                    "</div>");

            });
        },
        error: function () {
            swal("Error","Ocurrio un error en el servidor", "error");
        },
        complete: function () {
            App.unblockUI("#portlet-comments");
        }
    });
}