//TransferOrder Screen Js//

$(document).ready(function () {

    if (parseInt($('#HdnIs_OTBed').val()) == 0) {
        $('#div_FromBed').show();
        $('#div_shedule').hide();
    }
    else {
        $('#div_FromBed').hide();
        $('#div_shedule').show();
    }

    WardNo();
    LoadWardCategory();
    LoadOT_Schedule();
    
});

function WardNo() {

    var Warddata = {
        Dml_Indicator: "AWL",
        branchid: $('#hdnbranch_id').val()
    }

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

            }
            else {

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

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

                        WardNo = WardNo + '<option value="' + emp.wardid + '">' + emp.ward_name + '</option>';

                    });
                    $('#ddFrom').append(default_opt + WardNo);
                    //$('#ddFrom').val($('#IPwardcategory').val());
                    ShowTransferOrder();
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

function LoadWardCategory() {

    var DMl_Flag = "";

    if (parseInt($('#HdnIs_OTBed').val()) == 2) {
        DMl_Flag = "PROTW";
    }
    else if (parseInt($('#HdnIs_OTBed').val()) == 3) {
        DMl_Flag = "PSOTW";
    }
    else {
        DMl_Flag = "WCD";
    }

    var Warddata = {
        //Dml_Indicator: "WCD",
        //ward_no: $("#ddTo").val(),  // category type id
        Dml_Indicator: DMl_Flag,
        branchid: $('#hdnbranch_id').val(),
        IP_visit_report_id: $('#IPVisitReportId').val()
    }

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

            }
            else {

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

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

                        wardno = wardno + '<option value="' + emp.wardid + '">' + emp.ward_name + '</option>';

                    });
                    $('#ddTo').append(default_opt + wardno);
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

//$('#ddTo').change(function () {

//    if ($('#ddTo').val() == 0 || $('#ddTo').val() == null || $('#ddTo').val() == "") {
//        return false;
//    }

   
//    var Warddata = {
//        Dml_Indicator: "WCD",
//        ward_no: $("#ddTo").val(),  // category type id
//        branchid: $('#hdnbranch_id').val()
//    }

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

//            }
//            else {

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

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

//                        wardno = wardno + '<option value="' + emp.wardid + '">' + emp.ward_id + "/" + emp.ward_name + '</option>';

//                    });
//                    $('#ddWard').append(default_opt + wardno);
//                }
//                else {
//                    console.log(data);
//                }
//            }
//        }
//    }); 
//});


$('#ddTo').change(function () {
   
    if ($('#ddTo').val() == 0)
    {
        $('#ddBedNo').empty();
       return false;
    }

    var Beddata = {
        Dml_Indicator: "BN",
        ward_no: $("#ddTo").val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/AdmitPatient_Ins_Upd_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Beddata),
        success: function (data) {
            var data2 = JSON.parse(data);
            $('#ddBedNo').empty();
            if (jQuery.isEmptyObject((data2.data))) {
                toastr.warning('No beds available for this ward, Please select another Ward');
            }
            else {

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

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

                        BedName = BedName + '<option value="' + emp.bedid + '">' + emp.Bed_Name + '</option>';

                    });
                    $('#ddBedNo').append(default_opt + BedName);
                }
                else {
                    console.log(data);
                }
            }
        }
    });
});

function ShowTransferOrder() {
    debugger;
    var Transferdata = {
        Dml_Indicator: "S",
        visit_report_id: $('#IPVisitReportId').val(),
        is_OTBed: $('#HdnIs_OTBed').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/TransferOrder_Ins_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Transferdata),
        success: function (data) {
            debugger;
            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><tr><th>Patient Name</th><th>From</th><th>Transfer To</th><th>Transfer Date Time</th><th>Bed No</th><th>Received By</th><th>Notes</th><th>Time Spend</th><th>Transfered by</th></tr></thead><tbody>");

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

                    row_data = "<tr><td>" + emp.first_name + "</td><td>" + emp.ip_From + "</td><td>" + emp.ip_to + "</td><td>" + emp.transfer_date_time + "</td><td>" + emp.bed_id + "</td><td>" + emp.ip_recived_by + "</td><td>" + emp.ip_nodes + "</td><td>" + emp.timespend + "</td><td>" + emp.created_by + "</td></tr>";
                    tr.append(row_data);

                    $('#HLastTrans_Date').val(emp.TransferDate);
                    $('#HLastTrans_time').val(emp.TransferTime);
                });
                tr.append("</tbody></table>");
                $('#tblTransferOrder').html(tr);

                $('#ddFrom').val(data1[0].wardid);
            }
            else {
                console.log(data);
            }
        },
        error: function (data) {
            toastr.warning('some issue');
        }
    });
}

