$(document).ready(function () {

    LoadBranch();

});

function LoadBranch() {
    debugger;
    $('#SelBranch').empty();
    $('#SelBranchFilter').empty();

    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") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#SelBranch').append(SubDepartment);
                        $('#SelBranch').change();
                        $('#SelBranchFilter').append(SubDepartment);
                        $('#SelBranchFilter').change();
                    }
                    else {
                        $('#SelBranch').append(default_opt + SubDepartment);
                        $('#SelBranchFilter').append(default_opt + SubDepartment);
                    }
                 
                    if ($('#hiddenRole_id').val() != 2) {
                        $('#SelBranch').val($('#HospitalBranchID').val());
                        $('#SelBranch').attr('disabled', true);
                        LoadDoctor();

                        $('#SelBranchFilter').val($('#HospitalBranchID').val());
                        $('#SelBranchFilter').attr('disabled', true);
                        LoadDoctorFilter();
                       
                    }

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

        }
    });
}

function LoadAllTemplateList() {

    var subdepartment = {
        Dml_Indicator: "S",
        doctor_id: $('#SelDoctorFilter').val(),
        branch_id: $('#SelBranchFilter').val(),
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Template/Discharge_summary_template_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
               
                $('#tblTemplateList').dataTable().fnClearTable();
                $('#tblTemplateList').dataTable().fnDestroy();
                $('#tblTemplateList').dataTable().fnDestroy();
                $('#tblTemplateList_body').empty();

                if (jQuery.isEmptyObject((data1))) {
                }
                else {                   

                    if (data1.Table.length > 0) {
                        var tr = '';
                       
                        for (var i = 0; i < data1.Table.length; i++) {

                            var edit = '<button type="button" class="btn btn-edit btn-xs mr-5" onclick=templateedit(' + data1.Table[i].template_id + ')><i class="fa fa-pencil" aria-hidden="true"></i></button>';

                            var Del = '<button type="button" class="btn btn-delete btn-xs mr-5" onclick=templatedelete(' + data1.Table[i].template_id + ')><i class="fa fa-trash-o" aria-hidden="true"></i></button>';


                            tr = tr + '<tr><td>' + (i + 1) + '</td><td>' + data1.Table[i].template_name + '</td><td>' + data1.Table[i].branch_name + '</td><td>' + data1.Table[i].doctor_name + '</td><td>' + data1.Table[i].template_title + '</td><td>' + edit + Del + '</td></tr>';
                        }

                        $('#tblTemplateList_body').append(tr);
                        $('#tblTemplateList').DataTable();

                    }
                }
            }
        }
    })
}

