$(document).ready(function () {
    if ($('#hiddenRole_id').val() == "1" || $('#hiddenRole_id').val() == "5") {
        $('#seldoc').hide();
        var overlay = document.getElementById('tblDoctorUser1');
        $('#tblDoctorUserAccount').modal('toggle');
    }
    else if ($('#hiddenRole_id').val() == "2") {
        $('#shtable').show();
        doctorlist();
        fee_table();
    }
    else {
        $('#shtable').show();
        var overlay = document.getElementById('tblDoctorUser1');
        $('#tblDoctorUserAccount').modal('toggle');
        doctorlist();
        fee_table();
    }
});

function doctorlist() {
    var DoctorData = {
        Dml_Indicator: "DLA",
        user_role: $('#hiddenRole_id').val(),
        doctor_id: $('#HiddenDoctorID').val(),
        hospital_id: $('#HospitalId').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Prescription_Settings/casesheet_configurations',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    console.log(data1);
                    var b = data1.Table;
                    var tr = '';
                    var SelRole = '';
                    $('#SelDoctorapp').empty();
                    var default_opt = '<option value="">--Select Doctor--</option>';
                    default_opt = default_opt + '<option value= "' + $('#HiddenDoctorID').val() + '" >Default Charges</option>';
                    $.each(b, function (i, emp) {
                        SelRole = SelRole + '<option value="' + emp.doctor_id + '">' + emp.doctor_name + '</option>';
                    });
                    $('#SelDoctorapp').html(default_opt + SelRole);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

function fee_table() {
    var DocData = {
        indicator_id: "TFDATA"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/BankAccountSetting/fee_collection_table',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DocData),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                $('#fee_table_list').empty();
                if (jQuery.isEmptyObject((data)) || data.length == "0") {
                    tr = $('<table class="table-responsive table table-condensed body-borderless table_borderless" id="fee_table" />');
                    tr.append("<thead><tr><th>S No</th><th>Doctor Id</th><th>Doctor Name</th><th>Consultation Charge</th><th>Video Charge</th><th>Char Charge</th><th>Actions</th></tr></thead><tbody>");
                    tr.append("</tbody>");
                }
                else if (Object.keys(data1).length > 0) {
                    $('#fee_table_list').empty();
                    var j = 0, hosp_id = $('#HiddenDoctorID').val();
                    console.log(data1);
                    var b = data1.Table;
                    var bc = data1.Table1;
                    var con_type, video_type, chat_type;
                    tr = $('<table class="table-responsive table table-condensed body-borderless table_borderless" id="fee_table" />');
                    tr.append("<thead><tr><th>S No</th><th>Doctor Id</th><th>Doctor Name</th><th>Consultation Charge(Rs)</th><th>Video Charge(Rs)</th><th>Chat Charge(Rs)</th><th>Actions</th></tr></thead><tbody>");
                    $.each(b, function (i, emp) {
                        debugger;
                        j++;
                        if (emp.consultation_fee_type == "1") con_type = "Prepaid";
                        else con_type = "Postpaid";
                        if (emp.video_consultation_type == "1") video_type = "Prepaid";
                        else if (emp.video_consultation_type == "2") video_type = "Postpaid";
                        else video_type = "Pay After";
                        if (emp.chat_type == "1") chat_type = "Prepaid";
                        else chat_type = "Postpaid";
                        if (emp.doctor_id == hosp_id) {
                            row_data = "<tr><td>" + j + "</td><td>ADMIN</td><td>Default Charges</td><td>" + emp.consultation_fee + "<span style='color: #999; font-size: 11px;'>(" + con_type + ")</td><td>" + emp.video_consultation + "<span style='color: #999; font-size: 11px;'>(" + video_type + ")</td><td>" + emp.chat + "<span style='color: #999; font-size: 11px;'>(" + chat_type + ")</td><td><button class='edit btn btn-default  btn-sm btn-add' onclick=edit_fee_collection_data('" + emp.doctor_id + "')><i class='fa fa-pencil' aria-hidden='true'></i></button></td></tr>";
                            console.log(row_data);
                            tr.append(row_data);
                        }
                        else {
                            row_data = "<tr><td>" + j + "</td><td>" + emp.doctor_id + "</td><td>" + emp.first_name + "</td><td>" + emp.consultation_fee + "<span style='color: #999; font-size: 11px;'>(" + con_type + ")</td><td>" + emp.video_consultation + "<span style='color: #999; font-size: 11px;'>(" + video_type + ")</td><td>" + emp.chat + "<span style='color: #999; font-size: 11px;'>(" + chat_type + ")</td><td><button class='edit btn btn-default  btn-sm btn-add' onclick=edit_fee_collection_data('" + emp.doctor_id + "')><i class='fa fa-pencil' aria-hidden='true'></i></button></td></tr>";
                            tr.append(row_data);
                        }
                    });
                    debugger;
                    $.each(bc, function (i, emp) {
                        j++;
                        debugger;
                        if (emp.consultation_fee_type == "1") con_type = "Prepaid";
                        else con_type = "Postpaid";
                        if (emp.video_consultation_type == "1") video_type = "Prepaid";
                        else if (emp.video_consultation_type == "2") video_type = "Postpaid";
                        else video_type = "Pay After";
                        if (emp.chat_type == "1") chat_type = "Prepaid";
                        else chat_type = "Postpaid";
                        if (emp.doctor_id == hosp_id) {
                            row_data = "<tr><td>" + j + "</td><td>ADMIN</td><td>Default Charges</td><td>" + emp.consultation_fee + "<span style='color: #999; font-size: 11px;'>(" + con_type + ")</td><td>" + emp.video_consultation + "<span style='color: #999; font-size: 11px;'>(" + video_type + ")</td><td>" + emp.chat + "<span style='color: #999; font-size: 11px;'>(" + chat_type + ")</td><td><button class='edit btn btn-default  btn-sm btn-add' onclick=edit_fee_collection_data('" + emp.doctor_id + "')><i class='fa fa-pencil' aria-hidden='true'></i></button></td></tr>";
                            tr.append(row_data);
                            console.log(row_data);
                        }
                        else {
                            row_data = "<tr><td>" + j + "</td><td>" + emp.doctor_id + "</td><td>" + emp.first_name + "</td><td>" + emp.consultation_fee + "<span style='color: #999; font-size: 11px;'>(" + con_type + ")</td><td>" + emp.video_consultation + "<span style='color: #999; font-size: 11px;'>(" + video_type + ")</td><td>" + emp.chat + "<span style='color: #999; font-size: 11px;'>(" + chat_type + ")</td><td><button class='edit btn btn-default  btn-sm btn-add' onclick=edit_fee_collection_data('" + emp.doctor_id + "')><i class='fa fa-pencil' aria-hidden='true'></i></button></td></tr>";
                            tr.append(row_data);
                        }
                    });
                    tr.append("</tbody>");
                    $.when($('#fee_table_list').append(tr)).then($('#fee_table').DataTable({
                        "columnDefs": [{
                            "orderable": false,
                        }]
                    }));
                }
            }
            else {
                console.log(data1);
            }
        },
    });
}

