$(document).ready(function () {

    if ($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5) {
        $('#SelDoctor_div').hide();
        $('#divSelectDoctor').hide();
        $('#SelBranch_div').hide();
        $('#divSelectBranch').hide();
        casesheet_addedlist();
    }
    //doctorlist();
    LoadBranch();
    check_previlige_tabs(20,'CC_V1');

});
function check_previlige_tabs(formid,tab_id) {
    debugger;
    var RightData = {
        Stringtype: formid,
        Dml_Indicator: "CTABR",
        tab_id: tab_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserRight/User_rights_privileges',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(RightData),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {

                }
                else {
                    var j = 0

                    //$.each(data1.Table, function (i, emp1) {
                    //    $("[data-id=" + emp1.tab_id + "]").removeClass('hidden');
                    //})

                    $.each(data1.Table1, function (i, emp) {
                        if (emp.status == 0) {
                            $("[data-id=" + emp.role_rights_view + "]").addClass('hidden');
                            $("[data-id=" + emp.role_rights_view + "]").removeClass('active');
                        }
                        else if (emp.status == 1) {
                            if (j == 0) {
                                $("[data-id=" + emp.role_rights_view + "]").removeClass('hidden');
                                $("[data-id=" + emp.role_rights_view + "]").addClass('active');
                                $('#' + emp.tab_name).click();
                            }
                            j = j + 1;

                        }
                    });
                }
            }
        }
    });
}

var isTextChanged = false;
$('#SelDoctor1').change(function () {
    $('#selCasesheetSection').empty();
    $('#TabDetails').empty();
    $('#TabDescDetails').empty();
    LoadDoctorCasesheetList();
});

$('#id_app').click(function () {
    LoadDoctorCasesheetList();
});

function LoadDoctorCasesheetList() {
    var doctor_id = "";
    if ($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5) {
        doctor_id = $('#HiddenDoctorID').val();
    }
    else {
        doctor_id = $('#SelDoctor1').val();
    }

    var DoctorData = {
        Dml_Indicator: "DD",
        doctor_id: doctor_id,
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Prescription_Settings/casesheet_configurations',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data = JSON.parse(data)
            if (data.msg == "Success") {
                var data = data.data;
                var default_opt = '<option value="0">--Select--</option><option value="1">Patient Overview</option><option value="14">Prescription</option>';
                if (jQuery.isEmptyObject((data))) {
                    $('#selCasesheetTabs').empty();
                    $('#selCasesheetSection').empty();
                    $('#TabDetails').empty();
                    $('#TabDescDetails').empty();
                    $('#selCasesheetTabs').html(default_opt);
                }
                else {
                    debugger;

                    tr = ''; var SelTab = "";
                    
                    $('#selCasesheetTabs').empty();
                    $('#selCasesheetSection').empty();
                    $('#TabDetails').empty();
                    $('#TabDescDetails').empty();
                    if (data.Table.length > 0) {
                        for (var j = 0; j < data.Table.length; j++) {
                            SelTab = SelTab + '<option value="' + data.Table[j].casesheet_module_id + '">' + data.Table[j].tap_name + '</option>';
                        }
                        $('#selCasesheetTabs').html(default_opt + SelTab);
                    }
                    else {
                        $('#selCasesheetTabs').html(default_opt);
                    }
                }

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

function LoadCasesheetType() {
    debugger;
    var casesheetConfig = {
        Dml_Indicator: 'HS'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CaseStudy/Casesheet_Configuration',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(casesheetConfig),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {

                }
                else {
                    debugger;

                    $('#selCasesheetType').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    var SelType = "";
                    if (data1 != null) {
                        if (data1.Table.length > 0) {

                            for (var i = 0; i < data1.Table.length; i++) {
                                SelType = SelType + '<option value="' + data1.Table[i].casesheet_type_id + '">' + data1.Table[i].casesheet_type_name + '</option>';
                            }
                            $('#selCasesheetType').html(default_opt + SelType);


                            //set default casesheet type value if exists
                            var casesheetdetails = getCookie("casesheetdetails");

                            casesheetdetails = casesheetdetails.split('=');
                            var temp3 = casesheetdetails[1];
                            var casesheetType = temp3.split(',');
                            var casesheet_type_id = casesheetType[0];

                            if (casesheet_type_id != null && casesheet_type_id != "") {
                                $('#selCasesheetType').val(casesheet_type_id);
                                $('#selCasesheetType').prop('disabled', true);
                            }

                        }

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

function getCookie(cname) {
    console.log(cname);
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    //var decodedCookie = decodeURIComponent(unescape(unescape(s)));
    var ca = decodedCookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

$('#SelBranch').change(function () {
    doctorlist();
});

function LoadBranch() {
    $('#SelBranch').empty();
    $('#SelBranch1').empty();
    var subdepartment = {
        Dml_Indicator: "LBRAN"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });
                    $('#SelBranch').append(default_opt + SubDepartment);
                    $('#SelBranch1').append(default_opt + SubDepartment);

                    if ($('#hiddenRole_id').val() != 2) {
                        $('#SelBranch').val($('#HospitalBranchID').val());
                        $('#SelBranch').attr('disabled', true);
                        doctorlist();

                        $('#SelBranch1').val($('#HospitalBranchID').val());
                        $('#SelBranch1').attr('disabled', true);
                        doctorlist1();
                    }
                }
            }
            else {
                console.log(data1);
            }

        }
    });
}

$('#SelBranch1').change(function () {
    doctorlist1();
});

function doctorlist() {
    var DoctorData = {
        //Dml_Indicator: "DL",
        Dml_Indicator: "PDL",
        user_role: $('#hiddenRole_id').val(),
        doctor_id: $('#SelDoctor').val(),
        hospital_id: $('#SelHospital').val(),
        branch_id: $('#SelBranch').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Prescription_Settings/casesheet_configurations',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data1 = JSON.parse(data)
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    var tr = '';
                    var SelRole = '';
                    $('#SelDoctor').empty();

                    var default_opt = '<option value="0">--Select--</option>';
                    var j = 0;
                    debugger;
                    if (data1.Table.length > 0) {
                        for (var i = 0; i < data1.Table.length; i++) {
                            j++;
                            SelRole = SelRole + '<option value="' + data1.Table[i].doctor_id + '">' + data1.Table[i].doctor_name + '</option>';
                        }
                        $('#SelDoctor').html(default_opt + SelRole);


                    }
                    var module_id = '';
                    if (data1.Table1.length > 0) {
                        for (var i = 0; i < data1.Table1.length; i++) {
                            j++;
                            module_id = module_id + '<option value="' + data1.Table1[i].casesheet_module_id + '">' + data1.Table1[i].tap_name + '</option>';
                        }
                        $('#uscslistmenus').html(default_opt + module_id);
                    }

                    $('#csmenusortable').empty();

                }


                //LoadDoctorCasesheetList();
            }
            else {
                console.log(data1);
            }

        }
    });
}

