$(document).ready(function(){
    $('body').on('click', '[data-ticket]', function () {
        arr = $(this).attr('data-ticket').split('::');
        loadContentTicket(arr[0],arr[1]);
        $("#ticket-list").hide();
        $("#ticket-open").show();
    });
    function loadContentTicket(idTicket,idModule) {
        $.ajax({
            type: 'POST',
            data: "moduleID=" + idModule + "&ticket_id="+idTicket,
            url: '/',
            cache: false,
            success: function ( response ) {

                try {

                    var object = jQuery.parseJSON( response );
                    if ( object.code == 0 )
                    {
                        if(object.statusid == 3)
                            $( ".message" ).hide();
                        else
                            $( ".message" ).show();

                        $( "input#id" ).val(object.id);
                        $( "p.title1" ).html(object.title);
                        $( "p.category" ).html(object.category);
                        $( "p.status" ).html(object.status);
                        setTimeout(function () {
                            $("#ticket-messages").html('<div class="chat-message">'
                            + '<div class="chat-message-body on-left" style="border-right: 2px solid #000000;  margin-right: 0px;">'
                            + '<span class="arrow" style="border-left: 5px solid #101010;"></span>'
                            + '<div class="sender  badge-info2">Main message</div>'
                            + '<div class="text">'
                            + object.task
                            + '</div>'
                            + '<div class="sender">' + object.date + '</div>'
                            + '</div>'
                            + '</div>');


                            if (object.comments != undefined) {
                                jQuery.each(object.comments, function (i, comments) {
                                    if (comments.type == 0) {
                                        $("#ticket-messages").append('<div class="chat-message">'
                                        + '<div class="chat-message-body on-left" style="border-right: 2px solid #000000;  margin-right: 0px;">'
                                        + '<span class="arrow" style="border-left: 5px solid #101010;"></span>'
                                        + '<div class="sender  badge-info2">' + comments.author + '</div>'
                                        + '<div class="text">'
                                        + comments.comment
                                        + '</div>'
                                        + '<div class="sender">' + comments.date + '</div>'
                                        + '</div>'
                                        + '</div>');
                                    } else {
                                        $("#ticket-messages").append('<div class="chat-message">'
                                        + '<div class="chat-message-body" style="border-left: 2px solid #000000;  margin-left: 0px;">'
                                        + '<span class="arrow" style="border-right: 5px solid #101010;"></span>'
                                        + '<div class="sender   badge-warning2">' + comments.author + '</div>'
                                        + '<div class="text">'
                                        + comments.comment
                                        + '</div>'
                                        + '<div class="sender">' + comments.date + '</div>'
                                        + '</div>'
                                        + '</div>');
                                    }
                                });
                            }


                        }, 200);



                        var view = $("#ticket-messages")[0];
                        view.scrollTop = view.scrollHeight;


                    }
                    else
                    {
                        $.notij(object.message + " #" + lot_id,{'type':'error'});
                    }
                }
                catch ( e ) {
                    $.notij(" ERROR: " + e ,{'type':'error'});
                }
            },
            error: function ( xhr, ajaxOptions, thrownError ) {
                $.notij("load page error" ,{'type':'error'});
            }
        });
    }





});
