
$(document).ready(function () {
    debugger;
    // One Master Concept
    CheckCodeTemplate()
});


//CheckCodeTemplate
function CheckCodeTemplate() {
    debugger;
    var codeData = {
        Dml_Indicator: "BCT",
        hospital_id: $('#HospitalId').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) {
                alert('Not Added Code Template. Please Add the Code Template at SetupIds');
                window.location.href = "/Hospitalsetup"
            }
            else {
                // app speed
                TableCategoryorDepartment();
                $('#divNoCS').addClass('hidden');
                $('.divNoSD').hide();
                $('.divdlbnocs1').hide();
                $('#add_sub_dpt_cate').html('Add');
                $('#Update_Test_record').hide();
                $('#mbl_desc').addClass('hidden')
                $('#divmicon').addClass('hidden')
            }
        }
    });
}

//branch dropdown 
function LoadBranch() {
    debugger;

    var subdepartment = {
        Dml_Indicator: "LBRAN",
        name: $('#HiddenDoctorID').val()
    }
    $.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") {
                debugger;
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    var default_opt = '<option value="">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        debugger;
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#CD_branch_id').append(SubDepartment);
                        CheckCodeTemplate();
                    }
                    else {
                        $('#CD_branch_id').append(default_opt + SubDepartment);
                    }

                    $('#Tbl_branchid').append(SubDepartment);
                    TableCategoryorDepartment();

                    if ($('#hiddenRole_id').val() != 2) {
                        $('#CD_branch_id').val($('#HospitalBranchID').val());
                        $('#CD_branch_id').attr('disabled', true);
                        $('#Tbl_branchid').val($('#HospitalBranchID').val());
                        $('#Tbl_branchid').attr('disabled', true);
                        CheckCodeTemplate();
                        TableCategoryorDepartment();
                        CheckCodeTemplate();
                    }
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}


$('#CD_branch_id').change(function () {
    debugger;
    $('#txtName').val('')

    if ($('#CD_branch_id').val() != '') {
        $('#ddSelectType').attr('disabled', false)
        $('#ddSelectType').val('');
        $('#Mdisplay_order').val('');
    }
    else {
        $('#ddSelectType').attr('disabled', true)
        $('#ddSelectType').val('');
        $('#Mdisplay_order').val('')
    }
    CheckCodeTemplate();
})