function doctorlist1() {
    var roleId = $('#hiddenRole_id').val();
    var DoctorId = $('#HiddenDoctorID').val();
    if (roleId != "1" && roleId != "3") {
        var DoctorData = {
            Dml_Indicator: "DL",
            hospital_id: $('#SelHospital').val(),
            branch_id: $('#SelBranch1').val()
        }
    }
    else {
        var DoctorData = {
            Dml_Indicator: "DO",
            doctor_id: DoctorId,
        }
    }
    //var DoctorData = {
    //    Dml_Indicator: "DL",
    //    user_role: $('#hiddenRole_id').val(),
    //    doctor_id: $('#SelDoctor').val(),
    //}
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Prescription_Settings/casesheet_configurations',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data1 = JSON.parse(data)
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    var tr = '';
                    var SelRole = '';

                    $('#SelDoctor1').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    var j = 0;
                    debugger;
                    if (data1.Table.length > 0) {
                        for (var i = 0; i < data1.Table.length; i++) {
                            j++;
                            SelRole = SelRole + '<option value="' + data1.Table[i].doctor_id + '">' + data1.Table[i].doctor_name + '</option>';
                        }
                        $('#SelDoctor1').html(default_opt + SelRole);

                    }

                }

                LoadDoctorCasesheetList();
            }
            else {
                console.log(data1);
            }

        }
    });
}

$('#SelDoctor').change(function () {
    var DoctorData = {
        Dml_Indicator: "LS",
        doctor_id: $('#SelDoctor').val(),
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Prescription_Settings/casesheet_configurations',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data = JSON.parse(data)
            if (data.msg == "Success") {
                var data = data.data;
                if (jQuery.isEmptyObject((data))) {
                    $('#csmenusortable').empty();
                }
                else {

                    tr = '';
                    $('#csmenusortable').empty();
                    if (data.Table.length > 0) {
                        for (var i = 0; i < data.Table.length; i++) {
                            tr = tr + '<li class="ui-state-default" id=' + data.Table[i].casesheet_module_id + '> ' + data.Table[i].tap_name + ' <span class="us-list-cs-remove delete btn btn-xs btn-delete  btn-sm btn-add" data-id="CSC_D1" data-type="role_rights_delete" data-nav="sp_div" data-name=""><i class="fa fa-trash" aria-hidden="true"></i></span></li>';
                        }
                    }
                    $('#csmenusortable').append(tr);

                    $('.us-list-cs-remove i').click(function () {
                        debugger;
                        var id = $(this).closest('li').attr('id');
                        $(this).closest('li').remove();
                        var doctor_id = $('#SelDoctor').val();
                        if ($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5) {
                            doctor_id = $('#HiddenDoctorID').val();
                        }
                        var DoctorData = {
                            Dml_Indicator: "D",
                            casesheet_module_id: id,
                            doctor_id: doctor_id
                        }
                        $.ajax({
                            async: true,
                            type: 'POST',
                            url: '/Prescription_Settings/casesheet_configurations',
                            contentType: 'application/json; charset=UTF-8',
                            data: JSON.stringify(DoctorData),
                            success: function (data) {
                                var data = JSON.parse(data);
                                casesheet_addedlist();
                                if (data.msg == "Success") {
                                    var data = data.data;
                                    if (jQuery.isEmptyObject((data))) {
                                    }
                                    else {

                                        if (data.Table.length > 0) {
                                        }
                                    }
                                }
                                else {
                                    console.log(data);
                                }
                            }
                        });
                    });
                }
            }
            else {
                console.log(data);
            }
        }
    });

});