function edit_fee_collection_data(id) {
    $("#radioBtn > a.active").removeClass("active").addClass("not-active");
    var DocData = {
        doctor_id: id,
        indicator_id: "MPCHA"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/BankAccountSetting/fee_collection_mpin_check',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DocData),
        success: function (data) {
            debugger;
            console.log(data);
            var data1 = JSON.parse(data);
            if (data1.msg1 == "Success") {
                var b = data1["package_ist"]["Table"];
                if (data1["msg"] == "success") {
                    $('#confee').val(b[0].consultation_fee);
                    $('#vconfee').val(b[0].video_consultation);
                    $('#cconfee').val(b[0].chat);
                    $('a[data-toggle=contype][data-title="' + b[0].consultation_fee_type + '"]').removeClass('not-active');
                    $('a[data-toggle=contype][data-title="' + b[0].consultation_fee_type + '"]').addClass('active');
                    $('#contype').val(b[0].consultation_fee_type);
                    $('a[data-toggle=vcontype][data-title="' + b[0].video_consultation_type + '"]').removeClass('not-active');
                    $('a[data-toggle=vcontype][data-title="' + b[0].video_consultation_type + '"]').addClass('active');
                    $('#vcontype').val(b[0].video_consultation_type);
                    $('a[data-toggle=ccontype][data-title="' + b[0].chat_type + '"]').removeClass('not-active');
                    $('a[data-toggle=ccontype][data-title="' + b[0].chat_type + '"]').addClass('active');
                    $('#ccontype').val(b[0].chat_type);
                    $('a[data-toggle=sendtype][data-title="' + b[0].sms + '"]').removeClass('not-active');
                    $('a[data-toggle=sendtype][data-title="' + b[0].sms + '"]').addClass('active');
                    $('#SelDoctorapp').val(id);
                    $('#Bfsubmit').html("Update");
                }
                else {
                    toastr.warning("No Records Found");
                    location.reload();
                }
            }
            else {
                console.log(data1);
            }
        }

    });
}


