//All type master creation -> Service Type / Billing Class / Discount Type/ Payment Type / Refund Type / Visit Type / Designation /Salutation / Package Type
//Development Start Date: 21st Dec 2021

//On load master type data binding
$(document).ready(function () {
    debugger;
     CheckCodeTemplate()   //--Uncomment for One master concept--

    if ($('#hiddenRole_id').val() == 0) {
        $('#div_filter_hospital_name').show();
        HospitalDetails()
    }
    else {
        $('#div_filter_hospital_name').hide();
    }

    //--One master concept--
    //if ($('#hiddenRole_id').val() != 0) {
    //    LoadBranch();
    //}
});


//CheckCodeTemplate
function CheckCodeTemplate() {
    debugger;
    var codeData = {
        //Dml_Indicator: "CT",
        Dml_Indicator: "BCT",
        hospital_id: $('#HospitalId').val(),
        //branch_id: $('#pkg_branch_id').val()    //--One master concept
        //branch_id: $('#HospitalBranchID').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Code_Templates_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(codeData),
        success: function (data) {
            debugger;

            var data1 = JSON.parse(data)
            data1 = data1.data;

            if (data1[0].count1 == 0) {
                //--One master concept--
                // alert('' + $('#pkg_branch_id  option:selected').html() + ' Not Added Code Template. Please Add the Code Template at SetupIds');
                alert(' Not Added Code Template. Please Add the Code Template at SetupIds');
                window.location.href = "/Hospitalsetup"
            }
            else {
                Load_Master_Type();
                table_service()
            }          
        }
    });
}


//Hospital Detail drop down Function
function HospitalDetails(hospitalid, branchid) {

    debugger;
    var HosTbldata = {

        Dml_Indicator: 'AHP'
    }

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

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;
                    $('#hospitalname').empty();
                    var hospital;
                    var default_opt = '<option value="0" data-id="">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        hospital = hospital + '<option value="' + emp.hospitalid + '" data-id="' + emp.hospital_name + '">' + emp.hospital_name + '</option>';
                    });

                    if (data1.Table.length == 1) {
                        $('#hospitalname_id').append(hospital);
                        $('#filter_hospitalname_id').append(hospital);
                    }
                    else {
                        $('#hospitalname_id').append(default_opt + hospital);
                        $('#filter_hospitalname_id').append(default_opt + hospital);
                        //LoadBranch()
                    }

                    if ($('#hiddenRole_id').val() == 0) {
                        if (hospitalid != '' && hospitalid != 0 && hospitalid != undefined) {
                            $('#hospitalname_id').val(hospitalid)
                            //LoadBranch(branchid)  //--One master concept--
                        }
                    }

                }
                else {
                    console.log(data);
                }

            }
        },
        error: function (data) {

        }
    });

}

//--One master concept--
//$('#hospitalname_id').change(function () {
//    debugger;
//    LoadBranch()
//})

//$('#filter_hospitalname_id').change(function () {
//    debugger;
//    LoadBranch()
//})

//Binding Master Type Data
function Load_Master_Type() {
    debugger;
    $('#shade_slot').show();
   
      var   Load_Master = {
            Dml_Indicator: "SMD",
            master_service_name: $('#Hservice_type_id').val()
        }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/MasterServiceType_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Load_Master),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var tr = '';
                    var SelRole = '';
                    $('#Master_Type').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    var j = 0;
                    if (data1.length > 0) {
                        for (var i = 0; i < data1.length; i++) {
                            j++;
                            SelRole = SelRole + '<option value="' + data1[i].service_type_id + '">' + data1[i].service_name + '</option>';
                        }
                        $('#Master_Type').html(default_opt + SelRole);
                        $('#Master_Type_filter').html(default_opt + SelRole);
                        $('#HbedTymCharge').val(data1[0].BedTimeCount)
                    }
                   // table_service();
                }
                $('#shade_slot').hide();
            }
            else {
                console.log(data1);
            }
        }
    });
}

