<?php include('header.php'); ?>

<body class="hold-transition layout-top-nav">
  <div class="wrapper">
    <div class="preloader flex-column justify-content-center align-items-center">
    </div>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper" style="display: flex; align-items: center;">
      <!-- Content Header (Page header) -->
      <div class="content-header">
        <div class="container-fluid">
        </div><!-- /.container-fluid -->
      </div>
      <!-- /.content-header -->

      <section class="content">
        <div class="container-fluid">
          <div class="row" style="justify-content: center;">
            <!-- SELECT2 EXAMPLE -->
            <div class="col-md-6">
              <div class="card card-default">
                <div class="card-header">
                  <h3 class="card-title">Sistema</h3>

                  <div class="card-tools">
                    <button type="button" class="btn btn-tool" data-card-widget="collapse">
                      <i class="fas fa-minus"></i>
                    </button>
                    <button type="button" class="btn btn-tool" data-card-widget="remove">
                      <i class="fas fa-times"></i>
                    </button>
                  </div>
                </div>
                <div class="card-body">
                  <div class="login-logo logo" style="padding-top: 20px;">
                    <img style="width: 40%;" src="images/logo/logo.svg" alt="Imagem Buesimples" class="brand-image">
                  </div>
                  <div class="card-body login-card-body">
                    <form method="post">
                      <div class="row">
                        <div class="col-md-4">
                          <div class="form-group" style="margin-bottom: 0;">
                            <small class="not-required">Servidor</small>
                            <input type="text" class="form-control" id="servidor" placeholder="Nome do Servidor" title="Nome do Servidor">
                            <small>Mensagem de erro</small>
                          </div>
                          <div class="form-group" style="margin-bottom: 0;">
                            <small class="not-required">Porta</small>
                            <input type="text" class="form-control" id="porta" placeholder="Número da Porta" title="Número da Porta">
                            <small>Mensagem de erro</small>
                          </div>
                        </div>
                        <div class="col-md-4">
                          <div class="form-group" style="margin-bottom: 0;">
                            <small class="not-required">Usuário</small>
                            <input type="text" class="form-control" id="usuario" placeholder="Nome do Usuário" title="Nome do Usuário">
                            <small>Mensagem de erro</small>
                          </div>
                          <div class="form-group" style="margin-bottom: 0;">
                            <small class="not-required">Palavra-Passe</small>
                            <input type="password" class="form-control" id="password" placeholder="Palavra-Passe" title="Palavra-Passe">
                            <small>Mensagem de erro</small>
                          </div>
                        </div>
                        <div class="col-md-4">
                          <div class="form-group" style="margin-bottom: 0;">
                            <small class="not-required">Base de Dados</small>
                            <input type="text" class="form-control" id="database" placeholder="Nome da Base de Dados" title="Nome da Base de Dados">
                            <small>Mensagem de erro</small>
                          </div>
                          <div class="form-group" style="margin-bottom: 0;">
                            <small class="not-required">Terminal</small>
                            <input type="text" class="form-control" id="terminal" placeholder="Terminal" title="Terminal">
                            <small>Mensagem de erro</small>
                          </div>
                        </div>
                      </div>
                      <div class="row">
                        <div class="col-md-6">
                          <button type="submit" id="btn-test" class="btn btn-block btn-primary" style="background-color: #560085; border-color: #560085;">Testar</button>
                        </div>
                        <div class="col-md-6">
                          <button type="submit" id="btn-save" class="btn btn-block btn-primary" style="background-color: #560085; border-color: #560085;">Guardar</button>
                        </div>
                      </div>
                    </form>
                  </div>
                </div>
                <!-- /.card-body -->
                <div class="card-footer">
                  Configurações do sistema.
                </div>
              </div>
            </div>
          </div>
          <!-- /.card -->
        </div>
        <!-- /.container-fluid -->
      </section>
    </div>
    <!-- /.content-wrapper -->
    <?php include('footer.php'); ?>
</body>

</html>
<script>
  $(document).ready(function() {
    reload();
    $.ajax({
      url: "includes/read/serie.inc.php",
      type: "post",
      dataType: "json",
      success: function(data) {
        let select = "";
        for (var key in data) {
          select += "<option value=" + data[key]['idSerie'] + ">" + data[key]['descricao'] + "</option>";
        }
        console.log(select);
        $("#serie").html(select);
      }
    });
  });

  function reload() {
    $.ajax({
      url: "includes/read/definicao.inc.php",
      type: "post",
      dataType: "json",
      success: function(data) {
        let select = "";
        for (var key in data) {
          if (data[key]["idDefinicao"] == 1) {
            qtdLinhaPreco.value = data[key]["valor"];
            for (let i = 0; i < data[key]["valor"]; i++) {
              select += `<option value="${i + 1}">P.Venda (${i + 1})</option>`;
            }
          }
        }
        $("#linhaPrecoPredefinida").html(select);
      }
    }).done(function(data) {
      for (var key in data) {
        if (data[key]["idDefinicao"] == 2) {
          $('#linhaPrecoPredefinida').val(data[key]['valor']);
          $('#linhaPrecoPredefinida').select2().trigger('change');
        }
      }
    });
  }
  $(document).on("click", "#btn-submit", function(e) {
    e.preventDefault();
    $.ajax({
      url: "includes/create/definicao.inc.php",
      type: "post",
      data: {
        serie: serie.value,
        qtdLinhaPreco: qtdLinhaPreco.value,
        linhaPrecoPredefinida: linhaPrecoPredefinida.value
      },
      success: function(data) {
        reload();
        console.log(data);
        swal.fire({
          title: "Alterações gravadas com sucesso!",
          text: "Sistema gravadas!",
          confirmButtonText: 'Confirmar',
          confirmButtonColor: '#560085',
          icon: "success",
        });
      },
      error: function(data) {
        reload();
        swal.fire({
          title: "Erro ao alterar Sistema!",
          text: "Erro ao alterar Sistema.",
          confirmButtonText: 'Confirmar',
          confirmButtonColor: '#560085',
          icon: "error",
        });
      }
    });
  });
</script>
<script src="./validation/definicao.js"></script>