function templateedit(template_id) {

    var subdepartment = {
        Dml_Indicator: "TE",
        template_id: template_id        
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Template/Discharge_summary_template_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;

                if (data1.Table.length > 0) {
                    $('#Hdntemplateid').val(template_id);
                    $('#SelBranch').val(data1.Table[0].branch_id);
                    //$('#SelDoctor').val(data1.Table[0].doctor_id);
                    LoadDoctor(data1.Table[0].doctor_id)
                    $('#txtTemplateName').val(data1.Table[0].template_name);
                    $('#txtTemplateName').attr('readonly',true);
                    $('#txtTemplateTitle').val(data1.Table[0].template_title);
                    $('#is_Header').val(data1.Table[0].template_header);
                    $('a[data-toggle=is_Header][data-title="' + data1.Table[0].template_header + '"]').removeClass('not-active');
                    $('a[data-toggle=is_Header][data-title="' + data1.Table[0].template_header + '"]').addClass('active');

                    $('#is_Footer').val(data1.Table[0].template_footer);
                    $('a[data-toggle=is_Footer][data-title="' + data1.Table[0].template_footer + '"]').removeClass('not-active');
                    $('a[data-toggle=is_Footer][data-title="' + data1.Table[0].template_footer + '"]').addClass('active');

                    $('#txtPageTop').val(data1.Table[0].page_top);
                    $('#txtPageBottom').val(data1.Table[0].page_bottom);
                    $('#Hdn_doctorsign').val(data1.Table[0].sign);
                    var filepath = $('#hdnfilepath1').val();
                    var profilephoto = $('#Hdn_doctorsign').val();                                        
                    var drsigimage = filepath + $('#HospitalId').val() + '/DischargeSummaryTemplate/Sign/' + profilephoto;
                    $('#doctorsignImages').empty();
                    $('#doctorsignImages').show();
                    $('#doctorsignImages').append('<a href="' + drsigimage + '" target="_blank">Signature</a>');

                    $('#txtFontSize').val(data1.Table[0].font_size);
                    $('#txtFontStyle').val(data1.Table[0].font_style);
                    $('#Hdn_headerlogo').val(data1.Table[0].header_logo);
                    var profilephoto1 = $('#Hdn_headerlogo').val();
                    var logoimage = filepath + $('#HospitalId').val() + '/DischargeSummaryTemplate/Logo/' + profilephoto1;
                    $('#headerlogoimages').empty();
                    $('#headerlogoimages').show();
                    $('#headerlogoimages').append('<a href="' + logoimage + '" target="_blank">Logo</a>');


                    if (data1.Table1.length > 0) {

                        for (var j = 0; j < data1.Table1.length; j++) {

                            var parametername = data1.Table1[j].parameter_name;
                            var parameterlabel = data1.Table1[j].parameter_label;
                            var is_head = data1.Table1[j].is_head;
                            var is_default_text = data1.Table1[j].is_default_text;
                            var default_text = (data1.Table1[j].default_text!=null && data1.Table1[j].default_text!="" ? data1.Table1[j].default_text : "");
                            var fieldname = "";
                            var displayorder = data1.Table1[j].display_order;
                            var is_visible = data1.Table1[j].is_visible;
                            var summary_template_parameters_id = data1.Table1[j].summary_template_parameters_id;

                            var sess_key;
                            var rowcount = $.trim($('#ParametersRowCount').val());
                            var tot_row = (Number(rowcount) + 1);
                            var sess_data_ser = parametername + ",,," + parameterlabel + ",,," + is_head + ",,," + is_default_text + ",,," + default_text + ",,," + fieldname + ",,," + displayorder + ",,," + is_visible + ",,," + summary_template_parameters_id;
                            var key = '';
                            if (rowcount >= 1) {
                                var getEid = $('#tblParameters  tr').last();
                                var actId = getEid.prop('id');
                                var afterSplit = actId.split('row');
                                afterSplit = afterSplit[1].split('_')
                                afterInc = parseInt(afterSplit[0]) + 1;
                                console.log(afterInc);
                                ikey_Deit = 'row' + afterInc;
                            }
                            else {
                                ikey_Deit = 'row' + tot_row;
                            }

                            sessionStorage.setItem(ikey_Deit, sess_data_ser);

                            value = "<tr id=" + ikey_Deit + "><td>" + tot_row + "</td><td>" + parametername + "</td><td>" + parameterlabel + "</td><td>" + default_text + "</td><td>" + displayorder + "</td><td>" + (is_visible == "1" ? "Yes" : "No") + "</td><td><button class='delete btn btn-delete btn-xs btn-add' onclick='Parameterdelete(\"" + ikey_Deit + "\")'><i class='fa fa-trash' aria-hidden='true'></i></button></td></tr>";

                            $('#tblParameters > tbody:last').append(value);
                            $('#ParametersRowCount').val(tot_row);

                        }
                    }

                    $('#btnsubmit').html('Update');
                    $('#dischargesummarytemp_form').show();
                    $('#dischargesummarytemp_list').hide();
                    $('#add_new_dis_temp').hide();
                    
                }
            }
        }
    });
}

function templatedelete(template_id) {

    var msg = 'Are you sure, you want to Delete this template?'
    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 () {
                               
                               var subdepartment = {
                                   Dml_Indicator: "D",
                                   template_id: template_id
                                   
                               }
                               $.ajax({
                                   async: true,
                                   type: 'POST',
                                   url: '/Template/Discharge_summary_template_Ins_Upd',
                                   contentType: 'application/json; charset=UTF-8',
                                   data: JSON.stringify(subdepartment),
                                   success: function (data) {
                                       debugger;
                                       var data1 = JSON.parse(data);
                                       if (data1.msg == "Success") {
                                           toastr.success("Deleted Successfully");
                                           LoadAllTemplateList();
                                       }
                                   }
                               });

                           });

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

                           })
                       }
                   });

}