// Master Category And Department insert and update //
$('#btnMCDSubmit').click(function () {
    debugger;

    var check_displayorder = check_displayOrder()
    $.when(check_displayorder)
        .then(function (data) {

            //if ($.trim($('#CD_branch_id').val()) == "") {
            //    $('#PCD_branch_id').show();
            //    $('#PCD_branch_id').fadeOut(8000);
            //    $('#PCD_branch_id').html('Select Branch');
            //    $('#CD_branch_id').focus();
            //    $('#CD_branch_id').val('');
            //    return false;
            //}

            if ($.trim($('#ddSelectType').val()) == "") {
                $('#PddSelectType').show();
                $('#PddSelectType').fadeOut(8000);
                $('#PddSelectType').html('Select Type');
                $('#ddSelectType').focus();
                $('#ddSelectType').val('');
                return false;
            }

            if ($.trim($('#txtName').val()) == "") {
                $('#PtxtName').show();
                $('#PtxtName').fadeOut(8000);
                $('#PtxtName').html('Enter ' + $('#lblName').html());
                $('#txtName').focus();
                $('#txtName').val('');
                return false;
            }

            if ($('#ddSelectType').val() == 2) {
                if ($('#is_mobile').val() == '') {
                    $('#Pis_mobile').show();
                    $('#Pis_mobile').fadeOut(8000);
                    $('#Pis_mobile').html('Select Is Mobile Yes / No').html();
                    $('#is_mobile').focus();
                    $('#is_mobile').val('');
                    return false;
                }
            }

            var dpt_lvl = $.trim($('#NoSubdepartment').attr('data_flgvalue'));

            if ($('#NoSubdepartment').val() == "") {
                if ($('#ddSelectType').val() == 1) {
                    $('#PNoSubdepartment').show();
                    $('#PNoSubdepartment').fadeOut(8000);
                    $('#PNoSubdepartment').html('Select Sub-Category Yes / No').html();
                    $('#NoSubdepartment').focus();
                    $('#NoSubdepartment').val('');
                    return false;
                }
                else {
                    $('#PNoSubdepartment').show();
                    $('#PNoSubdepartment').fadeOut(8000);
                    $('#PNoSubdepartment').html('Select Sub-Department Yes / No').html();
                    $('#NoSubdepartment').focus();
                    $('#NoSubdepartment').val('');
                    return false;
                }
            }

            if ($('#ddSelectType').val() == 2 && $('#NoSubdepartment').val() == 1) {
                if ($('#AppointmentType1').val() == '') {
                    $('#PAppointmentType1').show();
                    $('#PAppointmentType1').fadeOut(8000);
                    $('#PAppointmentType1').html('select department level booking').html();
                    $('#AppointmentType1').focus();
                    $('#AppointmentType1').val('');
                    return false;
                }
            }
            if ($('#HdnMaster_cd_id').val() == '') {
                if ($('#ddSelectType').val() == 2 && $('#NoSubdepartment').val() == 1) {
                    if ($('#AppointmentType1').val() == 2) {
                        if ($('#txtNoCD1').val() == "" || $('#txtNoCD1').val() == 0) {
                            $('#PtxtNoCD1').show();
                            $('#PtxtNoCD1').fadeOut(8000);
                            $('#PtxtNoCD1').html('Enter Number of Concurrent Slots ').html();
                            $('#txtNoCD1').focus();
                            $('#txtNoCD1').val('');
                            return false;
                        }
                    }
                }
            }

            if ($.trim($('#Mdisplay_order').val()) == "" || $.trim($('#Mdisplay_order').val()) == 0) {
                $('#PMdisplay_order').show();
                $('#PMdisplay_order').fadeOut(8000);
                $('#PMdisplay_order').html('Enter Display Order');
                $('#Mdisplay_order').focus();
                $('#Mdisplay_order').val('');
                return false;
            }

            var subcatedpt = $('#AddTestTable >tbody>tr').length;
            if ($('#NoSubdepartment').val() == 2 && subcatedpt == 0) {
                if (subcatedpt == 0);
                {
                    toastr.warning("Please add atleast one Sub-Category");
                    return false;
                }
            }

            var no_of_cun_slot = ''
            if ($('#AppointmentType1').val() == 1) {
                no_of_cun_slot = 1
            }
            else {
                no_of_cun_slot = $('#txtNoCD1').val();
            }
            var display_order = $('#Mdisplay_order').val()
            var test = 0;
            $("#CategoryandDepartmentDataTable tbody tr").each(function () {
                var Ename = ($(this).find("td").eq(3).html() == '' ? 0 : $(this).find("td").eq(2).html())
                if (name == Ename) {
                    test = 1;
                }
            });
            if (test == 1) {
                toastr.warning('Name already exist, kindly add different name');
                $('#txtName').val('');
                return false;
            }


            var img_name = $('#idpathvalue1').val();
            $('#shade_slot_submit').show();

            var MCDdata = {
                Dml_Indicator: "MI",
                Master_cd_id: $('#HdnMaster_cd_id').val(),
                type: $('#ddSelectType').val(),
                name: $('#txtName').val(),
                remarks: $('#txtRemarks').val(),
                mobile_description: $('#txtSMblDescription').val(),
                mobile_icon: $('#idpathvalue').val(),
                /*branch_id: $('#CD_branch_id').val(),*/  // One Master Concept
                mobile_booking_allowed: $('#is_mobile').val(),
                work_order_enabled: $('#NoSubdepartment').val(),
                appointment_booking_type: $('#AppointmentType1').val(),
                number_of_concurrent_slots: no_of_cun_slot,
                mobile_icon: $('#idpathvalue').val(),
                display_order: $('#Mdisplay_order').val()
            }

            $.ajax({
                async: true,
                type: 'POST',
                url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
                contentType: 'application/json; charset=UTF-8',
                data: JSON.stringify(MCDdata),
                success: function (data) {
                    $('#shade_slot_submit').hide();
                    debugger;
                    var data2 = JSON.parse(data);
                    console.log(data2);
                    if (data2.data.Table[0].msg == "already" && data2.data.Table[0].msg1 == '1') {
                        toastr.warning("Nname already exist")
                        $('#txtName').val('')
                        return;
                    }
                    else if (data2.data.Table[0].msg == "already" && data2.data.Table[0].msg1 == '2') {
                        var msg = 'Name already exist, Do you want to activate it'
                        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>", msg,
                            {

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

                                onShown: function (toast) {
                                    debugger;
                                    $("#confirmationRevertYes").click(function () {
                                        debugger;
                                        var master_category_department_id = data2.data.Table[0].msg2
                                        var SCDTbldata = {
                                            Dml_Indicator: 'active',
                                            Master_cd_id: master_category_department_id
                                        }

                                        $.ajax({
                                            async: true,
                                            type: 'POST',
                                            url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
                                            contentType: 'application/json; charset=UTF-8',
                                            data: JSON.stringify(SCDTbldata),
                                            success: function (data) {
                                                debugger;
                                                var data2 = JSON.parse(data);
                                                if (data2.data.Table[0].msg == 'Activate successfully') {
                                                    toastr.success("Activated Successfully.");
                                                    setTimeout(function () {
                                                        location.reload();
                                                    }, 1000);
                                                }
                                                if (jQuery.isEmptyObject(data)) {

                                                }
                                            },
                                            error: function (data) {

                                            }
                                        });

                                    })
                                    $("#confirmationRevertNo").click(function () {
                                        toastr.warning("Add different name")
                                        $('#txtName').val('')
                                        return;
                                    })
                                }
                            })
                    }
                    else {
                        if ($('#NoSubdepartment').val() == 2) {
                            var nosubdpt_id = data2.data.Table1;
                            var nosubdpt_id1 = nosubdpt_id[0].master_category_department_id;
                            var work_order_enable = nosubdpt_id[0].work_order_enable;
                            var Dml_Indicator = 'ctpt';
                            //var branch_id = $('#CD_branch_id').val();  // One Master Concept
                            var hospital_id = $('#HospitalId').val();
                            var created_by = $('#hiddenUser_id').val();

                            var arr = [];
                            var tble_length = $('#AddTestTable >tbody>tr').length;
                            for (i = 1; i <= tble_length; i++) {
                                debugger;
                                var line_ins = {}
                                line_ins.Dml_Indicator = Dml_Indicator,
                                    line_ins.sub_category_department_id = $('#subcatedptid_' + i).html()
                                line_ins.name = $('#txtSCDName_' + i).html(),
                                    line_ins.remarks = $('#txtSRemarks_' + i).html(),
                                    line_ins.mobile_description = $('#txtSMobileDescription_' + i).html(),
                                    line_ins.appointment_booking_type = $('#AppointmentTypehdn_' + i).html(),
                                    line_ins.number_of_concurrent_slots = $('#txtNoCD_' + i).html(),
                                    line_ins.mobile_icon_hdn = $('#fileupload_' + i).html(),
                                    line_ins.work_order_enabled = work_order_enable,
                                    line_ins.Master_cd_id = nosubdpt_id1,
                                    //line_ins.branch_id = branch_id,  // One Master Concept
                                    line_ins.hospital_id = hospital_id,
                                    line_ins.created_by = created_by,
                                    line_ins.display_order = $('#dis_order_' + i).html(),
                                    arr.push(line_ins);
                                console.log(arr);
                            }

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

                        if (jQuery.isEmptyObject((data2.data))) {
                        }
                        else {

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

                                if ($('#HdnMaster_cd_id').val() == '') {
                                    toastr.success("Added Successfully");
                                    $('#ddSelectType').removeClass('disabled')

                                }
                                else {
                                    toastr.success("Updated Successfully");
                                    $('#ddSelectType').removeClass('disabled')

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

function ClearCDdata() {
    debugger;
    $('#HdnMaster_cd_id').val('');

    // // one master concept
    //if ($('#hiddenRole_id').val() != 2) {
    //    $('#CD_branch_id').val($('#HospitalBranchID').val());
    //    $('#CD_branch_id').attr('disabled', true)
    //}
    //else {
    //    $('#CD_branch_id').val('');
    //    $('#CD_branch_id').attr('disabled', false)
    //}

    $('#ddSelectType').val('');
    $('#txtName').val('');
    $('#txtMobileDescription').val('');
    $('#txtRemarks').val('');
    $('#idpathvalue').val('');
    $('#fileToUpload').val('');
    $('#photolog').val('');
    $('#photolog').hide();
    $('#photolog').empty();
    $('#Mdisplay_order').val('');
}


//Category or Department Table
$('#Tbl_branchid').change(function () {
    debugger;
    if ($.trim($('#Tbl_branchid').val()) != "" && $('#ddTblSelectType').val() != "") {
        TableCategoryorDepartment();
    }
})

$('#ddTblSelectType').change(function () {
    debugger;
    TableCategoryorDepartment();
})

$('#ddTblSelectStatus').change(function () {
    debugger;
    TableCategoryorDepartment();
})

function TableCategoryorDepartment() {
    $('#shade_slot').show();
    debugger;

    // one master concept
    //if ($.trim($('#Tbl_branchid').val()) == "") {
    //    $('#PTbl_branchid').show();
    //    $('#PTbl_branchid').fadeOut(8000);
    //    $('#PTbl_branchid').html('Select Branch');
    //    $('#Tbl_branchid').focus();
    //    $('#Tbl_branchid').val('');
    //    return false;
    //}

    var MCDTbldata = {

        Dml_Indicator: 'CDS',
        type: $('#ddTblSelectType').val(),
        //branch_id: $('#Tbl_branchid').val(), // one master concept
        status: $('#ddTblSelectStatus').val()
    }

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

            }
            else {

                if (data2.msg == "Success") {
                    debugger;
                    var data1 = data2.data;
                    tr = $('<table class="table table-bordered table-striped table-hover dt-responsive" id="CategoryandDepartmentDataTable"/>');
                    var TypeName = '';
                    if ($('#ddTblSelectType').val() == 1) {
                        $('#list_name').html('Category list');
                        tr.append("<thead><tr><th>S.No</th><th>Category Code</th><th>Category Name</th><th>Report Display Order</th><th>Status</th><th class='tbl-action'>Actions</th></tr></thead><tbody>");
                    }
                    else if ($('#ddTblSelectType').val() == 2) {
                        $('#list_name').html('Department list');
                        tr.append("<thead><tr><th>S.No</th><th>Department Code</th><th>Department Name</th><th>Report Display Order</th><th>Status</th><th class='tbl-action'>Actions</th></tr></thead><tbody>");
                    }
                    else if ($('#ddTblSelectType').val() == 4) {
                        $('#list_name').html('Primary Cost Centre list');
                        tr.append("<thead><tr><th>S.No</th><th>Primary Cost Centre Code</th><th>Primary Cost Centre Name</th><th>Report Display Order</th><th>Status</th><th class='tbl-action'>Actions</th></tr></thead><tbody>");
                    }
                    else {
                        $('#list_name').html('Department / Category list')
                        tr.append("<thead><tr><th>S.No</th><th>Department / Category Code</th><th>Department / Category Name</th><th>Report Display Order</th><th>Status</th><th class='tbl-action'>Actions</th></tr></thead><tbody>");
                    }

                    $.each(data1.Table, function (i, emp) {
                        debugger;
                        var TypeName = '';
                        if (emp.type == 1) {
                            TypeName = 'Add New Sub-Category';
                        }
                        else if (emp.type == 2) {
                            TypeName = 'Add New Sub-Department';
                        }
                        else if (emp.type == 3) {
                            TypeName = 'Add New Secondary Cost Centre';
                        }

                        // view = '<button type="button" class="btn btn-edit btn-xs"  data-toggle="tooltip" data-placement="bottom" title="Edit" data-id="CD_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="edit_CategoryDepartment_form" onclick=CategoryorDepartmentEdit(this,"' + emp.master_category_department_id + '");><i class="fa fa-pencil" aria-hidden="true"></i></button>';
                        // Del = '<button type="button" class="btn btn-delete btn-xs" data-id="CD_D1"  data-toggle="tooltip" data-placement="bottom" title="Delete" data-type="role_rights_delete" data-nav="sp_div" data-name="delete_CategoryDepartment_form" onclick=DELCategoryorDepartment(this,"' + emp.master_category_department_id + '","MCDEL");><i class="fa fa-trash-o" aria-hidden="true"></i></button>';

                        view = '<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=CategoryorDepartmentEdit(this,"' + emp.master_category_department_id + '");><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        Del = '<a class="delete_icon" title="Delete" data-id="CD_D1" data-type="role_rights_delete" data-nav="sp_div" data-name="delete_CategoryDepartment_form" onclick=DELCategoryorDepartment(this,"' + emp.master_category_department_id + '","MCDEL");><i class="fa fa-trash-o" aria-hidden="true"></i></a>';

                        if (emp.status == "2") {
                            // active_inactive = '<p><a type="button" class="btn btn-xs btn-blue" data-id="CD_F1"  data-toggle="tooltip" data-placement="bottom" title="Make Active" data-type="role_rights_function" data-nav="sp_div" data-name="delete_CategoryDepartment_form" onclick=DELCategoryorDepartment(this,"' + emp.master_category_department_id + '","active");><i class="fa fa-check-square-o mr-5" aria-hidden="true"></i>Active</a></p>';

                            active_inactive = '<a class="active_icon" data-id="CD_F1"  title="Make Active" data-type="role_rights_function" data-nav="sp_div" data-name="delete_CategoryDepartment_form" onclick=DELCategoryorDepartment(this,"' + emp.master_category_department_id + '","active");><i class="fa fa-plus-square-o" aria-hidden="true"></i></a>';
                        }
                        else {
                            // active_inactive = '<p><a type="button" class="btn  btn-xs btn-blue" data-id="CD_F1"  data-toggle="tooltip" data-placement="bottom" title="Make In Active" data-type="role_rights_function" data-nav="sp_div" data-name="delete_CategoryDepartment_form" onclick=DELCategoryorDepartment(this,"' + emp.master_category_department_id + '","inactive");><i class="fa fa-minus-square-o mr-5" aria-hidden="true"></i>In Active</a></p>';

                            active_inactive = '<a class="inactive_icon" data-id="CD_F1"   title="Make In Active" data-type="role_rights_function" data-nav="sp_div" data-name="delete_CategoryDepartment_form" onclick=DELCategoryorDepartment(this,"' + emp.master_category_department_id + '","inactive");><i class="fa fa-minus-square-o" aria-hidden="true"></i></a>';
                        }

                        if (TypeName == 'Add New Sub-Category') {
                            //CDType = '<p class="btn btn-xs"><a class="btn_r btn-blue" data-toggle="tooltip" title="Add New Sub-Category" data-id="SCD_A1" data-type="role_rights_add" data-nav="redirect" data-name="/Hospitalsetup/subCategoryandDeptCreation?ID=' + emp.master_category_department_id + '&type=' + emp.type + '"><i class="fa fa-plus" aria-hidden="true">&nbsp;&nbsp;</i>' + TypeName + '</a></p>';

                            CDType = '<a class="btn_r"  title="Add New Sub-Category" data-id="SCD_A1" data-type="role_rights_add" data-nav="redirect" data-name="/Hospitalsetup/subCategoryandDeptCreation?ID=' + emp.master_category_department_id + '&type=' + emp.type + '">' + TypeName + '</a>';
                        }
                        else if (TypeName == 'Add New Sub-Department') {
                            //CDType = '<p class="btn btn-xs"><a class="btn_r btn-blue" data-toggle="tooltip" title="Add New Sub-Department" data-id="SCD_A1" data-type="role_rights_add" data-nav="redirect" data-name="/Hospitalsetup/subCategoryandDeptCreation?ID=' + emp.master_category_department_id + '&type=' + emp.type + '"><i class="fa fa-plus" aria-hidden="true">&nbsp;&nbsp;</i>' + TypeName + '</a></p>';

                            CDType = '<a class="btn_r" title="Add New Sub-Department" data-id="SCD_A1" data-type="role_rights_add" data-nav="redirect" data-name="/Hospitalsetup/subCategoryandDeptCreation?ID=' + emp.master_category_department_id + '&type=' + emp.type + '">' + TypeName + '</a>';
                        }
                        else {
                            //CDType = '<p class="btn btn-xs"><a class="btn_r btn-blue" data-toggle="tooltip" title="Add New Secondary Cost Centre" data-id="SCD_A1" data-type="role_rights_add" data-nav="redirect" data-name="/Hospitalsetup/subCategoryandDeptCreation?ID=' + emp.master_category_department_id + '&type=' + emp.type + '"><i class="fa fa-plus" aria-hidden="true">&nbsp;&nbsp;</i>' + TypeName + '</a></p>';

                            CDType = '<a class="btn_r" title="Add New Secondary Cost Centre" data-id="SCD_A1" data-type="role_rights_add" data-nav="redirect" data-name="/Hospitalsetup/subCategoryandDeptCreation?ID=' + emp.master_category_department_id + '&type=' + emp.type + '">' + TypeName + '</a>';
                        }

                        if (emp.status == "2") {
                            //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-new" id=""> <div class="text-left">' + active_inactive + '</div></div></div></div>';

                            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" 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_inactive + '</li></div></ul></div>';

                            //other_info = '<a href="#" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover">Toggle popover</a>';

                        }
                        else {

                            //other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + view + ' ' + 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-new" id=""> <div class="text-left">' + CDType + '' + active_inactive + '</div></div></div></div>';

                            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + view + '</li><li>' + Del + '</li><li>' + active_inactive + '</li></div><li>' + CDType + '</li></ul></div>';

                            // other_info = '<a href="#" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover">Toggle popover</a>';

                        }

                        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>';
                        }

                        var CateandDpt_name = emp.cate_and_dpt_name
                        row_data = "<tr><td>" + emp.RowNo + "</td><td>" + emp.master_code + "</td><td class='tbl-name'>" + CateandDpt_name + "</td><td>" + emp.Orderby + "</td><td>" + active_inactive + "</td><td>" + other_info + "</td></tr>";
                        tr.append(row_data);

                        //$('[data-toggle="popover"]').popover({
                        //    html: true,
                        //    trigger: 'manual'
                        //});
                        //$('#dataTableBody').on('click', '[data-toggle="popover"]', function (e) {
                        //    e.preventDefault();
                        //    $(this).popover('toggle');
                        //});
                    });

                    tr.append("</tbody></table>");
                    $('#shade_slot').hide();
                    $('#TblCategoryorDepartment').html(tr);
                    datatable('CategoryandDepartmentDataTable', 'Category and Department Creation', 'A4');
                }
                else {
                    console.log(data);
                }
            }
        },

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

//upload Mobile Icon
$('#fileToupload').change(function () {
    debugger;
    packageimageupload();
});

//Showing Save File
function id_image_show(id) {
    window.open(id, '_blank');
}

//Saved Mobile Icon upload file
function packageimageupload() {
    debugger;
    var uploadpath = $('#fileToupload').val();
    var fileExtension = uploadpath.substring(uploadpath.lastIndexOf(".") + 1, uploadpath.length);
    if (window.FormData !== undefined) {
        var fileUpload = $("#fileToupload").get(0);
        var files = fileUpload.files;

        if (files[0].size > 2e+6) {

            toastr.warning('up to 2 MB per file!');
            $('#fileToupload').val('');
            return false;
        };
        var filePath = fileUpload.value;

        // Allowing file type
        var allowedExtensions =
            /(\.jpg|\.jpeg|\.png)$/i;

        if (!allowedExtensions.exec(filePath)) {
            toastr.warning('Invalid File Type');
            $('#fileToupload').val('');
            fileInput.value = '';
            return false;
        }
        var fileData = new FormData();
        for (var i = 0; i < files.length; i++) {
            fileData.append(files[i].name, files[i]);
        }
        fileData.append('username', 'Manas');
        $.ajax({
            //url: '/Hospitalsetup/UploadPhotoFiles?branch_id=' + $('#CD_branch_id').val(), // one master concept``
            url: '/Hospitalsetup/UploadPhotoFiles',
            type: "POST",
            contentType: false,
            processData: false,
            data: fileData,
            success: function (result) {
                $.each(result, function (i, emp) {
                    debugger;
                    $('#photolog').empty();
                    $('#photolog').show();
                    $('#idpathvalue').val(emp);
                    //$('#photolog').append('<a href="' + $('#hdnCDfilepath').val() + "/" + $('#HospitalId').val() + "/" + $('#CD_branch_id').val() + "/" + "CategoryAndDepartment/" + emp + '" target="_blank" class="normal-eye">View</a>');   // one master concept
                    $('#photolog').append('<a href="' + $('#hdnCDfilepath').val() + "/" + $('#HospitalId').val() + "/" + "CategoryAndDepartment/" + emp + '" target="_blank" class="normal-eye">View</a>');

                });
            }
        });
    }
}
// sub category & department
$('#fileToUpload1').change(function () {
    debugger;
    sub_categorydepartment_imageupload();
});

function sub_categorydepartment_imageupload() {
    debugger;
    var uploadpath = $('#fileToUpload1').val();
    var fileExtension = uploadpath.substring(uploadpath.lastIndexOf(".") + 1, uploadpath.length);
    if (window.FormData !== undefined) {
        var fileUpload = $("#fileToUpload1").get(0);
        var files = fileUpload.files;

        if (files[0].size > 5e+6) {
            toastr.warning('up to 5 MB per file!');
            $('#fileToUpload1').val('');
            return false;
        };
        var filePath = fileUpload.value;

        // Allowing file type
        var allowedExtensions =
            /(\.jpg|\.jpeg|\.png)$/i;

        if (!allowedExtensions.exec(filePath)) {
            toastr.warning('Invalid File Type');
            $('#fileToUpload1').val('');
            fileInput.value = '';
            return false;
        }
        $.when(checkimagedimensionvalidation(fileUpload.files[0], 512, 512)).
            done(function (Results) {
                if (Results == 'Success') {
                    var fileData = new FormData();
                    for (var i = 0; i < files.length; i++) {
                        fileData.append(files[i].name, files[i]);
                    }
                    fileData.append('username', 'Manas');
                    $.ajax({
                        //url: '/Hospitalsetup/UploadPhotoFiles?branch_id=' + $('#CD_branch_id').val(), // one master concept
                        url: '/Hospitalsetup/UploadPhotoFiles',
                        type: "POST",
                        contentType: false,
                        processData: false,
                        data: fileData,
                        success: function (result) {
                            $.each(result, function (i, emp) {
                                debugger;
                                $('#photolog1').empty();
                                $('#photolog1').show();
                                $('#idpathvalue1').val(emp);
                                //$('#photolog1').append('<a href="' + $('#hdnCDfilepath').val() + "/" + $('#HospitalId').val() + "/" + $('#CD_branch_id').val() + "/" + "CategoryAndDepartment/" + emp + '" target="_blank">Mobile Icon</a>'); // one master concept
                                $('#photolog1').append('<a href="' + $('#hdnCDfilepath').val() + "/" + $('#HospitalId').val() + "/" + "CategoryAndDepartment/" + emp + '" target="_blank">Mobile Icon</a>');

                            });
                        }
                    });
                }
                else {
                    $('#fileToUpload1').val('');
                    $('#photolog1').empty();
                }
            });
    }
}

function CategoryorDepartmentEdit(event, id) {
    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");

    $('#divsubdis').addClass('disabled');
    debugger;
    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 {
                        AddNewCategory();
                        $('#HdnMaster_cd_id').val(id);

                        var SCDTbldata = {
                            Dml_Indicator: 'MCDIDS',
                            Master_cd_id: $('#HdnMaster_cd_id').val()
                        }

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

                                }
                                else {
                                    if (data2.msg == "Success") {
                                        var data1 = data2.data;
                                        debugger;
                                        $(".dtfh-floatingparent.dtfh-floatingparenthead th").click();

                                        if (data1.Table[0].mobile_booking_allowed == 1) {
                                            $('a[data-toggle=is_mobile][data-title="1"]').removeClass('not-active');
                                            $('a[data-toggle=is_mobile][data-title="1"]').addClass('active');
                                            $('a[data-toggle=is_mobile][data-title="2"]').removeClass('active');
                                            $('a[data-toggle=is_mobile][data-title="2"]').addClass('not-active');
                                        }
                                        else if (data1.Table[0].mobile_booking_allowed == 2) {
                                            $('a[data-toggle=is_mobile][data-title="1"]').removeClass('active');
                                            $('a[data-toggle=is_mobile][data-title="1"]').addClass('not-active');
                                            $('a[data-toggle=is_mobile][data-title="2"]').removeClass('not-active');
                                            $('a[data-toggle=is_mobile][data-title="2"]').addClass('active');
                                        }
                                        $('#is_mobile').val(data1.Table[0].mobile_booking_allowed);

                                        var type = data1.Table[0].type
                                        if (type == 2) {
                                            $('#mbl_booking_div').removeClass('hidden')
                                            if (data1.Table[0].mobile_booking_allowed == 1) {
                                                $('#divmicon').removeClass('hidden')
                                                $('#mbl_desc').removeClass('hidden')
                                            }
                                            else {
                                                $('#divmicon').addClass('hidden')
                                                $('#mbl_desc').addClass('hidden')
                                            }
                                        }
                                        else {
                                            $('#mbl_booking_div').addClass('hidden')
                                            $('#divmicon').addClass('hidden')
                                            $('#mbl_desc').addClass('hidden')
                                        }

                                        $('#edit_mbl_bkg_allowed').val(data1.Table[0].mobile_booking_allowed)
                                        $('#edit_mbl_value').val(1);

                                        if (data1.Table[0].work_order_enabled == 1) {
                                            $('a[data-toggle=NoSubdepartment][data-title="1"]').removeClass('not-active');
                                            $('a[data-toggle=NoSubdepartment][data-title="1"]').addClass('active');
                                            $('a[data-toggle=NoSubdepartment][data-title="2"]').removeClass('active');
                                            $('a[data-toggle=NoSubdepartment][data-title="2"]').addClass('not-active');

                                            $('#divsubdis').addClass('hidden')
                                            $('#divdptlb').addClass('hidden')
                                            $('.divNoSD').hide();
                                        }
                                        else if (data1.Table[0].work_order_enabled == 2) {
                                            $('a[data-toggle=NoSubdepartment][data-title="2"]').removeClass('not-active');
                                            $('a[data-toggle=NoSubdepartment][data-title="2"]').addClass('active');
                                            $('a[data-toggle=NoSubdepartment][data-title="1"]').removeClass('active');
                                            $('a[data-toggle=NoSubdepartment][data-title="1"]').addClass('not-active');

                                            $('#divsubdis').removeClass('hidden')
                                            $('#divdptlb').addClass('hidden')
                                            $('.divNoSD').show();
                                            display_order();
                                        }
                                        else {
                                            $('a[data-toggle=NoSubdepartment][data-title="1"]').removeClass('active disabled');
                                            $('a[data-toggle=NoSubdepartment][data-title="2"]').removeClass('active disabled');
                                        }
                                        $('#NoSubdepartment').val(data1.Table[0].work_order_enabled);

                                        if (data1.Table[0].appointment_booking_type == 2) {
                                            $('a[data-toggle=AppointmentType1][data-title="2"]').removeClass('not-active');
                                            $('a[data-toggle=AppointmentType1][data-title="2"]').addClass('active');
                                            $('a[data-toggle=AppointmentType1][data-title="1"]').removeClass('active');
                                            $('a[data-toggle=AppointmentType1][data-title="1"]').addClass('not-active');

                                            $('#divdptlb').addClass('hidden')
                                            $('.divdlbnocs1').hide();
                                        }
                                        else if (data1.Table[0].appointment_booking_type == 1) {
                                            $('a[data-toggle=AppointmentType1][data-title="1"]').removeClass('not-active');
                                            $('a[data-toggle=AppointmentType1][data-title="1"]').addClass('active');
                                            $('a[data-toggle=AppointmentType1][data-title="2"]').removeClass('active');
                                            $('a[data-toggle=AppointmentType1][data-title="2"]').addClass('not-active');

                                            $('#divdptlb').addClass('hidden')
                                            $('.divdlbnocs1').hide();
                                        }
                                        $('#AppointmentType1').val(data1.Table[0].appointment_booking_type);

                                        $('#photolog').show();
                                        $('#txtRemarks').val(data1.Table[0].remarks),
                                            $('#ddSelectType').val(data1.Table[0].type),
                                            $('#ddSelectType').attr('disabled', true);

                                        $('#ddSelectType').change();

                                        $('#txtName').val(data1.Table[0].name),
                                            $('#txtSMblDescription').val(data1.Table[0].mobile_description),
                                            //$('#CD_branch_id').val(data1.Table[0].branch_id); // one master concept

                                            $('#Mdisplay_order').val(data1.Table[0].Orderby)

                                        //$('#CD_branch_id').attr('disabled', true); // one master concept

                                        if (data1.Table[0].mobile_icon != null && data1.Table[0].mobile_icon != "") {
                                            $('#photolog').show();
                                            //$('#photolog').append('<a href="' + $('#hdnCDfilepath').val() + "/" + $('#HospitalId').val() + "/" + data1.Table[0].branch_id + "/" + "CategoryAndDepartment/" + data1.Table[0].mobile_icon + '" target="_blank" class="normal-eye">View</a>');  // one master concept
                                            $('#photolog').append('<a href="' + $('#hdnCDfilepath').val() + "/" + $('#HospitalId').val() + "/" + "CategoryAndDepartment/" + data1.Table[0].mobile_icon + '" target="_blank" class="normal-eye">View</a>');

                                            $('#idpathvalue').val(data1.Table[0].mobile_icon);
                                        }

                                        var type = data1.Table[0].type;
                                        var work_ord_enbl = data1.Table[0].work_order_enabled;
                                        if (type == 1 && work_ord_enbl == 2) {
                                            $('#nme').show()
                                            $('#rmks').show()
                                            $('#dlb').hide()
                                            $('#divNoCS').hide()
                                        }
                                        else if (type == 2 && work_ord_enbl == 2) {
                                            $('#nme').show()
                                            $('#rmks').show()
                                            $('#dlb').show()
                                            $('#divNoCS').show()
                                        }

                                        var edit_value = 1;
                                        $('#Hedit_value').val(edit_value)

                                        var ikey = "0";
                                        var rowcount = $('#AddTestTable >tbody >tr').length;
                                        $('#AddTestTable tbody').empty();
                                        CD_branch_id = $('#CD_branch_id').val();
                                        var value = ''
                                        $.each(data1.Table1, function (j, emp2) {
                                            debugger;
                                            var dpt_level = '', active_inactive = '', act_inact_status = '', remarks = '';
                                            var ikey = j + 1;
                                            if (emp2.types_of_appointment_booking == 1) {
                                                dpt_level = 'No'
                                            }
                                            else {
                                                dpt_level = 'Yes'
                                            }

                                            if (emp2.sub_cd_description != null && emp2.sub_cd_description != '') {
                                                remarks = emp2.sub_cd_description
                                            }
                                            else {
                                                remarks = '-'
                                            }

                                            if (emp2.status == "2") {
                                                active_inactive = '<a type="button" class="btn btn-xs" data-id="SCD_F1"  data-toggle="tooltip" data-placement="bottom" title="Make Active" data-type="role_rights_function" data-nav="sp_div" data-name="delete_CategoryDepartment_form" onclick=DELSubCateorDept_edit(this,"' + emp2.sub_category_department_id + '","subactive");><i class="fa fa-check-square-o mr-5" aria-hidden="true"></i></a>';
                                            }
                                            else {
                                                active_inactive = '<a type="button" class="btn  btn-xs" data-id="SCD_F1"  data-toggle="tooltip" data-placement="bottom" title="Make In Active" data-type="role_rights_function" data-nav="sp_div" data-name="delete_CategoryDepartment_form" onclick=DELSubCateorDept_edit(this,"' + emp2.sub_category_department_id + '","subinactive");><i class="fa fa-minus-square-o mr-5" aria-hidden="true"></i></a>';
                                            }

                                            if (emp2.status == '1') {
                                                act_inact_status = '<span class="label label-success">Active</span>';
                                            }
                                            else {
                                                act_inact_status = '<span class="label label-warning">Inactive</span>';
                                            }

                                            $('#showedit_only').removeClass('hidden');

                                            var type_cate_dpt = $('#ddSelectType').val()
                                            if (type_cate_dpt == 2) {
                                                $('#tbl_clm_hs_dpt_dlb').removeClass('hidden')
                                                $('#tbl_clm_hs_dpt_nocs').removeClass('hidden')
                                                if (emp2.status == "1") {
                                                    value = value + "<tr class='tr_input number' id='master_catedpt_id_" + emp2.master_category_department_id + "'><td>" + ikey + "</td><td id='subcatedptid_" + ikey + "' hidden>" + emp2.sub_category_department_id + "</td><td id='master_catdpt_id_" + ikey + "' hidden>" + emp2.master_category_department_id + "</td><td id='txtSCDName_" + ikey + "'>" + emp2.sub_cd_name + "</td><td id='txtSRemarks_" + ikey + "'>" + remarks + "</td><td id='fileupload_" + ikey + "' hidden>" + emp2.mobile_icon + "</td><td id='AppointmentTypehdn_" + ikey + "' hidden>" + emp2.types_of_appointment_booking + "</td><td id='AppointmentType_" + ikey + "'>" + dpt_level + "</td><td id='txtNoCD_" + ikey + "'>" + emp2.number_of_concurrent_slots + "</td><td id='dis_order_" + ikey + "'>" + emp2.Orderby + "</td><td>" + act_inact_status + "</td><td><button type='button' class='btn btn-edit btn-xs' data-toggle='tooltip' data-placement='bottom' title='Edit' onclick=labedit('" + ikey + "')><i class='fa fa-pencil' aria-hidden='true'></i></button><button type='button' class='btn btn-delete btn-xs' data-toggle='tooltip' data-placement='bottom' title='Delete' onclick='labdelete(" + ikey + ",\"1\",\"" + emp2.sub_category_department_id + "\")'><i class='fa fa-trash-o' aria-hidden='true'></i></button> " + active_inactive + "</td></tr>";
                                                }
                                                else {
                                                    value = value + "<tr class='tr_input number' id='master_catedpt_id_" + emp2.master_category_department_id + "'><td>" + ikey + "</td><td id='subcatedptid_" + ikey + "' hidden>" + emp2.sub_category_department_id + "</td><td id='master_catdpt_id_" + ikey + "' hidden>" + emp2.master_category_department_id + "</td><td id='txtSCDName_" + ikey + "'>" + emp2.sub_cd_name + "</td><td id='txtSRemarks_" + ikey + "'>" + remarks + "</td><td id='fileupload_" + ikey + "' hidden>" + emp2.mobile_icon + "</td><td id='AppointmentTypehdn_" + ikey + "' hidden>" + emp2.types_of_appointment_booking + "</td><td id='AppointmentType_" + ikey + "'>" + dpt_level + "</td><td id='txtNoCD_" + ikey + "'>" + emp2.number_of_concurrent_slots + "</td><td id='dis_order_" + ikey + "'>" + emp2.Orderby + "</td><td>" + act_inact_status + "</td><td><button type='button' class='btn btn-delete btn-xs' data-toggle='tooltip' data-placement='bottom' title='Delete' onclick='labdelete(" + ikey + ",\"1\",\"" + emp2.sub_category_department_id + "\")'><i class='fa fa-trash-o' aria-hidden='true'></i></button> " + active_inactive + "</td></tr>";
                                                }
                                            }
                                            else {
                                                $('#tbl_clm_hs_dpt_dlb').addClass('hidden')
                                                $('#tbl_clm_hs_dpt_nocs').addClass('hidden')
                                                if (emp2.status == "1") {
                                                    value = value + "<tr class='tr_input number' id='master_catedpt_id_" + emp2.master_category_department_id + "'><td>" + ikey + "</td><td id='subcatedptid_" + ikey + "' hidden>" + emp2.sub_category_department_id + "</td><td id='master_catdpt_id_" + ikey + "' hidden>" + emp2.master_category_department_id + "</td><td id='txtSCDName_" + ikey + "'>" + emp2.sub_cd_name + "</td><td id='txtSRemarks_" + ikey + "'>" + remarks + "</td><td id='dis_order_" + ikey + "'>" + emp2.Orderby + "</td><td>" + act_inact_status + "</td><td><button type='button' class='btn btn-edit btn-xs' data-toggle='tooltip' data-placement='bottom' title='Edit' onclick=labedit('" + ikey + "')><i class='fa fa-pencil' aria-hidden='true'></i></button><button type='button' class='btn btn-delete btn-xs' data-toggle='tooltip' data-placement='bottom' title='Delete' onclick='labdelete(" + ikey + ",\"1\",\"" + emp2.sub_category_department_id + "\")'><i class='fa fa-trash-o' aria-hidden='true'></i></button>" + active_inactive + "</td></tr>";
                                                }
                                                else {
                                                    value = value + "<tr class='tr_input number' id='master_catedpt_id_" + emp2.master_category_department_id + "'><td>" + ikey + "</td><td id='subcatedptid_" + ikey + "' hidden>" + emp2.sub_category_department_id + "</td><td id='master_catdpt_id_" + ikey + "' hidden>" + emp2.master_category_department_id + "</td><td id='txtSCDName_" + ikey + "'>" + emp2.sub_cd_name + "</td><td id='txtSRemarks_" + ikey + "'>" + remarks + "</td><td id='dis_order_" + ikey + "'>" + emp2.Orderby + "</td><td>" + act_inact_status + "</td><td><button type='button' class='btn btn-delete btn-xs' data-toggle='tooltip' data-placement='bottom' title='Delete' onclick='labdelete(" + ikey + ",\"1\",\"" + emp2.sub_category_department_id + "\")'><i class='fa fa-trash-o' aria-hidden='true'></i></button>" + active_inactive + "</td></tr>";
                                                }
                                            }
                                        })
                                        $('#AddTestTable > tbody:last').append(value);
                                    }
                                    else {
                                        console.log(data);
                                    }
                                }
                            },
                            error: function (data) {
                            }
                        });
                    }
                }
            }
        });
}