function casesheet_addedlist() {
    var doctor_id = $('#SelDoctor').val();
    if ($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5) {
        doctor_id = $('#HiddenDoctorID').val();
    }

    var DoctorData = {
        Dml_Indicator: "LS",
        doctor_id: doctor_id,
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Prescription_Settings/casesheet_configurations',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data = JSON.parse(data)
            if (data.msg == "Success") {
                var data = data.data;
                if (jQuery.isEmptyObject((data))) {
                    $('#csmenusortable').empty();
                }
                else {

                    tr = ''; var SelTab = "";
                    var default_opt = '<option value="0">--Select--</option><option value="1">Patient Overview</option><option value="14">Prescription</option>';
                    $('#csmenusortable').empty();
                    if (data.Table.length > 0) {
                        for (var i = 0; i < data.Table.length; i++) {
                            tr = tr + '<li class="ui-state-default" id=' + data.Table[i].casesheet_module_id + '> ' + data.Table[i].tap_name + ' <span class="us-list-cs-remove delete btn btn-xs btn-delete  btn-sm btn-add" data-id="CSC_D1" data-type="role_rights_delete" data-nav="sp_div" data-name=""><i class="fa fa-trash" aria-hidden="true"></i></span></li>';

                        }
                    }

                    //if (data.Table1.length > 0)
                    //{
                    //    for (var j = 0; j < data.Table1.length; j++)
                    //    {
                    //        SelTab = SelTab + '<option value="' + data.Table1[j].casesheet_module_id + '">' + data.Table1[j].tap_name + '</option>';
                    //    }
                    //    $('#selCasesheetTabs').html(default_opt + SelTab);
                    //}

                    $('#csmenusortable').append(tr);
                    $('.us-list-cs-remove i').click(function () {
                        debugger;
                        var id = $(this).closest('li').attr('id');
                        $(this).closest('li').remove();
                        var doctor_id = $('#SelDoctor').val();
                        if ($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5) {
                            doctor_id = $('#HiddenDoctorID').val();
                        }
                        var DoctorData = {
                            Dml_Indicator: "D",
                            casesheet_module_id: id,
                            doctor_id: doctor_id
                        }
                        $.ajax({
                            async: true,
                            type: 'POST',
                            url: '/Prescription_Settings/casesheet_configurations',
                            contentType: 'application/json; charset=UTF-8',
                            data: JSON.stringify(DoctorData),
                            success: function (data) {
                                var data = JSON.parse(data)
                                if (data.msg == "Success") {
                                    //casesheet_addedlist();
                                    var data = data.data;
                                    if (jQuery.isEmptyObject((data))) {
                                    }
                                    else {

                                        if (data.Table.length > 0) {
                                        }
                                    }
                                }
                                else {
                                    console.log(data);
                                }
                            }
                        });
                    });

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

$(".us-list-cs-add").click(function () {
    debugger;
    var did = $(".us-list-cs-add").attr("data-id");
    var navigation = $(".us-list-cs-add").attr("data-nav");
    var name = $(".us-list-cs-add").attr("data-name");
    var columntype = $(".us-list-cs-add").attr("data-type");
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         debugger;
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {

                     return;
                 }
                 else {
                     if ($('#SelDoctor').val() == 0 && $('#hiddenRole_id').val() != 1 && $('#hiddenRole_id').val() != 5) {
                         $('#PSelDoctor').show();
                         $('#PSelDoctor').fadeOut(8000);
                         $('#PSelDoctor').html('Select the doctor');
                         $('#SelDoctor').focus();
                         return false;
                     }
                     if ($('#uscslistmenus').val() == 0 || $('#uscslistmenus').val() == null || $('#uscslistmenus').val() == "") {
                         $('#Puscslistmenus').show();
                         $('#Puscslistmenus').fadeOut(8000);
                         $('#Puscslistmenus').html('Select CaseSheet');
                         $('#uscslistmenus').focus();
                         return false;
                     }
                     var cslistmenutext = $("#uscslistmenus option:selected").text();
                     var cslistmenutext_id = $("#uscslistmenus").val();
                     var ListItems = $('#csmenusortable li').find(cslistmenutext);

                     const listItems = document.querySelectorAll('#csmenusortable li');
                     var k = 0;
                     for (let i = 0; i < listItems.length; i++) {
                         var a = listItems[i].textContent;
                         if ($.trim(a) == cslistmenutext) {
                             k = 1;
                         }
                         else {
                         }
                     }
                     if (k == 0) {
                         $('#csmenusortable').append('<li class="ui-state-default" id=' + cslistmenutext_id + '> ' + cslistmenutext + ' <span class="us-list-cs-remove delete btn btn-xs btn-delete  btn-sm btn-add" data-id="CSC_D1" data-type="role_rights_delete" data-nav="sp_div" data-name=""><i class="fa fa-trash" aria-hidden="true"></i></span></li>');
                         $('#Puscslistmenus').show();
                         $('#Puscslistmenus').fadeOut(8000);
                         $('#Puscslistmenus').html('Casesheet added successfully. Kindly save it.');
                         $('#Puscslistmenus').focus();
                     }
                     else {
                         $('#Puscslistmenus').show();
                         $('#Puscslistmenus').fadeOut(8000);
                         $('#Puscslistmenus').html('Casesheet already added');
                         $('#Puscslistmenus').focus();
                         k = 0;
                         return false;
                     }


                     $('.us-list-cs-remove i').click(function () {
                         debugger;
                         var id = $(this).closest('li').attr('id');
                         var did = $('.us-list-cs-remove').attr("data-id");
                         var navigation = $('.us-list-cs-remove').attr("data-nav");
                         var name = $('.us-list-cs-remove').attr("data-name");
                         var columntype = $('.us-list-cs-remove').attr("data-type");
                         var chkprevilige = check_previlige(did, navigation, name, columntype);
                         $.when(chkprevilige)
                          .then(function (data) {
                              debugger;
                              var data1 = JSON.parse(data);
                              if (data1.msg == "Success") {
                                  var data1 = data1.data;
                                  if (jQuery.isEmptyObject((data1))) {
                                  }
                                  else {
                                      var j = 0;
                                      var status = data1.Table[0].role_rights_add
                                      if (status == 0) {

                                          return;
                                      }
                                      else {
                                          //var id = $(this).closest('li').attr('id');
                                          //$(this).closest('li').remove();
                                          $('#' + id).remove();
                                          var doctor_id = $('#SelDoctor').val();
                                          if ($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5) {
                                              doctor_id = $('#HiddenDoctorID').val();
                                          }
                                          var DoctorData = {
                                              Dml_Indicator: "D",
                                              casesheet_module_id: id,
                                              doctor_id: doctor_id
                                          }
                                          $.ajax({
                                              async: true,
                                              type: 'POST',
                                              url: '/Prescription_Settings/casesheet_configurations',
                                              contentType: 'application/json; charset=UTF-8',
                                              data: JSON.stringify(DoctorData),
                                              success: function (data) {
                                                  var data = JSON.parse(data)
                                                  //casesheet_addedlist();
                                                  if (data.msg == "Success") {
                                                      var data = data.data;
                                                      if (jQuery.isEmptyObject((data))) {
                                                      }
                                                      else {

                                                          if (data.Table.length > 0) {
                                                          }
                                                      }
                                                  }
                                                  else {
                                                      console.log(data);
                                                  }
                                              }
                                          });
                                      }
                                  }
                              }
                          });
                     });
                 }
             }
         }
     });
});




