<?php
session_start();
include "classes/db.class.php";
include "classes/fc.class.php";
include "config.php";


if (!isset($_SESSION['uid'])) {
    header("Location: login.php");
    exit;
}
$tabela = "produtos";
$coluna = "DESCRICAO ASC";
$primaria = 'idprod';
if (isset($_SESSION['tabela'])) {
    unset($_SESSION['tabela']);
}
if (isset($_SESSION['coluna'])) {
    unset($_SESSION['coluna']);
}
if (isset($_SESSION['primaria'])) {
    unset($_SESSION['primaria']);
}

$_SESSION['primaria'] = $primaria;
$_SESSION['tabela'] = $tabela;
$_SESSION['coluna'] = $coluna;

$uid = $_SESSION['uid'];
$nome = $_SESSION['un'];
$email = $_SESSION['ue'];
$idsql = $_SESSION['uids'];

$pag = $_SERVER['REQUEST_URI'];
$pag = explode("/", $pag);
$page = $pag[2];





?>
<!DOCTYPE html>

<html lang="pt-br">

<head>

    <?php include "includes/header.php"; ?>

</head>

<body>

    <!-- navbar -->
    <?php include "includes/navbar.php"; ?>


    <div id="corpo" class="container card shadow-lg  my-2 py-1">

        <div class="row align-items-center justify-content-center my-2">

            <div class="col-12 col-md-12 text-start">
                <h4 class="text-dark"><i class='fas fa-boxes me-2'></i>Produtos</h4>


            </div>




        </div>

    </div>

    <div class="container card shadow-lg  mb-3 py-1">

        <div class="row mb-3">

            <div class="col-12 col-md-12 col-sm-12 col-lg-12 col-xl-12 mb-3">

                <table class="table table-striped mb-3   table-hover">

                    <thead class="border-0 border-1 border-bottom border-secondary">
                        <tr>
                            <th class="col-2 text-center"></th>
                            <th>Produto</th>
                            <th>Valor</th>
                            <th class="col-2 text-end"></th>

                        </tr>
                    </thead>
                    <tbody>


                    </tbody>
                </table>

            </div>

        </div>

    </div>



    <!-- footer -->
    <?php include "includes/footer.php"; ?>

    <?php include "includes/libs.php"; ?>



    <!-- Modal -->
    <div class="modal fade" id="mDados" tabindex="-1" aria-labelledby="mDadosLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="mDadosLabel">Dados Produto:</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">

                    <form id="fdados" class="w-100">

                        <div class='col-md-6 col-sm-12 mb-1'>
                            <label for='REGISTRO' class='form-label'>REGISTRO:</label>
                            <input type='text' id='REGISTRO' name='REGISTRO' value="" class='form-control' >
                        </div>
                        <div class='col-md-6 col-sm-12 mb-1'>
                            <label for='CODIGO' class='form-label'>CODIGO:</label>
                            <input type='text' id='CODIGO' name='CODIGO' value="" class='form-control' >
                        </div>
                        <div class='col-md-6 col-sm-12 mb-1'>
                            <label for='REFERENCIA' class='form-label'>REFERENCIA:</label>
                            <input type='text' id='REFERENCIA' name='REFERENCIA' value="" class='form-control' >
                        </div>
                        <div class='col-md-6 col-sm-12 mb-1'>
                            <label for='DESCRICAO' class='form-label'>DESCRICAO:</label>
                            <input type='text' id='DESCRICAO' name='DESCRICAO' value="" class='form-control' >
                        </div>
                        <div class='col-md-6 col-sm-12 mb-1'>
                            <label for='VALORVENDA' class='form-label'>VALOR VENDA:</label>
                            <input type='text' id='VALORVENDA' name='VALORVENDA' value="" class='form-control  money' >
                        </div>
                        <div class='col-md-6 col-sm-12 mb-1'>
                            <label for='OBS' class='form-label'>OBS:</label>
                            <input type='text' id='OBS' value="" name='OBS' class='form-control' >
                        </div>
                        <div class='col-md-6 col-sm-12 mb-1'>
                            <label for='TIPOPROD' class='form-label'>TIPO PROD:</label>
                            <input type='number' value="" id='TIPOPROD' name='TIPOPROD' class='form-control' >
                        </div>
                       
                    </form>


                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-warning  w-100" data-bs-dismiss="modal">Cancelar</button>

                </div>
            </div>
        </div>
    </div>


    <script>
        $(function() {
            // Initialize tooltips
            pulse('Carregando...');

        });
        $(document).ready(function() {
            removeloader();
            var page = '<?php echo $page; ?>';
            if (page == 'clientes.php') {
                $('#btnradio2').prop('checked', true);
                $('#btnradio3').prop('checked', false);
                $('#btnradio4').prop('checked', false);
                $('#btnradio1').prop('checked', false);
            } else if (page == 'servicos.php') {
                $('#btnradio3').prop('checked', true);
                $('#btnradio2').prop('checked', false);
                $('#btnradio4').prop('checked', false);
                $('#btnradio1').prop('checked', false);
            } else if (page == 'produtos.php') {
                $('#btnradio4').prop('checked', true);
                $('#btnradio2').prop('checked', false);
                $('#btnradio3').prop('checked', false);
                $('#btnradio1').prop('checked', false);
            } else if (page == 'index.php') {
                $('#btnradio1').prop('checked', true);
                $('#btnradio2').prop('checked', false);
                $('#btnradio3').prop('checked', false);
                $('#btnradio4').prop('checked', false);
            }

            var lista = $('.table tbody');
            lista.empty();

            $.get('./rest/buscaprodutos.php', function(resposta) {

                if (resposta != 'erro') {

                    lista.html(resposta);
                    $('.table').dataTable({
                        dom: 'Bfrtip',
                        buttons: [
                            'copy',
                            'excel',
                            'pdf',
                            'print'
                        ],
                        language: {
                            url: './langPT.json'
                        }

                    });

                } else {
                    mensagem('Erro ao carregar Registros!', 'erro');
                }


            });





        });

        function removeloader() {



            Notiflix.Loading.remove(1923);
            // $('#corpo').fadeIn(500);
            //
        }


        function dados(id) {

             //alert(id);

            if (id != '' || id != null) {

                //$('#idcliente').val(id);

                $.post('./rest/bsprodutos.php', {
                    id: id
                }, function(e) {
                    console.log(e);
                    
                    var ds = JSON.parse(e);
                    $.each(ds[0], function(key, value) {
                        //console.log(key+' - '+value);
                        $('#' + key).val(value);
                    });

                    $('#mDados').modal('show');
                    //alert($('#idcliente2').val());


                    $('#mDados input').prop('disabled', true); // Disable all input fields
                  

                });

            }

        }
    </script>


</body>

</html>