<?php
session_start();

if (!isset($_SESSION['userID'])) {
    header("Location: sign-in.php");
    exit();
}
$_SESSION['focus']=2;
require_once "SqlConnection/MySqlConnection.php";
$sql = "Select * from invoices order by id";
$data = mysqli_query($conn, $sql);
$userID = $_SESSION['userID'];
//declaring 
$totalHst=0;
$subtotal=0;
$totalTax=0;

?>
<!DOCTYPE html>
<html lang="en" dir="ltr" data-scompiler-id="0">


<head>
    <meta charSet="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="format-detection" content="telephone=no" />
    <title>Invoices</title><!-- icon -->
    <?php
        include('headerLinks.php');
     ?>   
</head>

<body><!-- sa-app -->
    <div class="sa-app sa-app--desktop-sidebar-shown sa-app--mobile-sidebar-hidden sa-app--toolbar-fixed">
    <?php
        include('sidebar.php');
     ?> 
        
        <div class="sa-app__content"><!-- sa-app__toolbar -->
            <?php
            include('navbar.php');
            ?> 
            <div id="top" class="sa-app__body">
                <div class="mx-sm-2 px-2 px-sm-3 px-xxl-4 pb-6">
                    <div class="container">
                        <div class="py-5">
                        </div>
                       
                        <div class="card">
                                            <div class="p-4"><input type="text" onkeyup="myFunction()" id="myInput" placeholder="Start typing to search" class="form-control form-control--search mx-auto" id="table-search" />
                                                <div class="sa-divider"><br></div>
                                                <table class="sa-datatables-init text-nowrap" id="myTable" data-order="[[ 1, &quot;asc&quot; ]]" data-sa-search-input="#table-search">
                                                    <thead style="background-color:silver">
                                                        <tr>
                                                            <th>Invoice#</th>
                                                            <th>Date</th>
                                                            <th>Customer Name</th>
                                                            <th>Customer Address</th>                                                            
                                                            <th>Service Type</th>
                                                            <th>Payment</th>
                                                            <th>HST</th>
                                                            <th>Subtotal</th>
                                                            <th>Total Tax</th>                                                            
                                                            <th class="w-min" data-orderable="false"></th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>

                                                        <?php
                                                        while ($row = mysqli_fetch_array($data)) {
                                                        ?>
                                                            <tr>
                                                                <td><?php echo $row['invoiceNumber'] ?></td>
                                                                <td><?php echo $row['date'] ?></td>
                                                                <td><?php echo $row['customerName'] ?></td>
                                                                <td><?php echo $row['customerAddress'] ?></td>                                                                
                                                                <td><?php echo $row['serviceType'] ?></td>
                                                                <td><?php echo $row['payment'] ?></td>
                                                                <td><?php echo $row['hst'] ?></td>
                                                                <td><?php echo $row['subtotal']?></td>
                                                                <td><?php echo $row['totalTax']?></td>                                                                
                                                                <td>
                                                                    <div class="dropdown"><button class="btn btn-sa-muted btn-sm" type="button" id="order-context-menu-0" data-bs-toggle="dropdown" aria-expanded="false" aria-label="More"><svg xmlns="http://www.w3.org/2000/svg" width="3" height="13" fill="currentColor">
                                                                                <path d="M1.5,8C0.7,8,0,7.3,0,6.5S0.7,5,1.5,5S3,5.7,3,6.5S2.3,8,1.5,8z M1.5,3C0.7,3,0,2.3,0,1.5S0.7,0,1.5,0 S3,0.7,3,1.5S2.3,3,1.5,3z M1.5,10C2.3,10,3,10.7,3,11.5S2.3,13,1.5,13S0,12.3,0,11.5S0.7,10,1.5,10z">
                                                                                </path>
                                                                            </svg></button>
                                                                        <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="order-context-menu-0">
                                                                            <!-- <li>
                                                                                <form action="print.php" method="GET" target="_blank">
                                                                                <input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
                                                                                <input type="hidden" name="invoiceNumber" value="<?php echo $row['invoiceNumber']; ?>" />
                                                                                <input type="hidden" name="date" value="<?php echo $row['date']; ?>" />
                                                                                <input type="hidden" name="customerName" value="<?php echo $row['customerName']; ?>" />
                                                                                <input type="hidden" name="customerAddress" value="<?php echo $row['customerAddress']; ?>" />
                                                                                <input type="hidden" name="serviceType" value="<?php echo $row['serviceType']; ?>" />
                                                                                <input type="hidden" name="payment" value="<?php echo $row['payment']; ?>" />
                                                                                <input type="hidden" name="hst" value="<?php echo $row['hst']; ?>" />
                                                                                <input type="hidden" name="subtotal" value="<?php echo $row['subtotal']; ?>" />
                                                                                <input type="hidden" name="totalTax" value="<?php echo $row['totalTax']; ?>" />
                                                                                    <input type="submit" class="dropdown-item text-danger" value="Print Slip" />
                                                                                </form>
                                                                            </li>
                                                                            <li>
                                                                               <hr>
                                                                            </li> -->
                                                                            <li>
                                                                                <form action="delete-invoice.php" method="POST">
                                                                                <input type="hidden" name="invoiceNumber" value="<?php echo $row['invoiceNumber']; ?>" />                                                                                
                                                                                    <input type="submit" class="dropdown-item text-danger" value="Delete" />
                                                                                </form>
                                                                            </li>
                                                                        </ul>
                                                                    </div>
                                                                </td>
                                                            </tr>

                                                        <?php
                                                        }
                                                        ?>
                                                    </tbody>
                                                </table>
                                            </div>


                                        </div>
                                        <div class="py-5">

                                        </div>
                                        <div class="card">                                        
                                                   <b> <label style="padding:10px">Total HST: <?php echo $totalHst; ?></label></b>
                                                   <b> <label style="padding:10px">Subtotal: <?php echo $subtotal; ?></label></b>
                                                   <b> <label style="padding:10px">Total Tax: <?php echo $totalTax; ?></label></b>
                                        </div>
                    </div>
                </div>
            </div>
            <?php
            include('footer.php');
            ?> 
        </div>
    </div>        
    <script>
            function myFunction() {
                // Declare variables
                var input, filter, table, tr, td, i, j, txtValue;
                input = document.getElementById("myInput");
                filter = input.value.toUpperCase();
                table = document.getElementById("myTable");
                tr = table.getElementsByTagName("tr");

                // Loop through all table rows, and hide those who don't match the search query
                for (i = 0; i < tr.length; i++) {
                    // Reset display of row
                    tr[i].style.display = "";

                    // Check if search query is empty
                    if (filter === "") {
                        continue;
                    }

                    // Loop through all columns of the table row
                    for (j = 0; j < tr[i].cells.length; j++) {
                        td = tr[i].cells[j];
                        if (td) {
                            txtValue = td.textContent || td.innerText;
                            if (txtValue.toUpperCase().indexOf(filter) > -1) {
                                // Show row if search query matches any column value
                                tr[i].style.display = "";
                                break;
                            } else {
                                // Hide row if search query doesn't match any column value
                                tr[i].style.display = "none";
                            }
                        }
                    }
                }
            }
        </script>
    <?php 
        include('footerLinks.php');
    ?>
</body>

</html>