//If we change the master service type, we need to change the labels
$('#Master_Type').change(function () {
    debugger;
    master_type_change();
});
function master_type_change(hospitalid, branchid) {
    debugger;
    var dropdown_text = $('#Master_Type option:selected').text();
    if ($('#Master_Type').val() == 26 && parseInt($('#HbedTymCharge').val())>0 &&   $('#master_service_id').val()==0) {
        $('#btnsubmit').hide();
    }
    else {
        $('#btnsubmit').show();
    }

   // $('#btnsubmit').show();

    $('#master_service_name_label').html(dropdown_text + '  <span class="red">*</span>');
    $('#master_service_Description_label').html(dropdown_text + ' Description <span class="red">*</span>');

    if ($('#Master_Type').val() != 14) {
        $('#service_name').attr('onkeyup', 'alphanumeric_only("service_name")');
    }
    else {
        $('#service_name').removeAttr('onkeyup');
    }
    if ($('#Master_Type').val() == 21) {
        $('#service_name').attr('onkeyup', 'numeric_only("service_name")');
    }
    else {
        $('#service_name').removeAttr('onkeyup');
    }
    $('#div_service_name').show();
    $('#div_service_desc').show();
    $('#div_remarks').show();
    $('#div_branch').show();
    if ($('#hiddenRole_id').val() == 0) {
        $('#div_hospital_name').show();
        HospitalDetails(hospitalid, branchid)
    }
    else {
        $('#div_hospital_name').hide();
    }

    if ($('#Master_Type').val() == 5) {
        $('#div_scheme').attr('hidden', false)
    }
    else {
        $('#div_scheme').attr('hidden', true)
    }
    if ($('#Master_Type').val() == 14) {
        $('#div_refdoctor').attr('hidden', false)
    }
    else {
        $('#div_refdoctor').attr('hidden', true)
    }

    //if($('#Master_Type').val() == "1")
    //{
    //    $('#master_service_name_label').html("Discount Name " + ' <span class="red">*</span>');
    //    $('#master_service_Description_label').html("Discount Description " + ' <span class="red">*</span>');
    //    $('#appointment_div').hide();
    //    $('#master_service_Partial_div').hide();
    //    $('#master_service_Authorization_div').show();
    //    $('#increase_div').hide();
    //    $('#active_from_div').show();
    //    $('#active_to_div').show();
    //    $('#div_service_name').show();
    //    $('#div_service_desc').show();
    //    $('#div_remarks').show();
    //    $('#div_branch').show();
    //}
    //else if ($('#Master_Type').val() == "2") {
    //    $('#master_service_name_label').html("Service Name " + ' <span class="red">*</span>');
    //    $('#master_service_Description_label').html("Service Description " + ' <span class="red">*</span>');
    //    $('#appointment_div').hide();
    //    $('#master_service_Partial_div').hide();
    //    $('#master_service_Authorization_div').hide();
    //    $('#increase_div').hide();
    //    $('#active_from_div').hide();
    //    $('#active_to_div').hide();
    //    $('#div_service_name').show();
    //    $('#div_service_desc').show();
    //    $('#div_remarks').show();
    //    $('#div_branch').show();
    //}
    //else if ($('#Master_Type').val() == "3") {
    //    $('#master_service_name_label').html("Payment Type Name " + ' <span class="red">*</span>');
    //    $('#master_service_Description_label').html("Payment Type Description " + ' <span class="red">*</span>');
    //    $('#appointment_div').hide();
    //    $('#master_service_Partial_div').hide();
    //    $('#master_service_Authorization_div').hide();
    //    $('#increase_div').hide();
    //    $('#active_from_div').hide();
    //    $('#active_to_div').hide();
    //    $('#div_service_name').show();
    //    $('#div_service_desc').show();
    //    $('#div_remarks').show();
    //    $('#div_branch').show();
    //}
    //else if ($('#Master_Type').val() == "4") {
    //    $('#master_service_name_label').html("Refund Type Name " + ' <span class="red">*</span>');
    //    $('#master_service_Description_label').html("Refund Type Description " + ' <span class="red">*</span>');
    //    $('#appointment_div').hide();
    //    $('#master_service_Partial_div').hide();
    //    $('#master_service_Authorization_div').hide();
    //    $('#increase_div').hide();
    //    $('#active_from_div').hide();
    //    $('#active_to_div').hide();
    //    $('#div_service_name').show();
    //    $('#div_service_desc').show();
    //    $('#div_remarks').show();
    //    $('#div_branch').show();
    //}
    //else if ($('#Master_Type').val() == "5") {
    //    $('#master_service_name_label').html("Billing Class Name " + ' <span class="red">*</span>');
    //    $('#master_service_Description_label').html("Billing Class Description " + ' <span class="red">*</span>');
    //    $('#appointment_div').hide();
    //    $('#master_service_Partial_div').hide();
    //    $('#master_service_Authorization_div').hide();
    //    $('#increase_div').show();
    //    $('#active_from_div').hide();
    //    $('#active_to_div').hide();
    //    $('#div_service_name').show();
    //    $('#div_service_desc').show();
    //    $('#div_remarks').show();
    //    $('#div_branch').show();
    //}
    //else if ($('#Master_Type').val() == "6") {
    //    $('#master_service_name_label').html("Package Type Name " + ' <span class="red">*</span>');
    //    $('#master_service_Description_label').html("Package Type Description " + ' <span class="red">*</span>');
    //    $('#appointment_div').hide();
    //    $('#master_service_Partial_div').hide();
    //    $('#master_service_Authorization_div').hide();
    //    $('#increase_div').hide();
    //    $('#active_from_div').hide();
    //    $('#active_to_div').hide();
    //    $('#div_service_name').show();
    //    $('#div_service_desc').show();
    //    $('#div_remarks').show();
    //    $('#div_branch').show();
    //}
    //else if ($('#Master_Type').val() == "7") {
    //    $('#master_service_name_label').html("Designation Name " + ' <span class="red">*</span>');
    //    $('#master_service_Description_label').html("Designation Description " + ' <span class="red">*</span>');
    //    $('#appointment_div').show();
    //    $('#master_service_Partial_div').hide();
    //    $('#master_service_Authorization_div').hide();
    //    $('#increase_div').hide();
    //    $('#active_from_div').hide();
    //    $('#active_to_div').hide();
    //    $('#div_service_name').show();
    //    $('#div_service_desc').show();
    //    $('#div_remarks').show();
    //    $('#div_branch').show();
    //}
    //else if ($('#Master_Type').val() == "8") {
    //    $('#master_service_name_label').html("Visit Type Name " + ' <span class="red">*</span>');
    //    $('#master_service_Description_label').html("Visit Type Description " + ' <span class="red">*</span>');
    //    $('#appointment_div').hide();
    //    $('#master_service_Partial_div').hide();
    //    $('#master_service_Authorization_div').hide();
    //    $('#increase_div').hide();
    //    $('#active_from_div').hide();
    //    $('#active_to_div').hide();
    //}
    //else if ($('#Master_Type').val() == "9") {
    //    $('#master_service_name_label').html("Floor Name " + ' <span class="red">*</span>');
    //    $('#master_service_Description_label').html("Floor Description " + ' <span class="red">*</span>');
    //    $('#appointment_div').show();
    //    $('#master_service_Partial_div').hide();
    //    $('#master_service_Authorization_div').hide();
    //    $('#increase_div').hide();
    //    $('#active_from_div').hide();
    //    $('#active_to_div').hide();
    //    $('#div_service_name').show();
    //    $('#div_service_desc').show();
    //    $('#div_remarks').show();
    //    $('#div_branch').show();
    //}
    //else if ($('#Master_Type').val() == "10") {
    //    $('#master_service_name_label').html("Section Name " + ' <span class="red">*</span>');
    //    $('#master_service_Description_label').html("Section Description " + ' <span class="red">*</span>');
    //    $('#appointment_div').show();
    //    $('#master_service_Partial_div').hide();
    //    $('#master_service_Authorization_div').hide();
    //    $('#increase_div').hide();
    //    $('#active_from_div').hide();
    //    $('#active_to_div').hide();
    //    $('#div_service_name').show();
    //    $('#div_service_desc').show();
    //    $('#div_remarks').show();
    //    $('#div_branch').show();
    //}
    //else if ($('#Master_Type').val() == "11") {
    //    $('#master_service_name_label').html("Payment Method Name " + ' <span class="red">*</span>');
    //    $('#master_service_Description_label').html("Payment Method Description " + ' <span class="red">*</span>');
    //    $('#appointment_div').show();
    //    $('#master_service_Partial_div').hide();
    //    $('#master_service_Authorization_div').hide();
    //    $('#increase_div').hide();
    //    $('#active_from_div').hide();
    //    $('#active_to_div').hide();
    //    $('#div_service_name').show();
    //    $('#div_service_desc').show();
    //    $('#div_remarks').show();
    //    $('#div_branch').show();
    //}
    //else
    //{
    //    //$('#master_service_name_label').html("Discount Name " + ' <span class="red">*</span>');
    //    //$('#master_service_Description_label').html("Discount Description " + ' <span class="red">*</span>');
    //    $('#appointment_div').hide();
    //    $('#master_service_Partial_div').hide();
    //    $('#master_service_Authorization_div').hide();
    //    $('#increase_div').hide();
    //    $('#active_from_div').hide();
    //    $('#active_to_div').hide();
    //    $('#div_service_name').hide();
    //    $('#div_service_desc').hide();
    //    $('#div_remarks').hide();
    //    $('#div_branch').hide();
    //    $('#btnsubmit').hide();
    //}
    var indicator_id = '', type = 0;
    //if ($('#Master_Type').val() == '') {

    //    if ($('#hiddenRole_id').val() == 2) {
    //        indicator_id = 'TL'
    //    }
    //    else {
    //        indicator_id = 'TLB'
    //    }

    //    type = 0
    //}
    //else {
    //    var indicator_id = '';
    //    if ($('#hiddenRole_id').val() == 2) {
    //        indicator_id = 'TLT'
    //    }
    //    else {
    //        indicator_id = 'TLTB'
    //    }
    //    type=$('#Master_Type').val();
    //}
    //var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
    //var master_data_type = {
    //    Dml_Indicator: indicator_id,
    //    service_type_id: type,
    //    branch_id:branch_id
    //}
    //$.ajax({
    //    async: true,
    //    type: 'POST',
    //    url: '/Billing/MasterServiceType_Ins_Upd_Del',
    //    contentType: 'application/json; charset=UTF-8',
    //    data: JSON.stringify(master_data_type),
    //    success: function (data) {
    //        var data1 = JSON.parse(data)
    //        if (data1.msg == "Success") {
    //            var data1 = data1.data;
    //            if (jQuery.isEmptyObject((data1))) {
    //            }
    //            else {
    //                binding_master_values(data);
    //            }
    //        }
    //        else {
    //            console.log(data1);
    //        }
    //    }
    //});
}


