<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$db = new SQLite3('../a/.eggziedb.db');

if(isset($_GET['index'])) {
    $index = $_GET['index'];

} else {

    $index = ""; // Default value

}

include '../includes/header2.php';
$id_revenda = $_SESSION['id'];
$res1 = $db->query("SELECT * FROM revenda WHERE id = '$id_revenda'");
    while ($row1 = $res1->fetchArray()) {
                                  
        $name = $row1['nome'];
        $username = $row1['usuario'];
        $user = $username;
        $password = $row1['senha'];
        
    }

if (isset($_POST['submit'])) {
    
    
        	$username_new = $_POST['username'];
    	
    		$result = $db->query("SELECT COUNT(*) as count FROM revenda WHERE usuario = '$username_new'");
            $row = $result->fetchArray(SQLITE3_ASSOC);
            $count = $row['count'];
            
            $result = $db->query("SELECT COUNT(*) as count FROM sub_revenda WHERE usuario = '$username_new'");
            $row = $result->fetchArray(SQLITE3_ASSOC);
            $count2 = $row['count'];
            
            $count = $count + $count2;
              
            if($count || $username_new == "ADM") {
              echo "<script>alert('Já existe um sub revenda/revenda com esse usuario, por favor escolha outro!');window.location.href='index.php';</script>";
            } else {
    	
        	    $result = $db->query("SELECT usuario FROM revenda WHERE id = '$revenda_id'");
                // Iterate over the fetched results
                 while ($row = $result->fetchArray()) {
                     // Output the value of the column
                     $user = $row['usuario'];
                     break;
                 }
                 
            	$password = $_POST['password'];
            	$name = $_POST['name'];
                $id_revenda = $_SESSION['id'];
            	
            	$db1->exec("UPDATE revenda SET nome = '$name', usuario = '$username_new', senha = '$password' WHERE id = '$id_revenda'");
            	$db1->exec("UPDATE ibo SET seller = '$username_new' WHERE seller = '$username'");
                $_SESSION['seller_name'] = $username_new;
                $usuario = $username_new;
                // renomear arquivos e outros
                rename("../a/rtx/Setting_$user.json", "../a/rtx/Setting_$usuario.json");
                rename("../a/rtx/logo_filenames_$user.json", "../a/rtx/logo_filenames_$usuario.json");
                rename("../a/rtx/image_filenames_$user.json", "../a/rtx/image_filenames_$usuario.json");
                rename("../a/note_$user.json", "../a/note_$usuario.json");
                rename("../api/theme_$user.json", "../api/theme_$usuario.json");
                
                $db2 = new SQLite3("../api/.adb.db");
                $db2->exec("UPDATE ads SET owner = '$usuario' WHERE owner = '$user'");
                $db3 = new SQLite3("../api/intro.db");
                $db3->exec("UPDATE intro SET owner = '$usuario' WHERE owner = '$user'");
        
            	$db1->close();
                echo "<script>window.location.href='index.php';</script>";
            }
        
}


?>

<div class="container-fluid">

          <h1 class="h3 mb-1 text-gray-800">Update Login</h1>
         
          <!-- Content Row -->
          <div class="row">

            <!-- First Column -->
            <div class="col-lg-12">

              <!-- Custom codes -->
                <div class="card border-left-primary shadow h-100 card shadow mb-4">
                <div class="card-header py-3">
                <h6 class="m-0 font-weight-bold text-primary2"><i class="fa fa-user"></i> Atualizar Perfil</h6>
                </div>
                <div class="card-body">
                            <form method="post">
                            <div class="form-group ">
                            <label class="control-label " for="name">
                            <strong>Name</strong>
                            </label>
                            <div class="input-group">
                            <input type="text" class="form-control text-primary" name="name" value="<?php echo $name;?>" placeholder="Enter Name">
                            </div>
                            </div>
                            <div class="form-group ">
                            <label class="control-label " for="username">
                            <strong>Username</strong>
                            </label>
                            <div class="input-group">
                            <input type="text" class="form-control text-primary" name="username" value="<?php echo $username;?>" placeholder="Enter Username">
                            </div>
                            </div>
                            <div class="form-group ">
                            <label class="control-label " for="password">
                            <strong>Password</strong>
                            </label>
                            <div class="input-group">
                            <input type="password" class="form-control text-primary" name="password" value="<?php echo $password;?>" id="myInput" placeholder="Enter Password">
                            </div>
                            </div>
                            <div class="form-group ">
 <input type="checkbox" onclick="myFunction()">
                            <label class="control-label " for="password">
                            <strong>Show Password</strong>
                            </label>
                            </div>
                            
								                       <div class="form-group">
                        <button class="btn btn-success btn-icon-split" name="submit" type="submit">
                        <span class="icon text-white-50"><i class="fas fa-check"></i></span><span class="text">Submit</span>
                        </button>
                            </div>
                            </form></div>
                </div>
                            </div>
                            
            </div>
                </div>
                <script>
function myFunction() {
  var x = document.getElementById('myInput');
  if (x.type === 'password') {
    x.type = 'text';
  } else {
    x.type = 'password';
  }
}</script>

<?php 
include '../includes/footer.php';
?>

</html>