function cslistorderSubmit() {
    debugger;
    var idsInOrder = []
    idsInOrder = $("#csmenusortable").sortable("toArray");
    //console.log(idsInOrder.length);
    //alert(idsInOrder.length);
    var doctor_id = $('#SelDoctor').val();
    if ($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5) {
        doctor_id = $('#HiddenDoctorID').val();
    }
    for (var i = 0; i < idsInOrder.length; i++) {
        var DoctorData = {
            Dml_Indicator: "I",
            casesheet_module_order: parseInt(i) + parseInt(1),
            casesheet_module_id: idsInOrder[i],
            doctor_id: doctor_id
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/Prescription_Settings/casesheet_configurations',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(DoctorData),
            success: function (data) {
                var data = JSON.parse(data);
                if (data.msg == "Success") {
                    if (jQuery.isEmptyObject((data))) {
                    }
                    else {
                    }
                }
                else {
                    console.log(data);
                }
            }
        });
    }
    setTimeout(function () { toastr.success('Saved Successfully'); }, 1000);

}



// casesheet configuration tab wise start

$('#selCasesheetTabs').change(function () {

    if ($('#selCasesheetType').val() == "" || $('#selCasesheetType').val() == null || $('#selCasesheetType').val() == 0) {
        $('#PselCasesheetType').show();
        $('#PselCasesheetType').fadeOut(8000);
        $('#PselCasesheetType').html('Select Casesheet Type');
        $('#selCasesheetType').focus();
        return false;
    }

    if ($('#selCasesheetTabs').val() == "" || $('#selCasesheetTabs').val() == null || $('#selCasesheetTabs').val() == 0) {
        $('#PselCasesheetTabs').show();
        $('#PselCasesheetTabs').fadeOut(8000);
        $('#PselCasesheetTabs').html('Select Casesheet Tab');
        $('#selCasesheetTabs').focus();
        return false;
    }

    $('#btnSubmitCasesheetDesc').hide();
    var doctor_id = "";
    if ($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5) {
        doctor_id = $('#HiddenDoctorID').val();
    }
    else {
        doctor_id = $('#SelDoctor1').val();
    }

    var casesheetConfig = {
        casesheet_type_id: $('#selCasesheetType').val(),
        casesheet_doctor_module_id: $('#selCasesheetTabs').val(),
        Dml_Indicator: 'SS',
        Doctor_id: doctor_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CaseStudy/Casesheet_Configuration',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(casesheetConfig),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {

                }
                else {
                    debugger;

                    $('#selCasesheetSection').empty();
                    $('#TabDetails').empty();
                    $('#TabDescDetails').empty();
                    isTextChanged = false;
                    var default_opt = '<option value="0">--Select--</option>';
                    var SelSection = "";
                    if (data1 != null) {
                        if (data1.Table.length > 0) {

                            for (var i = 0; i < data1.Table.length; i++) {
                                SelSection = SelSection + '<option value="' + data1.Table[i].casesheet_section_master_id + '">' + data1.Table[i].section_name + '</option>';
                            }
                            $('#selCasesheetSection').html(default_opt + SelSection);
                        }

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

var lastSel;
$("#selCasesheetSection").click(function () {
    lastSel = $("#selCasesheetSection").val();
});

$('#selCasesheetSection').change(function () {
    debugger;
    if ($('#selCasesheetType').val() == "" || $('#selCasesheetType').val() == null || $('#selCasesheetType').val() == 0) {
        $('#PselCasesheetType').show();
        $('#PselCasesheetType').fadeOut(8000);
        $('#PselCasesheetType').html('Select Casesheet Type');
        $('#selCasesheetType').focus();
        return false;
    }

    if ($('#selCasesheetTabs').val() == "" || $('#selCasesheetTabs').val() == null || $('#selCasesheetTabs').val() == 0) {
        $('#PselCasesheetTabs').show();
        $('#PselCasesheetTabs').fadeOut(8000);
        $('#PselCasesheetTabs').html('Select Casesheet Tab');
        $('#selCasesheetTabs').focus();
        return false;
    }

    if ($('#selCasesheetSection').val() == "" || $('#selCasesheetSection').val() == null || $('#selCasesheetSection').val() == 0) {
        $('#PselCasesheetSection').show();
        $('#PselCasesheetSection').fadeOut(8000);
        $('#PselCasesheetSection').html('Select Section');
        $('#selCasesheetSection').focus();
        $('#TabDetails').empty();
        $('#TabDescDetails').empty();
        $('#btnSubmitCasesheetDesc').hide();
        return false;
    }

    $('#btnSubmitCasesheetDesc').show();

    if (isTextChanged) {
        debugger;
        toastr.warning('Kindly Save Your Changes');
        $("#selCasesheetSection").val(lastSel);
        return false;
    }

    var doctor_id = "";
    if ($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5) {
        doctor_id = $('#HiddenDoctorID').val();
    }
    else {
        doctor_id = $('#SelDoctor1').val();
    }

    var casesheetConfig = {
        casesheet_type_id: $('#selCasesheetType').val(),
        casesheet_doctor_module_id: $('#selCasesheetTabs').val(),
        Dml_Indicator: 'SD',
        casesheet_section_id: $('#selCasesheetSection').val(),
        Doctor_id: doctor_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CaseStudy/Casesheet_Configuration',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(casesheetConfig),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {

                }
                else {
                    debugger;

                    var rowData = "";
                    var tr = "";
                    isTextChanged = false;
                    var tabDescData = "";
                    $('#TabDetails').empty();
                    $('#TabDescDetails').empty();

                    if (data1 != null) {
                        if (data1.Table.length > 0) {
                            rowData = '<div class="row"><div class="col-md-12 mt20"><div class="forms_height" style="display:inline-flex;"><h3 class="label-name mt5">' + data1.Table[0].section_name + '</h3>'
                            + '<div class="input-group ml-20 mt5"><div id="radioBtn" class="btn-group"><a class="btn btn-primary btn-sm not-active" id="ChkSectionName_Yes" data-toggle="ChkSectionName" data-title="1">YES</a><a class="btn btn-primary btn-sm not-active" id="ChkSectionName_No" data-toggle="ChkSectionName" data-title="0">NO</a></div><input type="hidden" name="ChkSectionName" id="ChkSectionName" onchange=ChangeSectionVisible();></div></div></div></div>';

                            tr = '<div class="row"><div class="col-md-12 label-name"><h4>Heading Caption Change</h4></div><div class="col-md-4"><label class="mt5">Heading Caption <span class="red">*</span></label><div class="forms_height"><input type="text" class="forms_field" id="SectionLabelName" value="' + data1.Table[0].label_name + '" onchange=ChangeInputs(); maxlength="50"></div><p style="color:red;" id="PSectionLabelName"></p></div></div>';

                            $('#txtSectionName').val(data1.Table[0].section_name);
                            $('#txtFieldName').val(data1.Table[0].field_name);
                            $('#txtLabelFieldName').val(data1.Table[0].label_field_name);
                        }

                        $('#TabDetails').append(rowData);

                        $('#txtTotalSectionDesc').val(data1.Table1.length);

                        if (data1.Table1.length > 0) {

                            var headStart = '<div class="row mt10"><div class="col-md-12 label-name"><h4>Options</h4></div></div><div class="row mt10">';
                            var headEnd = '</div>';
                            for (var i = 0; i < data1.Table1.length; i++) {
                                var option = '';
                                var tageditoroption = '';
                                if (data1.Table1[i].name == "Allergies") {
                                    option = 'hidden';
                                    tageditoroption = 'hidden';
                                }
                                else if (data1.Table1[i].name == "Past Medical History") {
                                    tageditoroption = 'hidden';
                                }
                                if ($('#txtFieldName').val() == "divObservation") {
                                    tabDescData += '<div class="col-md-6 col-sm-12"><div class="col-md-12 label-name"><h4>' + data1.Table1[i].name + '</h4></div><div class="col-md-12"><div class="col-sm-4"><div class="forms_height"><label class="mt5">Label Change</label><input type="text" class="forms_field" name="optionsLabel" id="Label_' + (i) + '" value="' + data1.Table1[i].label + '" onchange=ChangeInputs(); maxlength="50"></div><p class="error_msg" id="PLabel_' + (i) + '"></p></div>'
                                    + '<div class="col-sm-4" ' + option + '><label class="mt5">Editable</label><div class="forms_height">'
                                    + '<div id="radioBtn" class="btn-group"><a class="btn btn-primary btn-sm not-active" id="chkEditableYes_' + (i) + '" data-toggle="chkEditable_' + (i) + '" data-title="1">YES</a><a class="btn btn-primary btn-sm not-active" id="chkEditableNo_' + (i) + '" data-toggle="chkEditable_' + (i) + '" data-title="0">NO</a></div><input type="hidden" name="chkEditable" id="chkEditable_' + (i) + '" onchange=ChangeInputs();></div></div>'
                                    + '<div class="col-sm-4"><label class="mt5">Visible</label><div class="forms_height"><div id="radioBtn" class="btn-group"><a class="btn btn-primary btn-sm not-active" id="chkVisibleYes_' + (i) + '" data-toggle="chkVisible_' + (i) + '" data-title="1">YES</a><a class="btn btn-primary btn-sm not-active" id="chkVisibleNo_' + (i) + '" data-toggle="chkVisible_' + (i) + '" data-title="0">NO</a></div><input type="hidden" name="chkVisible" id="chkVisible_' + (i) + '" onchange=ChangeInputs();></div></div>'
                                    + '<div class="col-sm-4" ' + tageditoroption + '><div class="form-group"><label class="mt5">TagEditor</label><div class="input-group"><div id="radioBtn" class="btn-group"><a class="btn btn-primary btn-sm not-active" id="chkTagEditorYes_' + (i) + '" data-toggle="chkTagEditor_' + (i) + '" data-title="1">YES</a><a class="btn btn-primary btn-sm not-active" id="chkTagEditorNo_' + (i) + '" data-toggle="chkTagEditor_' + (i) + '" data-title="0">NO</a></div><input type="hidden" name="chkTagEditor" id="chkTagEditor_' + (i) + '" onchange=ChangeInputs();></div></div></div>'
                                    + '</div></div>'
                                    + '<input type="hidden" id="txtCasesheetSectionDescID_' + (i) + '" value="' + data1.Table1[i].casesheet_section_desc_id + '"/>'
                                    + '<input type="hidden" id="txtDesSectionName_' + (i) + '" value="' + data1.Table1[i].name + '"/>'
                                    + '<input type="hidden" id="txtDesFieldName_' + (i) + '" value="' + data1.Table1[i].field_name + '"/>'
                                    + '<input type="hidden" id="txtDesLabelFieldName_' + (i) + '" value="' + data1.Table1[i].label_field_name + '"/>'
                                }
                                else {
                                    tabDescData += '<div class="col-md-6 col-sm-12 padding0"><div class="col-md-12 label-name"><h4>' + data1.Table1[i].name + '</h4></div><div class="col-md-12"><div class="col-sm-4"><label class="">Label Change</label><div class="forms_height"><input type="text" class="forms_field" name="optionsLabel" id="Label_' + (i) + '" value="' + data1.Table1[i].label + '" onchange=ChangeInputs(); maxlength="50"></div><p class="error_msg" id="PLabel_' + (i) + '"></p></div><div class="col-sm-4"><label class="mt5">Editable</label><div class="forms_height">'
                                   + '<div id="radioBtn" class="btn-group"><a class="btn btn-primary btn-sm not-active" id="chkEditableYes_' + (i) + '" data-toggle="chkEditable_' + (i) + '" data-title="1">YES</a><a class="btn btn-primary btn-sm not-active" id="chkEditableNo_' + (i) + '" data-toggle="chkEditable_' + (i) + '" data-title="0">NO</a></div><input type="hidden" name="chkEditable" id="chkEditable_' + (i) + '" onchange=ChangeInputs();></div></div>'
                                   + '<div class="col-sm-4"><label class="mt5">Visible</label><div class="forms_height"><div id="radioBtn" class="btn-group"><a class="btn btn-primary btn-sm not-active" id="chkVisibleYes_' + (i) + '" data-toggle="chkVisible_' + (i) + '" data-title="1">YES</a><a class="btn btn-primary btn-sm not-active" id="chkVisibleNo_' + (i) + '" data-toggle="chkVisible_' + (i) + '" data-title="0">NO</a></div><input type="hidden" name="chkVisible" id="chkVisible_' + (i) + '" onchange=ChangeInputs();></div></div>'
                                   + '</div></div>'
                                   + '<input type="hidden" id="txtCasesheetSectionDescID_' + (i) + '" value="' + data1.Table1[i].casesheet_section_desc_id + '"/>'
                                   + '<input type="hidden" id="txtDesSectionName_' + (i) + '" value="' + data1.Table1[i].name + '"/>'
                                   + '<input type="hidden" id="txtDesFieldName_' + (i) + '" value="' + data1.Table1[i].field_name + '"/>'
                                   + '<input type="hidden" id="txtDesLabelFieldName_' + (i) + '" value="' + data1.Table1[i].label_field_name + '"/>'
                                }
                            }
                            debugger;

                            $('#TabDescDetails').append(tr + headStart + tabDescData + headEnd);
                        }
                        else {
                            $('#TabDescDetails').append(tr);
                        }

                        //set values
                        if (data1.Table.length > 0) {
                            $('#ChkSectionName').val(data1.Table[0].is_visible);
                            $('a[data-toggle=ChkSectionName][data-title="' + data1.Table[0].is_visible + '"]').removeClass('not-active');
                            $('a[data-toggle=ChkSectionName][data-title="' + data1.Table[0].is_visible + '"]').addClass('active');
                        }

                        if (data1.Table1.length > 0) {
                            for (var i = 0; i < data1.Table1.length; i++) {
                                $('#chkEditable_' + (i)).val(data1.Table1[i].is_enable);
                                $('a[data-toggle=chkEditable_' + (i) + '][data-title="' + data1.Table1[i].is_enable + '"]').removeClass('not-active');
                                $('a[data-toggle=chkEditable_' + (i) + '][data-title="' + data1.Table1[i].is_enable + '"]').addClass('active');

                                $('#chkVisible_' + (i)).val(data1.Table1[i].is_visible);
                                $('a[data-toggle=chkVisible_' + (i) + '][data-title="' + data1.Table1[i].is_visible + '"]').removeClass('not-active');
                                $('a[data-toggle=chkVisible_' + (i) + '][data-title="' + data1.Table1[i].is_visible + '"]').addClass('active');

                                if ($('#txtFieldName').val() == "divObservation") {
                                    $('#chkTagEditor_' + (i)).val(data1.Table1[i].is_tag_editor);
                                    $('a[data-toggle=chkTagEditor_' + (i) + '][data-title="' + data1.Table1[i].is_tag_editor + '"]').removeClass('not-active');
                                    $('a[data-toggle=chkTagEditor_' + (i) + '][data-title="' + data1.Table1[i].is_tag_editor + '"]').addClass('active');
                                }
                            }
                        }
                    }
                }

                $('#radioBtn a').click(function () {
                    debugger;
                    var sel = $(this).data('title');
                    var tog = $(this).data('toggle');
                    $('#' + tog).prop('value', sel).trigger('change');;

                    $('a[data-toggle="' + tog + '"]').not('[data-title="' + sel + '"]').removeClass('active').addClass('not-active');
                    $('a[data-toggle="' + tog + '"][data-title="' + sel + '"]').removeClass('not-active').addClass('active');
                });
            }
            else {
                console.log(data1);
            }
        }

    });

});

