<?php
$empresasDao = new \Dao\Empresas();
$empresasDao->getEmpresaPorId($conecta, base64_decode(filter_input(INPUT_GET, 'id')));
?>
<div class="breadcrumb-holder">
    <div class="container-fluid">
        <ul class="breadcrumb">
            <li class="breadcrumb-item"><a href="./">Home</a></li>
            <li class="breadcrumb-item"><a href="empresas.php">Empresas</a></li>
            <li class="breadcrumb-item active"><?php echo ($empresasDao->id == 0 ? 'Adicionar Empresa' : 'Editar Empresa'); ?></li>
        </ul><!--/.breadcrumb -->
    </div><!---/.container-fluid -->
</div><!--/.breadcrumb-holder -->
<section class="dashboard-counts section-padding">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-header d-flex align-items-center">
                        <h2 class="h5 display"><?php echo ($empresasDao->id == 0 ? 'Adicionar Empresa' : 'Editar - ' . $empresasDao->nome); ?></h2>
                    </div><!--/.card-header -->
                    <div class="card-body">
                        <form class="form-horizontal" method="post" action="?p=salvar" enctype="multipart/form-data">
                            <input type="hidden" name="id" id="id" value="<?php echo $empresasDao->id; ?>">
                            <div class="form-group row">
                                <div class="col-md-2">
                                    <label class="form-label">Tipo</label>
                                    <select class="form-control" id="tipo" name="tipo" required>
                                        <option value="F" <?php echo ($empresasDao->tipo == 'F' ? 'selected' : ''); ?>>Física</option>
                                        <option value="J" <?php echo ($empresasDao->tipo == 'J' ? 'selected' : ''); ?>>Jurídica</option>
                                    </select>
                                </div><!--/.col-md-2 -->
                                <div class="col-md-3">
                                    <label class="form-label">CPF/CNPJ</label>
                                    <input type="text" name="cpf_cnpj" id="cpf_cnpj" placeholder="CPF/CNPJ" value="<?php echo $empresasDao->cpf_cnpj; ?>" maxlength="18" class="form-control" required autofocus>
                                </div><!--/.col-md-3 -->
                                <div class="col-md-7">
                                    <label class="form-label">Nome</label>
                                    <input type="text" name="nome" id="nome" placeholder="Nome" value="<?php echo $empresasDao->nome; ?>" maxlength="70" class="form-control" required>
                                </div><!--/.col-md-7 -->
                            </div><!--/.form-group -->
                            <div class="form-group row">
                                <div class="col-md-3">
                                    <label class="form-label">RG/Insc. Est.</label>
                                    <input type="text" id="rg_ie" name="rg_ie" placeholder="RG/Insc. Est." value="<?php echo $empresasDao->rg_ie; ?>" class="form-control" maxlength="15">
                                </div><!--/.col-md-3 -->
                                <div class="col-md-3">
                                    <label class="form-label">Insc. Mun.</label>
                                    <input type="text" id="im" name="im" placeholder="Insc. Mun." value="<?php echo $empresasDao->im; ?>" class="form-control" maxlength="20">
                                </div><!--/.col-md-3 -->
                                <div class="col-md-3">
                                    <label class="form-label">Telefone</label>
                                    <input type="tel" name="telefone" id="telefone" class="form-control" value="<?php echo $empresasDao->telefone; ?>" placeholder="Telefone" maxlength="20">
                                </div><!--/.col-md-3 -->
                            </div><!--/.form-group -->
                            <div class="form-group row">
                                <div class="col-md-4">
                                    <label class="form-label">Endereço</label>
                                    <input type="text" name="endereco" id="endereco" class="form-control" value="<?php echo $empresasDao->endereco; ?>" placeholder="Endereço" maxlength="60">
                                </div><!--/.col-md-4 -->
                                <div class="col-md-3">
                                    <label class="form-label">Bairro</label>
                                    <input type="text" name="bairro" id="bairro" class="form-control" value="<?php echo $empresasDao->bairro; ?>" placeholder="Bairro" maxlength="30">
                                </div><!--/.col-md-3 -->
                                <div class="col-md-2">
                                    <label class="form-label">Nº</label>
                                    <input type="text" name="numero" id="numero" class="form-control" value="<?php echo $empresasDao->numero; ?>" placeholder="Nº" maxlength="7">
                                </div><!--/.col-md-2 -->
                                <div class="col-md-3">
                                    <label class="form-label">Cidade</label>
                                    <div class="input-group">
                                        <select name="cidade" id="cidade" class="form-control" data-name="cidades[]" required>
                                            <option value="0"></option>
                                            <?php
                                            $cidadesDao = new \Dao\Cidades();
                                            $lst_cidades = $cidadesDao->getLista($conecta, 'ORDER BY nome ASC');
                                            foreach ($lst_cidades as $cidade) {
                                                $selected = ($cidade->id == $empresasDao->fk_id_cidade ? 'selected' : '');
                                                echo '<option value="' . $cidade->id . '" ' . $selected . '>' . $cidade->nome . '</option>';
                                            }
                                            ?>
                                        </select>
                                        <span class="input-group-addon btn-info" data-toggle="modal" data-target="#modal_cidades">
                                            <i class="fa fa-plus"></i>
                                        </span>
                                    </div><!--/.input-group -->
                                </div><!--/.col-md-3 -->
                            </div><!--/.form-group -->
                            <div class="form-group row">
                                <div class="col-md-12 text-right">
                                    <button class="btn btn-sm btn-primary">
                                        <i class="fa fa-database"></i> Salvar
                                    </button>
                                </div>
                            </div><!--/.form-group -->
                        </form>
                    </div><!--/.card-body -->
                </div><!--/.card -->
            </div><!--/.col-lg-12 -->
        </div><!--/.row -->
    </div><!--/.container-fluid -->
</section><!--/.dashboard-counts -->