$(document).ready(function () {
    LoadRole_();
});
$("#btnRole").click(function () {

    if ($("#btnRole").html() == "Add") {

        AddRole();
    }
    if ($("#btnRole").html() == "Update") {

        UpdateRole();
    }
});
$('.close').click(function () {
    $("#btnRole").html("Add");
});
function AddRole() {

    if ($.trim($('#txtRole').val()) == "") {
        $('#PtxtRole').show();
        $('#PtxtRole').fadeOut(8000);
        $('#PtxtRole').html('Enter Role');
        $('#txtRole').focus();
        return false;
    }
    if ($.trim($('#txtRole').val()).length > 0) {
        var RoleData = {
            doctor_user_role_name: $('#txtRole').val(),
            can_delete: 0,
            created_by: $('#HiddenDoctorID').val(),
            Dml_Indicator: "I"
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/CreateRole/DoctorRole',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(RoleData),
            success: function (data) {
                if (jQuery.isEmptyObject((data))) {
                    
                }
                else {

                    for (var i = 0; i < data.length; i++) {
                        $('#PtxtRole').show();
                        $('#PtxtRole').fadeOut(8000);
                        $('#PtxtRole').html(data[i].StringMsg);
                        $('#txtRole').focus();
                        $("#txtRole").val('');
                        LoadRole_();

                    }

                }
            }

        });
    }
}

function LoadRole_() {
    var RoleData = {
        Dml_Indicator: "S",
        created_by: $('#HiddenDoctorID').val()
    }

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

            if (jQuery.isEmptyObject((data))) {

                
            }
            else {
                var tr = '';
                var SelRole = '';
                $('#SelRole').empty();
                $('#tblRole tbody').empty();
                var default_opt = '<option value="0">--Select--</option>';
                var j=0;
                if (data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        j++;
                        SelRole = SelRole + '<option value="' + data[i].doctor_user_role_id + '">' + data[i].doctor_user_role_name + '</option>';

                        tr = tr + "<tr id=" + data[i].doctor_user_role_id + ">";
                        tr = tr + "<td>" + j + "</td>";
                        tr = tr + "<td style='display:none;'>" + data[i].doctor_user_role_id + "<input type='hidden' id='type_" + data[i].doctor_user_role_id + "' value='" + data[i].doctor_user_role_name + "'</td>";
                        tr = tr + " <td>" + data[i].doctor_user_role_name + "</td>";
                        tr = tr + "<td>";
                        tr = tr + "<div class='table_actions'>";
                        tr = tr + '<a onclick="ResourceTypeEdit(\'' + data[i].doctor_user_role_id + '\'); return false;"href=\'' + data[i].doctor_user_role_id + '\' ><i class="btn btn-warning btn-xs" aria-hidden="true">Edit</i></a>';
                        tr = tr + "</div>";
                        tr = tr + "</td>";
                        tr = tr + "</tr>";
                    }

                    $('#SelRole').append(default_opt + SelRole);
                    $('#tblRole').append(tr);
                }

            }
        }

    });
}

function ResourceTypeEdit(url) {
    debugger;
    var edit_text = $('#type_' + url).val();

    $('#txtRole').val(edit_text);

    $("#btnRole").html("Update");

     $('#RoleID').val(url);
}
function UpdateRole() {
    debugger;
    if ($.trim($('#txtRole').val()).length > 0) {
        var RoleData = {
            doctor_user_role_id: $('#RoleID').val(),
            doctor_user_role_name: $('#txtRole').val(),
            created_by: $('#HiddenDoctorID').val(),
            can_delete: 0,
            Dml_Indicator: "U"
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/CreateRole/DoctorRole',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(RoleData),
            success: function (data) {
                if (jQuery.isEmptyObject((data))) {
                }
                else {

                    for (var i = 0; i < data.length; i++) {
                        $('#PtxtRole').show();
                        $('#PtxtRole').fadeOut(8000);
                        $('#PtxtRole').html(data[i].StringMsg);
                        $('#txtRole').focus();
                        $("#txtRole").val('');
                        $("#btnRole").html("Add");
                        LoadRole_();

                    }

                }
            }

        });
    }
}