<?php
require '../../conexao/conexao.php';
//require '../sessao.php';
require '../../conexao/acesso.php';
require '../../configuacoes.php';

require '../../querys/query-filiais.php';

require '../../querys/query-cidades.php';

$cidades = new Cidades($connectionmysql);
$listaCidades = $cidades->listarCidades();

$Filiais = new Filiais($connectionmysql);

if (isset($_GET['id'])) {
    $id_tipo_meta = (int)$_GET['id'];

    // Verifica se o formulário foi enviado
    $Filiais->updateFilial();
}


$DadosFiliais = $Filiais->listarFiliais($_GET['id'])[0];

//print_r($DadosFiliais);

if (isset($_SESSION['message'])) {
    echo "<script>alert('" . $_SESSION['message'] . "');</script>";
    unset($_SESSION['message']); // Limpa a mensagem após exibição
}

?>

<!DOCTYPE html>
<html lang="pt-br">
<?php
$pageTitle = "Altera Meta"; // Definir o título da página aqui
include('../header.php'); ?>

<!-- Navigation Bar-->
<?php
include('../menu.php');
?>
<!-- End Navigation Bar-->

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.min.css">

<head>
    <link rel="icon" href="/mandareceita/pages/configuracoes/logo-sm.png">
    <link rel="shortcut icon" href="/mandareceita/pages/configuracoes/logo-sm.png">

</head>

<body>
    <style>
        .chosen-container {
            width: 100% !important;
        }

        .chosen-container .chosen-single {
            height: calc(1.5em + 0.75rem + 2px);
            line-height: 1.5;
            padding: 0.375rem 0.75rem;
            font-size: 1rem;
            color: #495057;
            background-color: #fff;
            background-image: none;
            border: 1px solid #ced4da;
            border-radius: 0.25rem;
            box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.075);
            transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
        }

        .chosen-container .chosen-single div b {
            background: none;
            display: inline-block;
            margin-top: 0.25em;
            vertical-align: middle;
        }

        .chosen-container .chosen-single:hover,
        .chosen-container .chosen-single:focus {
            border-color: #80bdff;
            outline: 0;
            box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
        }

        .chosen-container .chosen-drop {
            margin-top: 0.25rem;
            border-radius: 0.25rem;
            border: 1px solid #ced4da;
            box-shadow: 0 4px 5px rgba(0, 0, 0, 0.15);
            background-color: #fff;
        }

        .chosen-container .chosen-search input[type="text"] {
            font-size: 1rem;
            height: calc(1.5em + 0.75rem + 2px);
            padding: 0.375rem 0.75rem;
            border: 1px solid #ced4da;
            border-radius: 0.25rem;
            box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.075);
            width: 100%;
        }

        .chosen-container .chosen-results {
            max-height: 300px;
            overflow-y: auto;
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .chosen-container .chosen-results li {
            padding: 0.375rem 0.75rem;
            font-size: 1rem;
            color: #495057;
        }

        .chosen-container .chosen-results li.highlighted {
            background-color: #007bff;
            color: #fff;
        }
    </style>

    <!-- ============================================================== -->
    <!-- Start Page Content here -->
    <!-- ============================================================== -->
    <div class="wrapper candidato">
        <div class="container-fluid">

            <div class="card-box mt-3">
                <div class="row">

                    <div class="col-6 text-left">
                        <h4 class="page-title">Alterar filial</h4>
                    </div>

                    <div class="col-6 text-right">
                        <button onclick="history.back()" class="btn btn-light"><i class="mdi mdi-keyboard-backspace"></i> Voltar</button>
                    </div>

                    <div class="col-12">
                        <hr>
                    </div>

                    <div class="col-12">
                        <form method="post">
                            <div class="form-row mb-3">


                                <table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap">
                                    <thead>
                                        <tr class="text-center">
                                            <th width="97">Nome Filial</th>
                                            <th width="97">Código Prime</th>
                                            <th width="89" nowrap="nowrap">Cidade </th>
                                            <th width="96" nowrap="nowrap">Situação</th>
                                        </tr>
                                        <tr class="text-center">
                                            <th>
                                                <input type="text" name="nome_filial" value="<?php echo $DadosFiliais['nome_filial'] ?? ''; ?>" id="nome_filial" class="form-control">
                                            </th>
                                            <th>
                                                <input type="text" name="codigo_fc" value="<?php echo $DadosFiliais['codigo_fc'] ?? ''; ?>" id="codigo_fc" class="form-control">
                                            </th>
                                            <th>
                                                <select name="id_cidade" class="form-control form-control-chosen-required" id="id_cidade">
                                                    <option value="">Selecione</option>
                                                    <?php foreach ($listaCidades as $item) { ?>
                                                        <option value="<?php echo $item['id_cidade']; ?>" <?php if (isset($DadosFiliais) and $DadosFiliais['id_cidade'] == $item['id_cidade']) {
                                                                                                                echo 'selected';
                                                                                                            } ?>><?php echo $item['nome_cidade'] . ' - ' .  $item['uf']; ?></option>
                                                    <?php }; ?>

                                                </select>
                                            </th>
                                            <th>
                                                <select name="situacao" class="form-control" id="situacao">
                                                    <!-- <option value=""></option> -->
                                                    <option value="S" <?php if (isset($DadosFiliais) and $DadosFiliais['situacao'] == 'S') {
                                                                            echo 'selected';
                                                                        } ?>>Ativo</option>
                                                    <option value="N" <?php if (isset($DadosFiliais) and $DadosFiliais['situacao'] == 'N') {
                                                                            echo 'selected';
                                                                        } ?>>Desativado</option>

                                                </select>
                                            </th>
                                        </tr>
                                    </thead>
                                </table>

                                <div class="col-12 text-right">
                                    <!--  <a href="index.php" class="btn btn-warning mr-2"> Limpar</a> -->
                                    <input type="hidden" name="id_filial" id="id_filial" value="<?php echo $_GET['id']; ?>">
                                    <button type="submit" class="btn btn-primary"><i class="mdi mdi-plus"></i> Alterar Dados</button>
                                </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div> <!-- end container -->

    <!-- end wrapper -->

    <!-- ============================================================== -->
    <!-- End Page content -->
    <!-- ============================================================== -->

    <?php include('../footer.php'); ?>
    <?php include('../mensagens_modais.php'); ?>


    <script>
        // Selecionar todos os checkboxes quando o checkbox "Selecionar Todos" for marcado
        document.getElementById('select-all').addEventListener('change', function() {
            const checked = this.checked;
            document.querySelectorAll('.month').forEach(checkbox => {
                checkbox.checked = checked;
            });
        });

        // Desmarcar o checkbox "Selecionar Todos" se algum checkbox individual for desmarcado
        document.querySelectorAll('.month').forEach(checkbox => {
            checkbox.addEventListener('change', function() {
                if (!this.checked) {
                    document.getElementById('select-all').checked = false;
                }
            });
        });
    </script>

    <!-- jQuery (precisa para o Chosen) -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <!-- Chosen JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js"></script>
    <!-- Inicializar Chosen -->
    <script>
        var $j = jQuery.noConflict();
        $j(document).ready(function() {
            $j('#id_cidade').chosen();
        });
    </script>


    <!-- Vendor js -->
    <script src="../../../assets/js/vendor.min.js"></script>

    <!-- knob plugin -->
    <script src="../../../assets/libs/jquery-knob/jquery.knob.min.js"></script>
    <!-- App js-->
    <script src="../../../assets/js/app.min.js"></script>

    <script src="../../geral/javascript.js"></script>


</body>

</html>