<?php
session_start();
require 'conexao.php';
require 'config.php';
require 'includes/inc_verifica_caixa.php';

$sql = "SELECT * FROM impressoras WHERE (computador = '" . SERVIDOR . "' OR computador IS NULL) ORDER BY codigo";
$stm = $pdo->prepare($sql);
$stm->execute();
$impressoras = $stm->fetchAll(PDO::FETCH_OBJ);

?>
<!DOCTYPE html>
<html>

<head>
  <title>Comanda Eletrônica</title>
  <?php require 'includes/inc_head.php'; ?>
  <style>
    .card-config {
      background: #f5f6fa;
      border: 1px solid #e0e3eb;
      border-radius: 12px !important;
      margin: 12px;
      width: calc(100% - 24px);
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04) !important;
    }

    .card-config .mdl-card__title {
      padding: 16px 20px 8px;
      border-bottom: 1px solid #e0e3eb;
    }

    .card-config .mdl-card__title-text {
      font-size: 15px;
      font-weight: 700;
      color: #3f51b5;
    }

    .card-config-body {
      padding: 16px 20px;
    }

    .card-config-body textarea {
      width: 100%;
      border: 1.5px solid #e0e3eb;
      border-radius: 10px;
      padding: 12px;
      font-size: 14px;
      resize: vertical;
      background: #fff;
      box-sizing: border-box;
      outline: none;
      transition: border-color 0.2s;
    }

    .card-config-body textarea:focus {
      border-color: #3f51b5;
    }

    .impressoras-list {
      margin-top: 14px;
      display: flex;
      flex-direction: column;
      gap: 6px;
    }

    .impressoras-list label {
      display: flex;
      align-items: center;
      gap: 10px;
      padding: 10px 14px;
      background: #fff;
      border: 1.5px solid #e0e3eb;
      border-radius: 10px;
      font-size: 14px;
      font-weight: 500;
      cursor: pointer;
    }

    .impressoras-list label input {
      transform: scale(1.3);
      accent-color: #3f51b5;
    }
  </style>
</head>

<body>
  <div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
    <header class="mdl-layout__header">
      <div class="mdl-layout__header-row login">
        <span class="mdl-layout-title"><?= $_COOKIE['usuario'] ?></span>
      </div>
    </header>

    <?php include 'includes/inc_menu.php'; ?>

    <main class="mdl-layout__content">
      <form id="formulario" action="">
        <div class="card-config mdl-card">
          <div class="mdl-card__title">
            <h3 class="mdl-card__title-text">ENVIAR IMPRESSÃO</h3>
          </div>
          <div class="card-config-body">
            <textarea rows="8" id="texto" name="texto" placeholder="Digite o texto a imprimir..."></textarea>
            <div class="impressoras-list">
              <?php foreach ($impressoras as $impressora) : ?>
                <label for="ckb-<?= $impressora->id ?>">
                  <input type="checkbox" class="ckb" id="ckb-<?= $impressora->id ?>" name="impressoras[]" value="<?= $impressora->codigo ?>">
                  <i class="material-icons" style="color:#9e9e9e;font-size:20px;">print</i>
                  <?= $impressora->nome ?>
                </label>
              <?php endforeach; ?>
            </div>
          </div>
        </div>
        <div class="box-altura"></div>
        <div class="footer-fixo">
          <button type="button" class="btn-footer btn-footer-danger" onclick="javascript:window.history.go(-1)">
            <i class="material-icons">arrow_back</i> VOLTAR
          </button>
          <button type="button" class="btn-footer btn-footer-primary btn-impressao">
            <i class="material-icons">print</i> IMPRIMIR
          </button>
        </div>
      </form>
    </main>
  </div>

  <?php include 'includes/inc_js.php'; ?>
  <script>
    $(document).ready(function($) {

      $('.btn-impressao').click(function(event) {
        var texto = $('#texto').val();

        if (texto != "") {
          swal({
            title: 'Deseja enviar Impressão?',
            type: 'question',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Sim, Enviar!',
            cancelButtonText: 'Não Enviar!',
            confirmButtonClass: 'btn btn-success',
            cancelButtonClass: 'btn btn-danger',
            buttonsStyling: false,
            reverseButtons: true
          }).then((result) => {
            if (result.value) {
              $.ajax({
                url: 'ajax_envia_texto.php',
                type: 'POST',
                dataType: 'JSON',
                data: $('#formulario').serialize(),
                beforeSend: function() {
                  ProcessingModal.show({});
                },
                success: function(retorno) {
                  ProcessingModal.hide();
                  if (retorno.status == 1) {
                    Snackbar.show({
                      text: 'Texto enviado com sucesso!',
                      actionText: 'Fechar',
                      duration: 2000,
                      pos: 'bottom-left'
                    });
                  } else {
                    swal(
                      'Alerta',
                      retorno.text,
                      'warning'
                    )
                  }
                }
              });
            }
          })
        } else {
          swal(
            'Alerta',
            'Digite um Texto e Selecione uma Impressora!',
            'warning'
          )
        }
      });

    });
  </script>
</body>

</html>