<?php

require_once('config.php');
authenticate();

if($_POST){
  $license_id = $_POST['id'];

  do{
    $key = generate(3,4);
    $check = $db->query("SELECT * FROM licenses WHERE license_key='$key'")->rowCount();
  } while($check>0);

  $db->exec("INSERT INTO licenses (license_key,user_id) VALUES ('$key', '$user')");
}

?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <?php
    include('menu.php');
    ?>
    <table border="1" width="1000">
      <thead>
        <tr>
          <th>İşletme</th>
          <th>Yönetici</th>
          <th>Lisans Anahtarı</th>
          <th>Lisans SKT</th>
          <th>Kullanıcı</th>
          <th>Telefon</th>
          <th>Yenile</th>
        </tr>
      </thead>
      <tbody>
        <?php
          $licenses = $db->query("SELECT licenses.id AS license_id, licenses.license_key, licenses.expires_at, licenses.name AS name, licenses.model, venues.name AS venue, venues.manager AS manager
            FROM licenses
            LEFT JOIN venues ON venues.id = licenses.venue_id
            WHERE licenses.deleted_at IS NULL
            ORDER BY venue ASC, expires_at ASC")->fetchAll();
          foreach($licenses as $license){
        ?>
        <tr>
          <td><?=$license['venue'];?></td>
          <td><?=$license['manager'];?></td>
          <td><?=$license['license_key'];?></td>
          <td><?=$license['expires_at'];?></td>
          <td><?=$license['name'];?></td>
          <td><?=$license['model'];?></td>
          <td><a href="refresh-license.php?key=<?=$license['license_key'];?>">Yenile</a></td>
        </tr>
        <?php
          }
        ?>
      </tbody>
    </table>
  </body>
</html>
