$(document).ready(function () {
    var DoctorId = $('#myHiddenVar').val();
    debugger;
    LoginDetails();
});

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

//});

function LoginDetails() {
    debugger;
    var cname = 'logindetails';
    var doctor_details = getCookie(cname);
    debugger;
    var logindetails = doctor_details.split('=');
    logindetails = logindetails[1];
    logindetails = logindetails.split('$');
    console.log("---------------------------------------------------");
    console.log(logindetails);
    for (var i = 0; i < logindetails.length; i++) {
        //$("#txtDusername").html(logindetails[0].substring(0, 2));
        $("#txtDusername").html(logindetails[5].substring(0, 1));
        $("#txtusername").html(logindetails[0]);

        if ($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5) {
            $('#specialityLabel').html('Speciality : ')
            $("#txtuserSpeciality").html(logindetails[2]);
            $("#txtuserid").html(logindetails[1]);
        }
        else {
            // $('#specialityLabel').html('Role : ')
            $('.speciality_label').hide();
        }

        var photoformat = (logindetails[3]);
        var DoctorId = logindetails[1];
        var filepath = $('#hdnfilepath').val();
        var photo = $('#hdnProfilePhoto').val();
        var photoSrc;
        if ($.trim(photoformat) == "" || photoformat == null) {
            photoSrc = "/Content/img/profile_icon.png";
        }
        else {
            photoSrc = $('#hdnfilepath1').val() + $('#HiddenDoctorID').val() + "/Photos/" + photoformat;
        }
        $('#ProfilePhotoTop,#ProfilePhoto').attr('src', photoSrc);
    }
    var cname1 = 'namedetails';
    var name_details = getCookie(cname1);
    var details = name_details.split('=');
    console.log(details)


    $('#txtbranchname').html(details[1].split('&')[0]);
    $('#txtdepartmentname').html(details[2].split('&')[0]);
    $('#txtsubdepartmentname').html(details[3]);
    $('#txtdesignation').html(logindetails[4]);

    if ($('#hiddenRole_id').val() != 1 && $('#hiddenRole_id').val() != 5) {
        $('#editprofile_link').hide();
    }
    else {
        $('#editprofile_link').show();
    }
}


function changedefault(e) {
    var photoSrc = "/Content/img/profile_icon.png";
    console.log("Image Error")
    $(e).attr("src", photoSrc)
}

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

function LoginDetails_old() {
    var DoctorId = $('#myHiddenVar').val();

    var LoginData = {
        doctor_id: DoctorId
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/LoginDetails',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(LoginData),
        success: function (data1) {
            if (jQuery.isEmptyObject((data1))) {

            }
            else {
                var data = JSON.parse(data1);

                if (data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        $("#txtDusername").html(data[i].doctor_name);
                        $("#txtusername").html(data[i].doctor_name);
                        $("#txtuserid").html(data[i].doctor_id);
                        $("#txtdesignation").html(data[i].designation);
                        if ($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5) {
                            $('#specialityLabel').html('Speciality : ')
                        }
                        else {
                            $('#specialityLabel').html('Role : ')
                        }
                        $("#txtuserSpeciality").html(data[i].speciality_name);
                        var photoformat = (data[i].photo);
                        var DoctorId = data[i].doctor_id;
                        var filepath = $('#hdnfilepath').val();
                        var photo = $('#hdnProfilePhoto').val();
                        var photoSrc;
                        if ($.trim(photoformat) == "" || photoformat == null) {
                            photoSrc = "/Content/img/profile_icon.png";
                        }
                        else {
                            photoSrc = filepath + DoctorId + photo + photoformat;
                        }
                        $('#ProfilePhotoTop,#ProfilePhoto').attr('src', photoSrc);
                    }
                }
            }
        }

    });
}

$('#logout').click(function () {
    logout();
});

function logout() {
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Home/LogoutSession',
        contentType: 'application/json; charset=UTF-8',
        success: function (data) {

            window.location = "/Home";


        }

    });
}
