<?php
    session_start();
    include("include/Functions.php");   
    $db = new Functions();  
    $result = $db->fetchAllTicketTransaction($_SESSION['companyfk']);
    $transactions = $result['transactions']; 
    $statistics = $db->fetchTicketSalesStatistics($_SESSION['companyfk']);

    if(empty($_SESSION['userid']))
        header('Location: index.php');

?>
<?php
    include("include/head.html");   
     
?>
        <!-- End Left Sidebar  -->
        <!-- Page wrapper  -->
        <div class="page-wrapper">
            <!-- Bread crumb -->
            <div class="row page-titles">
                <div class="col-md-5 align-self-center">
                    <h3 class="text-primary">Dashboard</h3> </div>
                <div class="col-md-7 align-self-center">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="javascript:void(0)">Home</a></li>
                        <li class="breadcrumb-item active">Ticket Transactions</li>
                    </ol>
                </div>
            </div>
            <!-- End Bread crumb -->
            <!-- Container fluid  -->


            <div class="container-fluid">
                <!-- Start Page Content -->
              <div class="row">
         <?php $revenue = $db->fetchTicketDashboardRevenues($_SESSION['companyfk']);?>
                    <div class="col-md-3">
                        <div class="card p-30">
                            <div class="media">
                                <div class="media-left meida media-middle">
                                    <span> <h1 style="color:#9b59b6;">K</h1></span>
                                </div>
                                <div class="media-body media-text-right">
                                   <h2><?php echo number_format( $revenue["lifetimeRevenue"],2) ?></h2>
                                    <p class="m-b-0">All Time Revenue</p>
                                </div>
                            </div>
                        </div>
                    </div>


                        <div class="col-md-3">
                        <div class="card p-30">
                            <div class="media">
                                <div class="media-left meida media-middle">
                                    <span> <h1 style="color:#2ecc71;">K</h1></span>
                                </div>
                                <div class="media-body media-text-right">
                                <h2><?php echo number_format( $revenue["dayRevenue"],2) ?></h2>
                                    <p class="m-b-0">Today's Revenue</p>
                                </div>
                            </div>
                        </div>
                    </div>


                     <div class="col-md-3">
                        <div class="card p-30">
                            <div class="media">
                                <div class="media-left meida media-middle">
                                    <span> <h1 style="color:#f1c40f;">K</h1></span>
                                </div>
                                <div class="media-body media-text-right">
                                       <h2><?php echo number_format( $revenue["weekRevenue"],2) ?></h2>
                                    <p class="m-b-0">This Weekly's Revenue</p>
                                </div>
                            </div>
                        </div>
                    </div>



                     <div class="col-md-3">
                        <div class="card p-30">
                            <div class="media">
                                <div class="media-left meida media-middle">
                                    <span> <h1 style="color:#34495e;">K</h1></span>
                                </div>
                                <div class="media-body media-text-right">
                                    <h2><?php echo number_format( $revenue["monthRevenue"],2) ?></h2>
                                    <p class="m-b-0">This Month's Revenue</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>



                <div class="row">
                    <div class="col-lg-12">
                           <div class="card">
                            <div class="card-title">
                             <h4>Ticket Sale volume</h4>
                                <div id="chart_div"></div>
                                <div class="sales-chart">
                                <canvas id="team-chart"></canvas>
                            </div>
                            </div>
                        </div>
                    </div>
                </div>





                <div class="row bg-white m-l-0 m-r-0 box-shadow ">

                    <!-- column -->
                    <div class="col-lg-12">
                           <div class="card">
                            <div class="card-title">
                                <h4>Recent Transactions </h4>
                            </div>
                            <div class="card-body">
                <h6 class="card-subtitle">Export data to Copy, CSV, Excel, PDF & Print</h6>
                                <div class="table-responsive m-t-40">
                                    <table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
                                        <thead>
                                              <tr>
                                                <th>Receipt #</th>
                                                <th>Fair</th>
                                                <th>Luggage Fair</th>
                                                <th>Amount paid</th>
                                                <th>Route</th>
                                                <th>Station</th>
                                                <th>Number</th>
                                                <th>Next Of Kin</th>
                                                <th>Cashier</th>  
                                                <th>Time</th>
                                                <th>Date</th>
                                                <th>Reverse</th>  
                                            </tr>
                                        </thead>
                                        <tfoot>
                                               <tr>
                                                <th>Receipt #</th>
                                                <th>Fair</th>
                                                <th>Luggage Fair</th>
                                                <th>Amount paid</th>
                                                <th>Route</th>
                                                <th>Station</th>
                                                <th>Number</th>
                                                <th>Next Of Kin</th>
                                                <th>Cashier</th>  
                                                <th>Time</th>
                                                <th style="width: 7%;">Date</th>
                                                <th>Reverse</th>  
                                            </tr>
                                        </tfoot>
                                      
                                                  
                                            <?php foreach ($transactions as $transaction) {?>
                                                <tr class="info">
                                                <td><?php echo  $transaction['ticketid']; ?> </td>
                                                <td> <?php echo number_format($transaction['fair'], 2); ?></td>
                                                <td><?php echo  $transaction['luggagefair']; ?> </td>
                                                <td> <?php echo number_format($transaction['fair'] + $transaction['luggagefair'], 2); ?></td>  
                                                <td> <?php echo $transaction['routeName']; ?></td>
                                                <td> <?php echo $transaction['name']; ?></td>
                                                <td> <?php echo $transaction['custnumber']; ?></td>
                                                <td> <?php echo $transaction['custnextofkin']; ?></td>
                                                <td><?php echo  $transaction['cashiername']; ?> </td>
                                                 
                                                <td> <?php echo date('H:i', strtotime($transaction['datecreated']))."Hrs"; ?></td>
                                                <td> <?php echo date('d-M-Y', strtotime($transaction['datecreated'])); ?></td>
                                                        <td><a href="reverseticket.php?reversed=<?php echo  $transaction['ticketid']; ?>"
                                                        <form   action="reverseticket.php"  method="post">
                                                        <button class="btn btn-success btn-rounded" type="button" type="submit" name="submit">Reverse</button>
                                                        </form>
                                                </td> 

                                                 </tr>
                                                     <?php } ?>

                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                  
                </div>




                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->
            <!-- footer -->
       
            <!-- End footer -->
        </div>
             <footer class="footer"> © 2020 All rights reserved. Template designed by <a href="https://brigafrica.com">Meit Systems</a></footer>
        <!-- End Page wrapper  -->
    </div>
     <!-- Amchart -->
     <script src="js/lib/morris-chart/raphael-min.js"></script>
    <script src="js/lib/morris-chart/morris.js"></script>
    <script src="js/lib/morris-chart/dashboard1-init.js"></script>


    <script src="js/lib/calendar-2/moment.latest.min.js"></script>
    <!-- scripit init-->
    <script src="js/lib/calendar-2/semantic.ui.min.js"></script>
    <!-- scripit init-->
    <script src="js/lib/calendar-2/prism.min.js"></script>
    <!-- scripit init-->
    <script src="js/lib/calendar-2/pignose.calendar.min.js"></script>
    <!-- scripit init-->
    <script src="js/lib/calendar-2/pignose.init.js"></script>

    <script src="js/lib/owl-carousel/owl.carousel.min.js"></script>
    <script src="js/lib/owl-carousel/owl.carousel-init.js"></script>
    <script src="js/scripts.js"></script>
    <!-- scripit init-->

    <script src="js/custom.min.js"></script>



    <script src="js/lib/datatables/datatables.min.js"></script>
    <script src="js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js"></script>
    <script src="js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.flash.min.js"></script>
    <script src="js/lib/datatables/cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
    <script src="js/lib/datatables/cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
    <script src="js/lib/datatables/cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
    <script src="js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.html5.min.js"></script>
    <script src="js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.print.min.js"></script>
    <script src="js/lib/datatables/datatables-init.js"></script>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
        <!-- End Wrapper -->
    <!-- All Jquery -->
    <script src="js/lib/jquery/jquery.min.js"></script>
    <!-- Bootstrap tether Core JavaScript -->
    <script src="js/lib/bootstrap/js/popper.min.js"></script>
    <script src="js/lib/bootstrap/js/bootstrap.min.js"></script>
    <!-- slimscrollbar scrollbar JavaScript -->
    <script src="js/jquery.slimscroll.js"></script>
    <!--Menu sidebar -->
    <script src="js/sidebarmenu.js"></script>
    <!--stickey kit -->
    <script src="js/lib/sticky-kit-master/dist/sticky-kit.min.js"></script>

    <script src="js/lib/chart-js/Chart.bundle.js"></script>

    <!--Custom JavaScript -->
    <script src="js/custom.min.js"></script>

    <script src="js/lib/toastr/toastr.min.js"></script>
    <!-- scripit init-->
    <script src="js/lib/toastr/toastr.init.js"></script>
    <!--Custom JavaScript -->
    <script src="js/custom.min.js"></script>
   <script type="text/javascript">

