<?php session_start();
if(!empty($_SESSION['admin_connect_id']))
{ require('../../config.php');

date_default_timezone_set('Africa/Algiers');
$d_aj=date('Y-m-d H:i:s');


$nom=mysqli_real_escape_string($con,htmlspecialchars(trim($_POST['nom']))) ;
$prenom=mysqli_real_escape_string($con,htmlspecialchars(trim($_POST['prenom']))) ;
$type=mysqli_real_escape_string($con,htmlspecialchars(trim($_POST['type']))) ;

$username=mysqli_real_escape_string($con,htmlspecialchars(trim($_POST['username']))) ;
$password=mysqli_real_escape_string($con,htmlspecialchars(trim($_POST['password']))) ;
$password=md5($password);


$name_f=  "user.png" ;




if(isset($_FILES['files']))
{   $file_name = $_FILES['files']['name'];
     $namef = $_FILES['files']['name'];
     $file_size =$_FILES['files']['size'];
     $file_tmp =$_FILES['files']['tmp_name'];
     $file_type=$_FILES['files']['type'];
     $desired_dir="../../assets/img_user/";

    if(is_dir("$desired_dir/".$file_name)==false){
      $name_f=   md5(uniqid()) . ".jpg" ;
      $target = "$desired_dir/" . $name_f ;
      move_uploaded_file($file_tmp, $target );
    }

      }

$result_username=mysqli_query($con,"SELECT * FROM users WHERE username='$username'");
$count=mysqli_num_rows($result_username);

if($count > 0){
    echo 2;
   }
 else{
$sql2 = "INSERT INTO users (nom,prenom,username,password,img,type,created_at) VALUES ('$nom','$prenom','$username','$password','$name_f','$type','$d_aj')";

  if(mysqli_query($con,$sql2)){
  echo 1;
  } else {
   /* 
   //fonction permer d'afficher les erreurs Sql 
     echo 0;
   */echo mysqli_error($con);
  

  }

    }
     }
?>