$(document).ready(function () {
    LoadUserName();
    LoadFromName();
});

$("#btnSubmit").click(function () {

    GetLogData();
});

function LoadUserName() {

    var UserNameData = {
        Dml_Indicator: 'US'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserLog/DoctorLog',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(UserNameData),
        success: function (data) {
            if (jQuery.isEmptyObject((data))) {

            }
            else {
                if (data.length > 0) {
                    var data1 = JSON.parse(data);
                    $('#SelUserName').empty();
                    var UserName;
                    $.each(data1, function (i, emp) {

                        UserName = UserName + '<option value="' + emp.doctor_user_id + '">' + emp.doctor_user_name + '</option>';
                    });
                    $('#SelUserName').append(UserName);

                }

            }
        }

    });
}

function LoadFromName() {

    var UserFromData = {
        Dml_Indicator: 'FS'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserLog/DoctorLog',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(UserFromData),
        success: function (data) {
            if (jQuery.isEmptyObject((data))) {

            }
            else {
                if (data.length > 0) {
                    var data1 = JSON.parse(data);
                    $('#SelFromName').empty();
                    var FromName;
                    $.each(data1, function (i, emp) {

                        FromName = FromName + '<option value="' + emp.doctor_module_id + '">' + emp.doctor_module_name + '</option>';

                    });
                    $('#SelFromName').append(FromName);

                }

            }
        }

    });
}

function GetLogData() {

    var UserName = $('#SelUserName').val();
    var FromName = $('#SelFromName').val();
    var FDate = $('#start_date').val();
    var EDate = $('#end_date').val();

    if ($.trim(UserName) == "") {
        $('#PSelUserName').show();
        $('#PSelUserName').fadeOut(8000);
        $('#PSelUserName').html('Select User Name');
        $('#SelUserName').val('');
        $('#SelUserName').focus();
        return false;
    }
    if ($.trim(FromName) == "") {
        $('#PSelFromName').show();
        $('#PSelFromName').fadeOut(8000);
        $('#PSelFromName').html('Select from Name');
        $('#SelFromName').val('');
        $('#SelFromName').focus();
        return false;
    }
    if ($.trim(FDate) == "") {
        $('#Pstart_date').show();
        $('#Pstart_date').fadeOut(8000);
        $('#Pstart_date').html('Select From Date');
        $('#start_date').val('');
        $('#start_date').focus();
        return false;
    }
    if ($.trim(EDate) == "") {
        $('#Pend_date').show();
        $('#Pend_date').fadeOut(8000);
        $('#Pend_date').html('Select To Date');
        $('#end_date').val('');
        $('#end_date').focus();
        return false;
    }




    var FDate1 = FDate.split('/');
    var EDate1 = EDate.split('/');
    FDate = FDate1[2] + "-" + FDate1[1] + "-" + FDate1[0]
    EDate = EDate1[2] + "-" + EDate1[1] + "-" + EDate1[0]

    var a = $('#SelUserName').val();
    if (a != "") {
        var Condition_Doctor;
        j = 0;
        for (var i = 0; i < a.length; i++) {
            if (j == 0) {
                Condition_Doctor = "DUL.doctor_user_id = " + a[i];
                j++;
            }
            else {
                Condition_Doctor += " or DUL.doctor_user_id = " + a[i];
            }
        }
    }
    var b = $('#SelFromName').val();
    if (b != "") {

        var Condition_module;
        j = 0;
        for (var i = 0; i < b.length; i++) {
            if (j == 0) {
                Condition_module = "DUL.doctor_user_module_id = " + b[i];
                j++;
            }
            else {
                Condition_module += " or DUL.doctor_user_module_id = " + b[i];
            }
        }
    }
    var UserFromData = {
        Condition_Doctor: Condition_Doctor,
        Condition_module: Condition_module,
        FDate: FDate,
        EDate: EDate,
        Dml_Indicator: 'S'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserLog/DoctorLog',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(UserFromData),
        success: function (data) {
            var data22 = JSON.stringify(UserFromData);
            if (jQuery.isEmptyObject((data))) {
                
            }
            else {
                var FDate2 = $('#start_date').val();
                var EDate2 = $('#end_date').val();
                $('#tbllog').empty();
                if (data.length > 0) {
                    if (jQuery.isEmptyObject((data))) {
                        $('#tbllog').empty();
                        tr = $('<table id="example1" class="table table-bordered table-striped text-center" />');
                        tr.append("<thead><tr><th>S No</th><th>User Name</th><th>From Date</th><th>To Date</th><th>Form Name</th></tr></thead><tbody>");
                        tr.append("<tr><td colspan='5' style='text-align:center'><h4>No Record Found...</h4></td></tr>")
                        tr.append("</tbody>");

                        $('#tbllog').append(tr);
                    }
                    else {
                        var data11 = JSON.parse(data);
                        var data1 = JSON.parse(data22);
                        tr = $('<table id="example1" class="table table-bordered table-striped text-center" />');

                        tr.append("<thead><tr><th>S No</th><th>User Name</th><th>From Date</th><th>To Date</th><th>From Name</th></tr></thead><tbody>");
                        var a = $('#SelUserName').val();
                        var k = 0;
                        var Doctor_id = "";
                        // $.each(data1, function (i, emp) {
                        for (var i = 0; i < a.length; i++) {
                            Doctor_id = a[i];
                            //var id = emp.admin_id[i];
                            var FromName = "";
                            var User_name = "";
                            $.each(data11, function (j, emp1) {
                                if (Doctor_id == emp1["doctor_user_id"]) {
                                    FromName += emp1.doctor_module_name + "</br>";
                                    User_name = emp1.doctor_user_name;
                                }

                            });
                            if (FromName != "") {
                                k = k + 1;
                                tr.append("<tr><td>" + k + "</td><td>" + User_name + "</td><td>" + FDate2 + "</td><td>" + EDate2 + "</td><td>" + FromName + "</td></tr>");
                            }
                        }

                        // });

                        tr.append("</tbody>");

                        $('#tbllog').append(tr);

                    }
                }
            }
        }

    });


}