//active & in-active sub-category and department
function DELSubCateorDept_edit(event, id, Dml_Indicator) {
    debugger;
    var status = "";
    if (Dml_Indicator == 'subactive') {
        status = "Are you sure you want to activate?";
    }
    else if (Dml_Indicator == 'subinactive') {
        status = "Are you sure you want to In-activate?";
    }
    else {
        console.log(status)
    }

    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 SCDTbldata = {
                        Dml_Indicator: Dml_Indicator,
                        MasterSub_cd_id: id
                    }

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

                            }
                            else {
                                if (data2.msg == "Success") {
                                    var data1 = data2.data;
                                    if (Dml_Indicator == 'subinactive') {
                                        if (data1.Table[0].Message == 'Mapped') {
                                            var sub_cat_dept_id = data1.Table[0].sub_category_department_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 inactive = {
                                                                Dml_Indicator: 'inact',
                                                                MasterSub_cd_id: sub_cat_dept_id,
                                                            }
                                                            $.ajax({
                                                                async: true,
                                                                type: 'POST',
                                                                url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
                                                                contentType: 'application/json; charset=UTF-8',
                                                                data: JSON.stringify(inactive),
                                                                success: function (data) {
                                                                    debugger;
                                                                    var data1 = JSON.parse(data)
                                                                    if (data1.msg == "Success") {
                                                                        var data2 = data1.data;
                                                                        toastr.success('In Activated successfully');
                                                                        setTimeout(function () {
                                                                            location.reload();
                                                                        }, 1000);
                                                                    }
                                                                    else {
                                                                        console.log(data2)
                                                                    }
                                                                }
                                                            })
                                                        })
                                                    }
                                                })

                                        }
                                        else {
                                            toastr.success("In Activated Successfully");
                                            $('#HdnSubM_cd_id').val('');
                                            setTimeout(function () {
                                                location.reload();
                                            }, 1000);
                                        }
                                    }
                                    else if (Dml_Indicator == 'subactive') {
                                        toastr.success("Activated Successfully");
                                        $('#HdnSubM_cd_id').val('');
                                        setTimeout(function () {
                                            location.reload();
                                        }, 1000);
                                    }
                                    else {
                                        console.log(data);
                                    }
                                }
                                else {
                                    console.log(data);
                                }
                            }
                        },
                        error: function (data) {

                        }
                    });
                });
            }
        });
}


