<?php

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

$svaTool = new svaTool();

//print_r($_REQUEST); 


$errorCode = 0;

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






if (isset($_REQUEST['assigneeProfile']) && $_REQUEST['assigneeProfile'] != "-1")
    $profileId = addslashes($_REQUEST['assigneeProfile']);
else
    $errorCode = 2;




if (isset($_REQUEST['comment']) && strlen($_REQUEST['comment']) > 0)
    $comment = addslashes($_REQUEST['comment']);
else
    $errorCode = 3;

$ticketId = addslashes($_REQUEST['ticketId']);

if ($dba->checkSaveTicketDetails($ticketId) == 0) {

    $errorCode = 1;
}

$userId = $_SESSION['ownerId'];

if ($errorCode == 0) {
    $dba->UploadTicketHistory($userId, $profileId, $ticketId, 0, $comment, 10);
    $dba->updateTicketTransferMode($ticketId, 10);

// Send mail
    //$errorCode = 10;

    $emailRes = $dba->getEmailList($profileId);
    $ccList = "";
    if (count($emailRes) > 0) {

        for ($i = 0; $i < count($emailRes); $i++) {

            $ccList .= $emailRes[$i]['email_v'] . ",";
        }

        $eRes = $dba->getSystemUserById($userId);

        $ccList .= $eRes['email_v'];

        //$mailAction->svaSendMail($emailRes[0]['email_v'],$ccList,"TICKET DANS VOTRE CORBEILLE","VEUILLEZ CONSULTER VOTRE CORBEILLE DE TICKET");

        $subject = " NOTIFICATION TICKET N° " . $ticketId;
        $ccList = rtrim($ccList, ",");
        $attachments = "";

        $queryres = $dba->getSCCFwdTicketDetailsByTicket($ticketId);
        
        $resSla =  $dba->getSLABySubMenuId( $queryres[0]['reason_n']);

        $content = $svaTool->prepareMailBody($ticketId, $queryres[0]['fwdBy'], $queryres[0]['fwdTo'], $resSla[0]['sla_level_v'], $queryres[0]['clientComment'], $moduleName, $submoduleName, $queryres[0]['ticket_reason_desc_v'], $resSla[0]['sla_duration_n']);

        $dba->SCCUploadMailNotification($emailRes[0]['email_v'], $ccList, $subject, $content, $attachments);
    }
}




switch ($errorCode) {

    case 0 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"TICKET EDITE AVEC SUCCES !\"  }";
        break;
    case 1 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ ENREGISTRER LE TICKET AVANT DE TRANSFERER !\"  }";
        break;
    case 2 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER  LE PROFIL!\"  }";
        break;
    case 3 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ ECRIRE UN COMMENTAIRE! \"  }";
        break;

    /*   case 4 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER LA STRATEGIE DES APPELS ENTRANTS.\"  }";
      break;
      /* case 5 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE LOGIN EST INVALIDE.\"  }";
      break;
      case 6 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! L'EMAIL EST INVALIDE.\"  }";
      break;
      /*case 7 : $msg =  "ERREUR ! LE MOT DE PASSE EST INVALIDE.";
      break;
      case 8 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE MOT DE PASSE DE CONFIRMATION EST INVALIDE .!\"  }";
      break;
      case 9 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LES MOTS DE PASSE NE CONCORDENT PAS.\"  }";
      break; */
    case 13 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! IMPOSSIBLE DE CREER LE COMPTE. VEUILLEZ RE-ESSAYER ULTERIEUREMENT!\"  }";
        break;
    /*
      case 14 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER LE PLAN TARIFAIRE ASSOCIE AU COMPTE !\"  }";   break; */
}


echo $msg;
?>