$('#SelBranch').change(function () {
    LoadDoctor();

})

$('#SelBranchFilter').change(function () {
    LoadDoctorFilter();

})

$('#SelDoctorFilter').change(function () {
    LoadAllTemplateList();
})

function LoadDoctor(s_doctor_id) {
    debugger;
    var roleId = $('#hiddenRole_id').val();
    var DoctorId = $('#HiddenDoctorID').val();
    if (roleId == "1") {
        var DoctorData = {
            Dml_Indicator: "DO",
            doctor_id: DoctorId,
        }
    }
    else {
        var DoctorData = {
            Dml_Indicator: "HDL",
            hospital_id: $('#HospitalId').val(),
            branch_id: $('#SelBranch').val()
        }
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/CalendarConfiguration/HosiptalSelect',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            debugger;
            if (jQuery.isEmptyObject((data))) {
            }
            else {
                var data1 = JSON.parse(data)
                if (data1.msg = "Success") {
                    
                    var SelRole = '';
                    var default_opt = '<option value="0">--Select--</option>';                    
                    $('#SelDoctor').empty();
                    if (data1.data.length > 0) {
                        for (var i = 0; i < data1.data.length; i++) {
                            
                            SelRole = SelRole + '<option value="' + data1.data[i].doctor_id + '">' + data1.data[i].first_name + '</option>';
                        }
                        $('#SelDoctor').append(default_opt + SelRole);

                        if (data1.data.length == 1) {
                            $('#SelDoctor').val(data1.data[0].doctor_id);
                            $('#SelDoctor').attr('disabled', true);
                        }

                        if (s_doctor_id != null && s_doctor_id != "") {
                            $('#SelDoctor').val(s_doctor_id);
                            $('#SelDoctor').attr('disabled', true);
                        }
                    }
                }
                else {
                    console.log(data1);
                }
            }
        }
    });
}

