<?php

  $server = "localhost";
  $user = "area_root";
  $pass = "Legiao123*";
  $db = "area_poisson";
  $connect = mysqli_connect($server,$user,$pass,$db);

  $mysqli = new mysqli($server, $user, $pass, $db);

  $mysqli->set_charset("utf8");

  if(mysqli_connect_errno()){
      die("Conexão falhou: ".mysqli_connect_errno());
  }

  $usr = $_SESSION['username'];

  $sql = "SELECT fullname, tel1, username, email, mini, address FROM members WHERE username = '$usr'";

  $result = mysqli_query($connect,$sql);

  $data = mysqli_fetch_assoc($result);
  
  $explodedAddress = explode(', ', $data['address']);

  if( $_SERVER['REQUEST_METHOD'] == 'POST') {
    if(isset($_POST['cpf'])){
      $mini = $_POST["mini"];
      $nome = $_POST["nome"];
      $email = $_POST["email"];
      $cpf = $_POST["cpf"];
      $tel = $_POST["tel"];
      
            /* Address Concat */
      $end  = $_POST["street"];
      $end .= ', '.$_POST["num"];
      $end .= ', '.$_POST["comp"];
      $end .= ', '.$_POST["bairro"];
      $end .= ', '.$_POST["city"];
      $end .= ', '.$_POST["uf"];
      $end .= ', '.$_POST["cep"];
      
      $addMiniQuery = "UPDATE `members` SET mini='$mini', fullname='$nome', username='$cpf', tel1='$tel', email='$email', address='$end' WHERE username = '$usr'";
      $miniResult = mysqli_query($connect,$addMiniQuery);
    }
  }

  /* echo "Seu CPF: ".$data['username'];
  echo '<br/>';
  echo "Seu E-MAIL: ".$data['email'];
  echo '<br/>'; */
  

?>
<div>
  <br/>
  <br/>
  <form action="" method="post">
    
    <label for="nome">Nome completo:</label><br/>
    <input type="text" name="nome" id="nome" value="<?php echo $data['fullname'];?>" class="form-control"><br/><br/>
    
    <label for="email">E-mail:</label><br/>
    <input type="text" name="email" id="email" value="<?php echo $data['email'];?>" class="form-control"><br/><br/>
    
    <label for="cpf">CPF:</label><br/>
    <input type="text" name="cpf" id="cpf" value="<?php echo $data['username'];?>" class="form-control"><br/><br/>
    
    <label for="tel">Telefone:</label><br/>
    <input type="text" name="tel" id="tel" value="<?php echo $data['tel1'];?>" class="form-control"><br/><br/>

    <!-- Address Form -->
    <p>Endereço:</p>
    <div id="adressForm" style='display: flex; flex-direction: row; flex-wrap: wrap;'>
      <input type="text" style="width: 80%;"  name="street" id="street" placeholder="Nome da Rua ou Avenida" class="form-control" value="<?php echo $explodedAddress[0];?>">
      <input type="text" style="width: 20%;" name="num" id="num" placeholder="Num" class="form-control" value="<?php echo $explodedAddress[1];?>">
      <input type="text" style="width: 20%;" name="comp" id="comp" placeholder="complemento" class="form-control" value="<?php echo $explodedAddress[2];?>">
      <input type="text" style="width: 20%;" name="bairro" id="bairro" placeholder="bairro" class="form-control" value="<?php echo $explodedAddress[3];?>">
      <input type="text" style="width: 60%;" name="city" id="city" placeholder="Cidade" class="form-control" value="<?php echo $explodedAddress[4];?>">
      <input type="text" style="width: 20%;" name="uf" id="uf" placeholder="UF" class="form-control" value="<?php echo $explodedAddress[5];?>">
      <input type="text" style="width: 20%;" name="cep" id="cep" placeholder="cep" class="form-control" value="<?php echo $explodedAddress[6];?>">
    </div>
    <br/><br/>

    <label for="mini">Minicurriculum:</label><br/>
    <textarea name="mini" id="mini" cols="70" rows="10" maxlength="20000" required="required" class="form-control"><?php echo $data['mini'];?></textarea><br/>
    

    <button type="submit" class="btn btn-primary">Enviar</button>
  </form>
</div>