<?php

    use \koolreport\widgets\koolphp\Table;
    use koolreport\core\Utility as Util;
    use koolreport\datagrid\DataTables;
    use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding;
    use \koolreport\export\Exportable;
    use \koolreport\excel\ExcelExportable;

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    if (empty($_POST["datefrom"])) {
        $selected_date = NULL;
    } else {
        $selected_date = test_input($_POST["datefrom"]);
    }
    if (empty($_POST["dateto"])) {
        $dateto = NULL;
    } else {
        $dateto = test_input($_POST["dateto"]);
    }

?>

</div>

<div class="report-content">

    <div class="text-center">
        <h1>Invoice Report</h1>

        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <?php
            function addDayswithdate($date, $days){
                $date = strtotime("+".$days." days", strtotime($date));
                return date("Y-m-d", $date);
            }

            $today2 = date('yy-m-d');
            $today3 = DateTime::createFromFormat('Y-m-d', $today2);
            $today = $today3->format("d")."-".$today3->format("m")."-20".$today3->format("y");
            $today2 = "20".$today3->format("y")."-".$today3->format("m")."-".$today3->format("d");
            $prev_date = addDayswithdate($today2, -7);
            $mystartdate = $selected_date;
            $myenddate = $dateto;
            
            if ($selected_date != NULL) {
                $prev_date = $selected_date;
            }

            echo("<input type=\"date\" id=\"datefrom\" name=\"datefrom\" value=\"".$selected_date."\">");
            echo("<input type=\"date\" id=\"dateto\" name=\"dateto\" value=\"".$dateto."\"><br>");

            require 'filters.php';
            echo("<br>");
            echo("<button class=\"button\" name=\"direction\" value=\"0\"><span>Preview</span></button>");
            // echo("<button class=\"button\" name=\"direction\" value=\"0\" onClick=\"handleDownload('".$selected_date."','".$dateto."')\"><span>Preview</span></button>");
            echo("<button class=\"button\" name=\"Download\" value=\"Download\" type=\"button\" onClick=\"handleDownload('".$selected_date.",".$dateto."')\"><span>Download</span></button>");

        ?>
        </form>
    </div>

    <?php
        if ($prev_date == $today2){
            $today2 = addDayswithdate($today2, +1);
        }

        DataTables::create(array(
            "name" => "MyTable1",
            "dataStore" => $this->dataStore('sales_by_customer'),
            "themeBase" => "bs4", // Reference Bootstrap 4 theme
            "cssClass" => array("table" => "table table-bordered table-hover table-striped"),
            "showFooter" => false,
            "columns" => array(
                "number22" => array("label" => "Invoice Number", "class" => "text-center"),
                "date2" => array("label" => "Invoice Date", "class" => "text-center"),
                "number2" => array("label" => "Payable", "class" => "text-center"),
                "number20" => array("label" => "Invoice total", "class" => "text-center", "formatValue" => function($value) {
                    return '£' . number_format($value, 2, '.', ',');
                
                }  ),

            ),
            "plugins" => array("Buttons", "FixedColumns", "FixedHeader", "KeyTable", "Responsive", "RowReorder", "Scroller", "SearchPanes", "RowGroup"),
            "buttons" => array(
                array(
                    "extend" => "pdfHtml5",
                    "orientation" => "landscape",
                    "pageSize" => "A4",
                    "title" => "Invoice Report"
                ),
                "copy", "csv", "excel", "print", "colvis"
            ),
            "options" => array(
                "paging" => true,
                "pageLength" => 200,
                "searching" => true,
                "dom" => 'Bfrtip',
                "buttons" => array("copy", "csv", "excel", "pdf", "print", "colvis"),
            ),
            "clientRowGroup" => array(
                "number22" => array(
                    "calculate" => array(
                        "number2" => array("sum", "number2")
                        
                    ),
                    "top" => "<td colspan='3'>{expandCollapseIcon} Invoice No #{number22} | Total: £{number2} </td>",
                    "bottom" => "<td colspan='3'>Total: £{number2}</td>",
                    "groupCollapse" => false,
                )
            ),
            
            
        
               
         
            )
                );
        
        
        
    ?>
</div>