var result = '<?php echo $statistics; ?>';
var statistics = JSON.parse(result);
var months = [];
var ticketsSold = [];


for (var i = 0; i < statistics.length; i++) {
        months.push(statistics[i].month);
        ticketsSold.push(statistics[i].count);
}
    


    //Team chart
    var ctx = document.getElementById( "team-chart" );
    ctx.height = 150;
    var myChart = new Chart( ctx, {
        type: 'line',
        data: {

            labels: months,
            type: 'line',
            defaultFontFamily: 'Roboto',
            datasets: [ {
                data: ticketsSold,
                label: 'Tickets sold',
                backgroundColor: 'rgba(0,103,255,.15)',
                borderColor: 'rgba(0,103,255,0.5)',
                borderWidth: 3.5,
                pointStyle: 'circle',
                pointRadius: 5,
                pointBorderColor: 'transparent',
                pointBackgroundColor: 'rgba(0,103,255,0.5)',
                    }, ]
        },
        options: {
            responsive: true,
            tooltips: {
                mode: 'index',
                titleFontSize: 12,
                titleFontColor: '#000',
                bodyFontColor: '#000',
                backgroundColor: '#fff',
                titleFontFamily: 'Roboto',
                bodyFontFamily: 'Roboto',
                cornerRadius: 3,
                intersect: false,
            },
            legend: {
                display: false,
                position: 'top',
                labels: {
                    usePointStyle: true,
                    fontFamily: 'Roboto',
                },


            },
            scales: {
                xAxes: [ {
                    display: true,
                    gridLines: {
                        display: false,
                        drawBorder: false
                    },
                    scaleLabel: {
                        display: false,
                        labelString: 'Month'
                    }
                        } ],
                yAxes: [ {
                    display: true,
                    gridLines: {
                        display: false,
                        drawBorder: false
                    },
                    scaleLabel: {
                        display: true,
                        labelString: 'Ticket sales for a period of '+statistics.length+' Month(s)'
                    }
                        } ]
            },
            title: {
                display: false,
            }
        }
    } );




    //Sales chart
    var ctx = document.getElementById( "sales-chart" );
    ctx.height = 150;
    var myChart = new Chart( ctx, {
        type: 'line',
        data: {
            labels: [ "2010", "2011", "2012", "2013", "2014", "2015", "2016" ],
            type: 'line',
            defaultFontFamily: 'Montserrat',
            datasets: [ {
                label: "Foods",
                data: [ 0, 30, 10, 120, 50, 63, 10 ],
                backgroundColor: 'transparent',
                borderColor: 'rgba(220,53,69,0.75)',
                borderWidth: 3,
                pointStyle: 'circle',
                pointRadius: 5,
                pointBorderColor: 'transparent',
                pointBackgroundColor: 'rgba(220,53,69,0.75)',
                    }, {
                label: "Electronics",
                data: [ 0, 50, 40, 80, 40, 79, 120 ],
                backgroundColor: 'transparent',
                borderColor: 'rgba(40,167,69,0.75)',
                borderWidth: 3,
                pointStyle: 'circle',
                pointRadius: 5,
                pointBorderColor: 'transparent',
                pointBackgroundColor: 'rgba(40,167,69,0.75)',
                    } ]
        },
        options: {
            responsive: true,

            tooltips: {
                mode: 'index',
                titleFontSize: 12,
                titleFontColor: '#000',
                bodyFontColor: '#000',
                backgroundColor: '#fff',
                titleFontFamily: 'Montserrat',
                bodyFontFamily: 'Montserrat',
                cornerRadius: 3,
                intersect: false,
            },
            legend: {
                display: false,
                labels: {
                    usePointStyle: true,
                    fontFamily: 'Montserrat',
                },
            },
            scales: {
                xAxes: [ {
                    display: true,
                    gridLines: {
                        display: false,
                        drawBorder: false
                    },
                    scaleLabel: {
                        display: false,
                        labelString: 'Month'
                    }
                        } ],
                yAxes: [ {
                    display: true,
                    gridLines: {
                        display: false,
                        drawBorder: false
                    },
                    scaleLabel: {
                        display: true,
                        labelString: 'Value'
                    }
                        } ]
            },
            title: {
                display: false,
                text: 'Normal Legend'
            }
        }
    } );




   </script>
</body>

</html>