<?php
    session_start();
    include("include/Functions.php");   
    $db = new Functions();  
    $result = $db->fetchAllTicketTransaction(1);
    $transactions = $result['transactions']; 
    $statistics = $db->fetchTicketSalesStatistics();
    $cashiers = $db->fetchAllCashiers(1)['cashiers'];
    $routes = $db->fetchAllRoutesByCompanyId(1)['routes'];
    $stations = $db->fetchAllStationsByCompanyId(1)['stations'];

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

    $custNameError = ""; 
    $custPhoneError = "";  
    $custNextOfKinError = ""; 
    $seatNoError = ""; 
    $routeError = "";
    $cashierError = "";
    $stationError = "";  


    $custName = "";
    $custPhone = "";
    $custNxtOfKin = "";
    $seatNo = "";
    $luggageFair = "";
    $discountGiven = "";
    $transactiondate = "";

   
    $sendername ="";
    $receivername ="";
    $sendernumber="";
    $receivernumber ="";
    $price = "";
    $discount = "";
    $barcode ="";
    $depaturedate ="";
    $transactiondate ="";

    $posId = "35300706980459M";
   


   
if ($_SERVER["REQUEST_METHOD"] == "POST") {

         $barcode = rand (100000000 , 1000000000)."L";

        if(empty($_POST["custname"]))
            $custNameError = "Enter customer name";
        else $custName = $_POST["custname"];

        if(empty($_POST["custphone"]))
             $custPhoneError = "Enter customer phone number";
         else $custPhone = $_POST["custphone"];

        if(empty($_POST["nextofkin"]))
             $custNextOfKinError = "Enter next of kin phone number"; 
         else $custNxtOfKin = $_POST["nextofkin"];

        if(empty($_POST["seatno"]))
             $seatNoError = "Enter seat number";

          else if ($_POST["seatno"] > 68)
                $seatNoError = "Enter seat number between 1 and 68";
          else $seatNo = $_POST["seatno"];


        if(!empty($_POST["luggagefair"])){
             $luggageFair = $_POST["luggagefair"];
             $barcode = $barcode."L";   
         }

        if(!empty($_POST["discount"]))
             $discountGiven = $_POST["discount"];


    

     if(!empty($_POST["sendername"])
        && !empty($_POST["receivername"])
        && !empty($_POST["sendernumber"])
        && !empty($_POST["receivernumber"])
        &&  !empty($_POST["price"])
        &&  isset($_POST["route"])
        && !empty($_POST["depaturedate"])
        && isset($_POST["route"])
        && isset($_POST["station"])
        && !empty($_POST['transactiondate'])){


            $routefk = $_POST["route"];
            $stationfk =  $_POST["station"];  
            $cashierfk = $_POST["cashier"];
            $transactiondate = date("Y-m-d H:i:s", strtotime($_POST['transactiondate']));

            foreach ($routes as $route) {

                    if(strcmp($routefk, $route['routeid']) == 0) {
                            $barcode = substr(strtoupper($route['jorney']), 0, 3) . $barcode;

                    }    

             } 




            $db->insertSingleParcelTransaction(1, $companyfk, $routefk, $stationfk, $cashierfk, $posid, $sendername, $receivername, $sendernumber,  $receivernumber, $price, $discount,$barcode,$depaturedate, $transactiondate);
            $custNameError = ""; 
            $custPhoneError = "";  
            $custNextOfKinError = ""; 
            $seatNoError = ""; 
            $routeError = "";
            $cashierError = "";
            $stationError = "";  
            $custName = "";
            $custPhone = "";
            $custNxtOfKin = "";
            $seatNo = "";
            $luggageFair = "";
            $discountGiven = "";
           // $transactiondate = "";

    }   

}



?>
<?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">Add Parcel Transactions</li>
                    </ol>
                </div>
            </div>

    <!-- Container fluid  -->
 <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row justify-content-center">
                    <div class="col-lg-6">
                        <div class="card">
                             <div class="card-title">
                             <h4>Enter Parcel Transaction Details</h4>
                         </div>
                            <div class="card-body">
                                <div class="form-validation">
                                    <form class="form-valide" action="addparceltransaction.php" method="post">
                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Sender Name <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control"  id="val-username" name="sendername" required value = "<?php echo $custName; ?>" placeholder="Full name">
                                                
                                            </div>
                                            
                                        </div>

                                              <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-phoneus">Sender Phone Number <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" id="val-phoneus" name="sendernumber" required value = "<?php echo $custPhone; ?>" placeholder="(097)-999-0000">
                                            
                                            </div>
                                        </div>


                                              <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-phoneus">Receiver Name <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" id="val-phoneus" name="receivername"  required value = "<?php echo $custNxtOfKin; ?>" placeholder="(097)-999-0000">
                                     
                                            </div>
                                        </div>


                                           <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-phoneus">Receiver Phone Number <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" id="val-phoneus" name="receivernumber"  required value = "<?php echo $custNxtOfKin; ?>" placeholder="(097)-999-0000">
                                     
                                            </div>
                                        </div>



                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-number">Fair</label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" id="val-number" value = "<?php echo $luggageFair; ?>" name="price" placeholder="0.0">
                                            </div>
                                        </div>



                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-number">Discount given</label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" id="val-number" value = "<?php echo $discountGiven; ?>" name="discount" placeholder="0.0">
                                            </div>
                                        </div>



                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-skill">Cashier <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <select class="form-control" id="val-skill" required name="cashier">
                                                    <option value="" selected disabled hidden>Please select a cashier</option>

                                                    <?php foreach ($cashiers as $cashier) {?>
                                                         <option value="<?php echo  $cashier['userid']; ?>"> <?php echo  $cashier['name']; ?></option>
                                                       <?php } ?>
                                                </select>
                                                <span class="text-danger"> <?php echo $cashierError;?></span>
                                            </div>
                                        </div>


                                              <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-skill">Route <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <select class="form-control" required id="val-skill" name="route">
                                                    <option value="" selected disabled hidden>Please select a route</option>

                                                    <?php foreach ($routes as $route) {?>
                                                         <option value="<?php echo  $route['routeid']; ?>"> <?php echo  $route['jorney']; ?></option>
                                                       <?php } ?>
                                                </select>
                                                <span class="text-danger"> <?php echo $routeError;?></span>
                                            </div>
                                        </div>
                                

                                      <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-skill">Station <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <select class="form-control" required id="val-skill" name="station">
                                                    <option value="" selected disabled hidden>Please select a station</option>

                                                    <?php foreach ($stations as $station) {?>
                                                         <option value="<?php echo  $station['stationid']; ?>"> <?php echo  $station['name']; ?></option>
                                                       <?php } ?>
                                                </select>
                                                <span class="text-danger"> <?php echo $stationError;?></span>
                                            </div>
                                        </div>
                                


                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-number">Depature date<span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                             <input type="date" class="form-control"  required placeholder="dd-mm-yyyy" value = "<?php echo $depaturedate; ?>" name="depaturedate">
                                            </div>
                                        </div>     



                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-number">Date purchased<span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                             <input type="date" class="form-control"  required placeholder="dd-mm-yyyy" value = "<?php echo $transactiondate; ?>" name="transactiondate">
                                            </div>
                                        </div>     

                                
                                      
                                        <div class="form-group row">
                                            <div class="col-lg-8 ml-auto">
                                                <button type="submit" class="btn btn-primary" id="toastr-success-top-right">Submit</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
    </div>
      <footer class="footer"> © 2020 All rights reserved. Template designed by <a href="https://brigafrica.com">Meit Systems</a></footer>

     <!-- 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>