$('#btnSubmitCasesheetDesc').click(function () {

    isTextChanged = false;
    if ($('#selCasesheetType').val() == "" || $('#selCasesheetType').val() == null || $('#selCasesheetType').val() == 0) {
        $('#PselCasesheetType').show();
        $('#PselCasesheetType').fadeOut(8000);
        $('#PselCasesheetType').html('Select Casesheet Type');
        $('#selCasesheetType').focus();
        return false;
    }

    if ($('#selCasesheetTabs').val() == "" || $('#selCasesheetTabs').val() == null || $('#selCasesheetTabs').val() == 0) {
        $('#PselCasesheetTabs').show();
        $('#PselCasesheetTabs').fadeOut(8000);
        $('#PselCasesheetTabs').html('Select Casesheet Tab');
        $('#selCasesheetTabs').focus();
        return false;
    }

    if ($('#selCasesheetSection').val() == "" || $('#selCasesheetSection').val() == null || $('#selCasesheetSection').val() == 0) {
        $('#PselCasesheetSection').show();
        $('#PselCasesheetSection').fadeOut(8000);
        $('#PselCasesheetSection').html('Select Section');
        $('#selCasesheetSection').focus();
        return false;
    }

    if ($('#SectionLabelName').val() == "" || $('#SectionLabelName').val() == null || $('#SectionLabelName').val() == "null") {
        $('#PSectionLabelName').show();
        $('#PSectionLabelName').fadeOut(8000);
        $('#PSectionLabelName').html('Enter Heading Caption');
        $('#SectionLabelName').focus();
        return false;
    }


    var count = $('#txtTotalSectionDesc').val();
    if (parseInt(count) > 0) {
        for (var k = 0; k < count; k++) {
            if ($('#Label_' + k).val() == "" || $('#Label_' + k).val() == null) {
                $('#PLabel_' + k).show();
                $('#PLabel_' + k).fadeOut(8000);
                $('#PLabel_' + k).html('Enter label');
                $('#Label_' + k).focus();
                return false;
            }
        }
    }

    var isNonVisible = 0;
    // to check whether all parameters are invisible
    if (parseInt(count) > 0) {
        for (var k = 0; k < count; k++) {
            if ($('#chkVisible_' + k).val() == "0") {
                isNonVisible++;
            }
        }
    }

    if (isNonVisible > 0 && (parseInt(isNonVisible) == parseInt(count)) && $('#ChkSectionName').val() != "0") {
        if (confirm("All option is invisible. Do you want to hide this over all section ?.")) {

            $('#ChkSectionName').val(0);
            $('a[data-toggle=ChkSectionName][data-title=0]').removeClass('not-active');
            $('a[data-toggle=ChkSectionName][data-title=0]').addClass('active');

            $('a[data-toggle=ChkSectionName][data-title=1]').removeClass('active');
            $('a[data-toggle=ChkSectionName][data-title=1]').addClass('not-active');

        }
        else {
            return false;
        }
    }
    $('#shade_slot').show();
    debugger;
    var doctor_id = "";
    if ($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5) {
        doctor_id = $('#HiddenDoctorID').val();
    }
    else {
        doctor_id = $('#SelDoctor1').val();
    }

    var visible = ($('#ChkSectionName').val() == true || $('#ChkSectionName').val() == "true" || $('#ChkSectionName').val() == 1) ? "1" : "0";

    var casesheetConfig = {
        casesheet_type_id: $('#selCasesheetType').val(),
        casesheet_doctor_module_id: $('#selCasesheetTabs').val(),
        Dml_Indicator: 'I',
        casesheet_section_id: $('#selCasesheetSection').val(),
        section_label_name: $('#SectionLabelName').val(),
        is_visible: visible,
        section_name: $('#txtSectionName').val(),
        field_name: $('#txtFieldName').val(),
        label_field_name: $('#txtLabelFieldName').val(),
        Doctor_id: doctor_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CaseStudy/Casesheet_Configuration',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(casesheetConfig),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data
                if (jQuery.isEmptyObject((data))) {

                }
                else {
                    debugger;

                    if (data1.Table.length > 0) {
                        if (data1.Table[0].stringmsg == "success") {
                            var Vhospital_casesheet_id = data1.Table[0].Vhospital_casesheet_id;
                            var Vsection_master_id = data1.Table[0].Vsection_master_id;
                            var count = $('#txtTotalSectionDesc').val();
                            if (parseInt(count) > 0) {
                                var j = 0;
                                for (var i = 0; i < count; i++) {

                                    var casesheetConfig = {
                                        Dml_Indicator: 'ID',
                                        casesheet_section_id: Vsection_master_id,
                                        is_visible: $('#chkVisible_' + i).val(),
                                        is_enable: $('#chkEditable_' + i).val(),
                                        label: $('#Label_' + i).val(),
                                        section_name: $('#txtDesSectionName_' + i).val(),
                                        field_name: $('#txtDesFieldName_' + i).val(),
                                        label_field_name: $('#txtDesLabelFieldName_' + i).val(),
                                        casesheet_section_desc_id: $('#txtCasesheetSectionDescID_' + i).val(),
                                        Doctor_id: doctor_id,
                                        is_tag_editor: $('#chkTagEditor_' + i).val()
                                    }
                                    $.ajax({
                                        async: true,
                                        type: 'POST',
                                        url: '/CaseStudy/Casesheet_Configuration',
                                        contentType: 'application/json; charset=UTF-8',
                                        data: JSON.stringify(casesheetConfig),
                                        success: function (data) {
                                            j++;

                                            if (j == count) {

                                                toastr.success('Updated Successfully');
                                                $('#shade_slot').hide();
                                                $('#selCasesheetTabs').change();
                                                $('#TabDetails').empty();
                                                $('#TabDescDetails').empty();
                                                $('#btnSubmitCasesheetDesc').hide();
                                            }
                                        }
                                    });
                                }

                            }
                            else {

                                toastr.success('Updated Successfully');
                                $('#shade_slot').hide();
                                $('#selCasesheetTabs').change();
                                $('#TabDetails').empty();
                                $('#TabDescDetails').empty();
                                $('#btnSubmitCasesheetDesc').hide();
                            }
                        }
                    }
                }
            }
            else {
                console.log(data1);
            }
        }
    });

});


