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

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

$pCozinha = (isset($_COOKIE['pCozinha'])) ? $_COOKIE['pCozinha'] : '-1';
$pBalcao  = (isset($_COOKIE['pBalcao'])) ? $_COOKIE['pBalcao'] : '-1';
$pBar     = (isset($_COOKIE['pBar'])) ? $_COOKIE['pBar'] : '-1';
$pCaixa   = (isset($_COOKIE['pCaixa'])) ? $_COOKIE['pCaixa'] : '0';
$pPraca   = (isset($_COOKIE['pPraca'])) ? $_COOKIE['pPraca'] : '0';
$valorDeliveryComanda  = (isset($_COOKIE['valorDeliveryComanda'])) ? $_COOKIE['valorDeliveryComanda'] : '0';
?>
<!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 10px;
      border-bottom: 1px solid #e0e3eb;
    }

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

    .config-section {
      padding: 16px 20px;
      display: flex;
      flex-direction: column;
      gap: 18px;
    }

    .config-row {
      display: flex;
      flex-direction: column;
      gap: 4px;
    }

    .config-row label.config-label {
      font-size: 12px;
      font-weight: 700;
      color: #666;
      text-transform: uppercase;
      letter-spacing: 0.5px;
      display: block !important;
      width: auto !important;
    }

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

    .config-row select:focus {
      border-color: #3f51b5;
    }

    .config-nota {
      font-size: 12px;
      color: #999;
      margin-top: 2px;
    }

    .config-divider {
      border: none;
      border-top: 1px solid #e0e3eb;
      margin: 4px 0;
    }

    .config-check-row {
      display: flex;
      align-items: center;
      gap: 12px;
      padding: 12px 14px;
      background: #fff;
      border: 1.5px solid #e0e3eb;
      border-radius: 10px;
      cursor: pointer;
    }

    .config-check-row input[type=checkbox] {
      transform: scale(1.4);
      accent-color: #3f51b5;
    }

    .config-check-row span {
      font-size: 14px;
      font-weight: 600;
      color: #333;
    }

    /* ── Dark Theme Overrides ── */
    /* Handled in base.css via .card-config, .config-* selectors */
  </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">
      <div class="card-config mdl-card">
        <div class="mdl-card__title">
          <h3 class="mdl-card__title-text">CONFIGURAÇÕES</h3>
        </div>

        <form action="">
          <div class="config-section">

            <label class="config-check-row" for="valorDeliveryComanda">
              <input type="checkbox"
                class="ckb"
                id="valorDeliveryComanda"
                name="valorDeliveryComanda"
                value="1"
                <?= ($valorDeliveryComanda == 1) ? 'checked' : '' ?>>
              <span>Usar valor do DELIVERY nos Pedidos</span>
            </label>

            <hr class="config-divider">

            <div class="config-row">
              <label class="config-label">Praça de Atendimento</label>
              <select id="pPraca">
                <?php if ($pPraca != 0) : ?>
                  <option selected value='<?= $pPraca ?>'>PRAÇA <?= $pPraca ?></option>
                <?php endif; ?>
                <option value='0'>NENHUMA</option>
                <?php for ($i = 1; $i <= 10; $i++) : ?>
                  <option value='<?= $i ?>'>PRAÇA <?= $i ?></option>
                <?php endfor; ?>
              </select>
            </div>

            <hr class="config-divider">

            <div class="config-row">
              <label class="config-label">Impressora — Pré-Fechamento</label>
              <select id="pCaixa">
                <?php foreach ($impressoras as $impressora): ?>
                  <option value='<?= $impressora->codigo ?>' <?= ($pCaixa == $impressora->codigo) ? 'selected' : '' ?>><?= $impressora->nome ?></option>
                <?php endforeach; ?>
              </select>
              <span class="config-nota">No Servidor habilite o gerenciamento MOBILE em: Configurações › Vendas › Mobile.</span>
            </div>

            <hr class="config-divider">

            <div class="config-row">
              <label class="config-label">Impressora — Segundo Bar</label>
              <select id="pBar">
                <option value='-1' <?= ($pBar == -1) ? 'selected' : '' ?>>NENHUMA</option>
                <?php foreach ($impressoras as $impressora): ?>
                  <option value='<?= $impressora->codigo ?>' <?= ($pBar == $impressora->codigo) ? 'selected' : '' ?>><?= $impressora->nome ?></option>
                <?php endforeach; ?>
              </select>
              <span class="config-nota">Só alterar se houver 2 bares. A impressora principal do BAR deve ter código "3".</span>
            </div>

            <div class="config-row">
              <label class="config-label">Impressora — Segunda Cozinha</label>
              <select id="pCozinha">
                <option value='-1' <?= ($pCozinha == -1) ? 'selected' : '' ?>>NENHUMA</option>
                <?php foreach ($impressoras as $impressora): ?>
                  <option value='<?= $impressora->codigo ?>' <?= ($pCozinha == $impressora->codigo) ? 'selected' : '' ?>><?= $impressora->nome ?></option>
                <?php endforeach; ?>
              </select>
              <span class="config-nota">Só alterar se houver 2 cozinhas. A impressora principal da COZINHA deve ter código "2".</span>
            </div>

            <div class="config-row">
              <label class="config-label">Impressora — Segundo Balcão</label>
              <select id="pBalcao">
                <option value='-1' <?= ($pBalcao == -1) ? 'selected' : '' ?>>NENHUMA</option>
                <?php foreach ($impressoras as $impressora): ?>
                  <option value='<?= $impressora->codigo ?>' <?= ($pBalcao == $impressora->codigo) ? 'selected' : '' ?>><?= $impressora->nome ?></option>
                <?php endforeach; ?>
              </select>
              <span class="config-nota">Só alterar se houver 2 balcões. A impressora principal do BALCÃO deve ter código "1".</span>
            </div>

          </div>
        </form>

        <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-success btn-gravar">
            <i class="material-icons">done</i> GRAVAR
          </button>
        </div>
      </div>
    </main>
  </div>
  <?php include 'includes/inc_js.php'; ?>
  <script>
    $(document).ready(function($) {

      $('.btn-gravar').click(function(event) {
        var pCaixa = $('#pCaixa').val()
        var pPraca = $('#pPraca').val()
        var pBar = $('#pBar').val()
        var pBalcao = $('#pBalcao').val()
        var pCozinha = $('#pCozinha').val()
        var valorDeliveryComanda = 0

        if ($('#valorDeliveryComanda').prop('checked')) {
          valorDeliveryComanda = 1
        }

        swal({
          title: 'Gravar Configurações?',
          type: 'question',
          showCancelButton: true,
          confirmButtonColor: '#3085d6',
          cancelButtonColor: '#d33',
          confirmButtonText: 'Sim, Gravar!',
          cancelButtonText: 'Não Gravar!',
          confirmButtonClass: 'btn btn-success',
          cancelButtonClass: 'btn btn-danger',
          buttonsStyling: false,
          reverseButtons: true
        }).then((result) => {
          if (result.value) {
            $.ajax({
              url: 'ajax_gravar_config.php',
              type: 'POST',
              dataType: 'JSON',
              data: {
                pCaixa: pCaixa,
                pPraca: pPraca,
                valorDeliveryComanda: valorDeliveryComanda,
                pBar: pBar,
                pBalcao: pBalcao,
                pCozinha: pCozinha,
              },
              beforeSend: function() {
                ProcessingModal.show({});
              },
              success: function(retorno) {
                ProcessingModal.hide();
                if (retorno.status == 1) {
                  swal(
                    'Aviso',
                    'Gravado com sucesso!',
                    'success'
                  )
                }
              }
            });
          }
        })
      });

    });
  </script>
</body>

</html>