<?php
//include_once(_DIR_EXT.'/HTML/c_page_XML.php') ;
include_once(_DIR_EXT.'/HTML/c_page_TXT.php') ;
//class c_services_pl_get_med_orgs extends c_page_XML
class c_services_pl_set_med_orgs_uid extends c_page_txt
{

	function check_access()
  { /*if (!$this->setting['no_check_access']) $result=ESMO_GATEWAY()->api_check_access() ;
    else $result=['status'=>'success','comment'=>''] ;

    //if ($result['client_id']) $this->client_id=$result['client_id'] ;
		if ($result['status']!='success') { $this->response_error($mode,array('code'=>$result['comment'],'ip'=>$_SERVER['REMOTE_ADDR'])) ; }
*/
		$result=['status'=>'success','comment'=>''] ;
    return($result) ;
  }

  function body()
  {
     //$result=ESMO_GATEWAY()->api_check_access() ;
     //if ($result['status']!='success') { $this->response_error($mode,array('code'=>$result['comment'],'ip'=>$_SERVER['REMOTE_ADDR']));  return ; }

	 $XML_source = file_get_contents("php://input");
	 $data = json_decode(json_encode((array) simplexml_load_string($XML_source)), true);
	 if (isset($data['item'][0])) $data=$data['item'][0] ; // если были передано несколько организаций - берем первую
	  //var_dump($data);
	 $uid=$data['item']['@attributes']['uid'] ;
	 $uid_1c=$data['item']['uid_1c'] ;
	 $rec_med_org=ESMO()->get_med_org_by_uid($uid) ;
	 $result=ESMO()->med_org_save($rec_med_org['pkey'],['uid_1c'=>$uid_1c]) ;



	$this->response_success(['uid'=>$uid]);
  }

  function response_success($arr)
  {  header("Content-type: text/xml;charset=UTF-8") ;
	   $imp = new DOMImplementation; // Creates an instance of the DOMImplementation class
	   $xml_doc = $imp->createDocument("",""); // Creates a DOMDocument instance
		 $xml_doc->encoding = 'utf-8';
		 $xml_doc->formatOutput = true;
		 $xml = add_element($xml_doc,$xml_doc,'results','',array('date'=>date('Y-m-d H:i'))) ;
		 $result = add_element($xml_doc,$xml,'result','success',array('uid'=>$arr['uid'])) ;
		 if (_sizeof($arr)) foreach($arr as $name=>$value)  if ($name!='uid') add_element($xml_doc,$result,$name,$value) ;
		 echo $xml_doc->saveXML() ;
  }

	function response_error($mode,$arr)
  {  header("Content-type: text/xml;charset=UTF-8") ;
	   $imp = new DOMImplementation; // Creates an instance of the DOMImplementation class
	   $xml_doc = $imp->createDocument("",""); // Creates a DOMDocument instance
		 $xml_doc->encoding = 'utf-8';
		 $xml_doc->formatOutput = true;
		 $xml = add_element($xml_doc,$xml_doc,'xml_doc','',array('date'=>date('Y-m-d H:i'))) ;
		 $error=add_element($xml_doc,$xml,'error','',array()) ;
		 if (_sizeof($arr)) foreach($arr as $name=>$value) add_element($xml_doc,$error,$name,$value) ;
		 echo $xml_doc->saveXML() ;
  }

}



?>