//save master data type values
$('#btnsubmit').click(function () {
    if ($('#Master_Type').val() == "") {
        $('#PMaster_Type').show();
        $('#PMaster_Type').fadeOut(8000);
        $('#PMaster_Type').html('Select Type');
        $('#Master_Type').focus();
        return false;
    }

    if ($('#hiddenRole_id').val() == 0) {
        if ($.trim($('#hospitalname_id').val()) == "" || $('#hospitalname_id').val() == null) {
            $('#Phospitalname_id').show();
            $('#Phospitalname_id').fadeOut(8000);
            $('#Phospitalname_id').html('Select Hospital');
            $('#hospitalname_id').focus();
            $('#hospitalname_id').val('');
            return false;
        }
    }
    //--One master concept--
    //if ($.trim($('#pkg_branch_id').val()) == "" || $('#pkg_branch_id').val() == '0') {
    //    $('#Ppkg_branch_id').show();
    //    $('#Ppkg_branch_id').fadeOut(8000);
    //    $('#Ppkg_branch_id').html('Select Branch');
    //    $('#pkg_branch_id').focus();
    //    $('#pkg_branch_id').val('');
    //    return false;
    //}
    if ($('#service_name').val() == "") {
        $('#Pservice_name').show();
        $('#Pservice_name').fadeOut(8000);
        $('#Pservice_name').html('Enter name');
        $('#service_name').focus();
        return false;
    }
    if ($('#service_descrp').val() == "") {
        $('#Pservice_descrp').show();
        $('#Pservice_descrp').fadeOut(8000);
        $('#Pservice_descrp').html('Enter Description');
        $('#service_descrp').focus();
        return false;
    }
    if ($('#Master_Type').val() == "1") {
        if ($('#ChkAuthorization').val() == "") {
            $('#PChkAuthorization').show();
            $('#PChkAuthorization').fadeOut(8000);
            $('#PChkAuthorization').html('Select Authorization');
            $('#ChkAuthorization').focus();
            return false;
        }
        if ($('#active_from').val() == "") {
            $('#Pactive_from').show();
            $('#Pactive_from').fadeOut(8000);
            $('#Pactive_from').html('Select Active From Date');
            $('#active_from').focus();
            return false;
        }
        if ($('#active_to').val() == "") {
            $('#Pactive_to').show();
            $('#Pactive_to').fadeOut(8000);
            $('#Pactive_to').html('Select Active To Date');
            $('#active_to').focus();
            return false;
        }
    }
    if ($('#Master_Type').val() == "5") {
        if ($('#increase_val').val() == "") {
            $('#Pincrease_val').show();
            $('#Pincrease_val').fadeOut(8000);
            $('#Pincrease_val').html('Enter % Increase Value');
            $('#increase_val').focus();
            return false;
        }

        if ($('#is_scheme').val() == "") {
            $('#Pis_scheme').show();
            $('#Pis_scheme').fadeOut(8000);
            $('#Pis_scheme').html('Select the scheme Yes or No');
            $('#is_scheme').focus();
            return false;
        }
    }
    //if ($('#Master_Type').val() == "7") {
    //    if ($.trim($('#SelDay').val()) == "" || $.trim($('#SelDay').val()) == null) {
    //        $('#PSelDay').show();
    //        $('#PSelDay').fadeOut(8000);
    //        $('#PSelDay').html('Select Mode');
    //        $('#SelDay').focus();
    //        $('#SelDay :selected').val('0');
    //        return false;
    //    }
    //}

    Insert_Update_Master_Values();
});


