
function ShowQuestion() {
    var FeedBackdata = [

        { Dml_Indicator: "SQ" },
    ]
    
    
        objFeedback = JSON.stringify({ 'objFeedback': FeedBackdata });
   

    $.ajax({
        async: true,
        type: 'POST',
        url: 'Feedback_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: objFeedback,
        success: function (data) {

            var data2 = JSON.parse(data);
            if (data2.msg == "Success") {
                var data1 = data2.data;

                tr = $('<table id="example1" class="table table-striped" />');

                tr.append("<thead class='table-bg'><tr><th>Question</th><th>Rating</th></tr></thead><tbody id='tabledata'>");

                $.each(data1, function (i, emp) {

                    row_data = "<tr><td id=" + emp.IP_Question_ID + " class='Questions'>" + emp.IP_Question + "</td><td><div class='rating-stars text-center'><ul id='stars'><li class='star' title='Poor' data-value='1'><i class='fa fa-star fa-fw'></i></li><li class='star' title='Fair' data-value='2'><i class='fa fa-star fa-fw'></i></li><li class='star' title='Good' data-value='3'><i class='fa fa-star fa-fw'></i></li><li class='star' title='Excellent' data-value='4'><i class='fa fa-star fa-fw'></i></li><li class='star' title='WOW!!!' data-value='5'><i class='fa fa-star fa-fw'></i></li></ul></div></td></tr>";
                    tr.append(row_data);
                });
                tr.append("</tbody></table>");
                $('#tblQuestion').html(tr);
                LoadFeedBack();
            }
            else {
                console.log(data);
            }
        },
        error: function (data) {
            toastr.warning('some issue');
        }
    });
}

$(document).ready(function () {
    ShowQuestion();
});

$('#btnSubmit').click(function () {
    debugger;
    var objFeedback = [];
    $('#tabledata tr').each(function () {
        objFeedback.push({
            Question_ID: $(this).find('.Questions').attr('id'),
            Question: $(this).find('.Questions').html(),
            Rating_Count: $(this).find('#stars .selected').length,
            Comments: $('#comment').val(),
            Dml_Indicator: "I",
            IP_visit_number: $("#IPVisitReport_Id").val()

        })
    });
    

    objFeedback = JSON.stringify({ 'objFeedback': objFeedback });
    $.ajax({
        async: true,
        type: 'POST',
        url: 'Feedback_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: objFeedback,
        success: function (data) {
            //$("#stars").removeClass('selected');
            var data2 = JSON.parse(data);
            if (data2.msg == "Success") {
                var data1 = data2.data;
                var ChkForm = CheckFormRights($("#IPPatient_Id").val(), 106, 1, 2);
                $.when(ChkForm)
          .then(function (data3) {
              var data2 = JSON.parse(data3);     
              if (jQuery.isEmptyObject((data2.data))) {
                  feedback_submit()
              }
              else {
                  debugger;
                  if (data2.data.Table[0].StringMsg != "Rights") {
                      feedback_submit()
                  }
              }
          });
            }
            else {
                console.log(data);
            }
            
        },
        error: function (data) {
        }
    });
});


function feedback_submit() {
    toastr.success('Thank You for your feedback <br/>  <br/> <a type="button" href="/Ipmanagement/ippatientdetails?Patient_Id=' + $("#IPPatient_Id").val() + '&IPVisitReport_Id=' + $("#IPVisitReport_Id").val() + '&IPDischarge_Id=' + $("#IPDischarge_Id").val() + '&branch_id=' + $("#branch_id").val() + '" class="btn btn-xs btn-success">ok</a>');
}


/* 2. Action to perform on click */
    $(document).on('click', '#stars li', function () {
    var onStar = parseInt($(this).data('value'), 10); // The star currently selected
    var stars = $(this).parent().children('li.star');

    for (i = 0; i < stars.length; i++) {
        $(stars[i]).removeClass('selected');
    }

    for (i = 0; i < onStar; i++) {
        $(stars[i]).addClass('selected');
    }

});

    function LoadFeedBack() {
        var FeedBackdata = [

            { IP_visit_number: $("#IPVisitReport_Id").val() },
        ]


        objFeedback = JSON.stringify({ 'objFeedback': FeedBackdata });


        $.ajax({
            async: true,
            type: 'POST',
            url: 'Feedback_Ins_Upd',
            contentType: 'application/json; charset=UTF-8',
            data: objFeedback,
            success: function (data) {
                var data2 = JSON.parse(data);
                if (jQuery.isEmptyObject((data2.data)))
                {

                }
                else
                {


                    if (data2.msg == "Success") {
                        var data1 = data2.data;

                        var arrayIndex = 0;
                        $('#tabledata tr').each(function () {
                            $('#comment').val(data1[arrayIndex].IP_Question_Comment);
                            var onStar = parseInt(data1[arrayIndex].IP_Rating);
                            console.log(onStar);
                            var liElements = $(this).find('li.star');
                            for (i = 0; i < onStar; i++) {
                                $(liElements[i]).addClass('selected');
                            }
                            arrayIndex++;
                        });
                    }
                    else {
                        console.log(data);
                    }

                }
            },
            error: function (data) {
            }
        });
    }