//delete category & department and active & in-active
function DELCategoryorDepartment(event, id, Dml_Indicator) {
    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 {
                        $('#HdnMaster_cd_id').val(id);

                        var status = "";
                        if (Dml_Indicator == 'active') {
                            status = "Are you sure, Do you want to Activate?";
                        }
                        else if (Dml_Indicator == 'inactive') {
                            status = "Are you sure, Do you want to In-Activate?";
                        }
                        else if (id != '') {

                            status = "Are you sure, Do you want Delete?";
                        }

                        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 SCDTbldata = {
                                            Dml_Indicator: Dml_Indicator,
                                            Master_cd_id: id
                                        }

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

                                                }
                                                else {
                                                    if (Dml_Indicator == 'MCDEL') {
                                                        debugger;
                                                        if (data2.msg == "Success") {
                                                            debugger;
                                                            var data1 = data2.data;
                                                            if (data1.Table[0].Message == 'Mapped with sub-cate&dpt' && data1.Table[0].type == '1') {
                                                                toastr.warning("The selected category Mapped with Multiple Sub-Category. Unable to Delete.");
                                                                return;
                                                            }
                                                            else if (data1.Table[0].Message == 'Mapped with sub-cate&dpt' && data1.Table[0].type == '2') {
                                                                toastr.warning("The selected department Mapped with Multiple Sub-Department. Unable to Delete.");
                                                                return;
                                                            }
                                                            else if (data1.Table[0].Message == 'Mapped with sub-cate&dpt' && data1.Table[0].type == '3') {
                                                                toastr.warning("The selected department Mapped with Multiple Secondary Cost Centre. Unable to Delete.");
                                                                return;
                                                            }
                                                            else {
                                                                toastr.success("Deleted Successfully.");
                                                                $('#HdnMaster_cd_id').val('');
                                                                TableCategoryorDepartment();
                                                            }
                                                        }
                                                        else {
                                                            console.log(data);
                                                        }
                                                    }
                                                    else if (Dml_Indicator == 'active') {
                                                        debugger;
                                                        toastr.success("Activated Successfully.");
                                                        TableCategoryorDepartment();
                                                    }
                                                    else if (Dml_Indicator == 'inactive') {
                                                        debugger;
                                                        var data1 = data2.data;
                                                        if (data1.Table[0].Message == 'Mapped with sub-cate&dpt' && data1.Table[0].type == '1') {
                                                            toastr.warning("The selected category Mapped with Multiple Sub-Category. Unable to In-active.");
                                                            return;
                                                        }
                                                        else if (data1.Table[0].Message == 'Mapped with sub-cate&dpt' && data1.Table[0].type == '2') {
                                                            toastr.warning("The selected department Mapped with Multiple Sub-Department. Unable to In-active.");
                                                            return;
                                                        }
                                                        else if (data1.Table[0].Message == 'Mapped with sub-cate&dpt' && data1.Table[0].type == '3') {
                                                            toastr.warning("The selected department Mapped with Multiple Secondary Cost Centre. Unable to In-active.");
                                                            return;
                                                        }
                                                        else {
                                                            toastr.success("In-activated Successfully.");
                                                            TableCategoryorDepartment();
                                                        }
                                                    }
                                                    else {
                                                        console.log(data);
                                                    }
                                                }
                                            },
                                            error: function (data) {

                                            }
                                        });

                                    })
                                }
                            })
                    }
                }
            }
        });
}