//insert/update master data values
function Insert_Update_Master_Values() {
    debugger;
    $('#shade_slot_submit').show();
    var active_from = '', active_to = '';
    if ($('#Master_Type').val() == "1") {

        active_from = $('#active_from').val();
        active_from = active_from.split("/");
        active_from = active_from[2] + "-" + active_from[1] + "-" + active_from[0];
        active_to = $('#active_to').val();
        active_to = active_to.split("/");
        active_to = active_to[2] + "-" + active_to[1] + "-" + active_to[0];
    }
    var ref_doctor = 0;
    if ($('#Chkref_doctor').prop("checked") == true) {
        ref_doctor = 1;
    }
    var indicator_id = '';
    var auth = 0;
    if ($('#ChkAuthorization').val() == '' || $('#ChkAuthorization').val() == null) {
        auth = 0;
    }
    else {
        auth = $('#ChkAuthorization').val();
    }

    var is_scheme = $('#is_scheme').val() == '' ? 3 : $('#is_scheme').val();

    var msg = '';
    if ($('#btnsubmit').html() == 'Submit') {
        indicator_id = 'I'
        msg = 'Saved Successfully';
    }
    else {
        indicator_id = 'U'
        msg = 'Updated Successfully';
    }
    //var mode = $('#SelDay').val().toString();
    if ($('#hiddenRole_id').val() == 0) {
        var master_data_type = {
            Dml_Indicator: indicator_id,
            service_type_id: $('#Master_Type').val(),
            master_service_name: $('#service_name').val(),
            master_service_description: $('#service_descrp').val(),
            authorization_requireds: auth,
            increase_from: $('#increase_val').val(),
            active_from: active_from,
            active_to: active_to,
            remarks: $('#remarks').val(),
            //appointment_mode: mode,
            master_servie_type_id: $('#master_service_id').val(),
            //branch_id: $('#pkg_branch_id').val(), //--One master concept--
            OS_hospitalid: $('#hospitalname_id').val(),
            is_scheme: is_scheme,
            ref_doctor: ref_doctor
        }
    }
    else {
        var master_data_type = {
            Dml_Indicator: indicator_id,
            service_type_id: $('#Master_Type').val(),
            master_service_name: $('#service_name').val(),
            master_service_description: $('#service_descrp').val(),
            authorization_requireds: auth,
            increase_from: $('#increase_val').val(),
            active_from: active_from,
            active_to: active_to,
            remarks: $('#remarks').val(),
            //appointment_mode: mode,
            master_servie_type_id: $('#master_service_id').val(),
            // branch_id: $('#pkg_branch_id').val(), //--One master concept--
            is_scheme: is_scheme,
            ref_doctor: ref_doctor

        }
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/MasterServiceType_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(master_data_type),
        success: function (data) {
            debugger;
            $('#shade_slot_submit').hide();
            var data1 = JSON.parse(data)
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    if (data1[0].message == "already")
                    {
                        $('#service_name').val('');
                        toastr.warning('Name already exists');
                    }
                    else
                    {
                        toastr.success(msg,
                        {
                            closeButton: false,
                            progressBar: true,
                            allowHtml: true,
                            timeOut: 0,
                        })
                        setTimeout(function () {
                            location.reload();
                        }, 2000);
                    }
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}
//Service Type Table Binding
function table_service() {
    debugger;
    var indicator_id = '';
    //if ($('#hiddenRole_id').val() == 2) {
    //    indicator_id = 'TL'
    //}
    //else {
    //    indicator_id = 'TLB'
    //}

   //--One master concept--
    //var branchid = ''
    //var branch_id = ($('#HospitalBranchID').val() == '') ? '' : $('#HospitalBranchID').val();
    //if (branch_id == '') {
    //    branchid = 0
    //}
    //else {
    //    if ($('#hiddenRole_id').val() == 0) {
    //        branchid = 0
    //    }
    //    else {
    //        branchid = branch_id
    //    }
    //}

    if ($('#hiddenRole_id').val() == 0) {
        var master_data_type = {
            Dml_Indicator: 'STL'
            //,branch_id: branchid  //--One master concept--
        }
    }
    else {
        var master_data_type = {
            Dml_Indicator: 'TL',
            // branch_id: branchid,   //--One master concept--
            master_service_name: $('#Hservice_type_id').val()
            //service_type_id: master_type,
            //flag: $('#FStatus').val()
        }
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/MasterServiceType_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(master_data_type),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data)
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    binding_master_values(data);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}
//Delete Service Type
function type_delete(event, id, indicator_id, flag) {
    debugger;

    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             debugger;
             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 status = "";
                     if (indicator_id == 'active') {
                         status = "Are You Sure, You Want to Activate the Setup?";
                     }
                     else if (indicator_id == 'inactive') {
                         status = "Are You Sure, You Want to In-activate the Setup?";
                     }
                     else if (indicator_id == 'DD') {

                         status = "Are You Sure, You Want to Delete the Setup?";
                     }
                     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>", status,
                           {

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

                               onShown: function (toast) {
                                   debugger;
                                   $("#confirmationRevertYes").click(function () {
                                       debugger;
                                       var type = 0;
                                       if ($('#Master_Type').val() == '') {
                                           type = 0
                                       }
                                       else {
                                           type = $('#Master_Type').val();
                                       }
                                       var master_data_type = {
                                           Dml_Indicator: indicator_id,
                                           service_type_id: type,
                                           master_servie_type_id: id,
                                           flag: flag
                                       }
                                       $.ajax({
                                           async: true,
                                           type: 'POST',
                                           url: '/Billing/MasterServiceType_Ins_Upd_Del',
                                           contentType: 'application/json; charset=UTF-8',
                                           data: JSON.stringify(master_data_type),
                                           success: function (data) {
                                               debugger;
                                               var data1 = JSON.parse(data)
                                               if (data1.msg == "Success") {

                                                   var data2 = data1.data;
                                                   if (indicator_id == 'DD') {
                                                       if (data2[0].StringMsg == 'Deleted Successfully') {
                                                           toastr.success(data2[0].StringMsg);
                                                           table_service()
                                                       }
                                                       else if (data2[0].Message === 'mapped') {
                                                           toastr.warning('Mapped with some setup part, Unable to delete');
                                                       }
                                                       else if (data2[0].Message === 'mapped with delete') {
                                                           var master_servie_typeID = data2[0].master_servie_type_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>", 'Mapped with some setup part, Do you want to Delete',
                                                                {

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

                                                                    onShown: function (toast) {
                                                                        debugger;
                                                                        $("#confirmationRevertYes").click(function () {
                                                                            debugger;
                                                                            var master_data_type = {
                                                                                Dml_Indicator: 'DD',
                                                                                master_servie_type_id: master_servie_typeID,
                                                                            }
                                                                            $.ajax({
                                                                                async: true,
                                                                                type: 'POST',
                                                                                url: '/Billing/MasterServiceType_Ins_Upd_Del',
                                                                                contentType: 'application/json; charset=UTF-8',
                                                                                data: JSON.stringify(master_data_type),
                                                                                success: function (data) {
                                                                                    debugger;
                                                                                    var data1 = JSON.parse(data)
                                                                                    if (data1.msg == "Success") {
                                                                                        var data2 = data1.data;
                                                                                        toastr.success('Deleted successfully');
                                                                                        //location.reload();
                                                                                        table_service()
                                                                                    }
                                                                                    else {
                                                                                        console.log(data2)
                                                                                    }
                                                                                }
                                                                            })
                                                                        })
                                                                    }
                                                                })
                                                       }
                                                   }
                                                   else if (indicator_id == 'active') {
                                                       if (data2[0].msg == 'activated') {
                                                           toastr.success('Activated successfully');
                                                           table_service()
                                                       }
                                                   }
                                                   else if (indicator_id == 'inactive') {
                                                       if (data2[0].msg == 'inactivated') {
                                                           toastr.success('In Activated successfully');
                                                           table_service()
                                                       }
                                                       else if (data2[0].Message === 'mapped') {
                                                           var master_servie_typeID = data2[0].master_servie_type_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>", 'Mapped with some setup part, Do you want to In-activate',
                                                                {

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

                                                                    onShown: function (toast) {
                                                                        debugger;
                                                                        $("#confirmationRevertYes").click(function () {
                                                                            debugger;
                                                                            var master_data_type = {
                                                                                Dml_Indicator: 'inact',
                                                                                master_servie_type_id: master_servie_typeID,
                                                                            }
                                                                            $.ajax({
                                                                                async: true,
                                                                                type: 'POST',
                                                                                url: '/Billing/MasterServiceType_Ins_Upd_Del',
                                                                                contentType: 'application/json; charset=UTF-8',
                                                                                data: JSON.stringify(master_data_type),
                                                                                success: function (data) {
                                                                                    debugger;
                                                                                    var data1 = JSON.parse(data)
                                                                                    if (data1.msg == "Success") {
                                                                                        var data2 = data1.data;
                                                                                        toastr.success('In Activated successfully');
                                                                                        //location.reload();
                                                                                        table_service()
                                                                                    }
                                                                                    else {
                                                                                        console.log(data2)
                                                                                    }
                                                                                }
                                                                            })
                                                                        })
                                                                    }
                                                                })




                                                       }
                                                   }
                                                   
                                                   //if (jQuery.isEmptyObject((data1))) {
                                                   //}
                                                   //else {
                                                   //    if ($('#Master_Type').val() == '') {

                                                   //        //if ($('#hiddenRole_id').val() == 2) {
                                                               
                                                   //        //}
                                                   //        //else {
                                                   //        //    indicator_id = 'TLB'
                                                   //        //}
                                                   //        indicator_id = 'TL'
                                                   //        type = 0
                                                   //    }
                                                   //    //else {
                                                   //    //    var indicator_id = '';
                                                   //    //    if ($('#hiddenRole_id').val() == 2) {
                                                   //    //        indicator_id = 'TLT'
                                                   //    //    }
                                                   //    //    else {
                                                   //    //        indicator_id = 'TLTB'
                                                   //    //    }
                                                   //    //    type=$('#Master_Type').val();
                                                   //    //}
                                                   //    var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
                                                   //    var master_data_type = {
                                                   //        Dml_Indicator: indicator_id,
                                                   //        service_type_id: type,
                                                   //        branch_id: branch_id,
                                                   //        master_service_name: $('#Hservice_type_id').val()
                                                   //    }
                                                   //    $.ajax({
                                                   //        async: true,
                                                   //        type: 'POST',
                                                   //        url: '/Billing/MasterServiceType_Ins_Upd_Del',
                                                   //        contentType: 'application/json; charset=UTF-8',
                                                   //        data: JSON.stringify(master_data_type),
                                                   //        success: function (data) {
                                                   //            var data1 = JSON.parse(data)
                                                   //            if (data1.msg == "Success") {
                                                   //                var data1 = data1.data;
                                                   //                if (jQuery.isEmptyObject((data1))) {
                                                   //                }
                                                   //                else {
                                                   //                    binding_master_values(data);
                                                   //                }
                                                   //            }
                                                   //            else {
                                                   //                console.log(data1);
                                                   //            }
                                                   //        }
                                                   //    });
                                                   //}
                                               }
                                               else {
                                                   console.log(data1);
                                               }
                                           }
                                       });
                                   });
                               }
                           });
                 }
             }
         }
     });

}
//Service Type Edit
function service_edit(event, id) {
    // $('#service_tbl').dataTable().fnDraw();
    debugger;
    var id = id;
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         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 {
                     $('.table_view').hide();
                     $('#add_new_servicetype').hide();
                     $('#add_new_servicetype_form').show();
                     $('#goback2').show();
                     $('#goback1').hide();
                     $('#classfilter-section').removeClass('in');
                     $('#type_filter_section').hide();
                     form_clear();
                     debugger;
                     //$('#add_new_servicetype').click();

                     var master_data_type = {
                         Dml_Indicator: 'SE',
                         master_servie_type_id: id
                     }
                     $.ajax({
                         async: true,
                         type: 'POST',
                         url: '/Billing/MasterServiceType_Ins_Upd_Del',
                         contentType: 'application/json; charset=UTF-8',
                         data: JSON.stringify(master_data_type),
                         success: function (data) {
                             debugger;
                             $(".dtfh-floatingparent.dtfh-floatingparenthead th").click();
                             var data1 = JSON.parse(data)
                             console.log(data1);
                             if (data1.msg == "Success") {
                                 var data1 = data1.data;
                                 if (jQuery.isEmptyObject((data1))) {
                                 }
                                 else {
                                     $('#Master_Type').val(data1[0].service_type_id);
                                     $('#Master_Type').prop('disabled', true);
                                     $('#master_service_id').val(id);
                                     master_type_change(data1[0].hospitalid, data1[0].branch_id);

                                     $('#service_name').val(data1[0].master_service_name);
                                     $('#service_descrp').val(data1[0].master_service_description);
                                     $('#remarks').val(data1[0].remarks);

                                     //--One master concept--
                                     //$('#pkg_branch_id').val(data1[0].branch_id);
                                     //if ($('#pkg_branch_id').val() == null || $('#pkg_branch_id').val() == 0) {
                                     //    $('#pkg_branch_id').attr('disabled', false);
                                     //}
                                     //else {
                                     //    $('#pkg_branch_id').attr('disabled', true);
                                     //}
                                     $('a[data-toggle=ChkAuthorization][data-title="' + data1[0].authorization_required + '"]').removeClass('not-active');
                                     $('a[data-toggle=ChkAuthorization][data-title="' + data1[0].authorization_required + '"]').addClass('active');
                                     $('#ChkAuthorization').val(data1[0].authorization_required);
                                     $('#active_from').val(data1[0].active_from);
                                     $('#active_from').attr('disabled', true);
                                     $('#active_to').val(data1[0].active_to);
                                     $('#increase_val').val(data1[0].increase_from);

                                     if (data1[0].service_type_id == 5) {
                                         $('#div_scheme').attr('hidden', false)
                                         $('#div_scheme').addClass('disabled')
                                         if (data1[0].is_scheme == 1) {
                                             $('a[data-toggle=is_scheme][data-title="1"]').removeClass('not-active');
                                             $('a[data-toggle=is_scheme][data-title="1"]').addClass('active');
                                             $('a[data-toggle=is_scheme][data-title="0"]').removeClass('active');
                                             $('a[data-toggle=is_scheme][data-title="0"]').addClass('not-active');
                                         }
                                         else {
                                             $('a[data-toggle=is_scheme][data-title="1"]').removeClass('active');
                                             $('a[data-toggle=is_scheme][data-title="1"]').addClass('not-active');
                                             $('a[data-toggle=is_scheme][data-title="0"]').removeClass('not-active');
                                             $('a[data-toggle=is_scheme][data-title="0"]').addClass('active');
                                         }
                                         $('#is_scheme').val(data1[0].is_scheme);
                                     }
                                     else {
                                         $('#div_scheme').attr('hidden', true)
                                     }

                                     var appointment = [];
                                     var t = data1[0].appointment_mode.split(",");
                                     $('#SelDay').val(t).trigger('change');
                                    
                                     $('#btnsubmit').html('Update');
                                 }
                             }
                             else {
                                 console.log(data1);
                             }
                         }
                     })
                 }
             }
         }
     });
}


