$(document).ready(function () {
    load_vaccine_appointment_data();
    loadcountry();
    LoadBranch();
});

$('#fileToUpload').change(function () {
    debugger;
    packageimageupload();
});

function LoadBranch() {
    $('#SelBranch').empty();
    var subdepartment = {
        Dml_Indicator: "LBRAN"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {

                    var Branch;
                    var default_opt = '<option value="">--Select--</option>';
                    var fil_default_opt = '<option value="">All</option>';
                    $.each(data1.Table, function (i, emp) {
                        Branch = Branch + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });
                    $('#SelBranch').append(default_opt + Branch);
                    $('#CV_branch_id').append(fil_default_opt + Branch);

                    if ($('#hiddenRole_id').val() != 2) {
                        $('#SelBranch').val($('#HospitalBranchID').val());
                        $('#SelBranch').attr('disabled', true);
                        $('#CV_branch_id').val($('#HospitalBranchID').val());
                        $('#CV_branch_id').attr('disabled', true);
                    }
                }
            }
            else {
                console.log(data1);
            }

        }
    });
}







function load_state() {
    debugger;
    var state = {
        country_id: $("#txtCountry").val(),
        Dml_Indicator: "S"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(state),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (jQuery.isEmptyObject((data1.data))) {
            }
            else {

                if (data1.msg == "Success") {
                    var data1 = data1.data;
                    var state;
                    $('#txtState').empty();
                    $('#txtCity').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        state = state + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#txtState').append(default_opt + state);
                    $('#ddbranch_State').append(default_opt + state);
                    $('#txtState').val($('#HdnState').val());
                    $('#ddbranch_State').val($('#HdnState').val());
                    load_city();
                }
                else {
                    console.log(data);
                }

            }
        }
    });
}


function load_city() {
    debugger;
    var city = {
        state_id: $("#HdnState").val(),
        Dml_Indicator: "CI"
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(city),
        success: function (data) {
            debugger;
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {
            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;
                    var City;
                    $('#txtCity').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        City = City + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#txtCity').append(default_opt + City);
                    $('#ddbranch_City').append(default_opt + City);
                    $('#txtCity').val($('#HdnCity').val());
                    $('#ddbranch_City').val($('#HdnCity').val());
                }
                else {
                    console.log(data);
                }

            }
        }
    });

}

function loadcountry() {
    debugger;
    var country = {
        Dml_Indicator: "C"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(country),
        success: function (data) {
            var data2 = JSON.parse(data);
            var data1 = data2.data;
            if (jQuery.isEmptyObject((data2.data))) {
            }
            else {
                var Country;
                //var default_opt = '<option value="0">--Select--</option>';
                $.each(data1, function (i, emp) {
                    Country = Country + '<option value="' + emp.id + '">' + emp.name + '</option>';
                });
                $('#txtCountry').append(Country);
                $("#txtCountry").change();
            }
        }
    });
}


$("#txtCountry").change(function () {
   

    var state = {
        country_id: $("#txtCountry").val(),
        Dml_Indicator: "S"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(state),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {
            }
            else {

                var data2 = JSON.parse(data);
                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    var state;
                    $('#txtState').empty();
                    $('#txtCity').empty();
                   // var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        state = state + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#txtState').append(state);
                    $("#txtState").change()
                }
                else {
                    console.log(data);
                }
            }
        }
    });
});




