<?php
/*
 * Copyright (C) 2018 josh.north@point808.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

    ini_set('session.gc_maxlifetime', 24*60*60);                                // MIN SESSION
    ini_set('session.gc_probability', 1);                                       // GC RATES
    ini_set('session.gc_divisor', 100);                                         // TIMES
    ini_set('session.use_cookies', '1');
    ini_set('session.use_only_cookies', '1');
    ini_set('session.cookie_secure', '0');
    ini_set('session.cookie_httponly', '0');
    ini_set('session.cookie_samesite', 'Lax');
    session_save_path('.tmp');                                                  // TEMP
    session_start();                                                            // START
    require_once __DIR__ . '/autoload.php';                                     // AUTOLOAD
    use App\LobbySIO\Misc\Csrf;                                        // ANTICSRF
    $StaticFunctions = new \App\LobbySIO\Misc\StaticFunctions();                // DEFAULT CLASSES
    $SiteInfo = new \App\LobbySIO\Database\SiteInfo();
    $Users = new \App\LobbySIO\Database\Users();
    if (isset($_SESSION['user_id'])) {                                          // LOGGED IN? GET USER OBJECT
        $session_user = $Users->getUserInfo($_SESSION['user_id'], "1", "0"); }
    if (isset($session_user)) {                                                 // GET UID OR SET TO KIOSK
        $uid = $session_user["0"]["users_id"];} else { $uid = "2"; }
    $app_disp_lang = filter_input(INPUT_COOKIE, 'app_disp_lang');               // SETUP LANGUAGE
    if(!isset($app_disp_lang)) {
        $app_disp_lang=$StaticFunctions->getDefaultLanguage(); }
    $siteidcookie = filter_input(INPUT_COOKIE, 'app_site');                     // SETUP SITE
    foreach($SiteInfo->getSite("0", $uid, "0", "0") as $arr) {
        $lookup_array[$arr['sites_id']]=1; }
        if(isset($lookup_array[$siteidcookie])) {
            $siteid = $siteidcookie; } else { $siteid = "1"; }
        if(!isset($siteid)) { $siteid="1"; }
    $Translate = new \App\LobbySIO\Language\Translate($app_disp_lang);          // SETUP TRANSLATOR
    $transLang =  $Translate->userLanguage();
    $VisitInfo = new \App\LobbySIO\Database\VisitInfo();
    $VisitActions = new \App\LobbySIO\Database\VisitActions();
    $app_current_pagename = $transLang['SIGNOUT'];                              // PAGE SETUP
    $app_current_pageicon = '<i class="fas fa-sign-out-alt"></i> ';
    require_once("inc/header.inc.php");
    if ($StaticFunctions->getSessionStatus() == true) {                         // CHECK STATUS
        header('Location: index.php');                                          // ELSE HOME
    } else {
      header("X-Frame-Options: SAMEORIGIN");
      header("X-Content-Type-Options: nosniff");
      //header("Content-Security-Policy: script-src 'self' 'unsafe-inline'; script-src-elem 'self'; script-src-attr 'self'; style-src 'self'; style-src-elem 'self'; style-src-attr 'self'; img-src 'self'; connect-src 'self'; frame-src 'self'; font-src 'self'; media-src 'self'; object-src 'self'; manifest-src 'self'; worker-src 'self'; prefetch-src 'self'; form-action 'self'; frame-ancestors 'self'; default-src 'self'", false);
      if (!empty($_GET['a'])) {
        echo '<pre>' . print_r($_POST, true) . '</pre>';
        echo 'Verification has been : ' . (Csrf::verifyToken('home') ? 'successful' : 'unsuccessful');
      }
    ?>



<!-- START CONTENT -->
<?php 
  if (!empty($_POST['endvisit'])) {                    // PROCESS POST
  echo $VisitActions->endVisit($_POST['endvisit'], $StaticFunctions->getUTC());
?>
<!-- START SIGNOUT ACK -->
  <div class="container">
    <div class="row row-cols-1">
      <div class="col d-grid gap-2">
        <h2><i class="fas fa-sign-out-alt"></i> <?php echo $transLang['SIGNOUT_THANKYOU']; ?></h2>
      </div>
    </div>
  </div>
<!-- END SIGNOUT ACK -->
<?php } else {                                           // OR SHOW LIST
  $approval = "2";                                     // ONLY SHOW APPROVED
  $page_num = 1;                                       // PAGINATION
  if(!empty($_GET['pnum'])):
    $page_num = filter_input(INPUT_GET, 'pnum', FILTER_VALIDATE_INT);
    if(false === $page_num):
      $page_num = 1;
    endif;
  endif;
  $offset = ($page_num - 1) * $StaticFunctions->getPageRows();
  $row_count = count($VisitInfo->getVisitInfo($siteid, $approval, "empty", "%", "%", "%", "%", "%", "%"));
  $page_count = 0;
  if (0 === $row_count): else: $page_count = (int)ceil($row_count / $StaticFunctions->getPageRows()); if($page_num > $page_count): $page_num = 1; endif; endif;
?>
<!-- START SIGNOUT LIST -->
  <div class="container">
    <div class="row row-cols-2">
      <div class="col d-grid gap-2">
        <h2><i class="fas fa-sign-out-alt"></i> <?php echo $transLang['SIGNOUT']; ?></h2>
      </div>
      <div class="col d-grid gap-2">
        <a href="index.php" class="btn btn-info" tabindex="-1" role="button" aria-disabled="true"><?php echo $transLang['STR_COMMON_BACK']; ?></a>
      </div>
    </div>
    <form class="form-signout" method="post" onsubmit="return confirm('<?php echo $transLang['END_VISIT_WARNING']; ?>')">
      <?php echo Csrf::getInputToken('home') ?>
      <ul class="pagination pagination-sm"><li class="page-item disabled"><a class="page-link" href="#" tabindex="-1"><?php echo $transLang['STR_COMMON_PAGE']; ?></a></li>
<?php for ($i = 1; $i <= $page_count; $i++): ?>
        <li class="page-item<?php if ($i === $page_num): echo ' active'; else: echo ' '; endif; ?>"><a class="page-link" href="<?php echo $_SERVER['PHP_SELF'] . '?pnum=' . $i; ?>"><?php echo $i; ?></a></li>
<?php endfor; ?>
      </ul>
      <table class="table table-striped">
        <thead class="thead-dark">
          <tr><th><?php echo $transLang['BADGE']; ?></th><th><?php echo $transLang['IN']; ?></th><th><?php echo $transLang['NAME']; ?></th><th><?php echo $transLang['ESCORT']; ?></th><th><?php echo $transLang['ACTIONS']; ?></th></tr>
        </thead>
        <tbody>
<?php $approval="2"; foreach ($VisitInfo->getVisitInfo($siteid, $approval, "empty", "%", "%", "%", "%", $StaticFunctions->getPageRows(), $offset) as $row):
  $timein = new DateTime($row['visits_intime'], new DateTimeZone('UTC'));
  $timein->setTimezone(new DateTimeZone("$timezone"));
  $timein_disp = $timein->format('Y-m-d H:i:s');
  $isvendor = $row['visits_isvendor'];
?>
          <tr><td><?php echo $row['visits_badge']; ?></td><td><?php echo $timein_disp; ?></td><td><?php echo $row['visits_lastname'] . ", " . $row['visits_firstname']; ?><br /><img src="<?php echo $row['visits_signature']; ?>" width="200" height="50"></img></td><td><?php if (!empty($row['visits_escort'])) {echo $row['visits_escort'] . '<br /><img src="' . $row['visits_escort_signature'] . '" width="200" height="50"></img>'; } ?></td>
            <td><nobr><?php if($isvendor===0){ ?><button type="submit" name="endvisit" value="<?php echo $row['visits_id']; ?>" class="btn btn-warning btn-lg"><i class="fas fa-sign-out-alt"></i><?php echo $transLang['SIGNOUT']; ?></button>
              <?php } elseif($isvendor===1){
                foreach ($VisitInfo->getInfoVendorVisitFinal($row['visits_id']) as $rowvm) {
                  $db_vendorinfo_workcompleted = $rowvm['db_vendorinfo_workcompleted'];
                  $db_vendorinfo_sitecleanup = $rowvm['db_vendorinfo_sitecleanup'];
                  if ($db_vendorinfo_workcompleted === 1 && $db_vendorinfo_sitecleanup === 1) { $finalvendorformcomplete=1; } else { $finalvendorformcomplete=0; }
                  if($finalvendorformcomplete===1){ ?>
              <button type="submit" name="endvisit" value="<?php echo $row['visits_id']; ?>" class="btn btn-warning btn-lg"><i class="fas fa-sign-out-alt"></i><?php echo $transLang['SIGNOUT']; ?></button>
                  <?php } elseif ($finalvendorformcomplete===0) { ?>
              <span class="d-inline-block" tabindex="0" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-content="<?php echo $transLang['STR_VENDORINFO_UNABLETOSIGNOUT_DESC']; ?>">
              <button class="btn btn-warning btn-lg" disabled><i class="fas fa-sign-out-alt"></i><?php echo $transLang['SIGNOUT']; ?></button>
              </span>
              <?php } } } ?>
        </nobr></td></tr>
<?php endforeach; ?>
        </tbody>
      </table>
    </form>
  </div>
<?php } ?>
<!-- END SIGNOUT LIST -->

<!-- CONTENT END -->
<?php } require_once("inc/footer.inc.php");