//Master data type values binding code
function binding_master_values(data) {
    debugger;
    $('#service_tbl').DataTable().destroy();
    var data1 = JSON.parse(data)
    if (data1.msg == "Success") {
        var j = 1, row_data = '', auth_req = '';
        $('#service_tbl tbody').empty();
        $('#service_tbl').dataTable().fnClearTable();
        $('#service_tbl').dataTable().fnDestroy();
        $.each(data1.data, function (i, emp) {
            if (emp.authorization_required == 1) {
                auth_req = 'Yes';
            }
            else if (emp.authorization_required == 2) {
                auth_req = 'No'
            }
            else {
                auth_req = '-'
            }
            var appointment = [];
            var t = emp.appointment_mode.split(",");
            var v = 0;
            for (var k = 0; k < t.length; k++) {
                appointment[v] = $("#SelDay option[value='" + t[k] + "']").text();
                v++;
            }

            var active_inactive = '';
            if (emp.STATUS == '1') {
                active_inactive = '<span class="label label-success">Active</span>';
            }
            else if (emp.STATUS == '2') {
                active_inactive = '<span class="label label-warning">Inactive</span>';
            }

            //edit = "<button class='btn editicon-btn btn-xs' data-toggle='tooltip' data-placement='bottom' title='Edit' data-id='OS_E1'  data-type='role_rights_edit' data-nav='sp_div' data-name='add_new_servicetype_form'   onclick=service_edit(this,'" + emp.master_servie_type_id + "')><i class='fa fa-pencil' aria-hidden='true'></i></button>";
            //del = "<button class='btn deleteicon-btn btn-xs'  data-toggle='tooltip' data-placement='bottom' title='Delete' data-id='OS_D1' data-type='role_rights_delete' data-nav='sp_delete' data-name='add_new_servicetype_form'  onclick=type_delete(this,'" + emp.master_servie_type_id + "','DD',2)><i class='fa fa-trash-o' aria-hidden='true'></i></button>";
            //active_and_inactive = "<p><button class='btn btn-info btn-xs'  data-toggle='tooltip' data-placement='bottom' title='In Active'  data-id='OS_D1' data-type='role_rights_delete' data-nav='sp_delete' data-name='add_new_servicetype_form'  onclick=type_delete(this,'" + emp.master_servie_type_id + "','DA','inactive')><i class='fa fa-minus-square-o mr-5' aria-hidden='true'></i>In Active</button></p>";
            //other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-toggle btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active_and_inactive + '</div></div></div></div>';

            //if (emp.STATUS == 1) {
            //    row_data = row_data + "<tr><td id=service_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.service_name + "</td><td>" + emp.master_service_name + "</td><td>" + emp.master_service_description + "</td><td>" + emp.remarks + "</td><td>" + active_inactive + "</td><td>" + other_info + "</td></tr>";
            //}
            //else if (emp.STATUS == 2) {
            //    row_data = row_data + "<tr><td id=service_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.service_name + "</td><td>" + emp.master_service_name + "</td><td>" + emp.master_service_description + "</td><td>" + emp.remarks + "</td><td>" + active_inactive + "</td><td><button class='btn deleteicon-btn btn-xs'  data-toggle='tooltip' data-placement='bottom' title='Delete' data-id='OS_D1' data-type='role_rights_delete' data-nav='sp_delete' data-name='add_new_servicetype_form'  onclick=type_delete(this,'" + emp.master_servie_type_id + "','DD',2)><i class='fa fa-trash-o' aria-hidden='true'></i></button><button class='btn btn-info btn-xs'  data-toggle='tooltip' data-placement='bottom' title='Active'  data-id='OS_D1' data-type='role_rights_delete' data-nav='sp_delete' data-name='add_new_servicetype_form'  onclick=type_delete(this,'" + emp.master_servie_type_id + "','DA','active')><i class='fa fa-check-square-o' aria-hidden='true'></i></button></td></tr>";
            //}
            //else {
            //    row_data = row_data + "<tr><td id=service_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.service_name + "</td><td>" + emp.master_service_name + "</td><td>" + emp.master_service_description + "</td><td>" + emp.remarks + "</td><td>" + active_inactive + "</td><td><a  data-toggle='tooltip' title='In Active'  class='btn btn-info btn-xs mr-10' href='javascript:void(0);'  data-id='OS_D1' data-type='role_rights_delete' data-nav='sp_delete' data-name='add_new_servicetype_form'  onclick=type_delete(this,'" + emp.master_servie_type_id + "','DA',1)><i class='fa fa-check-square-o' aria-hidden='true'></i></a></td></tr>";
            //}
            //edit = '<a  class="edit_icon" title="Edit" data-id="CD_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="edit_CategoryDepartment_form" onclick=CategoryorDtitle="Edit" data-id="OS_E1"  data-type="role_rights_edit" data-nav="sp_div" data-name="add_new_servicetype_form"   onclick=service_edit(this,"' + emp.master_servie_type_id + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';
            edit = "<a class='edit_icon' data-toggle='tooltip' data-placement='bottom' title='Edit' data-id='OS_E1'  data-type='role_rights_edit' data-nav='sp_div' data-name='add_new_servicetype_form'   onclick=service_edit(this,'" + emp.master_servie_type_id + "')><i class='fa fa-pencil' aria-hidden='true'></i></a>";
            Del = '<a class="delete_icon" title="Delete" data-id="OS_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="add_new_servicetype_form"  onclick=type_delete(this,"' + emp.master_servie_type_id + '","DD")><i class="fa fa-trash-o" aria-hidden="true"></i></a>';


           // edit = "<button class='btn editicon-btn btn-xs' data-toggle='tooltip' data-placement='bottom' title='Edit' data-id='OS_E1'  data-type='role_rights_edit' data-nav='sp_div' data-name='add_new_servicetype_form'   onclick=service_edit(this,'" + emp.master_servie_type_id + "')><i class='fa fa-pencil' aria-hidden='true'></i></button>";
            //del = "<button class='btn deleteicon-btn btn-xs'  data-toggle='tooltip' data-placement='bottom' title='Delete' data-id='OS_D1' data-type='role_rights_delete' data-nav='sp_delete' data-name='add_new_servicetype_form'  onclick=type_delete(this,'" + emp.master_servie_type_id + "','DD')><i class='fa fa-trash-o' aria-hidden='true'></i></button>";

            if (emp.STATUS == 1) {
               // active_and_inactive = "<p><button class='btn btn-blue btn-xs'  data-toggle='tooltip' data-placement='bottom' title='In Active'  data-id='OS_D1' data-type='role_rights_delete' data-nav='sp_delete' data-name='add_new_servicetype_form'  onclick=type_delete(this,'" + emp.master_servie_type_id + "','inactive')><i class='fa fa-minus-square-o mr-5' aria-hidden='true'></i>In Active</button></p>";

                active_and_inactive = "<a class='inactive_icon'  data-toggle='tooltip' data-placement='bottom' title='In Active'  data-id='OS_D1' data-type='role_rights_delete' data-nav='sp_delete' data-name='add_new_servicetype_form'  onclick=type_delete(this,'" + emp.master_servie_type_id + "','inactive')><i class='fa fa-minus-square-o' aria-hidden='true'></i></a>";
            }
            else {
               // active_and_inactive = "<p><button class='btn btn-blue btn-xs'  data-toggle='tooltip' data-placement='bottom' title='Active'  data-id='OS_D1' data-type='role_rights_delete' data-nav='sp_delete' data-name='add_new_servicetype_form'  onclick=type_delete(this,'" + emp.master_servie_type_id + "','active')><i class='fa fa-check-square-o mr-5' aria-hidden='true'></i>Active</button></p>";

                active_and_inactive = "<a class='active_icon'  data-toggle='tooltip' data-placement='bottom' title='Active'  data-id='OS_D1' data-type='role_rights_delete' data-nav='sp_delete' data-name='add_new_servicetype_form'  onclick=type_delete(this,'" + emp.master_servie_type_id + "','active')><i class='fa fa-check-square-o' aria-hidden='true'></i></a>";
            }

            if (emp.STATUS == 1) {
                //other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-toggle btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active_and_inactive + '</div></div></div></div>';

                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" style="cursor: pointer;" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + edit + '</li><li>' + Del + '</li><li>' + active_and_inactive + '</li></div></ul></div>';

            }
            else {
               // other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + del + '<a href="#" class="dropdown-toggle btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active_and_inactive + '</div></div></div></div>';
                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" style="cursor: pointer;" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + Del + '</li><li>' + active_and_inactive + '</li></div></ul></div>';
            }
            row_data = row_data + "<tr><td id=service_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.service_name + "</td><td>" + emp.master_service_name + "</td><td>" + emp.master_service_description + "</td><td>" + emp.remarks + "</td><td>" + active_inactive + "</td><td>" + other_info + "</td></tr>";

            j = j + 1;
        })
        $('#service_tbl tbody').append(row_data);
        //$('#service_tbl').DataTable();
        datatable('service_tbl', 'Other_Setup', 'A4');
    }
}
//Load Branch
function LoadBranch(branchid) {
    debugger;
    document.getElementById("pkg_branch_id").innerHTML = null;
    var hospital_name_id = ''
    if ($('#filter_hospitalname_id').val() == 0) {
        hospital_name_id = $('#hospitalname_id').val()
    }
    else {
        hospital_name_id = $('#filter_hospitalname_id').val()
    }

    if ($('#hiddenRole_id').val() == 0) {
        var subdepartment = {
            Dml_Indicator: "OHB",
            OS_hospitalid: hospital_name_id
        }
    }
    else {
        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) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    $('#pkg_branch_id').empty();
                    $('#branch_id').empty();
                    var SubDepartment = '';
                    var default_opt = '<option value="0">--Select--</option>';
                    var fil_default_opt = '<option value="0">All</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#pkg_branch_id').append(SubDepartment);
                        $('#pkg_branch_id').val(data1.Table[0].hospital_branch_id)
                        $('#branch_id').append(SubDepartment);
                        CheckCodeTemplate();
                    }
                    else {
                        $('#pkg_branch_id').append(default_opt + SubDepartment);
                        $('#branch_id').append(fil_default_opt + SubDepartment);
                    }

                    if ($('#hiddenRole_id').val() != 2 && $('#hiddenRole_id').val() != 0) {
                        $('#pkg_branch_id').val($('#HospitalBranchID').val());
                        $('#pkg_branch_id').attr('disabled', true);

                        $('#branch_id').val($('#HospitalBranchID').val());
                        $('#branch_id').attr('disabled', true);
                        CheckCodeTemplate();
                    }

                    if ($('#hiddenRole_id').val() == 0 && branchid != undefined) {
                        $('#pkg_branch_id').val(branchid);
                    }
                    //$('#pkg_branch_id').val(val);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