$('#AppointmentType1').change(function () {
    debugger;
    $('#txtNoCD1').val('')
    if ($('#AppointmentType1').val() == 2) {
        $('.divdlbnocs1').show();
    }
    else {
        $('.divdlbnocs1').hide();

    }
});

$('#AppointmentType').change(function () {
    debugger;
    if ($('#AppointmentType').val() == 2) {
        $('#divNoCS').show();
    }
    else {
        $('#divNoCS').hide();
        $('#txtNoCD').val('');
    }
});

$('#is_mobile').change(function () {
    debugger;
    if ($('#is_mobile').val() == 2) {
        if ($('#ddSelectType').val() == 2) {
            $('#mbl_desc').addClass('hidden')
            $('#divmicon').addClass('hidden')
        }
    }
    else {
        if ($('#ddSelectType').val() == 2) {
            $('#mbl_desc').removeClass('hidden')
            $('#divmicon').removeClass('hidden')
        }
    }
});

$('#ddSelectType').change(function () {
    debugger;
    if ($('#HdnMaster_cd_id').val() == '') {
        display_order()
    }

    if ($('#HdnMaster_cd_id').val() == '') {

        $('#txtName').val('');

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

        $('#is_mobile').val('')

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

        $('#AppointmentType1').val('')
    }

    if ($('#ddSelectType').val() == 2) {
        $('#lblCode').html('Department Code');
        $('#lblName').html('Department Name');

        $('#mbl_desc').addClass('hidden');

        if ($('#edit_mbl_value').val() == 1) {
            if ($('#edit_mbl_bkg_allowed').val() == 1) {
                $('#divmicon').removeClass('hidden');
            }
            else {
                $('#divmicon').addClass('hidden');
            }
        }
        else {
            $('#divmicon').addClass('hidden');
        }

        $('#mbl_booking_div').removeClass('hidden');

        $('#add_sub_dpt_cate').html('Add Sub Department');
        $('#Sub-').html('Sub Department');
        $('#lblMName').html('Sub Department Name ')
        $('#sub_catedpt_name').html('Sub Department Name')

        if ($('#ddSelectType').val() == 2 && $('#NoSubdepartment').val() == 1) {
            $('#divdptlb').removeClass('hidden');
        }

        $('#tbl_clm_hs_dpt_dlb').removeClass('hidden')
        $('#tbl_clm_hs_dpt_nocs').removeClass('hidden')
    }
    else if ($('#ddSelectType').val() == 1) {
        $('#lblName').html('Category Name');

        $('#mbl_desc').addClass('hidden');
        $('#divmicon').addClass('hidden');

        $('#mbl_booking_div').addClass('hidden');

        $('#add_sub_dpt_cate').html('Add Sub Category');
        $('#Sub-').html('Sub Category');
        $('#lblMName').html('Sub Category Name')
        $('#sub_catedpt_name').html('Sub Category Name')

        if ($('#ddSelectType').val() == 1 && $('#NoSubdepartment').val() == 1 || $('#NoSubdepartment').val() == '') {
            $('#divdptlb').addClass('hidden');
        }

        $('#tbl_clm_hs_dpt_dlb').addClass('hidden')
        $('#tbl_clm_hs_dpt_nocs').addClass('hidden')
    }
    else if ($('#ddSelectType').val() == 3) {
        $('#lblName').html('Cost Centre Name');

        $('#mbl_desc').addClass('hidden');
        $('#divmicon').addClass('hidden');

        $('#mbl_booking_div').addClass('hidden');

        $('#add_sub_dpt_cate').html('Add Secondary Cost Centre');
        $('#Sub-').html('Secondary Cost Centre');
        $('#lblMName').html('Secondary Cost Centre Name')
        $('#sub_catedpt_name').html('Secondary Cost Centre Name')

        if ($('#ddSelectType').val() == 1 && $('#NoSubdepartment').val() == 1 || $('#NoSubdepartment').val() == '') {
            $('#divdptlb').addClass('hidden');
        }

        $('#tbl_clm_hs_dpt_dlb').addClass('hidden')
        $('#tbl_clm_hs_dpt_nocs').addClass('hidden')
    }
    else {
        $('#lblName').html('Name');
        $('#add_sub_dpt_cate').html('Add');
    }

    //no subdepartment
    var nosub = $('#NoSubdepartment').val();
    if (nosub != '') {

        if ($('#NoSubdepartment').val() == 1) {
            debugger;
            $('.divdlbnocs').show();
            $('.divdlbnocs1').hide();
            $('.divNoSD').hide();
        }
        else {
            $('.divdlbnocs').hide();
            $('.divdlbnocs1').hide();
            $('.divNoSD').show();

            if ($('#ddSelectType').val() == 1) {
                $('#nme').show()
                $('#rmks').show()
                $('#dlb').hide()
                $('#divNoCS').hide()
            }
            else if ($('#ddSelectType').val() == 2) {

                $('#nme').show()
                $('#rmks').show()
                $('#dlb').show()
                $('#divNoCS').show()
            }
        }
    }
})

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

    if ($('#HdnMaster_cd_id').val() == '') {
        display_order()
    }

    $('#ddSelectType').removeClass('disabled')
    $('#txtSCDName').val('');
    $('#txtSRemarks').val('');
    $('a[data-toggle=AppointmentType][data-title="1"]').removeClass('active');
    $('a[data-toggle=AppointmentType][data-title="1"]').addClass('not-active');
    $('a[data-toggle=AppointmentType][data-title="2"]').removeClass('active');
    $('a[data-toggle=AppointmentType][data-title="2"]').addClass('not-active');
    $('#AppointmentType').val('')
    $('#txtNoCD').val('');
    $('#Sdisplay_order').val('');

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

    $('#AddTestTable tbody').empty();
    if ($('#NoSubdepartment').val() == 1) {
        debugger;
        $('.divdlbnocs').show();
        $('.divdlbnocs1').hide();
        $('.divNoSD').hide();

        if (($('#ddSelectType').val() == 1 || $('#ddSelectType').val() == 3) && $('#NoSubdepartment').val() == 1) {
            $('#divdptlb').addClass('hidden');
        }
        else {
            $('#divdptlb').removeClass('hidden')
        }
    }
    else {
        $('.divdlbnocs').hide();
        $('.divdlbnocs1').hide();
        $('.divNoSD').show();

        $('#divdptlb').addClass('hidden')

        if ($('#ddSelectType').val() == 1) {
            $('#nme').show()
            $('#rmks').show()
            $('#dlb').hide()
            $('#divNoCS').hide()
            $('#divdis_odr').show()
        }
        else if ($('#ddSelectType').val() == 2) {

            $('#nme').show()
            $('#rmks').show()
            $('#dlb').show()
            $('#divNoCS').show()
            $('#divdis_odr').show()
        }
        else if ($('#ddSelectType').val() == 3) {

            $('#nme').show()
            $('#rmks').show()
            $('#dlb').hide()
            $('#divNoCS').hide()
            $('#divdis_odr').show()
        }
    }
});