function LoadDoctorFilter() {
    debugger;
    var roleId = $('#hiddenRole_id').val();
    var DoctorId = $('#HiddenDoctorID').val();
    if (roleId == "1") {
        var DoctorData = {
            Dml_Indicator: "DO",
            doctor_id: DoctorId,
        }
    }
    else {
        var DoctorData = {
            Dml_Indicator: "HDL",
            hospital_id: $('#HospitalId').val(),
            branch_id: $('#SelBranchFilter').val()
        }
    }

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

                    var SelRole = '';
                    var default_opt = '<option value="0">--Select--</option>';
                    $('#SelDoctorFilter').empty();
                    if (data1.data.length > 0) {
                        for (var i = 0; i < data1.data.length; i++) {

                            SelRole = SelRole + '<option value="' + data1.data[i].doctor_id + '">' + data1.data[i].first_name + '</option>';
                        }
                        $('#SelDoctorFilter').append(default_opt + SelRole);

                        if (data1.data.length == 1) {
                            $('#SelDoctorFilter').val(data1.data[0].doctor_id);
                            $('#SelDoctorFilter').attr('disabled', true);
                            LoadAllTemplateList();
                        }
                    }
                }
                else {
                    console.log(data1);
                }
            }
        }
    });
}

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

    if ($.trim($('#SelBranch :selected').val()) == "" || $('#SelBranch :selected').val() == "0") {
        $('#PSelBranch').show();
        $('#PSelBranch').fadeOut(8000);
        $('#PSelBranch').html('Select Branch');
        $('#SelBranch').focus();
        $('#SelBranch :selected').val('0');
        return false;
    }

    if ($.trim($('#SelDoctor :selected').val()) == "" || $('#SelDoctor :selected').val() == "0") {
        $('#PSelDoctor').show();
        $('#PSelDoctor').fadeOut(8000);
        $('#PSelDoctor').html('Select Doctor');
        $('#SelDoctor').focus();
        $('#SelDoctor').val('');
        return false;
    }

    if ($.trim($('#txtTemplateName').val()) == "" || $.trim($('#txtTemplateName').val()) == null) {
        $('#PtxtTemplateName').show();
        $('#PtxtTemplateName').fadeOut(8000);
        $('#PtxtTemplateName').html('Enter Template Name');
        $('#txtTemplateName').focus();
        $('#txtTemplateName').val('');
        return false;
    }

    if ($.trim($('#txtTemplateTitle').val()) == "" || $.trim($('#txtTemplateTitle').val()) == null) {
        $('#PtxtTemplateTitle').show();
        $('#PtxtTemplateTitle').fadeOut(8000);
        $('#PtxtTemplateTitle').html('Enter Template Title');
        $('#txtTemplateTitle').focus();
        $('#txtTemplateTitle').val('');
        return false;
    }

    var tot_rowM = $.trim($('#ParametersRowCount').val());
    if (tot_rowM <= 0) {
        toastr.warning("Kindly add Parameters List");
        return false;
                                   
    }

    var dml_indicator = "I";
    if ($('#btnsubmit').html() == "Update") {
        dml_indicator = "U";
    }

    var subdepartment = {
        Dml_Indicator: dml_indicator,
        doctor_id: $('#SelDoctor').val(),
        branch_id: $('#SelBranch').val(),
        template_name: $('#txtTemplateName').val(),
        template_tile: $('#txtTemplateTitle').val(),
        template_header: $('#is_Header').val(),
        template_footer: $('#is_Footer').val(),
        page_top: $('#txtPageTop').val(),
        page_bottom: $('#txtPageBottom').val(),
        sign: $('#Hdn_doctorsign').val(),
        font_size: $('#txtFontSize').val(),
        font_style: $('#txtFontStyle').val(),
        header_logo: $('#Hdn_headerlogo').val(),
        template_id: $('#Hdntemplateid').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Template/Discharge_summary_template_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    if (data1.Table.length > 0) {
                        var template_id = data1.Table[0].template_id;

                        var parameterArray = new Array();                        
                        var tot_rowM = $.trim($('#ParametersRowCount').val());

                        if (tot_rowM > 0) {
                            var L = 0;
                            for (j = 1; j <= tot_rowM; j++) {
                                debugger;
                                var parameter_data = {}
                                var a = $('#tblParameters tbody').children('tr').eq(L);
                                var b = a.prop('id');
                                L++;

                                var gkey = b;
                                var row_data = sessionStorage.getItem(gkey);
                                var K = 0;
                                if (row_data == "") {
                                    console.log(gkey + '--------->' + row_data);
                                }
                                if (row_data != "" && row_data != null) {
                                    row_data = row_data.split(',,,');
                                    parameter_data.template_id = template_id,
                                    parameter_data.parameter_name = row_data[0],
                                    parameter_data.parameter_label = row_data[1],
                                    parameter_data.is_head = row_data[2],                                    
                                    parameter_data.is_default_text = row_data[3],
                                    parameter_data.default_text = row_data[4],
                                    //parameter_data.field_name = row_data[5],
                                    parameter_data.field_name = "",
                                    parameter_data.display_order = row_data[6],
                                    parameter_data.is_visible = row_data[7],
                                    parameter_data.summarytemp_parameter_id = row_data[8],
                                    parameter_data.created_by = $('#hiddenUser_id').val(),
                                    parameter_data.hospital_id = $('#HospitalId').val(),
                                    parameter_data.Dml_Indicator = "PI"
                                    parameterArray.push(parameter_data);
                                }
                                
                            }
                        }
                        $.ajax({
                            async: true,
                            type: 'POST',
                            url: '/Template/Discharge_summary_template_Xml',
                            contentType: 'application/json; charset=UTF-8',
                            data: JSON.stringify(parameterArray),
                            success: function (data) {
                                debugger;
                                var data1 = JSON.parse(data);
                                if (data1.msg == "Success") {

                                    if ($('#btnsubmit').html() == "Update") {
                                        toastr.success("Updated Successfully");
                                    }
                                    else {
                                        toastr.success("Saved Successfully");                                        
                                    }

                                    location.reload();
                                }
                                
                            }
                        })

                    }
                }
            }
        }
    });

})




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


    if ($.trim($('#txtParameterName').val()) == "" || $.trim($('#txtParameterName').val()) == null) {
        $('#PtxtParameterName').show();
        $('#PtxtParameterName').fadeOut(8000);
        $('#PtxtParameterName').html('Enter Parameter Name');
        $('#txtParameterName').focus();
        $('#txtParameterName').val('');
        return false;
    }

    if ($.trim($('#txtParameterLabel').val()) == "" || $.trim($('#txtParameterLabel').val()) == null) {
        $('#PtxtParameterLabel').show();
        $('#PtxtParameterLabel').fadeOut(8000);
        $('#PtxtParameterLabel').html('Enter Parameter Label');
        $('#txtParameterLabel').focus();
        $('#txtParameterLabel').val('');
        return false;
    }

    if ($.trim($('#is_head').val()) == "" || $.trim($('#is_head').val()) == null) {
        $('#Pis_head').show();
        $('#Pis_head').fadeOut(8000);
        $('#Pis_head').html('Choose head or npt');
        $('#is_head').focus();
        $('#is_head').val('');
        return false;
    }

    if ($('#is_head').val() != "1") {

        if ($.trim($('#is_default_text').val()) == "" || $.trim($('#is_default_text').val()) == null) {
            $('#Pis_default_text').show();
            $('#Pis_default_text').fadeOut(8000);
            $('#Pis_default_text').html('Choose default text or not');
            $('#is_default_text').focus();
            $('#is_default_text').val('');
            return false;
        }

        if ($('#is_default_text').val() == "1") {

            if ($.trim($('#txtDefaultText').val()) == "" || $.trim($('#txtDefaultText').val()) == null) {
                $('#PtxtDefaultText').show();
                $('#PtxtDefaultText').fadeOut(8000);
                $('#PtxtDefaultText').html('Enter Default Text');
                $('#txtDefaultText').focus();
                $('#txtDefaultText').val('');
                return false;
            }
        }
    }

    if ($.trim($('#is_visible').val()) == "" || $.trim($('#is_visible').val()) == null) {
        $('#Pis_visible').show();
        $('#Pis_visible').fadeOut(8000);
        $('#Pis_visible').html('Choose Visible');
        $('#is_visible').focus();
        $('#is_visible').val('');
        return false;
    }

    var parametername = $('#txtParameterName').val();
    var parameterlabel = $('#txtParameterLabel').val();
    var is_head = $('#is_head').val();
    var is_default_text = $('#is_default_text').val()
    var default_text = $('#txtDefaultText').summernote('code')
    var fieldname = $('#Selfieldname').val()
    var displayorder = $('#txtDisplayOrder').val()
    var is_visible = $('#is_visible').val()
    var summary_template_parameters_id = "";

    var sess_key;
    var rowcount = $.trim($('#ParametersRowCount').val());
    var tot_row = (Number(rowcount) + 1);
    var sess_data_ser = parametername + ",,," + parameterlabel + ",,," + is_head + ",,," + is_default_text + ",,," + default_text + ",,," + fieldname + ",,," + displayorder + ",,," + is_visible + ",,," + summary_template_parameters_id;
    var key = '';
    if (rowcount >= 1) {
        var getEid = $('#tblParameters  tr').last();
        var actId = getEid.prop('id');
        var afterSplit = actId.split('row');
        afterSplit = afterSplit[1].split('_')
        afterInc = parseInt(afterSplit[0]) + 1;
        console.log(afterInc);
        ikey_Deit = 'row' + afterInc;
    }
    else {
        ikey_Deit = 'row' + tot_row;
    }
    
    sessionStorage.setItem(ikey_Deit, sess_data_ser);

    value = "<tr id=" + ikey_Deit + "><td>" + tot_row + "</td><td>" + parametername + "</td><td>" + parameterlabel + "</td><td>" + default_text + "</td><td>" + displayorder + "</td><td>" + (is_visible=="1" ? "Yes":"No") + "</td><td><button class='delete btn btn-delete btn-xs btn-add' onclick='Parameterdelete(\"" + ikey_Deit + "\")'><i class='fa fa-trash' aria-hidden='true'></i></button></td></tr>";

    $('#tblParameters > tbody:last').append(value);
    $('#ParametersRowCount').val(tot_row);

    $('#txtParameterName').val('');
    $('#txtParameterLabel').val('');
    $('a[data-toggle=is_head]').removeClass('active');
    $('a[data-toggle=is_head]').addClass('not-active');
    $('#is_head').val('');
    $('a[data-toggle=is_default_text]').removeClass('active');
    $('a[data-toggle=is_default_text]').addClass('not-active');
    $('#is_default_text').val('')
    $('#Selfieldname').val('')
    $('#txtDisplayOrder').val('')
    $('a[data-toggle=is_visible]').removeClass('active');
    $('a[data-toggle=is_visible]').addClass('not-active');
    $('#is_visible').val('');
    $('#txtDefaultText').summernote('code', '')

})