function mpin_check() {
    if ($.trim($('#mpinnum').val()) == "") {
        $('#Pmpinnum').show();
        $('#Pmpinnum').fadeOut(8000);
        $('#Pmpinnum').html('Please enter MPIN');
        $('#mpinnum').focus();
        $('#mpinnum').val('');
        return false;
    }
    if ($.trim($('#mpinnum')).length > 0) {
        //$('#tblDoctorUserAccount').modal('toggle');
        loadaccountdata();
    }
}



$('#Bfsubmit').click(function () {
    if ($('#hiddenRole_id').val() != "1" && $('#hiddenRole_id').val() != "5") {
        if ($.trim($('#SelDoctorapp').val()).length == "") {
            $('#PSelDoctorapp').show();
            $('#PSelDoctorapp').fadeOut(8000);
            $('#PSelDoctorapp').html('Please Select Doctor');
            $('#SelDoctorapp').focus();
            $('#SelDoctorapp').val('');
            return false;
        }
    }
    if ($.trim($('#confee').val()).length == "") {
        $('#Pconfee').show();
        $('#Pconfee').fadeOut(8000);
        $('#Pconfee').html('Please enter the Consultation Fee');
        $('#confee').focus();
        $('#confee').val('');
        return false;
    }
    if ($.trim($('#vconfee').val()).length == "") {
        $('#Pvconfee').show();
        $('#Pvconfee').fadeOut(8000);
        $('#Pvconfee').html('Please enter the Video Appointment Fee');
        $('#vconfee').focus();
        $('#vconfee').val('');
        return false;
    }
    //if ($.trim($('#cconfee').val()).length == "") {
    //    $('#Pcconfee').show();
    //    $('#Pcconfee').fadeOut(8000);
    //    $('#Pcconfee').html('Please enter the Chat Fee');
    //    $('#cconfee').focus();
    //    $('#cconfee').val('');
    //    return false;
    //}
    if ($.trim($('#contype').val()).length == "") {
        $('#Pcontype').show();
        $('#Pcontype').fadeOut(8000);
        $('#Pcontype').html('Please Select Consultation Fee Type');
        $('#contype').focus();
        $('#contype').val('');
        return false;
    }
    if ($.trim($('#vcontype').val()).length == "") {
        $('#Pvcontype').show();
        $('#Pvcontype').fadeOut(8000);
        $('#Pvcontype').html('Please Select Video Fee Type');
        $('#vcontype').focus();
        $('#vcontype').val('');
        return false;
    }
    //if ($.trim($('#ccontype').val()).length == "") {
    //    $('#Pccontype').show();
    //    $('#Pccontype').fadeOut(8000);
    //    $('#Pccontype').html('Please Select Chat Fee Type');
    //    $('#ccontype').focus();
    //    $('#ccontype').val('');
    //    return false;
    //}
    if ($.trim($('#confee,#vconfee,#vcontype,#contype')).length > 0) {
        savefee_collection();
    }
});