$('#AppointmentType').change(function () {
    if ($('#AppointmentType').val() == 2) {
        $('#txtNoCD').val('');
        $('#divNoCS').removeClass('hidden');
    }
    else {
        $('#divNoCS').addClass('hidden');
    }
});


// add line items
$('#Add_Test_record').click(function () {
    debugger;

    if ($.trim($('#ddSelectType').val()) == "") {
        $('#PddSelectType').show();
        $('#PddSelectType').fadeOut(8000);
        $('#PddSelectType').html('Select Type');
        $('#ddSelectType').focus();
        $('#ddSelectType').val('');
        return false;
    }

    if ($.trim($('#txtSCDName').val()) == "") {
        $('#PtxtSCDName').show();
        $('#PtxtSCDName').fadeOut(8000);
        $('#PtxtSCDName').html('Enter name');
        $('#txtSCDName').focus();
        $('#txtSCDName').val('');
        return false;
    }

    if ($('#ddSelectType').val() == 2) {
        if ($('#AppointmentType').val() == 2) {
            if ($.trim($('#txtNoCD').val()) == "" || $.trim($('#txtNoCD').val()) == 0) {
                $('#PtxtNoCD').show();
                $('#PtxtNoCD').fadeOut(8000);
                $('#PtxtNoCD').html('Enter Number of Concurrent Slots');
                $('#txtNoCD').focus();
                $('#txtNoCD').val('');
                return false;
            }
        }

        if ($.trim($('#AppointmentType').val()) == "") {
            $('#PAppointmentType').show();
            $('#PAppointmentType').fadeOut(8000);
            $('#PAppointmentType').html('Select Department Level Booking');
            $('#AppointmentType').focus();
            $('#AppointmentType').val('');
            return false;
        }
    }

    if ($.trim($('#Sdisplay_order').val()) == "" || $.trim($('#Sdisplay_order').val()) == 0) {
        $('#PSdisplay_order').show();
        $('#PSdisplay_order').fadeOut(8000);
        $('#PSdisplay_order').html('Enter Display Order');
        $('#Sdisplay_order').focus();
        $('#Sdisplay_order').val('');
        return false;
    }

    var name = $('#txtSCDName').val()
    var apt_type = $('#AppointmentType').val()
    var remarks = $('#txtSRemarks').val()
    var mbl_desc = $('#txtSMobileDescription').val()
    var NoCD = $('#txtNoCD').val()
    //var file_upl = $('#fileToUpload').val()
    var file_upl = $('#fileToUpload1').val()
    var display_order = $('#Sdisplay_order').val()

    var ikey = "0";
    var rowcount = $('#AddTestTable >tbody >tr').length;
    if (rowcount == 0) {
        var ikey = 1
    }
    else {
        var max = 0;
        var ikey = rowcount + 1;
    }

    var test = 0;
    $("#AddTestTable tbody tr").each(function () {
        var Ename = ($(this).find("td").eq(3).html() == '' ? 0 : $(this).find("td").eq(3).html())
        if (name == Ename) {
            test = 1;
        }
    });

    if (test == 1) {
        toastr.warning('Name already exist, kindly add different name');
        $('#txtSCDName').val('');
        return false;
    }

    var test1 = 0;
    $("#AddTestTable tbody tr").each(function () {
        if ($('#ddSelectType').val() == 2) {
            var columndisplayorder = ($(this).find("td").eq(9).html())
        }
        else {
            var columndisplayorder = ($(this).find("td").eq(5).html())
        }
        if (display_order == columndisplayorder) {
            test1 = 1;
        }
    });

    if (test1 == 1) {
        toastr.warning('Display Order already exist');
        $('#Sdisplay_order').val('');
        return false;
    }

    var ml_desc = '', dpt_level = '', NoCD1 = '', fle_upl = '', value = '';

    if ($('#ddSelectType').val() == 2) {
        if ($('#txtSMobileDescription').val() != '') {
            ml_desc = mbl_desc
        }
        else {
            ml_desc = '-'
        }

        var dpt_level1 = $('#AppointmentType').val();
        if ($('#AppointmentType').val() == 1) {
            dpt_level = 'No'
        }
        else if ($('#AppointmentType').val() == 2) {
            dpt_level = 'Yes'
            $('#divNoCS').show();
        }
        else {
            dpt_level = '-'
        }

        if ($('#txtNoCD').val() != '') {
            NoCD1 = NoCD
        }
        else {
            NoCD1 = 1
        }

        if ($('#fileToUpload1').val() != '') {
            fle_upl = file_upl
        }
        else {
            fle_upl = '-'
        }
    }

    var img_file_name = $('#idpathvalue1').val();
    var image_fle_name = ''
    if (img_file_name != '') {
        image_fle_name = img_file_name
    }
    else {
        image_fle_name = '-'
    }

    if ($('#Hedit_value').val() == 1) {
        if ($('#ddSelectType').val() == 2) {

            value = "<tr class='tr_input number' data-id='" + ikey + "'  id='t_" + ikey + "'><td>" + ikey + "</td><td id='subcatedptid_" + ikey + "' hidden>0</td><td id='master_catdpt_id_" + ikey + "' hidden>0</td><td id='txtSCDName_" + ikey + "'>" + name + "</td><td id='txtSRemarks_" + ikey + "'>" + remarks + "</td><td id='fileupload_" + ikey + "' hidden>" + img_file_name + "</td><td id='AppointmentTypehdn_" + ikey + "' hidden>" + dpt_level1 + "</td><td id='AppointmentType_" + ikey + "' data_flgvalue ='" + apt_type + "'>" + dpt_level + "</td><td id='txtNoCD_" + ikey + "'>" + NoCD1 + "</td><td id='dis_order_" + ikey + "'>" + display_order + "</td><td>-</td><td><button type='button' class='btn btn-edit btn-xs' data-toggle='tooltip' data-placement='bottom' title='Edit' onclick='labedit(\"" + ikey + "\")'><i class='fa fa-pencil' aria-hidden='true'></i></button>&nbsp;<button type='button' class='btn btn-delete btn-xs' data-toggle='tooltip' data-placement='bottom' title='Delete' onclick='labdelete(\"" + ikey + "\",\"1\",\"sub_category_department_id\")'><i class='fa fa-trash-o' aria-hidden='true'></i></button></td></tr>";
        }
        else {
            value = "<tr class='tr_input number' data-id='" + ikey + "'  id='t_" + ikey + "'><td>" + ikey + "</td><td id='subcatedptid_" + ikey + "' hidden>0</td><td id='master_catdpt_id_" + ikey + "' hidden>0</td><td id='txtSCDName_" + ikey + "'>" + name + "</td><td id='txtSRemarks_" + ikey + "'>" + remarks + "</td><td id='dis_order_" + ikey + "'>" + display_order + "</td><td>-</td><td><button type='button' class='btn btn-edit btn-xs' data-toggle='tooltip' data-placement='bottom' title='Edit' onclick='labedit(\"" + ikey + "\")'><i class='fa fa-pencil' aria-hidden='true'></i></button>&nbsp;<button type='button' class='btn btn-delete btn-xs' data-toggle='tooltip' data-placement='bottom' title='Delete' onclick='labdelete(\"" + ikey + "\",\"1\",\"sub_category_department_id\")'><i class='fa fa-trash-o' aria-hidden='true'></i></button></td></tr>";
        }
    }
    else {
        if ($('#ddSelectType').val() == 2) {

            value = "<tr class='tr_input number' data-id='" + ikey + "'  id='t_" + ikey + "'><td>" + ikey + "</td><td id='subcatedptid_" + ikey + "' hidden>0</td><td id='master_catdpt_id_" + ikey + "' hidden>0</td><td id='txtSCDName_" + ikey + "'>" + name + "</td><td id='txtSRemarks_" + ikey + "'>" + remarks + "</td><td id='fileupload_" + ikey + "' hidden>" + img_file_name + "</td><td id='AppointmentTypehdn_" + ikey + "' hidden>" + dpt_level1 + "</td><td id='AppointmentType_" + ikey + "' data_flgvalue ='" + apt_type + "'>" + dpt_level + "</td><td id='txtNoCD_" + ikey + "'>" + NoCD1 + "</td><td id='dis_order_" + ikey + "'>" + display_order + "</td><td><button type='button' class='btn btn-edit btn-xs' data-toggle='tooltip' data-placement='bottom' title='Edit' onclick='labedit(\"" + ikey + "\")'><i class='fa fa-pencil' aria-hidden='true'></i></button>&nbsp;<button type='button' class='btn btn-delete btn-xs' data-toggle='tooltip' data-placement='bottom' title='Delete' onclick='labdelete(\"" + ikey + "\",\"1\",\"sub_category_department_id\")'><i class='fa fa-trash-o' aria-hidden='true'></i></button></td></tr>";
        }
        else {
            value = "<tr class='tr_input number' data-id='" + ikey + "'  id='t_" + ikey + "'><td>" + ikey + "</td><td id='subcatedptid_" + ikey + "' hidden>0</td><td id='master_catdpt_id_" + ikey + "' hidden>0</td><td id='txtSCDName_" + ikey + "'>" + name + "</td><td id='txtSRemarks_" + ikey + "'>" + remarks + "</td><td id='dis_order_" + ikey + "'>" + display_order + "</td><td><button type='button' class='btn btn-edit btn-xs' data-toggle='tooltip' data-placement='bottom' title='Edit' onclick='labedit(\"" + ikey + "\")'><i class='fa fa-pencil' aria-hidden='true'></i></button>&nbsp;<button type='button' class='btn btn-delete btn-xs' data-toggle='tooltip' data-placement='bottom' title='Delete' onclick='labdelete(\"" + ikey + "\",\"1\",\"sub_category_department_id\")'><i class='fa fa-trash-o' aria-hidden='true'></i></button></td></tr>";
        }
    }

    $('#AddTestTable > tbody:last').append(value);

    var tblelength = $('#AddTestTable >tbody>tr').length;
    if (tblelength > 0) {
        $('#ddSelectType').addClass('disabled')
    }
    else {
        $('#ddSelectType').removeClass('disabled')
    }

    $('#txtSCDName').val('');
    $('#txtSRemarks').val('');
    $('#txtSMobileDescription').val('');
    $('a[data-toggle=AppointmentType][data-title="1"]').removeClass('active');
    $('a[data-toggle=AppointmentType][data-title="1"]').addClass('not-active');
    $('a[data-toggle=AppointmentType][data-title="2"]').removeClass('active');
    $('a[data-toggle=AppointmentType][data-title="2"]').addClass('not-active');
    $('#AppointmentType').val('')
    $('#divNoCS').hide();
    $('#txtNoCD').val('');
    $("#fileToUpload1").val('');
    $("#photolog1").html('');
    $("#idpathvalue1").val('');
    $('#Sdisplay_order').val(parseFloat(display_order) + parseFloat(1))

})