function Parameterdelete(id) {
    var msg = ''
    msg = 'Are you sure, you want to 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>", msg,
                   {
                       closeButton: true,
                       progressBar: true,
                       allowHtml: true,
                       timeOut: 20000000,

                       onShown: function (toast) {
                           debugger;
                           $("#confirmationRevertYes").click(function () {
                               //delete row
                               sessionStorage.removeItem(id);
                               $('#' + id).remove();
                               var rowcount = $.trim($('#ParametersRowCount').val());
                               rowcount = rowcount - 1;
                               $('#ParametersRowCount').val(rowcount);


                           });

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

                           })
                       }
                   });
}


$('#doctorsign').change(function () {
    signatureUpload()
});

function signatureUpload() {

    var uploadpath = $('#doctorsign').val();
    var fileExtension = uploadpath.substring(uploadpath.lastIndexOf(".") + 1, uploadpath.length);

    if (window.FormData !== undefined) {

        var fileUpload = $("#doctorsign").get(0);
        var files = fileUpload.files;
        var fileData = new FormData();
        $.when(checkimagedimensionvalidation(fileUpload.files[0], 512, 512)).
        done(function (Results) {
            debugger;
            if (Results == 'Success') {

                for (var i = 0; i < files.length; i++) {
                    fileData.append(files[i].name, files[i]);
                }
                fileData.append('username', 'Manas');

                $.ajax({
                    url: '/Template/UploadDischargeSummarySignFiles',
                    type: "POST",
                    contentType: false, // Not to set any content header
                    processData: false, // Not to process data
                    data: fileData,
                    success: function (result) {
                        console.log(result);
                        $.each(result, function (i, emp) {
                            $('#Hdn_doctorsign').val(emp);
                            var filepath = $('#hdnfilepath1').val();                            
                            var profilephoto = $('#Hdn_doctorsign').val();
                            var HospitalId = $('#HospitalId').val();
                            var d, n, drsigimage;                            
                            drsigimage = filepath + HospitalId + '/DischargeSummaryTemplate/Sign/' + profilephoto;
                            $('#doctorsignImages').empty();
                            $('#doctorsignImages').show();
                            $('#doctorsignImages').append('<a href="' + drsigimage + '" target="_blank">Signature</a>');
                        });
                    }
                });
            }
            else {
                $('#doctorsign').val('');              
            }
        });
    }
}