$("#txtState").change(function () {
    var state = {
        state_id: $("#txtState").val(),
        Dml_Indicator: "CI"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(state),
        success: function (data) {
            var data2 = JSON.parse(data)
            var data1 = data2.data;
            if (jQuery.isEmptyObject((data2.data))) {
            }
            else {

                if (data2.msg == "Success") {
                    var City;
                    $('#txtCity').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        City = City + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#txtCity').append(default_opt + City);

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

//function packageimageupload() {
//    debugger;
//    var uploadpath = $('#fileToUpload').val();
//    var fileExtension = uploadpath.substring(uploadpath.lastIndexOf(".") + 1, uploadpath.length);
//    if (window.FormData !== undefined) {
//        var fileUpload = $("#fileToUpload").get(0);
//        var files = fileUpload.files;
//        var fileData = new FormData();
//        if (allowedExtensions == /(\.pdf)$/i) {

//            for (var i = 0; i < files.length; i++) {
//                fileData.append(files[i].name, files[i]);
//            }
//            fileData.append('username', 'Manas');
//            $.ajax({
//                url: '/reports/UploadIDProof?branch_id=' + $('#CV_branch_id').val(),
//                type: "POST",
//                contentType: false,
//                processData: false,
//                data: fileData,
//                success: function (result) {
//                    debugger;
//                    $.each(result, function (i, emp) {
//                        $('#idpathvalue').val(emp);
                       
//                        var filepath = $('#hdnPatientPhoto').val();
//                        var profilephoto = $('#idpathvalue').val();
//                        var doctor_id = $('#HiddenDoctorID').val();
//                        var d, n, drloimage;
//                        d = new Date();
//                        n = d.getTime();
//                        drloimage = $('#hdnPatientPhoto').val() + $('#HospitalId').val() + '/' + $('#CV_branch_id').val() + '/vaccineimage/' + profilephoto + '?' + n;
//                        $('#photolog').empty();
//                        $('#photolog').show();
//                        $('#photolog').append('<a href="' + drloimage + '" target="_blank" class="another-eye">View</a>');

//                       // $('#photolog').append('<a href="' + $('#hdnPatientPhoto').val() + "/" + $('#HospitalId').val() + "/" + $('#CV_branch_id').val() + "/" + '/vaccineimage/' + emp.id_proof_path + '" target="_blank"><i class="fa fa-eye" aria-hidden="true"></i></a>');
//                    });
//                }
//            });
//        }
//        else {

//            var allowedExtensions =
//               /(\.jpg|\.jpeg)$/i;

//            $.when(checkimagedimensionvalidation(fileUpload.files[0], 512, 512)).
//        done(function (Results) {
//            debugger;
//            if (Results == 'Success') {
//                for (var i = 0; i < files.length; i++) {
//                    fileData.append(files[i].name, files[i]);
//                }

//                fileData.append('username', 'Manas');
//                $.ajax({
//                    url: '/reports/UploadIDProof?branch_id=' + $('#CV_branch_id').val(),
//                    type: "POST",
//                    contentType: false,
//                    processData: false,
//                    data: fileData,
//                    success: function (result) {
//                        debugger;
//                        $.each(result, function (i, emp) {                           
//                            $('#idpathvalue').val(emp);
                         
//                            var filepath = $('#hdnPatientPhoto').val();
//                            var profilephoto = $('#idpathvalue').val();
//                            var doctor_id = $('#HiddenDoctorID').val();
//                            var d, n, drloimage;
//                            d = new Date();
//                            n = d.getTime();
//                            drloimage = $('#hdnPatientPhoto').val() + $('#HospitalId').val() + '/' + $('#CV_branch_id').val() + '/vaccineimage/' + profilephoto + '?' + n;
//                            $('#photolog').empty();
//                            $('#photolog').show();
//                            $('#photolog').append('<a href="' +drloimage+ '" target="_blank" class="another-eye">View</a>');


//                          //  $('#photolog').append('<a href="' + $('#hdnPatientPhoto').val() + "/" + $('#HospitalId').val() + "/" + $('#CV_branch_id').val() + "/" + '/vaccineimage/' + emp.id_proof_path + '" target="_blank"><i class="fa fa-eye" aria-hidden="true"></i></a>');
//                        });
//                    }
//                });
//            }

//            else {
//                $('#fileToUpload').val('');
//                $('#idpathvalue').val('');
//                //  $('#HospitalFavicon').val('');
//                $('#photolog' ).empty();
//            }
//        });
//        }
//    }
//}
function packageimageupload() {
    debugger;
    var uploadpath = $('#fileToUpload').val();
    var fileExtension = uploadpath.substring(uploadpath.lastIndexOf(".") + 1, uploadpath.length);
    if (window.FormData !== undefined) {
        var fileUpload = $("#fileToUpload").get(0);
        var files = fileUpload.files;
        if (files[0].size > 5e+6) {
            //alert("up to 5 MB per file!");
            toastr.warning('Upto 5 MB Per File!');
            return false;
        };
        var filePath = fileUpload.value;

        // Allowing file type
        var allowedExtensions =
                /(\.jpg|\.jpeg)$/i;

        if (!allowedExtensions.exec(filePath)) {
            toastr.warning('Invalid File Type');
            fileInput.value = '';
            return false;
        }
        $.when(checkimagedimensionvalidation(fileUpload.files[0], 512, 512)).
        done(function (Results) {
            if (Results == 'Success') {
                var fileData = new FormData();
                for (var i = 0; i < files.length; i++) {
                    fileData.append(files[i].name, files[i]);
                }
                fileData.append('username', 'Manas');
                $.ajax({
                    url: '/reports/UploadIDProof?branch_id=' + $('#SelBranch').val() + '',
                    type: "POST",
                    contentType: false, // Not to set any content header
                    processData: false, // Not to process data
                    data: fileData,
                    success: function (result) {
                        debugger;
                        console.log(result);
                        $.each(result, function (i, emp) {
                            $('#idpathvalue').val(emp);
                            $('#photolog').empty();
                            $('#photolog').show();
                            $('#photolog').append('<a href="' + $('#hdnPatientPhoto').val() + $('#HospitalId').val() + '/' + $('#SelBranch').val() + '/Covidvaccine/' + emp + '" target="_blank" class="another-eye">View</a>');
                        });
                    }
                });
            }

            else {
                $('#idpathvalue').val('');
                //$('#imagefile').val('');
                $('#photolog').empty();
            }
            //this code is executed when all ajax calls are done
        });
    }
}


$('input[name="alpha_dot1"]').keyup(function (e) {
    debugger;
    var value = this.value;
    var id = this.id;
    var preg_check = /[^A-Za-z0-9-]/ig;

    if (value[0] == " " || value[0] == ".") {
        $('#P' + id).show();
        $('#P' + id).fadeOut(8000);
        $('#P' + id).html('Alpha numerics allowed,(.) and ( ) are allowed');
        //alert('Cannot Start With Dot and Space');
        $("#" + id).addClass('error');
        $("#" + id).val('');
        $("#" + id).focus();
    }

    if (preg_check.test(value)) {
        $('#P' + id).show();
        $('#P' + id).fadeOut(8000);
        $('#P' + id).html('Alpha numerics allowed,(.) and ( ) are allowed');
        //alert('Alphates only allowed');
        $("#" + id).val(value.replace(/[^A-Za-z0-9-]/ig, ''));
        $("#" + id).addClass('error');
        $("#" + id).focus();
    }
});
$('#btnregister').click(function () {
    debugger;
    if ($.trim($('#SelBranch').val()).length == "") {
        $('#PSelBranch').show();
        $('#PSelBranch').fadeOut(8000);
        $('#PSelBranch').html('Select The Branch');
        $('#PSelBranch').focus();
        $('#SelBranch').val('');
        return false;
    }
    if ($.trim($('#patsearch').val()).length == "") {
        $('#Ppatsearch').show();
        $('#Ppatsearch').fadeOut(8000);
        $('#Ppatsearch').html('Enter Patient Search');
        $('#patsearch').focus();
        $('#patsearch').val('');
        return false;
    }
    if ($.trim($('#patient_name').val()).length == "") {
        $('#Ppatient_name').show();
        $('#Ppatient_name').fadeOut(8000);
        $('#Ppatient_name').html('Enter Patient Name');
        $('#patient_name').focus();
        $('#patient_name').val('');
        return false;
    }
    if ($.trim($('#paytype').val()).length == "") {
        $('#Ppaytype').show();
        $('#Ppaytype').fadeOut(8000);
        $('#Ppaytype').html('Select Gender');
        $('#paytype').focus();
        $('#paytype').val('');
        return false;
    }
    if ($.trim($('#age').val()).length == "") {
        $('#Page').show();
        $('#Page').fadeOut(8000);
        $('#Page').html('Enter Age');
        $('#age').focus();
        $('#age').val('');
        return false;
    }
    if ($.trim($('#address1').val()).length == "") {
        $('#Paddress1').show();
        $('#Paddress1').fadeOut(8000);
        $('#Paddress1').html('Please Enter Line1 Address');
        $('#address1').focus();
        $('#address1').val('');
        return false;
    }
    if ($.trim($('#address2').val()).length == "") {
        $('#Paddress2').show();
        $('#Paddress2').fadeOut(8000);
        $('#Paddress2').html('Please Enter Line2 Address');
        $('#address2').focus();
        $('#address2').val('');
        return false;
    }
    if ($.trim($('#area').val()).length == "") {
        $('#Parea').show();
        $('#Parea').fadeOut(8000);
        $('#Parea').html('Enter Area');
        $('#area').focus();
        $('#area').val('');
        return false;
    }
    if ($.trim($('#txtCity').val()).length == "" || $('#txtCity').val() == 0) {
        $('#PtxtCity').show();
        $('#PtxtCity').fadeOut(8000);
        $('#PtxtCity').html('Enter City');
        $('#txtCity').focus();
        $('#txtCity').val('');
        return false;
    }
    if ($.trim($('#pin_code').val()).length == "" || $('#pin_code').val() == 0) {
        $('#Ppin_code').show();
        $('#Ppin_code').fadeOut(8000);
        $('#Ppin_code').html('Enter the Pin Code');
        $('#pin_code').focus();
        $('#pin_code').val('');
        return false;
    }
    if ($.trim($('#mphone').val()).length == "") {
        $('#Pmphone').show();
        $('#Pmphone').fadeOut(8000);
        $('#Pmphone').html('Enter Contact Number');
        $('#mphone').focus();
        $('#mphone').val('');
        return false;
    }
    if ($.trim($('#aadharname').val()).length == "") {
        $('#Paadharname').show();
        $('#Paadharname').fadeOut(8000);
        $('#Paadharname').html('Enter Id Proof Name');
        $('#aadharname').focus();
        $('#aadharname').val('');
        return false;
    }
    if ($.trim($('#aadharno').val()).length == "") {
        $('#Paadharno').show();
        $('#Paadharno').fadeOut(8000);
        $('#Paadharno').html('Enter Id Proof No');
        $('#aadharno').focus();
        $('#aadharno').val('');
        return false;
    }
    if ($.trim($('#fileToUpload').val()).length == "") {
        $('#PfileToUpload').show();
        $('#PfileToUpload').fadeOut(8000);
        $('#PfileToUpload').html('Upload Id Proof');
        $('#fileToUpload').focus();
        $('#fileToUpload').val('');
        return false;
    }
    if ($.trim($('#cotype').val()).length == "") {
        $('#Pcotype').show();
        $('#Pcotype').fadeOut(8000);
        $('#Pcotype').html('Select Co-Morbid Condition');
        $('#cotype').focus();
        $('#cotype').val('');
        return false;
    }
    if ($.trim($('#comp_name').val()).length == "") {
        $('#Pcomp_name').show();
        $('#Pcomp_name').fadeOut(8000);
        $('#Pcomp_name').html('Enter Company name');
        $('#comp_name').focus();
        $('#comp_name').val('');
        return false;
    }
    if ($.trim($('#app_date').val()).length == "") {
        $('#Papp_date').show();
        $('#Papp_date').fadeOut(8000);
        $('#Papp_date').html('Enter Appointment Date');
        $('#app_date').focus();
        $('#app_date').val('');
        return false;
    }
    if ($.trim($('#FTime').val()).length == "") {
        $('#PFTime').show();
        $('#PFTime').fadeOut(8000);
        $('#PFTime').html('Enter Appointment Time');
        $('#FTime').focus();
        $('#FTime').val('');
        return false;
    }

    if ($.trim($('#preferred_location').val()).length == "") {
        $('#Ppreferred_location').show();
        $('#Ppreferred_location').fadeOut(8000);
        $('#Ppreferred_location').html('Enter Preferred Location');
        $('#preferred_location').focus();
        $('#preferred_location').val('');
        return false;
    }

    if ($.trim($('#patient_name,#paytype,#age,#address1,#address2,#area,#mphone,#aadharname,#aadharno,#fileToUpload,#cotype,#app_date,#FTime,#FTime').val()).length > 0) {
        insert_vaccine_appointment();
    }
});


function insert_vaccine_appointment() {
    debugger;
    if ($('#hdpatid').val() == null || $('#hdpatid').val() == "" || $('#hdpatid').val() == '') {
        toastr.warning("Select the Patient from auto suggestion");
        return false;
    }
    if ($('#hdpatid').val() != null && $('#hdpatid').val() != "") {
        var date1 = $('#app_date').val().split('/');
        var date = date1[2] + "-" + date1[1] + "-" + date1[0];
        var package_data = {
            Dml_Indicator: "VAB",
            address_line_1: $('#address1').val(),
            address_line_2: $('#address2').val(),
            townvillage: $('#area').val(),
            name: $('#patient_name').val(),
            sex: $('#paytype').val(),
            age: $('#age').val(),
            phone_number_1: $("#mphone").val(),
            id_proof_name: $('#aadharname').val(),
            id_proof_number: $('#aadharno').val(),
            id_proof_path: $('#idpathvalue').val(),
            co_morbid: $('#cotype').val(),
            appointment_date: date,
            appointment_time: $('#FTime').val(),
            doctor_id: $('#HiddenDoctorID').val(),
            patient_id: $('#hdpatid').val(),
            company_name: $('#comp_name').val(),
            country: $('#txtCountry').val(),
            state: $('#txtState').val(),
            city: $('#txtCity').val(),
            pincode: $('#pin_code').val(),
            preferred_location: $('#preferred_location').val(),
            co_morbid_notes: $('#txtco_morbid_notes').val(),
            branchid: $('#SelBranch').val()
        }

        $.ajax({
            async: true,
            type: 'POST',
            url: '/reports/VaccineRegister',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(package_data),
            success: function (data) {
                console.log(data);
                if (jQuery.isEmptyObject((data)) || data.length == "0") {
                }
                else if (data.length > 0) {
                    var data1 = JSON.parse(data);
                    var b = data1.details.Table1;
                    console.log(b);
                    $.each(b, function (i, emp) {
                        debugger;
                        if (emp.msg == "1") {
                            toastr.success("Successfully Registered. Thank you!!!")
                            location.reload();
                        }
                        else {
                            toastr.warning("Already registered");
                            location.reload();
                        }
                    });
                }
            },
        });
    }
}


function load_vaccine_appointment_data() {
    $('#shade_slot').show();

    debugger;

    var date = '', dates='';
    date = $('#txtfrom_date').val();
    //if ($('#app_date_current').html() == 'Today') {
    //    var d = new Date();
    //    var month = d.getMonth() + 1;
    //    var day = d.getDate();
    //    var output = (day < 10 ? '0' : '') + day + '/' +
    //        (month < 10 ? '0' : '') + month + '/' +
    //        d.getFullYear();
    //    date = output;
    //}

    //else if ($('#app_date_current').html() == 'Yesterday') {
    //    date = $('#hndyesterday').val();
    //}
    //else if ($('#app_date_current').html() == 'Tomorrow') {
    //    date = $('#hndtomorrow').val();
    //}
    //else {
    //    date = $('#app_date_current').html();

    //}

    if (date == "" || date==null)
    {
        date=''
    }
    else
    {
        dates = date.split('/');
        date = dates[2] + '-' + dates[1] + '-' + dates[0];
    }
   

    var package_data = {
        Dml_Indicator: "TL",
        date: date,
        name: $('#Ftxtname').val(),
        patient_id: $('#Ftxtpatient_id').val(),
        branchid: $('#CV_branch_id').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/VaccineRegister',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            debugger;
            $('#shade_slot').hide();
            if (jQuery.isEmptyObject((data)) || data.length == "0") {

            }
            else if (data.length > 0) {
                $('#appointment_table_body').empty();
                $('#appointment_table').dataTable().fnClearTable();
                $('#appointment_table').dataTable().fnDraw();
                $('#appointment_table').dataTable().fnDestroy();
                var j = 0;
                var data1 = JSON.parse(data);
                console.log(data1);
                var b = data1.details;
                var row_data = '';
                $.each(b, function (i, emp) {
                    j++;
                    var booked_from = "-", co_morbid = "", co_morbid_list = "";
                    if (emp.booked_from == "1") booked_from = "In Clinic"
                    else booked_from = "Mobile App";
                    co_morbid_list = emp.co_morbid.split(",");
                    for (var i = 0; i < (co_morbid_list).length ; i++) {
                        if (co_morbid_list[i] == "1") co_morbid = co_morbid + " " + "Diabetes";
                        else if (co_morbid_list[i] == "2") co_morbid = co_morbid + " " + "Hypertension";
                        else if (co_morbid_list[i] == "3") co_morbid = co_morbid + " " + "Kidney disorders";
                        else if (co_morbid_list[i] == "4") co_morbid = co_morbid + " " + "Others";
                    }
                    
                    // row_data = row_data + "<tr><td>" + j + "</td><td>" + emp.Name + "</td><td>" + emp.phone_number_2 +"</td><td>" + emp.town_village + "</td><td>" + emp.id_proof_name + "," + emp.id_proof_number + "</td><td><a class='edit link-btn' onclick=id_image_show('" + emp.id_proof_path + "')>File</button></td><td>" + co_morbid + "</td><td>" + emp.company_name + "</td><td>" + emp.appointment_date + " " + emp.appointment_time + "</td><td>" + booked_from + "</td></tr>";
                    row_data = row_data + "<tr><td>" + j + "</td><td>" + emp.Name + "</td><td>" + emp.phone_number_2 + "</td><td>" + emp.town_village + "</td><td>" + emp.id_proof_name + "," + emp.id_proof_number + "</td><td><a href='" + $('#hdnCDfilepath').val() + "/" + $('#HospitalId').val() + "/" + emp.branchid + "/" + "Covidvaccine/" + emp.id_proof_path + "'>File</a></td><td>" + co_morbid + "</td><td>" + emp.company_name + "</td><td>" + emp.appointment_date + " " + emp.appointment_time + "</td><td>" + booked_from + "</td></tr>";

                    
                });
                $('#appointment_table_body').html(row_data);
                datatable('appointment_table', 'Covid_Vaccine_Registration', 'Legal');
            }
            
        },
    });
}


$('#CV_filter').click(function () {
    load_vaccine_appointment_data();
});

$('#CV_clear').click(function () {
    $('#Ftxtname').val('');
    $('#Ftxtpatient_id').val('');
    $('#CV_branch_id').val('');
    $('#txtfrom_date').val('');
    load_vaccine_appointment_data();
    var role_id = ''
    if ($('#hiddenRole_id').val() != 2) {
        LoadBranch();
    }
    
});


function id_image_show(id) {
    debugger;
    window.open(id, '_blank');
}

function patient_details(id) {
    debugger;
    var package_data = {
       
        Dml_Indicator: "PD",
        patient_id: id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/VaccineRegister',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            debugger;
            if (jQuery.isEmptyObject((data)) || data.length == "0") {
            }
            else if (data.length > 0) {
                var j = 0;
                var data1 = JSON.parse(data);
                var b = data1.details;
                $.each(b, function (i, emp) {
                    if (emp.first_name != null) {
                        $('#patient_name').val(emp.first_name);
                    }
                    if (emp.age != null) {
                        $('#age').val(emp.age);
                    }
                    if (emp.phone_number_1) {
                        $('#mphone').val(emp.phone_number_1);
                    }
                    if (emp.address_line_1) {
                        $('#address1').val(emp.address_line_1);
                    }
                    if (emp.address_line_2) {
                        $('#address2').val(emp.address_line_2);
                    }
                    if (emp.state != null) {
                        $('#HdnState').val(emp.state);

                    }
                    if (emp.city != null) {
                        $('#HdnCity').val(emp.city);

                    }
                    if (emp.country != null) {
                        $('#txtCountry').val(emp.country);
                        load_state();
                    }
                    
                    if (emp.town_village) {
                        $('#area').val(emp.town_village);
                     }
                    
                   
                    $('a[data-toggle=paytype]').addClass('not-active');
                    $('a[data-toggle=paytype]').removeClass('active');
                    $('a[data-toggle=paytype][data-title="' + emp.gender + '"]').addClass('active');
                    $('#paytype').val(emp.gender);
                    $('#hdpatid').val(id);
                    if (emp.count > 0) {
                        $('#PSpatient_name').show();
                        $('#PSpatient_name').fadeOut(8000);
                        $('#PSpatient_name').html('Already registered');
                    }
                });
            }
        },
    });
}


//date filter
function datefunction() {
    var date = $('#datepicker').val();
    $('#app_date_current').html(date);
    load_vaccine_appointment_data();
}
function leftarrow() {

    if ($('#app_date_current').html() == 'Today') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current').html(output);
    }
    else if ($('#app_date_current').html() == 'Yesterday') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() - 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current').html(output);
    }
    else if ($('#app_date_current').html() == 'Tomorrow') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() + 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current').html(output);
    }


    var displaydate = $('#app_date_current').html();
    var date = '';
    var month = '';
    var displaydates = displaydate.split("/");
    displaydate = displaydates[1];
    if (displaydate == '01' || displaydate == '03' || displaydate == '05' || displaydate == '07' || displaydate == '08' || displaydate == '10' || displaydate == '12') {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
        else {
            if (displaydate == '12') {
                date = parseInt(displaydates[1]) + parseInt(1);
                date = '30' + '/' + '11' + '/' + displaydates[2];
                $('#app_date_current').html(date);
            }
            else if (displaydate == '01') {
                date = parseInt(displaydates[1]) + parseInt(1);
                var year = (displaydates[2]) - (1);
                date = '31' + '/' + '12' + '/' + year;
                $('#app_date_current').html(date);
            }
            else if (displaydate == '03') {
                date = parseInt(displaydates[1]) + parseInt(1);
                date = '28' + '/' + '02' + '/' + displaydates[2];
                $('#app_date_current').html(date);
            }
            else {
                date = displaydates[1] - 1;
                if (date < 10) {
                    date = '0' + date;
                }
                date = '30' + '/' + date + '/' + displaydates[2];
                $('#app_date_current').html(date);
            }
        }

    }
    else if (displaydate == '04' || displaydate == '06' || displaydate == '09' || displaydate == '11') {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
        else {
            date = displaydates[1] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = '31' + '/' + date + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
    }
    else {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
        else {
            date = displaydates[1] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = '31' + '/' + '01' + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
    }
    if (date == $('#hndtoday').val()) {
        $('#app_date_current').html('Today');
    }
    if (date == $('#hndyesterday').val()) {
        $('#app_date_current').html('Yesterday');
    }
    if (date == $('#hndtomorrow').val()) {
        $('#app_date_current').html('Tomorrow');
    }
    load_vaccine_appointment_data();
}


function rightarrow() {
    if ($('#app_date_current').html() == 'Today') {


        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current').html(output);
    }


    else if ($('#app_date_current').html() == 'Yesterday') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() - 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current').html(output);
    }
    else if ($('#app_date_current').html() == 'Tomorrow') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() + 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current').html(output);
    }
    var displaydate = $('#app_date_current').html();
    var date = '';
    var month = '';
    var displaydates = displaydate.split("/");
    displaydate = displaydates[1];
    if (displaydate == '01' || displaydate == '03' || displaydate == '05' || displaydate == '07' || displaydate == '08' || displaydate == '10' || displaydate == '12') {
        if (displaydates[0] < 31) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
        else {
            if (displaydate == '12') {
                date = parseInt(displaydates[1]) + parseInt(1);
                year = parseInt(displaydates[2]) + parseInt(1);
                date = '01' + '/' + '01' + '/' + year;
                $('#app_date_current').html(date);
            }
            else {
                date = parseInt(displaydates[1]) + parseInt(1);
                if (date < 10) {
                    date = '0' + date;
                }
                date = '01' + '/' + date + '/' + displaydates[2];
                $('#app_date_current').html(date);
            }
        }
    }
    else if (displaydate == '04' || displaydate == '06' || displaydate == '09' || displaydate == '11') {
        if (displaydates[0] < 30) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
        else {
            date = parseInt(displaydates[1]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = '01' + '/' + date + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
    }
    else {
        if (displaydates[0] < 28) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
        else {
            date = parseInt(displaydates[1]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = '01' + '/' + date + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
    }
    if ($('#app_date_current').html() == $('#hndtoday').val()) {
        $('#app_date_current').html('Today');
    }
    if ($('#app_date_current').html() == $('#hndyesterday').val()) {
        $('#app_date_current').html('Yesterday');
    }
    if ($('#app_date_current').html() == $('#hndtomorrow').val()) {
        $('#app_date_current').html('Tomorrow');
    }
    load_vaccine_appointment_data();
}


function app_selected_options() {
    debugger;
    $('#appsellist').empty();
    if ($.trim($('#fromdatetodate').val()).length > 0) {
        var date = $('#fromdatetodate').val().split('-');
        var from_date = date[0];
        from_date = from_date.split("/").reverse().join("-");
        var to_date = date[1];
        to_date = to_date.split("/").reverse().join("-");
        var date_merge = from_date + " to " + to_date;
        $('#appsellist').append('<li> <span class="head-li-filter label label-primary">Date : ' + date_merge + ' <i class="fa fa-times"  onclick = app_sel_del(' + '1' + ')></i></span>  </li>');
    }

}

function app_sel_del(val) {
    $('#fromdatetodate').val('');
    app_selected_options();
}

$('#btnSearch').click(function () {
    if ($.trim($('#fromdatetodate').val()).length == "") {
        $('#Pfromdatetodate').show();
        $('#Pfromdatetodate').fadeOut(8000);
        $('#Pfromdatetodate').html('Please Select Date');
        $('#fromdatetodate').focus();
        $('#fromdatetodate').val('');
        return false;
    }
    LoadVaccineRegisterReport();
});

function LoadVaccineRegisterReport() {

    var date = $('#fromdatetodate').val().split('-');
    var from_date = date[0];
    from_date = from_date.split("/").reverse().join("-");
    var to_date = date[1];
    to_date = to_date.split("/").reverse().join("-");

    var package_data = {
        Dml_Indicator: "RL",
        date: from_date,
        todate: to_date
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/VaccineRegister',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            debugger;
            if (jQuery.isEmptyObject((data)) || data.length == 0) {
                tr = $('<table class="table-responsive table table-condensed body-borderless table_borderless" id="report_table" />');
                tr.append("<thead><tr><th>S No</th><th>Name</th><th>Details</th><th>Address</th><th>Id Proof Details</th><th>Id Proof</th><th>Co-Morbid</th><th>Company</th><th>Date & Time</th><th>Booked From</th></tr></thead><tbody>");
                tr.append("</tbody>");
            }
            else if (data.length > 0) {
                debugger;
                $('#divtblVaccineReport').empty();
                var j = 0;
                var data1 = JSON.parse(data);
                console.log(data1);
                var b = data1.details;
                tr = $('<table class="table-responsive table table-condensed body-borderless table_borderless" id="report_table" />');
                tr.append("<thead><tr><th>S No</th><th>Name</th><th>Details</th><th>Address</th><th>Id Proof Details</th><th>Id Proof</th><th>Co-Morbid</th><th>Company</th><th>Date & Time</th><th>Booked From</th></tr></thead><tbody>");
                $.each(b, function (i, emp) {
                    j++;
                    var booked_from = "-", co_morbid = "", co_morbid_list = "";
                    if (emp.booked_from == "1") booked_from = "In Clinic"
                    else booked_from = "Mobile App";
                    co_morbid_list = emp.co_morbid.split(",");
                    for (var i = 0; i < (co_morbid_list).length; i++) {
                        if (co_morbid_list[i] == "1") co_morbid = co_morbid + " " + "Diabetes";
                        else if (co_morbid_list[i] == "2") co_morbid = co_morbid + " " + "Hypertension";
                        else if (co_morbid_list[i] == "3") co_morbid = co_morbid + " " + "Kidney disorders";
                        else if (co_morbid_list[i] == "4") co_morbid = co_morbid + " " + "Others";
                    }
                  


                    
                   
                        row_data = "<tr><td>" + j + "</td><td>" + emp.Name + "</td><td>" + emp.phone_number_2 + "</td><td>" + emp.town_village + "</td><td>" + emp.id_proof_name + "," + emp.id_proof_number + "</td><td><a class='edit link-btn' onclick=id_image_show('" + emp.id_proof_path + "')>File</button></td><td>" + co_morbid + "</td><td>" + emp.company_name + "</td><td>" + emp.appointment_date + " " + emp.appointment_time + "</td><td>" + booked_from + "</td></tr>";
                        tr.append(row_data)
                    
                    
                });
                tr.append("</tbody>");
                $.when($('#divtblVaccineReport').append(tr)).then($('#report_table').DataTable({
                    "columnDefs": [{
                        "orderable": false,
                    }]
                }));

                var data_app = '';

                $("#appsellist li span").each(function () { data_app += $(this).html().split("").join("").split("").join("") + '\n\r' })
                $('#report_table').DataTable({
                    destroy: true,
                    dom: 'Bfrtip',
                    messageTop: data_app,
                    buttons: [
                        {
                            extend: 'pdf',
                            title: 'COVID Vaccine Registration Report: \n\r' + data_app
                        },
                        {
                            extend: 'excel',
                            //customize: _customizeExcelOptions,
                            title: 'COVID Vaccine Registration Report: \n\r' + data_app
                        },
                        {
                            extend: 'csv',
                            title: 'COVID Vaccine Registration Report: \n\r' + data_app
                        },
                    ]
                });
            }
        },
    });
}


function changeComorbid(val) {
    if (val == 4) {
        $('#divOtherComorbidNotes').show();
    }
    else {
        $('#divOtherComorbidNotes').hide();
    }
}