function form_clear() {
    debugger;
    $('#appointment_div').hide();
    $('#master_service_Partial_div').hide();
    $('#master_service_Authorization_div').hide();
    $('#increase_div').hide();
    $('#active_from_div').hide();
    $('#active_to_div').hide();
    $('#div_service_name').hide();
    $('#div_service_desc').hide();
    $('#div_remarks').hide();
    $('#div_hospital_name').hide();
    $('#div_branch').hide();
    $('#service_name').val('');
    $('#service_descrp').val('');
    $('#remarks').val('');
    $('a[data-toggle=ChkAuthorization][data-title=1]').removeClass('active');
    $('a[data-toggle=ChkAuthorization][data-title=1]').addClass('not-active');
    $('a[data-toggle=ChkAuthorization][data-title=2]').removeClass('active');
    $('a[data-toggle=ChkAuthorization][data-title=2]').addClass('not-active');
    $('#ChkAuthorization').val('');
    $('#active_from').val('');
    $('#active_to').val('');
    //--One master concept--
    //var length = $('#pkg_branch_id > option').length;
    //if ($('#hiddenRole_id').val() == 2) {
    //    $('#pkg_branch_id').val(0);
    //    $('#pkg_branch_id').prop('disabled', false);
    //}


    $('#Master_Type').val('');
    $('#Master_Type').prop('disabled', false);
    $('#btnsubmit').html('Submit');
    $('#btnsubmit').hide();

    if ($('#hiddenRole_id').val() == 0) {
        $('#filter_hospitalname_id').val(0);
        $('#branch_id').empty();

        $('#hospitalname_id').empty();
        //$('#pkg_branch_id').empty();

    }

    $('#div_scheme').attr('hidden', true)
    $('#div_scheme').removeClass('disabled')

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

    $('#is_scheme').val('');
}

