<?php

session_start();
$currentBaseDir = dirname($_SERVER['PHP_SELF']);
include($_SESSION['basedir'] . "core.php");

$svaTool = new svaTool();

//print_r($_REQUEST);


$errorCode = 0;
$opId = $userId = $username = $pwd = $userType = $profileId = $onwerId = $lang = $citizen = $firstName = $lastName = $primaryPhone = $secondaryPhone = $homePhone = $mobilePhone = $email = $primAddress = $secondAddress = $profession = $char1 = $char2 = $char3 = $char4 = $char5 = $char6 = $char7 = $char8 = $char9 = $char10 = $status = "";

$password = NULL;
$confPasswd = NULL;
// Ajout de nouvel utilisateur


if (isset($_REQUEST['name']) && strlen($_REQUEST['name']) > 0) {
    $name = $_REQUEST['name'];
} else {

    $errorCode = 1;
}

if (isset($_REQUEST['groupId']) && $_REQUEST['groupId'] != -1) {
    $groupId = $_REQUEST['groupId'];
} else {

    $errorCode = 2;
}


if (isset($_REQUEST['supervisorId']) && $_REQUEST['supervisorId'] != -1) {
    $supervisorId = $_REQUEST['supervisorId'];
} else {

    $errorCode = 3;
}

$description = $_REQUEST['description'];
$list = $_REQUEST['list'];
$opId = $_REQUEST['opId'];
$itemId = $_REQUEST['itemId'];
$teamId = $itemId;

$agentList = count($list) ;

//print_r($list);

if($agentList== 0 ){
    
     $errorCode = 4;
}






$userId = $_SESSION['ownerId'];



if ($opId == 0) {
    $label = "CREEE";
} else {
    $label = "MODIFIEE";
}


if ($errorCode == 0) {
    
  $res =   $dba->UpdateAddDelTeam($opId, $itemId,$name, $description, $userId, $supervisorId,$groupId, 0 ) ;
  $dba->UpdateAddDelTeamDetail(3, $itemId,$groupId, $agentId, $teamId, 0 ) ; 
  
  if($opId == 0){
        $itemId = $res ;
        $teamId = $res ;
    }
  
   for($i=0 ; $i <$agentList; $i++) {
       
      $dba->UpdateAddDelTeamDetail(0, $itemId,$groupId, $list[$i], $teamId,0 );
   }
    
   
}


switch ($errorCode) {

    case 0 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"EQUIPE  $label AVEC SUCCES !\"  }";
        break;
    case 1 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! L'IDENTIFIANT  EST INVALIDE !\"  }";
        break;
    case 2 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER LE GROUPE D'AGENT !\"  }";
        break;
    case 3 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER LE SUPERVISEUR  !\"  }";
        break;
    case 4 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER UN AGENT   !\"  }";
        break;
  
}


echo $msg;
?>