function ChangeInputs() {
    isTextChanged = true;

}

function ChangeSectionVisible() {
    isTextChanged = true;
    var value = $('#ChkSectionName').val();
    var count = $('#txtTotalSectionDesc').val();
    if (parseInt(count) > 0) {
        for (var k = 0; k < count; k++) {
            if (value == "0") {
                $('#chkVisible_' + (k)).val(0);
                $('a[data-toggle=chkVisible_' + (k) + '][data-title=0]').removeClass('not-active');
                $('a[data-toggle=chkVisible_' + (k) + '][data-title=0]').addClass('active');

                $('a[data-toggle=chkVisible_' + (k) + '][data-title=1]').removeClass('active');
                $('a[data-toggle=chkVisible_' + (k) + '][data-title=1]').addClass('not-active');
            }
            else {
                $('#chkVisible_' + (k)).val(1);
                $('a[data-toggle=chkVisible_' + (k) + '][data-title=1]').removeClass('not-active');
                $('a[data-toggle=chkVisible_' + (k) + '][data-title=1]').addClass('active');

                $('a[data-toggle=chkVisible_' + (k) + '][data-title=0]').removeClass('active');
                $('a[data-toggle=chkVisible_' + (k) + '][data-title=0]').addClass('not-active');
            }
        }
    }
}

//casesheet configuration tab wise end

