<!DOCTYPE html>
<html>
<head>
  <title> Windows Patch Report </title>
</head>
<body>
<center>
<h1>Ansible Windows Patching Report</h1>
<style>
@media print {
   .noprint {
        display: none !important;
    }
}
</style>
<div class="noprint">
<button type="button" onclick="tableToCSV()">Download CSV</button>
<input type="button" value="Print" onClick="window.print()">
</div>
</center>
<table border = "1" cellpadding = "5" cellspacing = "5">
    <thead>
        <tr>
            <th>Hostname</th>
            <th>Operating System</th>
            <th>Required Updates</th>
        </tr>
    </thead>
    <tbody>
{% for windows_host in groups['tag_Windows']|sort %}
        <tr>
            <td>{{hostvars[windows_host]['inventory_hostname']}}</td>
            <td>{{hostvars[windows_host]['ansible_distribution']|default("none")}}</td>
            <td>
              <ul>

{% if hostvars[windows_host].patchresult.updates is defined and hostvars[windows_host].patchresult.found_update_count|int > 0  %}
{% for update in hostvars[windows_host].patchresult.updates %}
{% set updatenum = hostvars[windows_host].patchresult.updates[update] %}
            <li>{{updatenum.title}}</li>
{% endfor %}
{% else %}
            <li>Compliant</li>
{% endif %}
            </ul>
            </td>

        </tr>
{% endfor %}
    </tbody>
</table>
{% for host in ansible_play_hosts %}
<center><p>Created with Ansible on {{hostvars[host].ansible_date_time.iso8601}}</p></center>
{% endfor %}
<script type="text/javascript">
function tableToCSV() {

    // Variable to store the final csv data
    var csv_data = [];

    // Get each row data
    var rows = document.getElementsByTagName('tr');
    for (var i = 0; i < rows.length; i++) {

        // Get each column data
        var cols = rows[i].querySelectorAll('td,th');

        // Stores each csv row data
        var csvrow = [];
        for (var j = 0; j < (cols.length); j++) {

            // Get the text data of each cell of
            // a row and push it to csvrow
            if ( j == cols.length-1 && i==0){}
            else{
            csvrow.push(cols[j].textContent.replace(/,/g, " "));
            }

       }
       csv_data.push(csvrow.join(","));
}

    // combine each row data with new line character
    csv_data = csv_data.join('\n');

    // Call this function to download csv file
    downloadCSVFile(csv_data);
}
function downloadCSVFile(csv_data) {

    // Create CSV file object and feed our
    // csv_data into it
    CSVFile = new Blob([csv_data], { type: "text/csv" });

    // Create to temporary link to initiate
    // download process
    var temp_link = document.createElement('a');
    var todayDate = new Date().toISOString().slice(0, 10);

    // Download csv file
    temp_link.download = "windowspatching-" + todayDate + ".csv";
    var url = window.URL.createObjectURL(CSVFile);
    temp_link.href = url;

    // This link should not be displayed
    temp_link.style.display = "none";
    document.body.appendChild(temp_link);

    // Automatically click the link to trigger download
    temp_link.click();
    document.body.removeChild(temp_link);
}
</script>
</body>
</html>