function labedit(val) {
    debugger;
    $('#rowid').val(val);
    $('#Add_Test_record').hide();
    $('#Update_Test_record').show();
    $('#txtSCDName').val($('#txtSCDName_' + val).html())
    $('#txtSRemarks').val($('#txtSRemarks_' + val).html())
    $('#Sdisplay_order').val($('#dis_order_' + val).html())
    var apttype = $('#AppointmentType_' + val).html();
    var img = $('#fileupload_' + val).html();
    var remarks = $('#txtSRemarks_' + val).html()

    if ($('#ddSelectType').val() != 1) {
        $('#AppointmentType').val($('#AppointmentType_' + val))
        var dpt_lvl = $.trim($('#AppointmentType_' + val).attr('data_flgvalue'));

        if (apttype == 'Yes') {
            $('a[data-toggle=AppointmentType][data-title="1"]').removeClass('active');
            $('a[data-toggle=AppointmentType][data-title="1"]').addClass('not-active');
            $('a[data-toggle=AppointmentType][data-title="2"]').removeClass('not-active');
            $('a[data-toggle=AppointmentType][data-title="2"]').addClass('active');

            $('#AppointmentType').val(2)

            $('#divNoCS').removeClass('hidden');
            $('#divNoCS').show();
        }
        else {
            $('a[data-toggle=AppointmentType][data-title="1"]').removeClass('not-active');
            $('a[data-toggle=AppointmentType][data-title="1"]').addClass('active');
            $('a[data-toggle=AppointmentType][data-title="2"]').removeClass('active');
            $('a[data-toggle=AppointmentType][data-title="2"]').addClass('not-active');

            $('#AppointmentType').val(1)

            $('#divNoCS').addClass('hidden')
            $('#divNoCS').hide();
        }

        $('#txtSMobileDescription').val($('#txtSMobileDescription_' + val).html())
        $('#txtNoCD').val($('#txtNoCD_' + val).html())

        if (img != null && img != "") {
            $('#photolog1').show();
            $('#photolog1').append('<a href="' + $('#hdnCDfilepath').val() + "/" + $('#HospitalId').val() + "" + "CategoryAndDepartment/" + img + '" target="_blank">Mobile Icon</a>');
            $('#idpathvalue1').val(img);
        }
    }
}

function labdelete(val, flag, sub_cate_dpt_id) {
    debugger;
    var sub_cate_dpt_id = sub_cate_dpt_id;
    toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", 'Are you sure you want to delete this Sub-Category?',
        {
            closeButton: true,
            progressBar: true,
            allowHtml: true,
            timeOut: 20000000,

            onShown: function (toast) {

                $("#confirmationRevertYes").click(function () {
                    debugger;
                    var line_value = {
                        Dml_Indicator: "D",
                        MasterSub_cd_id: sub_cate_dpt_id,
                        status: flag
                    }
                    if (sub_cate_dpt_id != 'sub_category_department_id') {
                        $.ajax({
                            async: true,
                            type: 'POST',
                            url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
                            contentType: 'application/json; charset=UTF-8',
                            data: JSON.stringify(line_value),
                            success: function (data) {
                                debugger;
                                var data2 = JSON.parse(data);
                                if (jQuery.isEmptyObject((data2.data))) {
                                }
                                else {
                                    if (data2.msg == "Success") {
                                        var data1 = data2.data;

                                        if (data1.Table[0].msg == 'Mapped') {
                                            toastr.warning("Mapped with setup part. Unable to Delete.");
                                            return;
                                        }
                                        else {
                                            toastr.success("Deleted successfully");
                                            window.location.reload();

                                            $('#txtSCDName').val('');
                                            $('#txtSRemarks').val('');
                                            $('#txtSMobileDescription').val('');
                                            $('a[data-toggle=AppointmentType][data-title="1"]').removeClass('active');
                                            $('a[data-toggle=AppointmentType][data-title="2"]').removeClass('active');
                                            $('a[data-toggle=AppointmentType][data-title="1"]').addClass('not-active');
                                            $('a[data-toggle=AppointmentType][data-title="2"]').addClass('not-active');
                                            $('#divNoCS').hide();
                                            $('#txtNoCD').val('');
                                            $("#fileToUpload1").val('');
                                            $("#photolog1").html('');
                                            $("#idpathvalue1").val('');

                                            $('#Update_Test_record').hide()
                                            $('#Add_Test_record').show()
                                        }
                                    }
                                }
                            }
                        });
                    }
                    else {
                        $('#t_' + val).remove();
                        toastr.success("Removed Successfully.");

                        $('#txtSCDName').val('');
                        $('#txtSRemarks').val('');
                        $('#txtSMobileDescription').val('');
                        $('a[data-toggle=AppointmentType][data-title="1"]').removeClass('active');
                        $('a[data-toggle=AppointmentType][data-title="2"]').removeClass('active');
                        $('a[data-toggle=AppointmentType][data-title="1"]').addClass('not-active');
                        $('a[data-toggle=AppointmentType][data-title="2"]').addClass('not-active');
                        $('#divNoCS').hide();
                        $('#txtNoCD').val('');
                        $("#fileToUpload1").val('');
                        $("#photolog1").html('');
                        $("#idpathvalue1").val('');

                        $('#Update_Test_record').hide()
                        $('#Add_Test_record').show()
                    }
                })
            }
        });
}