function savefee_collection() {
    var DoctorId;
    if ($('#hiddenRole_id').val() == "1" || $('#hiddenRole_id').val() == "5") {
        DoctorId = $('#HiddenDoctorID').val();
    }
    else {
        var DoctorId = $('#SelDoctorapp').val();
    }
    var DocData = {
        consultation_fee: $('#confee').val(),
        consultation_fee_type: $('#contype').val(),
        video_appiontment_fee: $('#vconfee').val(),
        video_appiontment_fee_type: $('#vcontype').val(),
        chat_fee: $('#cconfee').val(),
        chat_fee_type: $('#ccontype').val(),
        send_sms: $('#sendtype').val(),
        doctor_id: DoctorId
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/BankAccountSetting/Fee_collection_add',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DocData),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;
                if ($('#hiddenRole_id').val() == "2") {
                    location.reload();
                }
                else {
                    location.replace('/Dashboard');
                }
            }
            else {
                console.log(data);
            }
        }
    });
}

$('#closeid').click(function () {
    location.replace('/Dashboard');
});


function loadaccountdata() {
    var DoctorId;
    if ($('#hiddenRole_id').val() == "1" || $('#hiddenRole_id').val() == "5") {
        DoctorId = $('#HiddenDoctorID').val();
    }
    else {
        var DoctorId = $('#SelDoctorapp').val();
    }
    var DocData = {
        doctor_id: DoctorId,
        mpin: $('#mpinnum').val(),
        indicator_id: "MPCH"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/BankAccountSetting/fee_collection_mpin_check',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DocData),
        success: function (data) {
            console.log(data);
            var data1 = JSON.parse(data);
            if (data1.msg1 == "Success") {
                //var data1 = data1.data;
                console.log(data1);
                var b = data1["package_ist"]["Table"];
                if (data1["msg"] == "success") {
                    $('#tblDoctorUserAccount').modal('toggle');
                    if (b.length > 0) {
                        $('#confee').val(b[0].consultation_fee);
                        $('#vconfee').val(b[0].video_consultation);
                        $('#cconfee').val(b[0].chat);
                        $('a[data-toggle=contype][data-title="' + b[0].consultation_fee_type + '"]').removeClass('not-active');
                        $('a[data-toggle=contype][data-title="' + b[0].consultation_fee_type + '"]').addClass('active');
                        $('#contype').val(b[0].consultation_fee_type);
                        $('a[data-toggle=vcontype][data-title="' + b[0].video_consultation_type + '"]').removeClass('not-active');
                        $('a[data-toggle=vcontype][data-title="' + b[0].video_consultation_type + '"]').addClass('active');
                        $('#vcontype').val(b[0].video_consultation_type);
                        $('a[data-toggle=ccontype][data-title="' + b[0].chat_type + '"]').removeClass('not-active');
                        $('a[data-toggle=ccontype][data-title="' + b[0].chat_type + '"]').addClass('active');
                        $('#ccontype').val(b[0].chat_type);
                        $('a[data-toggle=sendtype][data-title="' + b[0].sms + '"]').removeClass('not-active');
                        $('a[data-toggle=sendtype][data-title="' + b[0].sms + '"]').addClass('active');
                        $('#sendtype').val(b[0].sms);
                        $('#Bfsubmit').html("Update");
                    }
                    else {
                        $('#Bfsubmit').html("SAVE");
                    }

                }
                else {
                    toastr.warning("Entered MPIN is Wrong");
                    //location.replace('/Dashboard');
                }
            }
            else {
                console.log(data1);
            }
        }

    });
}