$('#headerlogo').change(function () {
    logoUpload()
});

function logoUpload() {

    var uploadpath = $('#headerlogo').val();
    var fileExtension = uploadpath.substring(uploadpath.lastIndexOf(".") + 1, uploadpath.length);

    if (window.FormData !== undefined) {

        var fileUpload = $("#headerlogo").get(0);
        var files = fileUpload.files;
        var fileData = new FormData();
        $.when(checkimagedimensionvalidation(fileUpload.files[0], 512, 512)).
        done(function (Results) {
            debugger;
            if (Results == 'Success') {

                for (var i = 0; i < files.length; i++) {
                    fileData.append(files[i].name, files[i]);
                }
                fileData.append('username', 'Manas');

                $.ajax({
                    url: '/Template/UploadDischargeSummaryLogoFiles',
                    type: "POST",
                    contentType: false, // Not to set any content header
                    processData: false, // Not to process data
                    data: fileData,
                    success: function (result) {
                        console.log(result);
                        $.each(result, function (i, emp) {
                            $('#Hdn_headerlogo').val(emp);
                            var filepath = $('#hdnfilepath1').val();
                            var profilephoto = $('#Hdn_headerlogo').val();
                            var HospitalId = $('#HospitalId').val();
                            var d, n, drsigimage;
                            drsigimage = filepath + HospitalId + '/DischargeSummaryTemplate/Logo/' + profilephoto;
                            $('#headerlogoimages').empty();
                            $('#headerlogoimages').show();
                            $('#headerlogoimages').append('<a href="' + drsigimage + '" target="_blank">Logo</a>');
                        });
                    }
                });
            }
            else {
                $('#doctorsign').val('');
            }
        });
    }
}






