$('#Update_Test_record').click(function () {
    debugger;
    var name = $('#txtSCDName').val()
    var apt_type = $('#AppointmentType').val()
    var remarks = $('#txtSRemarks').val()
    var mbl_desc = $('#txtSMobileDescription').val()
    var NoCD = $('#txtNoCD').val()
    var file_upl = $('#fileToUpload1').val()
    var img_file_name = $('#idpathvalue1').val();
    var display_order = $('#Sdisplay_order').val();

    if ($.trim($('#txtSCDName').val()) == "") {
        $('#PtxtSCDName').show();
        $('#PtxtSCDName').fadeOut(8000);
        $('#PtxtSCDName').html('Enter name');
        $('#txtSCDName').focus();
        $('#txtSCDName').val('');
        return false;
    }

    if ($('#AppointmentType').val() == 2) {
        if ($.trim($('#txtNoCD').val()) == "" || $.trim($('#txtNoCD').val()) == 0) {
            $('#PtxtNoCD').show();
            $('#PtxtNoCD').fadeOut(8000);
            $('#PtxtNoCD').html('Enter Number of Concurrent Slots');
            $('#txtNoCD').focus();
            $('#txtNoCD').val('');
            return false;
        }
    }

    if ($('#ddSelectType').val() == 2 && $('#txtSRemarks').val() == 2) {
        if ($.trim($('#AppointmentType').val()) == "") {
            $('#PAppointmentType').show();
            $('#PAppointmentType').fadeOut(8000);
            $('#PAppointmentType').html('Select Department Level Booking');
            $('#AppointmentType').focus();
            $('#AppointmentType').val('');
            return false;
        }
    }

    if ($.trim($('#Sdisplay_order').val()) == "" || $.trim($('#Sdisplay_order').val()) == 0) {
        $('#PSdisplay_order').show();
        $('#PSdisplay_order').fadeOut(8000);
        $('#PSdisplay_order').html('Enter Display Order');
        $('#Sdisplay_order').focus();
        $('#Sdisplay_order').val('');
        return false;
    }

    var file_upload = ''
    if (img_file_name == '') {
        file_upload = '-'
    }
    else {
        file_upload = img_file_name
    }

    $('#Add_Test_record').show();
    $('#Update_Test_record').hide();
    var rowid = $('#rowid').val();
    var AppointmentType = ''
    if (apt_type == 2) {
        AppointmentType = 'Yes'
    }
    else {
        AppointmentType = 'No'
    }
    var No_of_Concurrent_Slots = ''
    if (NoCD != '') {
        No_of_Concurrent_Slots = NoCD
    }
    else {
        No_of_Concurrent_Slots = 1
    }
    var img_upload = ''
    if (img_file_name != '') {
        //img_upload = "<a class='edit link-btn' style='cursor:pointer;' onclick=id_image_show('" + $('#hdnCDfilepath').val() + "/" + $('#HospitalId').val() + "/" + $('#CD_branch_id').val() + "/" + "CategoryAndDepartment/" + file_upload + "')>View</a>"; // one master concept
        img_upload = "<a class='edit link-btn' style='cursor:pointer;' onclick=id_image_show('" + $('#hdnCDfilepath').val() + "/" + $('#HospitalId').val() + "/" + "CategoryAndDepartment/" + file_upload + "')>View</a>";
    }
    else {
        img_upload = file_upload
    }
    var test1 = 0;

    if (rowid != rowid) {
        $("#AddTestTable tbody tr").each(function () {
            if ($('#ddSelectType').val() == 2) {
                var columndisplayorder = ($(this).find("td").eq(9).html())
            }
            else {
                var columndisplayorder = ($(this).find("td").eq(5).html())
            }
            if (display_order == columndisplayorder) {
                test1 = 1;
            }
        });
    }


    if (test1 == 1) {
        toastr.warning('Display Order already exist');
        $('#Sdisplay_order').val('');
        return false;
    }
    $('#txtSCDName_' + rowid).html(name);
    $('#AppointmentTypehdn_' + rowid).html(apt_type);
    $('#AppointmentType_' + rowid).html(AppointmentType);
    $('#txtSRemarks_' + rowid).html(remarks);
    $('#txtSMobileDescription_' + rowid).html(mbl_desc);
    $('#txtNoCD_' + rowid).html(No_of_Concurrent_Slots);
    $('#fileToUpload_' + rowid).html(img_upload);
    $('#dis_order_' + rowid).html(display_order)

    $('#txtSCDName').val('');
    $('#txtSRemarks').val('');
    $('a[data-toggle=AppointmentType][data-title="1"]').removeClass('active');
    $('a[data-toggle=AppointmentType][data-title="1"]').addClass('not-active');
    $('a[data-toggle=AppointmentType][data-title="2"]').removeClass('active');
    $('a[data-toggle=AppointmentType][data-title="2"]').addClass('not-active');
    $('#AppointmentType').val('')
    $('#txtSMobileDescription').val('');
    $('#testNotes').val('');
    $("#txtNoCD").val('');
    $('#fileToUpload').val('');
    $('#idpathvalue1').val('');
    $('#photolog1').html('');
    $('#Sdisplay_order').val('');
    $('#divNoCS').addClass('hidden')
});

function clearSCDdata() {
    $('#txtNoCD').val('');
    $('#txtSCDName').val('');
    $('#txtSRemarks').val('');
    $('#txtSMobileDescription').val('');
    $('#idpathvalue').val('');
    $('#fileToUpload').val('');
    $('#photolog').val('');
    $('#photolog').hide();
    $('#photolog').empty();
    $('#HdnSubM_cd_id').val('');
}

function LoadSCDTable() {
    debugger;
    var SCDTbldata = {
        Dml_Indicator: 'SBCDS',
        Master_cd_id: $('#HdMCDID').val()
    }

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

            }
            else {

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

                    if (data1.Table1.length > 0) {
                        tr = $('<table class="table table-bordered table-striped table-hover dt-responsive" />');

                        var TypeName = '';
                        if ($('#HdMCDType').val() == 1) {
                            TypeName = 'Add Sub Category';
                            tr.append("<thead><tr><th>S.No</th><th>Sub Category Code</th><th>Sub Category Name</th><th>Mobile Icon</th><th>Mobile Booking Type</th><th>Actions</th></tr></thead><tbody>");
                        }
                        else {
                            TypeName = 'Add Sub Department';
                            tr.append("<thead><tr><th>S.No</th><th>Sub Department Code</th><th>Sub Department Name</th><th>Mobile Icon</th><th>Mobile Booking Type</th><th>Actions</th></tr></thead><tbody>");
                        }

                        $.each(data1.Table1, function (i, emp) {
                            debugger; ''

                            view = '<button type="button" class="btn btn-edit btn-xs" data-id="SCD_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="edit_SubCategoryDepartment_form" onclick=SubCategoryorDepartmentTable(this,"' + emp.sub_category_department_id + '");><i class="fa fa-pencil-square-o" aria-hidden="true"></i></button>';
                            Del = '<button type="button" class="btn btn-delete btn-xs" data-id="SCD_D1" data-type="role_rights_delete" data-nav="sp_div" data-name="delete_SubCategoryDepartment_form" onclick=DELSubCategoryorDepartment(this,"' + emp.sub_category_department_id + '");><i class="fa fa-trash" aria-hidden="true"></i></button>';

                            row_data = "<tr><td>" + emp.RowNo + "</td><td>" + emp.sub_code + "</td><td>" + emp.sub_cd_name + "</td><td><a class='edit link-btn' onclick=id_image_show('" + $('#hdnSCDfilepath1').val() + "/" + $('#HospitalId').val() + "/" + $('#HdBranchID').val() + "/" + "CategoryAndDepartment/" + emp.mobile_icon + "')>View</button></td><td>" + emp.appointment_booking_type_Name + "</td><td>" + view + " " + Del + "</td></tr>";
                            tr.append(row_data);
                        });
                        tr.append("</tbody></table>");
                        $('#TblSubCategoryorDepartment').html(tr);
                    }
                }
                else {
                    console.log(data);
                }
            }
        },
        error: function (data) {

        }
    });
}

function DELSubCategoryorDepartment(event, 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 {
                        $('#HdnSubM_cd_id').val(id);

                        var r = confirm("Are You sure You want delete..");
                        if (r == true) {

                            var SCDTbldata = {

                                Dml_Indicator: 'SCDEL',
                                MasterSub_cd_id: $('#HdnSubM_cd_id').val()
                            }

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

                                    }
                                    else {

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

                                            alert('Deleted Successfully...');
                                            $('#HdnSubM_cd_id').val('');
                                            LoadSCDTable();

                                        }
                                        else {
                                            console.log(data);
                                        }
                                    }
                                },
                                error: function (data) {

                                }
                            });

                        } else {

                        }
                    }
                }
            }
        });
}


function SubCategoryorDepartmentTable(event, id) {
    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") {
                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 {
                        clearSCDdata();
                        $('#HdnSubM_cd_id').val(id);

                        var SCDTbldata = {

                            Dml_Indicator: 'SCDIDS',
                            MasterSub_cd_id: $('#HdnSubM_cd_id').val()
                        }

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

                                }
                                else {

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

                                        $.each(data1.Table, function (i, emp) {
                                            debugger;
                                            $('#photolog').show();
                                            $('#txtNoCD').val(emp.number_of_concurrent_slots),
                                                $('#txtSCDName').val(emp.sub_cd_name),
                                                $('#txtSRemarks').val(emp.sub_cd_description),
                                                $('#txtSMobileDescription').val(emp.sub_mobile_description),
                                                $('#AppointmentType').val(emp.types_of_appointment_booking);

                                            $('a[data-toggle=AppointmentType][data-title="' + emp.types_of_appointment_booking + '"]').removeClass('not-active');
                                            $('a[data-toggle=AppointmentType][data-title="' + emp.types_of_appointment_booking + '"]').addClass('active');

                                            $('#photolog').empty();
                                            if (emp.mobile_icon != null && emp.mobile_icon != "") {
                                                $('#photolog').show();
                                                $('#photolog').append('<a href="' + $('#hdnSCDfilepath1').val() + "/" + $('#HospitalId').val() + "/" + $('#HdBranchID').val() + "/" + "CategoryAndDepartment/" + emp.mobile_icon + '" target="_blank" class="normal-eye">View</a>');
                                                $('#idpathvalue').val(emp.mobile_icon);
                                            }

                                        });

                                    }
                                    else {
                                        console.log(data);
                                    }
                                }
                            },
                            error: function (data) {

                            }
                        });
                    }
                }
            }
        });
}


//$('#txtName').change(function (){
//    debugger;
//    if ($('#HdnMaster_cd_id').val() == '') {
//        display_order()
//    }
//})


// Load category & department display order
function display_order() {
    debugger;
    var type_nd_id = '', indicator = '';

    if ($('#HdnMaster_cd_id').val() != '') {
        type_nd_id = $('#HdnMaster_cd_id').val()
        indicator = 'SCD_DO'
    }
    else {
        type_nd_id = $('#ddSelectType').val()
        indicator = 'CD_DO'
    }

    var data = {
        Dml_Indicator: indicator,
        Master_cd_id: type_nd_id,
        //branch_id: $('#CD_branch_id').val() // one master concept
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(data),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    if ($('#HdnMaster_cd_id').val() == '') {
                        $('#Mdisplay_order').val(data1.Table[0].Orderby_val)
                        $('#Sdisplay_order').val(1)
                    }

                    if ($('#HdnMaster_cd_id').val() != '') {
                        $('#Sdisplay_order').val(data1.Table[0].S_Orderby_val)
                    }
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}


$('#Mdisplay_order').change(function () {
    debugger
    check_displayOrder()
})

function check_displayOrder() {
    debugger;
    var data = {
        Dml_Indicator: 'CDDOL',
        type: $('#ddSelectType').val(),
        Master_cd_id: $('#HdnMaster_cd_id').val(),
        //branch_id: $('#CD_branch_id').val(),// one master concept
        display_order: $('#Mdisplay_order').val()
    }
    return $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(data),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    if (data1.Table[0].msg == 'already') {
                        toastr.warning('Display Order already exist');
                        $('#Mdisplay_order').val('');
                        return false;
                    }
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

