// DiabeticChart JS //

$(document).ready(function () {
    ShowDiabetic();

});

function ShowDiabetic() {
    debugger;

    //var time = $("#txtLogtime").val();
    //var hours = Number(time.match(/^(\d+)/)[1]);
    //var minutes = Number(time.match(/:(\d+)/)[1]);
    //var AMPM = time.match(/\s(.*)$/)[1];
    //if (AMPM == "PM" && hours < 12) hours = hours + 12;
    //if (AMPM == "AM" && hours == 12) hours = hours - 12;
    //var sHours = hours.toString();
    //var sMinutes = minutes.toString();
    //if (hours < 10) sHours = "0" + sHours;
    //if (minutes < 10) sMinutes = "0" + sMinutes;
    //var LTM = sHours + ":" + sMinutes;

    var Diabeticdata = {

        Dml_Indicator: "S",
        visit_report_id: $('#IPVisitReportId').val(),
       
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/Diabeticchart_Ins_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Diabeticdata),
        success: function (data) {

            var data2 = JSON.parse(data);
            if (data2.msg == "Success") {
                var data1 = data2.data;
                tr = $('<table id="example1" class="table table-responsive table-bordered table-striped dataTable" />');

                tr.append("<thead class='table-bg'><tr><th>Date</th><th>FBS</th><th>PPBS</th><th>RBS</th><th>BLBS</th><th>ALBS</th><th>BDBS</th><th>ADBS</th><th>3AM</th><th>Created By</th></tr></thead><tbody>");

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

                    row_data = "<tr><td>" + emp.created_on + " </td><td> " + emp.FBS + "</td><td>" + emp.PPBS + "</td><td>" + emp.RBS + "</td><td>" + emp.BLBS + "</td><td>" + emp.ALBS + "</td><td>" + emp.BDBS + "</td><td>" + emp.ADBS + "</td><td>" + emp.IP_3AM + "</td><td>" + emp.CreatedBy + "</td></tr>";
                    tr.append(row_data);
                });
                tr.append("</tbody></table>");
                $('#tblDiabeticchart').html(tr);
            }
            else {
                console.log(data);
            }
            
        },
        error: function (data) {
            toastr.warning('some issue');
        }
    });
}

$('#btnDBCAdd').click(function () {
    debugger;
    if ($('#txtFBS').val() == '' && $('#txtPPBS').val() == '' && $('#txtRBS').val() == '' && $('#txtBLBS').val() == '' && $('#txtALBS').val() == '' && $('#txtBDBS').val()=='' && $('#txtADBS').val() == '' && $('#txt3AM').val() == '')
    {
        toastr.warning('Enter Diabetic Values');
        return false;
    }


    var LDT = $('#txtLogdate').val();
    if (LDT == '') {
        LDT = '';
    }
    else {
        var a = LDT.split('/');
        LDT = a[2] + '-' + a[1] + '-' + a[0];
    }
    var time = $("#txtLogtime").val();
    var LTM = moment(time, 'hh:mm A').format('HH:mm');


    var Diabeticdata = {
        Dml_Indicator: "DIN",
        visit_report_id: $('#IPVisitReportId').val(),
        patient_id: $('#IPPatient_Id').val(),
        created_on: LDT + " " + LTM,

        patient_blood_sugar_value_1: $('#txtFBS').val(),
        patient_blood_sugar_unit_1: "1",
        patient_blood_sugar_type_1: "1",

        patient_blood_sugar_value_2: $('#txtPPBS').val(),
        patient_blood_sugar_unit_2: "1",
        patient_blood_sugar_type_2: "2",

        patient_blood_sugar_value_3: $('#txtRBS').val(),
        patient_blood_sugar_unit_3: "1",
        patient_blood_sugar_type_3: "3",

        patient_blood_sugar_value_4: $('#txtBLBS').val(),
        patient_blood_sugar_unit_4: "1",
        patient_blood_sugar_type_4: "4",

        patient_blood_sugar_value_5: $('#txtALBS').val(),
        patient_blood_sugar_unit_5: "1",
        patient_blood_sugar_type_5: "5",

        patient_blood_sugar_value_6: $('#txtBDBS').val(),
        patient_blood_sugar_unit_6: "1",
        patient_blood_sugar_type_6: "6",

        patient_blood_sugar_value_7: $('#txtADBS').val(),
        patient_blood_sugar_unit_7: "1",
        patient_blood_sugar_type_7: "7",

        patient_blood_sugar_value_8: $('#txt3AM').val(),
        patient_blood_sugar_unit_8: "1",
        patient_blood_sugar_type_8: "8",

    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/Diabeticchart_Ins_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Diabeticdata),
        success: function (data) {

            var data2 = JSON.parse(data);
            if (data2.msg == "Success") {
                var data1 = data2.data;
                toastr.success('Diabetic Chart Added Successfully')
                clearDiabetic();
                ShowDiabetic();

            }
            else {
                console.log(data);
            }
           
        },
        error: function (data) {
            toastr.warning('some issue');
        }
    });

});

function clearDiabetic() {

    $('#txtFBS').val(""),
    $('#txtPPBS').val(""),
    $('#txtBLBS').val(""),
    $('#txtBDBS').val(""),
    $('#txtADBS').val(""),
    $('#txt3AM').val(""),
    $('#txtRBS').val(""),
    $('#txtALBS').val("")
}