<?php

session_start();

header('Access-Control-Allow-Origin:*');

header('Content-Type; application/json; charset=utf-8');

header('Access-control-Allow-Method: POST');

header('Access-control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-request-With');

include('db.php');


    $inputdata = json_decode(file_get_contents("php://input"), true);

    $username = $inputdata['username'];
    $useremail = $inputdata['useremail'];
    $password = $inputdata['password'];

    $nbr="";

    
    // verifier si categorie existant

    $agence_verif=$db->prepare('SELECT COUNT(userid) AS nbr FROM tbl_user WHERE username=? OR useremail=?');

    $agence_verif->execute(array($username,$useremail));

    while($agence_verifs=$agence_verif->fetch()){

        $nbr=$agence_verifs['nbr'];

    }

     if ($nbr == 0) {

        # ajouter

        $user=$db->prepare('INSERT INTO tbl_user (username,useremail,password,role) VALUES(?,?,?,?)');

        $user->execute(array($username,$useremail,$password,"user"));

        if ($user) {
                //création agence réussie

                echo (json_encode(['response' => 1]));



        }else{
            // création non réussie

            echo (json_encode(['response' => 0]));

            

        }

     }else {
        // categorie existe déjà

        echo (json_encode(['response' => -1]));

     }





