$(document).ready(function () {
    debugger;
    //LoadRole();
   // LoadBranch();
    LoadDesignation_user
    //LoadHospital();
    //LoadDoctorUser();
    if ($('#DoctorUser_Id').val() != '' && $('#DoctorUser_Id').val() != null) {
        $('#SelRole').prop('disabled', 'disabled');
    }
    else {

    }

});

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

    $("#btnSubmit_role").attr("disabled", true);
    if ($("#btnSubmit_role").html() == "Submit") {

        AddUser();
    }
    if ($("#btnSubmit_role").html() == "Update") {

        UpdateUser();
    }
    
});


function LoadHospital() {

    var HospitalData = {
        Dml_Indicator: "HS",
        doctor_id: $('#HiddenDoctorID').val()
    }

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

                }
                else {
                    var tr = '';
                    var SelRole = '';
                    $('#SelHospital').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].hospital_id + '">' + data[i].hospital_name + '</option>';
                        }

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

                }
                //LoadRole();
                //LoadBranch();


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

}


function LoadRole() {
    var RoleData = {
        Dml_Indicator: "SS",
        created_by: $('#HiddenDoctorID').val(),
        branch_id: $('#Branch_user').val()
    }

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

                }
                else {
                    var tr = '';
                    var SelRole = '';
                    $('#SelRole').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>';
                        }

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

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

        }

    });
    
}