$('#btnSubmit').click(function () {
    if ($("#IPDischarge_Id").val() != "2")
    {
        debugger;
        if (parseInt($("#IPDischarge_Id").val()) == 1)
        {
            toastr.warning('The patient already Initiated Discharge.So you do not Transfer this patient.', {});
            return false;
        }

        if (parseInt($('#HdnIs_OTBed').val()) == 0) {

            if ($.trim($('#ddFrom').val()) == 0) {
                $('#PddFrom').show();
                $('#PddFrom').fadeOut(8000);
                $('#PddFrom').html('Select From');
                $('#ddFrom').focus();
                return false;
            }

        }

        if (parseInt($('#HdnIs_OTBed').val()) != 0) {

            if ($.trim($('#dd_shedule').val()) == 0) {
                $('#Pdd_shedule').show();
                $('#Pdd_shedule').fadeOut(8000);
                $('#Pdd_shedule').html('Select Shedule');
                $('#dd_shedule').focus();
                return false;
            }

        }

        
        if ($.trim($('#ddTo').val()) == 0) {
            $('#PddTo').show();
            $('#PddTo').fadeOut(8000);
            $('#PddTo').html('Select To');
            $('#ddTo').focus();
            return false;
        }

        //if ($.trim($('#ddWard').val()) == 0) {
        //    $('#PddWard').show();
        //    $('#PddWard').fadeOut(8000);
        //    $('#PddWard').html('Select Ward');
        //    $('#ddWard').focus();
        //    $('#ddWard').val(0);
        //    return false;
        //}

        if ($.trim($('#ddBedNo').val()) == 0) {
            $('#PddBedNo').show();
            $('#PddBedNo').fadeOut(8000);
            $('#PddBedNo').html('Select Bed');
            $('#ddBedNo').focus();
            return false;
        }

        if ($.trim($('#txtTransferdate').val()) =="") {
            $('#PtxtTransferdate').show();
            $('#PtxtTransferdate').fadeOut(8000);
            $('#PtxtTransferdate').html('Select Transfer Date');
            $('#txtTransferdate').focus();
            return false;
        }

        if ($.trim($('#ddRecivedBy').val()) == '') {
            $('#PddRecivedBy').show();
            $('#PddRecivedBy').fadeOut(8000);
            $('#PddRecivedBy').html('Enter Recived By');
            $('#ddRecivedBy').focus();
            return false;
        }

        var LDT = $('#txtTransferdate').val();
        if (LDT == '') {
            LDT = '';
        }
        else {
            var a = LDT.split('/');
            LDT = a[2] + '-' + a[1] + '-' + a[0];
        }

        var time = $("#txtTransfertime").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;

        if ($.trim(LDT) == $("#HLastTrans_Date").val()) {
            
            if ($.trim(LTM) <= $("#HLastTrans_time").val()) {
                $('#PtxtTransfertime').show();
                $('#PtxtTransfertime').fadeOut(8000);
                $('#PtxtTransfertime').html('The date and time is not lesser than previous transfer date and time');
                $('#txtTransfertime').focus();
                //$('#txtTransfertime').val('');
                return false;
            }

        }

        var DML = "";

        if (parseInt($('#HdnIs_OTBed').val()) == 0) {

            DML = "I";
        }
        else {
            DML = "OTI";
        }

        var transferDateTime = LDT + " " + LTM;
        var Transferdata = {
            ip_From: $('#ddFrom').val(),
            ip_to: $('#ddTo').val(),
            ip_reason: $('#txtReason').val(),
            ip_recived_by: $('#ddRecivedBy').val(),
            //Dml_Indicator: "I",
            Dml_Indicator: DML,
            visit_report_id: $('#IPVisitReportId').val(),
            patient_id: $('#IPPatient_Id').val(),
            bed_id: $('#ddBedNo').val(),
            transfer_date_time: transferDateTime,
            is_OTBed: $('#HdnIs_OTBed').val(),
            Schedule_id: $('#dd_shedule').val()
            //wardid: $('#ddWard').val()
        }

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

                toastr.success('Transfer Order Details Added Successfully',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
 
                Transfer_clear();

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

            }
        });
    }
    
});

function Transfer_clear()
{
    $('#ddFrom').val($('#ddTo').val());
    $('#ddTo').val(0);
    $('#ddBedNo').empty();
    $('#txtTransferdate').val('');
    $('#ddRecivedBy').val('');
    $('#txtReason').val('');

    LoadWardCategory();
}

// OT Schedule Start //

function LoadOT_Schedule() {

    var ScheduleData = {
        Dml_Indicator: "OTSHN",
        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(ScheduleData),
        success: function (data) {
            debugger;
            $('#dd_shedule').empty();
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

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

                    var data1 = data2.data;

                    var shedule;

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

                        shedule = shedule + '<option value="' + emp.id + '">' + emp.ip_operation_name + '</option>';

                    });
                    $('#dd_shedule').append(default_opt + shedule);
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

