<?php

require_once 'inc/bootstrap.php';
$scanner = new scanner();
$ticket = new ticket();
$returnCodes = array(0 => 'Invalid Ticket ID', 1 => 'Duplicate Scan', 2 => 'Missing username', 3 => 'Success (cleared to enter)');
$col = TICKET_COL;
$user = filter_input(INPUT_GET, 'user', FILTER_SANITIZE_SPECIAL_CHARS);
$barcode = $ticket->sanitizeBarcode(filter_input(INPUT_POST, 'ticket', FILTER_SANITIZE_SPECIAL_CHARS));
if (!$user) {
    $return = json_encode(array('message' => "No username specified", 'code' => 2));
    $scanner->logEvent("NU", "Tried to scan without a username");
    return;
}
if ($barcode) {
    $return = $ticket->scanTicket($barcode, $user);
} else {
    $return = json_encode(array('message' => "Barcode cannot be empty", 'code' => 2));
}
if (isset($_GET['barcode']) && is_admin()) {
    $return = $ticket->scanTicket($_GET['barcode'], $_GET['user']);
}
if (isset($_GET['format'])) {
    require_once 'header.php';
    ?>
    <div class="jumbotron">
      <h1>Ready to scan</h1>
      <input id="username" name="username" placeholder="Who are you" />
      <input id="ticket" name="ticket" placeholder="Barcode" />
    </div>
    <div class="panel panel-default hide" id="ticketInfo">
 public function importTickets($tickets)
 {
     $db = new database();
     $scanner = new scanner();
     //Split our bundle of tickets into separate arrays
     $tickets = explode("\n", $tickets);
     //Prep the query
     $db->query("INSERT INTO tbl_ticket (firstname, barcode, scanned) VALUES (?,?, 0)");
     $i = 0;
     //Number of attempted imports
     $f = 0;
     //Number of failures (invalid barcodes)
     foreach ($tickets as $ticket) {
         $i++;
         //Increse attempt count
         $ticket = explode(',', $ticket);
         //kaboom
         $barcode = $this->sanitizeBarcode($ticket[1]);
         //Check if the barcode fits whatever pattern we're using.
         $invalidBarcodes = '';
         //Empty string of invalid barcodes
         if (!$barcode) {
             $invalidBarcodes .= $ticket[1] . ', ';
             $f++;
             //Failed import increase
             $i--;
             //Decrease attempt count
         } else {
             $db->bind(1, $ticket[0]);
             $db->bind(2, $barcode);
             try {
                 $db->execute();
             } catch (Exception $e) {
                 $f++;
                 //Same thing here. This catches duplicate barcodes
                 $i--;
             }
         }
     }
     $scanner->logEvent("IT", "Imported {$i} tickets");
     return json_encode(array('message' => "Imported {$i} tickets. {$f} tickets were invalid or duplicates and ignored.", 'code' => 3));
 }