$(document).ready(function () {
    LoadMenu();
    LoadDatas();
});

function LoadMenu() {
    debugger;
    var DoctorData = {
        Dml_Indicator: 'FS'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserLog/DoctorLog',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            if (jQuery.isEmptyObject((data))) {
            }
            else {
                var data1 = JSON.parse(data);
                var menu;
                $('#SelMenu').empty();
                var default_opt = '<option value=0>-- Select--</option>';
                $.each(data1, function (i, emp) {
                    menu = menu + '<option value="' + emp.doctor_module_id + '">' + emp.doctor_module_name + '</option>';
                });

                $('#SelMenu').append(default_opt+menu);
                
            }
        }
    });
}

$('#BtnSubmit').click(function () {
    debugger;
         //save menu
    if ($('#SelMenu').val() == '' || $('#SelMenu').val() == null) {
        $('#PMainMenu').show();
        $('#PMainMenu').fadeOut(8000);
        $('#PMainMenu').html('Choose Main Menu');
        $('#SelMenu').focus();
        $('#SelMenu').val('');
        return false;
    }
    if ($('#txtSubMenu').val() == '' || $('#txtSubMenu').val() == null) {
        $('#PSubMenu').show();
        $('#PSubMenu').fadeOut(8000);
        $('#PSubMenu').html('Enter Sub Menu');
        $('#txtSubMenu').focus();
        $('#txtSubMenu').val('');
        return false;
    }
        var DoctorData = {
            dml_indicator: 'I',
            doctor_id: $('#myHiddenVar').val(),
            menu: $('#SelMenu').val(),
            sub_menu: $('#txtSubMenu').val(),
            content: $('#txtEditor').Editor("getText")
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/Template/Help_Document_Ins_Upd_Del',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(DoctorData),
            success: function (data) {
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    var data1 = JSON.parse(data);
                    $('#help_document_id').val(data1.Table[0].id);
                    InsertDocuments();
                    toastr.success('Saved Successfully');
                    $('#SelMenu').val('');
                    $('#txtSubMenu').val('');
                    $("#txtEditor").Editor("setText", '');
                    $('#Hdn_document').val('');
                    $('#divDocument').empty();
                    LoadDatas();
                }
            }
        });
});

function InsertDocuments() {
    debugger;
    if ($('#Hdn_document').val() != '' && $('#Hdn_document').val() != null && $('#help_document_id').val()!='') {
        var documentPath = $('#Hdn_document').val();
        var documentArray = documentPath.split(',');
        if (documentArray.length > 0) {
            for (var i = 0; i < documentArray.length; i++) {
                if (documentArray[i] != "" && documentArray[i] != null) {
                    var DoctorData = {
                        Dml_Indicator: "I",
                        help_document_id: $('#help_document_id').val(),
                        document: documentArray[i]
                    }
                    $.ajax({
                        async: true,
                        type: 'POST',
                        url: '/Template/Help_Document_Mapping',
                        contentType: 'application/json; charset=UTF-8',
                        data: JSON.stringify(DoctorData),
                        success: function (data) {
                            if (jQuery.isEmptyObject((data))) {

                            }
                        }
                    });
                }
            }
        }
    }
}

$('#BtnUpdate').click(function ()
{
    //Update
    if ($('#SelMenu').val() == '' || $('#SelMenu').val() == null) {
        $('#PMainMenu').show();
        $('#PMainMenu').fadeOut(8000);
        $('#PMainMenu').html('Choose Main Menu');
        $('#SelMenu').focus();
        $('#SelMenu').val('');
        return false;
    }
    if ($('#txtSubMenu').val() == '' || $('#txtSubMenu').val() == null) {
        $('#PSubMenu').show();
        $('#PSubMenu').fadeOut(8000);
        $('#PSubMenu').html('Enter Sub Menu');
        $('#txtSubMenu').focus();
        $('#txtSubMenu').val('');
        return false;
    }
    debugger;
    var DoctorData = {
        dml_indicator: 'U',
        doctor_id: $('#myHiddenVar').val(),
        menu: $('#SelMenu').val(),
        sub_menu: $('#txtSubMenu').val(),
        content: $('#txtEditor').Editor("getText"),
        id: focusedMenuID
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Template/Help_Document_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            if (jQuery.isEmptyObject((data))) {
            }
            else {
                var data1 = JSON.parse(data);
                toastr.success('Updated Successfully');
                $('#SelMenu').val('');
                $('#txtSubMenu').val('');
                $("#txtEditor").Editor("setText", '');
                $('#Hdn_document').val('');
                $('#divDocument').empty();
                focusedMenuID = '';
                $('#BtnSubmit').show();
                $('#BtnUpdate').hide();
                $("#contentMenuDetails-section").toggle();
                $('#addMenuDetails-section').toggle();
                LoadDatas();
            }
        }
    });

});

