$(document).ready(function () {
    $('#Back_href').attr('href', '/Ipmanagement/ippatientdetails?Patient_Id=' + $('#IPPatient_Id').val() + '&IPVisitReport_Id=' + $('#IPVisitReport_Id').val() + '&IPDischarge_Id=' + $('#IPDischarge_Id').val() + '&branch_id=' + $('#branch_id').val() + '');
    $('#Screen_Heading').html('Pre OT Check List');
    Operation_Form();
});



function Operation_Form() {
    debugger;
    var MultiOperation = {
        Dml_Indicator: "OFB",
        surgery_request_id: $('#IPFormHead_Id').val(),
        IP_visit_report_id: $('#IPVisitReport_Id').val(),
        Role_id: $('#hiddenRole_id').val(),
        User_id: $('#hiddenUser_id').val(),
        patient_ID: $('#IPPatient_Id').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/AdmitPatient_Ins_Upd_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(MultiOperation),
        success: function (data) {
            $('#ddWardNo').empty();
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {

                    var data1 = data2.data;

                    var Form_Name;

                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {

                        Form_Name = Form_Name + '<option value="' + emp.Formid + '" data-id="' + emp.is_multiple + '">' + emp.Form_Name + '</option>';

                    });
                    $('#dd_FormName').append(default_opt + Form_Name);
                    $('#HdnPatientid').val(data1[0].patientid);
                    $('#HdnVisitid').val(data1[0].visit_reportid);
                }
                else {
                    console.log(data);
                }
            }
        }
    });

}




$('#dd_FormName').change(function () {
    debugger;

    var Is_multipleid = $('#dd_FormName option:selected').data('id');

    var Form_Data = {
        Dml_Indicator: "EDFRM",
        surgery_request_id: $('#dd_FormName').val(),
        IP_visit_report_id: $('#IPVisitReport_Id').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/AdmitPatient_Ins_Upd_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Form_Data),
        success: function (data) {
            $('#ddWardNo').empty();
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {

                    var data1 = data2.data;


                    //$('#HdnPatientid').val(data1[0].data_Count);

                    if (parseInt(data1[0].data_Count) > 0 && parseInt(Is_multipleid) != 1) {
                        $('#formShow').attr('src', $('#Hdformurl').val() + 'edit-form/' + data1[0].id + '');
                    }
                    else {
                        $('#formShow').attr('src', $('#Hdformurl').val() + 'form/' + $('#dd_FormName').val() + '?visit_reportid=' + btoa($('#HdnVisitid').val()) + '&patientid=' + btoa($('#HdnPatientid').val()) + '');
                    }
                }
                else {
                    console.log(data);
                }
            }
        }
    });
    $('#showData').empty();
    if (parseInt(Is_multipleid) == 1) {
        MultipleRowTable_Load();
    }

});




function MultipleRowTable_Load() {

    var Form_Data = {
        Dml_Indicator: "FMRT",
        surgery_request_id: $('#dd_FormName').val(),
        IP_visit_report_id: $('#IPVisitReport_Id').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/AdmitPatient_Ins_Upd_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Form_Data),
        success: function (data) {
            $('#showData').empty();
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

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

                    // EXTRACT VALUE FOR HTML HEADER. 
                    var col = [];
                    for (var i = 0; i < data.length; i++) {
                        for (var key in data[i]) {
                            if (col.indexOf(key) === -1) {
                                col.push(key);
                            }
                        }
                    }

                    // CREATE DYNAMIC TABLE.
                    var table = document.createElement("table");

                    // CREATE HTML TABLE HEADER ROW USING THE EXTRACTED HEADERS ABOVE.

                    var tr = table.insertRow(-1);                   // TABLE ROW.

                    for (var i = 0; i < col.length; i++) {
                        var th = document.createElement("th");      // TABLE HEADER.
                        th.innerHTML = col[i];
                        tr.appendChild(th);
                    }

                    // ADD JSON DATA TO THE TABLE AS ROWS.
                    for (var i = 0; i < data.length; i++) {

                        tr = table.insertRow(-1);

                        for (var j = 0; j < col.length; j++) {
                            var tabCell = tr.insertCell(-1);
                            tabCell.innerHTML = data[i][col[j]];
                        }
                    }

                    // FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER.
                    var divContainer = document.getElementById("showData");
                    divContainer.innerHTML = "";
                    divContainer.appendChild(table);
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}


function IPFormEdit(id) {
    if (id != 0 && id != '' && id != null) {
        $('#iframe_form').attr('src', 'https://formbuilder.mocerohealth.in/edit-form/' + id + '');
    }
}



function DeleteIPForm(id) {
    toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", 'Are You Sure, You Want to Delete?',
        {

            closeButton: true,
            progressBar: true,
            allowHtml: true,
            timeOut: 20000000,

            onShown: function (toast) {
                $("#confirmationRevertYes").click(function () {

                    var Form_Data = {
                        Dml_Indicator: "FBDEL",
                        surgery_request_id: id,
                        IP_visit_report_id: $('#IPVisitReport_Id').val()
                    }

                    $.ajax({
                        async: true,
                        type: 'POST',
                        url: '/Ipmanagement/AdmitPatient_Ins_Upd_Sel',
                        contentType: 'application/json; charset=UTF-8',
                        data: JSON.stringify(Form_Data),
                        success: function (data) {
                            $('#showData').empty();
                            var data2 = JSON.parse(data);
                            if (jQuery.isEmptyObject((data2.data))) {

                            }
                            else {

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

                                    toastr.success("Deleted Successfully");
                                    MultipleRowTable_Load();
                                }
                                else {
                                    console.log(data);
                                }
                            }
                        }
                    });
                });
            }
        })
}

$('#formSubmit').click(function () {
    OTSchedule_Update();
})

var iframe = document.getElementById('formShow');

document.getElementById('formSubmit').addEventListener('click', function () {
    debugger;

    // Send data to the Vue.js project within the iframe

    iframe.contentWindow.postMessage('*');

    // Trigger the submit button in the Vue.js project

    iframe.contentWindow.postMessage('trigger-submit', '*');

});;

function OTSchedule_Update() {

    var MultiOperation = {
        Dml_Indicator: "SHOTU",
        IP_visit_report_id: $('#IPVisitReport_Id').val(),
        patient_ID: $('#IPPatient_Id').val(),
        is_package: 3
    }

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

            }
            else {

                if (data2.msg == "Success") {

                    
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}