<?php
header('Content-Type: text/html; charset=utf-8');
require 'database.inc.php';
function auto_client($Nom, $Prenom, $Mode)
{
  global $NoVisu, $Type, $Sous_Type;
  if (empty($Nom) && empty($Prenom))
    $requette = "SELECT * FROM clients ORDER BY Nom, Prenom";
  else if (empty($Nom))
    $requette = "SELECT * FROM clients WHERE " . Compare_Sans_Accents("Prenom", addslashes($Prenom)) . " ORDER BY Nom, Prenom";
  else if (empty($Prenom))
    $requette = "SELECT * FROM clients WHERE " . Compare_Sans_Accents("Nom", $Nom) . " ORDER BY Nom, Prenom";
  else
    $requette = "SELECT * FROM clients WHERE " . Compare_Sans_Accents("Nom", $Nom) . " AND " . Compare_Sans_Accents("Prenom", $Prenom) . "ORDER BY Nom, Prenom";
  if ($resultat = Connection::executeRawQueryWithfetchAll($requette)) {
    $nb = count($resultat);
    if ($nb == 1) {
      if ($ligne = $resultat[0]) {
        echo "if(confirm('" . addslashes($ligne['Nom']) . " " . addslashes($ligne['Prenom']) . " " . addslashes($ligne['Adresse']) . " " . addslashes($ligne['Localite']) . "'))\n";
        echo "{\n";
        echo Affecte_Resultat_Client($ligne, $Mode);
        echo "}\n";
        echo "else\n";
        echo "{\n";
        echo "if(confirm('Créer nouveau client?'))\n";
        echo "{\n";
        echo "  window.open('recherche_client_create.php?Nom=" . urlencode($Nom) . "&Prenom=" . urlencode($Prenom) . "&Mode=$Mode', '', 'top=0, left=0, width=800, height=600, resizable=yes, toolbar=no, scrollbars=yes, status=no');\n";
        echo "}\n";
        echo "else\n";
        echo "{\n";
        echo "}\n";
        echo "}\n";
      }
    } else if ($nb == 0) {
      echo "if(confirm('Voulez-vous créer ce client?'))\n";
      echo "{\n";
      echo "  window.open('recherche_client_create.php?Nom=" . urlencode($Nom) . "&Prenom=" . urlencode($Prenom) . "&Mode=$Mode', '', 'top=0, left=0, width=800, height=600, resizable=yes, toolbar=no, scrollbars=yes, status=no');\n";
      echo "}\n";
      echo "else\n";
      echo "{\n";
      echo "}\n";
      $Type = 3;
      $Sous_Type = 3;
    } else {
      echo "  window.open('recherche_client_filter.php?Nom=" . urlencode($Nom) . "&Prenom=" . urlencode($Prenom) . "&Mode=$Mode', '', 'width=300, height=100, resizable=yes, toolbar=no, scrollbars=yes, status=no');\n";
    }
  }
  return $nb;
}
if (Test_User($lg_user, $lg_password) == TRUE) {
  $nb = auto_client($Nom, $Prenom, $Mode);
}