function LoadDatas() {
    var DoctorData = {
        dml_indicator: 'SD',
        doctor_id: $('#myHiddenVar').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Template/Help_Document_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            if (jQuery.isEmptyObject((data))) {
            }
            else {
                debugger;
                var data1 = JSON.parse(data);
                $('#divData').empty();
                var trhead = '<td colspan="12"><div><table class="table table-condensed body-borderless table_borderless"; width:100%><thead><tr><th>S.No</th><th>Menu</th><th></th></tr></thead><tbody>';
                var row='';
                for (var i = 0; i < data1.Table.length; i++) {
                    row = row + '<tr data-toggle="collapse" data-target="#data_' + i + '" class="accordion-toggle"><td>'+(i+1)+'</td><td>' + data1.Table[i].doctor_module_name + '</td><td><button onclick=viewdetails("data_' + i + '","' + data1.Table[i].menu + '"); class="part-payment btn btn-default  btn-sm"><span class="glyphicon glyphicon-eye-open"></span></button></td></tr><tr class="hiddenRow accordian-body collapse" id="data_' + i + '"></tr>'
                    
                }
                var trend = '</tbody></table></div></td>';
                $('#divData').html(trhead + row + trend);
            }
        }
    });
}

function viewdetails(val1, val2) {
    var menuid = val2;
        var DoctorData = {
            dml_indicator: 'S',
            doctor_id: $('#myHiddenVar').val(),
            menu: menuid
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/Template/Help_Document_Ins_Upd_Del',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(DoctorData),
            success: function (data) {
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    debugger;
                    var data1 = JSON.parse(data);
                    var trhead = '<td colspan="12"><div><table class="table table-condensed body-borderless table_borderless"; width:100%><thead><tr><th></th><th>Sub Menu</th><th>Content</th><th>Document</th><th></th><th></th></tr></thead><tbody>';
                    var tr = '';
                    var document = '';
                    for (var i = 0; i < data1.Table.length; i++) {

                        if (data1.Table[i].document != '' && data1.Table[i].document != "null" && data1.Table[i].document != null) {
                            var documentArray = data1.Table[i].document.split(',');
                            if (documentArray.length > 0) {
                                for (var k = 0; k < documentArray.length; k++) {
                                    document = document +'<a href="javascript:void(0);" id="idDocument' + (k + 1) + '" onclick=\'ViewDocument(\"' + documentArray[k] + '\",\"' + (k + 1) + '\")\'>Attachment' + (k + 1) + '</a></br>';
                                }
                            }
                        }

                        tr = tr + '<tr><td></td><td>' + data1.Table[i].sub_menu + '</td><td>' + data1.Table[i].content + '</td><td>' + document + '</td><td><button onclick=\'EditMenuDetails(\"' + data1.Table[i].id + '\",\"' + data1.Table[i].menu + '\",\"' + data1.Table[i].sub_menu + '\",\"' + data1.Table[i].content + '\")\';><span class="edit btn btn-default  btn-sm btn-add"><i class="fa fa-pencil" aria-hidden="true"></i></span></button></td><td><button onclick=DeleteMenuDetails("' + data1.Table[i].id + '");> <span class="delete btn btn-default  btn-sm btn-add"><i class="fa fa-trash" aria-hidden="true"></i></span></button></td></tr>'

                        
                       
                    }

                    

                    var trend = '</tbody></table></div></td>';
                    $('#' + val1).html(trhead + tr + trend);
                }
            }
        });
}

