<?php
include($_SERVER['DOCUMENT_ROOT'] .'/script/HeaderInc.php');
if('XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH'] && true === isset($_SESSION['SessionAuth']['id']) && 0 < $_SESSION['SessionAuth']['id']) {
  $ObjUsersMod = new UsersMod($MS_SP);
  $VarWhereAdd = 'us.webservices_id = -1';
  if(true === isset($_SESSION['SessionAuth']['id']) && true === is_numeric($_SESSION['SessionAuth']['id'])){
    $VarWhereAdd = 'u.users_id = "'. $_SESSION['SessionAuth']['id'] .'"';
  }
  if(true === isset($_SESSION['SessionAuth']['SvtAdmin']['reg']) && 1 == $_SESSION['SessionAuth']['SvtAdmin']['reg']){
    $VarWhereAdd = 'us.webservices_id IN (1,2,3)';
  }
  $ArrUsersMaster = $ObjUsersMod->GetFromDb('SqlServices', 'GetAll', $VarWhereAdd);
  $smarty->assign('ArrUsersMaster', $ArrUsersMaster);
  
  $ObjSprStatusMod = new SprStatusMod($MS_SP);
  $ArrSprStatus = $ObjSprStatusMod->GetFromDb('SqlAll', 'GetAll', '', 'a.sprstatus_order');
  $smarty->assign('ArrSprStatus', $ArrSprStatus);
  
  $ObjDemandsClassifMod = new DemandsClassifMod($MS_SP);
  $ArrDemandsClassif = $ObjDemandsClassifMod->GetFromDb('SqlAll', 'GetAll', '', 'a.demandsclassif_order');
  $smarty->assign('ArrDemandsClassif', $ArrDemandsClassif);
  
  if(true === isset($_GET['demands_id']) && '' != $_GET['demands_id']){
    
    $ObjDemandsMod = new DemandsMod($MS_SP);
    $ArrDemands = $ObjDemandsMod->GetFromDb('SqlAll', 'GetRow', "a.demands_id = '". $_GET['demands_id'] ."'");
    //SiteLibs::GetVarDump($ArrDemands);

    $VarWhere = 'u.structureorganization_id = "'. $ArrDemands['structureorganization_id'] .'"';
    $ArrUsers = $ObjUsersMod->GetFromDb('SqlAll', 'GetAll', $VarWhere);
  //  SiteLibs::GetVarDump($ArrUsers);

    
    //SiteLibs::GetVarDump(array_merge($ArrUsers, $ArrUsersMaster));
    $smarty->assign('ArrUsers', array_merge($ArrUsers, $ArrUsersMaster));

    $ObjDemandsMod = new DemandsMod($MS_SP);
    $ArrDemands = $ObjDemandsMod->GetFromDb('SqlAll', 'GetRow', "a.demands_id = '". $_GET['demands_id'] ."'");
    $smarty->assign('ArrDemands', $ArrDemands);
    
    $ObjDemandsCommentsMod = new DemandsCommentsMod($MS_SP);
    $ArrDemandsComments = $ObjDemandsCommentsMod->GetFromDb('SqlAll', 'GetRow', "a.demands_id = '". $_GET['demands_id'] ."' AND a.demandscommentsparent_id = 0");
    $smarty->assign('ArrDemandsComments', $ArrDemandsComments);
  }
  $VarReturn = $smarty->fetch('Demands/Form.tpl');
}
else{
  $VarReturn = $MS_SP->Sp_LangMessage['error']['ErrFuncAccessDenied'];
}
echo $VarReturn;
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>