//$('#type_filter_section').click(function () {

//    $(".control-sidebar").show();
//});
//filter
$('#req_filter').click(function () {
    debugger;
    // $(".control-sidebar").hide();
    // $(".control-sidebar").toggle();
    debugger;
   // var branch_id = $('#branch_id').val();  //--one master concept--
    var master_type = $('#Master_Type_filter').val();
    var hospitalid = $('#filter_hospitalname_id').val()
    var condition = '';
    //if ($.trim((branch_id)) == "" || $.trim((branch_id)) == '0' ) {
    //    //&& (master_type == '' || master_type == 0) && (hospitalid == '' || hospitalid == 0)) {
    //    $('#Pbranch_id').show();
    //    $('#Pbranch_id').fadeOut(8000);
    //    $('#Pbranch_id').html('Please Enter Any Values');
    //    $('#Pbranch_id').focus();

    //    return false;
    //}
    //if (master_type == "" || master_type == '0') {
    //    //&& (master_type == '' || master_type == 0) && (hospitalid == '' || hospitalid == 0)) {
    //    $('#PMaster_Type_filter').show();
    //    $('#PMaster_Type_filter').fadeOut(8000);
    //    $('#PMaster_Type_filter').html('Please Select Type');
    //    $('#PMaster_Type_filter').focus();

    //    return false;
    //}

    //else {
        //if (branch_id != '' && branch_id != 0) {
        //    condition = condition + ' msd.branch_id =' + branch_id + ' and ';
        //}
        //if (master_type != '' && master_type != 0) {
        //    condition = condition + ' msd.service_type_id =' + master_type + ' and '
        //}
        var mastertype = ''
        if (master_type == '' || master_type == '0') {
            mastertype = $('#Hservice_type_id').val()
        }
        else {
            mastertype = master_type
        }

        if ($('#hiddenRole_id').val() == 0) {
            var master_data_type = {
                Dml_Indicator: 'SATB',
                OS_hospitalid: $('#filter_hospitalname_id').val(),
               // branch_id: branch_id, //one master concept
                service_type_id: mastertype,
                flag: $('#FStatus').val()
            }
        }
        else {
            var master_data_type = {
                Dml_Indicator: 'TLTB',
                //condition: condition,
                // branch_id: branch_id, //one master concept
                master_service_name: mastertype,
                flag: $('#FStatus').val()
            }
        }

        $.ajax({
            async: true,
            type: 'POST',
            url: '/Billing/MasterServiceType_Ins_Upd_Del',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(master_data_type),
            success: function (data) {
                debugger;
                var data1 = JSON.parse(data)
                if (data1.msg == "Success") {
                    var data1 = data1.data;
                    if (jQuery.isEmptyObject((data1))) {
                    }
                    else {
                        binding_master_values(data);
                    }
                }
                else {
                    console.log(data1);
                }
            }
        });

    //}

})
//clear
$('#req_clear').click(function () {
    debugger;
    if ($('#hiddenRole_id').val == 2) {
        $('#branch_id').val(0);
    }

    if ($('#hiddenRole_id').val() == 0) {
        $('#filter_hospitalname_id').val(0);
        $('#branch_id').empty();
    }

    $('#branch_id').val(0);
    $('#Master_Type_filter').val(0);
    $('#FStatus').val('');
    table_service();
   // LoadBranch()
})

//$("#service_name").keypress(function (e) {
//function specialchar() {
//    //alert('l')
//    if (e.charCode != 0) {
//        var regex = /^[a-zA-Z0-9\-\s]+$/
//        var key = String.fromCharCode(!e.charCode ? e.which : e.charCode);
//        if (!regex.test(key)) {
//            e.preventDefault();
//            return false;
//        }
//    }
//};

function alphanumeric_only(id) {
    debugger
    var value = $("#" + id).val();
    var preg_check = /^[a-zA-Z0-9_\.\s]+$/; 
    if (!preg_check.test(value)) {  
        $('#P' + id).show();
        $('#P' + id).fadeOut(8000);
        $("#P" + id).html('Not allow special characters');
        $("#" + id).val(value.replace(/[^a-zA-Z0-9_\.\s]+/g, ''));  
        $("#" + id).addClass('error');
        $("#" + id).focus();
    }
}


//--One master concept--
//$('#pkg_branch_id').change(function () {
//    CheckCodeTemplate();
//})