function AddUser() {

    debugger;
    
    if ($.trim($('#txtName').val()) == "") {
        $('#PtxtName').show();
        $('#PtxtName').fadeOut(8000);
        $('#PtxtName').html('Enter Name');
        $('#txtName').focus();
        $('#txtName').val('');
        $("#btnSubmit_role").attr("disabled", false);
        return false;
    }
    if ($.trim($('#txtEmailId').val()) == "") {
        $('#PtxtEmailId').show();
        $('#PtxtEmailId').fadeOut(8000);
        $('#PtxtEmailId').html('Enter Email ID');
        $('#txtEmailId').focus();
        $('#txtEmailId').val('');
        $("#btnSubmit_role").attr("disabled", false);
        return false;
    }
    if ($.trim($('#txtMoblie').val()) == "") {
        $('#PtxtMoblie').show();
        $('#PtxtMoblie').fadeOut(8000);
        $('#PtxtMoblie').html('Enter Mobile Number');
        $('#txtMoblie').focus();
        $('#txtMoblie').val('');
        $("#btnSubmit_role").attr("disabled", false);
        return false;
    }
    if ($.trim($('#txtMoblie').val()) != "" && $.trim($('#txtMoblie').val()).length !="10")  {
        $('#PtxtMoblie').show();
        $('#PtxtMoblie').fadeOut(8000);
        $('#PtxtMoblie').html('Invalid Mobile Number');
        $('#txtMoblie').focus();
        $('#txtMoblie').val('');
        $("#btnSubmit_role").attr("disabled", false);
        return false;
    }
    if ($.trim($('#SelDoctorDesignation_user').val()) == 0) {
        $('#PSelDoctorDesignation_user').show();
        $('#PSelDoctorDesignation_user').fadeOut(8000);
        $('#PSelDoctorDesignation_user').html('Enter Designation');
        $('#SelDoctorDesignation_user').focus();
        $('#SelDoctorDesignation_user').val('');
        $("#btnSubmit_role").attr("disabled", false);
        return false;
    }
    if ($.trim($('#SelRole').val()) == "" || $('#SelRole :selected').val() == "0") {
        $('#PSelRole').show();
        $('#PSelRole').fadeOut(8000);
        $('#PSelRole').html('Select Role');
        $('#SelRole').focus();
        $('#SelRole :selected').val('0');
        $("#btnSubmit_role").attr("disabled", false);
        return false;
    }
    var DoctorUserData = {
        doctor_user_name: $('#txtName').val(),
        doctor_user_email_id: $('#txtEmailId').val(),
        doctor_user_mobile_no: $('#txtMoblie').val(),
        doctor_user_designation: $('#SelDoctorDesignation_user').val(),
        doctor_user_role_id: $('#SelRole').val(),
        hospital_id: $('#HospitalId').val(),
        doctor_id:$('#HiddenDoctorID').val(),
        doctor_user_status: '1',
        Dml_Indicator: 'I',
       department_id : $('#Department_user').val(),
       subdepartment_id: $('#SubDepartemnt_user').val(),
       branch_id: $('#Branch_user').val(),

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

                }
                else {
                    debugger;
                    for (var i = 0; i < data.length; i++) {
                        var result = data[i].StringMsg
                        if (result == 'Already Exist') {
                            $('#PtxtEmailId').show();
                            $('#PtxtEmailId').fadeOut(8000);
                            $('#PtxtEmailId').html(result);
                            $('#txtEmailId').focus();
                            $('#txtEmailId').val('');
                            $("#btnSubmit_role").attr("disabled", false);
                        }
                        else {
                            window.location = "Previllages/index"

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

    });
}

function UpdateUser() {
    //if ($.trim($('#SelHospital :selected').val()) == "" || $('#SelHospital :selected').val() == "0") {
    //    $('#PSelHospital').show();
    //    $('#PSelHospital').fadeOut(8000);
    //    $('#PSelHospital').html('Select Hospital');
    //    $('#SelHospital').focus();
    //    $('#SelHospital :selected').val('0');
    //    return false;
    //}
    if ($.trim($('#txtName').val()) == "") {
        $('#PtxtName').show();
        $('#PtxtName').fadeOut(8000);
        $('#PtxtName').html('Enter Name');
        $('#txtName').focus();
        $('#txtName').val('');
        return false;
    }
    if ($.trim($('#txtEmailId').val()) == "") {
        $('#PtxtEmailId').show();
        $('#PtxtEmailId').fadeOut(8000);
        $('#PtxtEmailId').html('Enter Email ID');
        $('#txtEmailId').focus();
        $('#txtEmailId').val('');
        return false;
    }
    if ($.trim($('#txtMoblie').val()) == "" ) {
        $('#PtxtMoblie').show();
        $('#PtxtMoblie').fadeOut(8000);
        $('#PtxtMoblie').html('Enter Mobile Number');
        $('#txtMoblie').focus();
        $('#txtMoblie').val('');
        return false;
    }
    if ($.trim($('#txtMoblie').val()) != "" && $.trim($('#txtMoblie').val()).length != "10") {
        $('#PtxtMoblie').show();
        $('#PtxtMoblie').fadeOut(8000);
        $('#PtxtMoblie').html('Invalid Mobile Number');
        $('#txtMoblie').focus();
        $('#txtMoblie').val('');
        return false;
    }
    if ($.trim($('#SelDoctorDesignation_user').val()) == "") {
        $('#PSelDoctorDesignation_user').show();
        $('#PSelDoctorDesignation_user').fadeOut(8000);
        $('#PSelDoctorDesignation_user').html('Enter Designation');
        $('#SelDoctorDesignation_user').focus();
        $('#SelDoctorDesignation_user').val('');
        return false;
    }
    if ($.trim($('#SelRole').val()) == "" || $('#SelRole :selected').val() == "0") {
        $('#PSelRole').show();
        $('#PSelRole').fadeOut(8000);
        $('#PSelRole').html('Select Role');
        $('#SelRole').focus();
        $('#SelRole :selected').val('0');
        return false;
    }
    var DoctorUserData = {
        doctor_user_id: $('#DoctorUser_Id').val(),
        doctor_user_name: $('#txtName').val(),
        doctor_user_email_id: $('#txtEmailId').val(),
        doctor_user_mobile_no: $('#txtMoblie').val(),
        doctor_user_designation: $('#SelDoctorDesignation_user').val(),
        doctor_user_role_id: $('#SelRole').val(),
        hospital_id: $('#HospitalId').val(),
        doctor_user_status: '1',
        Dml_Indicator: 'U',
        department_id: $('#Department_user').val(),
        subdepartment_id: $('#SubDepartemnt_user').val(),
        branch_id: $('#Branch_user').val(),
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserCreate/UserCreationUpdated',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorUserData),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;
                if (jQuery.isEmptyObject((data))) {

                }
                else {
                    for (var i = 0; i < data.length; i++) {
                        var result = data[i].StringMsg
                        if (result == 'Already Exist') {
                            $('#PtxtEmailId').show();
                            $('#PtxtEmailId').fadeOut(8000);
                            $('#PtxtEmailId').html(result);
                            $('#txtEmailId').focus();
                            $('#txtEmailId').val('');
                        }
                        else {
                            window.location = "Previllages/index"

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

    });
}

function LoadDoctorUser() {
    if ($('#DoctorUser_Id').val() != '') {
        var DoctorID = $.trim($('#DoctorUser_Id').val());
        if (DoctorID == 1) {
            toastr.warning("You have no rights to edit the Doctor Profile");
            window.location.href = '/Previllages';           
        }
        else {
            $('#divrole').show();
        }
        var DoctorData = {
            doctor_user_id: DoctorID,
            Dml_Indicator: 'SS'
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/UserCreate/UserCreationUpdated',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(DoctorData),
            success: function (data) {
                var data = JSON.parse(data);
                if (data.msg == "Success") {
                    var data = data.data;
                    if (jQuery.isEmptyObject((data))) {


                    }
                    else {
                        debugger;
                        if (data.length > 0) {
                            debugger;
                            for (var i = 0; i < data.length; i++) {
                                $("#btnSubmit_role").html("Update");
                                $("#txtName").val(data[i].doctor_user_name);
                                $("#txtEmailId").val(data[i].doctor_user_email_id);
                                $("#txtMoblie").val(data[i].doctor_user_mobile_no);
                                $("#SelRole").val(data[i].doctor_user_role_id);
                                $("#SelHospital").val(data[i].hospital_id);
                                branch_user(data[i].branch_id);
                                department(data[i].department, data[i].subdepartment,data[i].branch_id);
                                Designation(data[i].doctor_user_designation, data[i].branch_id);
                                $('#txtEmailId').attr('readonly', true);
                            }
                        }
                    }
                }
                else {
                    console.log(data);
                }
            }
        });
    }
}


$('#SelRole').change(function () {
    if ($('#SelRole').val() == '1') {
        LoadLicenseCount();
       
    }
    else if ($('#SelRole').val() == '3' || $('#SelRole').val() == '4' || $('#SelRole').val() == '6') {
        $('#divdocdr').hide();
        $('#divrole').show();
    }
    else {
        $('#divdocdr').hide();
        $('#divrole').hide();
    }

});

function LoadLicenseCount() {
    var DoctorData = {
        Dml_Indicator: "LS",
        hospital_id: $('#HospitalId').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/DoctorUpdation',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    debugger;
                    if (data.length > 0) {
                        var no_of_license = data[0].no_of_license;
                        var total_doctors = data[0].total_doctors;
                        if (no_of_license != null && no_of_license != '' && total_doctors != 0 && total_doctors != '' && total_doctors != null) {
                            if (no_of_license == total_doctors) {
                                toastr.success('Doctor License Count Exceeded');

                            }
                            else {
                                $('#divdocdr').show();
                                $('#divrole').hide();
                                $('#divroledr').hide();
                            }
                        }


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