$("input[name='optionSubMenu']").change(function () {
    if ($("input[name='optionSubMenu']:checked").val() == 1) {
        $('#divSubMenu').show();
    }
    else {
        $('#divSubMenu').hide();
    }
});

function DeleteMenuDetails(id) {
    debugger;
    var rowID = id;
    var DoctorData = {
        dml_indicator: 'D',
        id: rowID
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Template/Help_Document_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            if (jQuery.isEmptyObject((data))) {
            }
            else {
                debugger;
                LoadDatas();
            }
        }
    });
}

var focusedMenuID = '';
function EditMenuDetails(id, menuValue, subMenu, content) {
    focusedMenuID = id;
    $('#SelMenu').val(menuValue);
    $('#txtSubMenu').val(subMenu);
    $("#txtEditor").Editor("setText", content);
    $('#BtnSubmit').html = 'Update';

    $("#contentMenuDetails-section").toggle();
    $('#addMenuDetails-section').toggle();

    $('#BtnUpdate').show();
    $('#BtnSubmit').hide();
}

var documentCount = 0;
$('#menuDocuments').change(function () {
    documentCount = documentCount + 1;
    documentupload();    
    
});



function documentupload() {
    debugger;
    if ($('#SelMenu').val() == '' || $('#SelMenu').val() == null || $('#SelMenu').val()==0) {
        $('#PMainMenu').show();
        $('#PMainMenu').fadeOut(8000);
        $('#PMainMenu').html('Choose Main Menu');
        $('#SelMenu').focus();
        $('#SelMenu').val('');
        return false;
    }

    var uploadpath = $('#menuDocuments').val();
    var fileExtension = uploadpath.substring(uploadpath.lastIndexOf(".") + 1, uploadpath.length);
    if (fileExtension == "pdf" || fileExtension == "doc" || fileExtension == "PDF" || fileExtension == "DOC" || fileExtension == "jpg" || fileExtension == "JPG" || fileExtension == "jpeg" || fileExtension == "JPEG") {
    }
    else {
        $('#PmenuDocuments').show();
        $('#PmenuDocuments').fadeOut(8000);
        $('#PmenuDocuments').html('Please upload file having extensions .pdf/.doc/.jpg/.jpeg only.');
        $('#menuDocuments').val('');
        $('#menuDocuments').focus();
        return false;
    }
    if (window.FormData !== undefined) {
        debugger;
        var fileUpload = $("#menuDocuments").get(0);
        var files = fileUpload.files;
        var fileData = new FormData();
        var name = $('#SelMenu').val()!=0 ? $('#SelMenu option:selected').text() : '';
        fileData.append(name, '');
        var id;
        if (fileExtension == "pdf" || fileExtension == "PDF") {
            id = "1";
        }
        else if (fileExtension == "jpg" || fileExtension == "JPG" || fileExtension == "jpeg" || fileExtension == "JPEG") {
            id = "2";
        }
        else{
            id = "3";
        }
        fileData.append(id, '');
        for (var i = 0; i < files.length; i++) {
            fileData.append(files[i].name, files[i]);
        }

        var apiUrl = $('#apiurl').val();

        $.ajax({
            url: apiUrl + 'ByteArrayAPI/PostHelpDocument',
            type: "POST",
            contentType: false,
            processData: false,
            data: fileData,
            success: function (result) {
                debugger;
                if (result.status) {
                    var pathName = $('#Hdn_document').val();
                    pathName = pathName + "," + result.C1;
                    $('#Hdn_document').val(pathName);
                    var referral = '<a href="javascript:void(0)" id="idDocument' + documentCount + '" onclick=\'ViewDocument(\"' + result.C1 + '\",\"' + documentCount + '\")\'>Attachment' + documentCount + '</a>&nbsp;| &nbsp;';
                    $('#divDocument').append(referral);
                    $('#menuDocuments').val('');
                }
            }
        });
    }
}

function ViewDocument(document,id) {
    debugger;
    var url = $('#apiurl').val();
    if (document != "null" && document != '') {
        $('a#idDocument'+id).attr({
            target: '_blank',
            href: url.replace('api/', "HelpDocuments/") + document
        });
    }
    else {
        $('a#idDocument').attr({
            href: "javascript:void(0);"
        });
    }
}