<?
include_once(_DIR_EXT.'/HTML/c_page_XML_AJAX.php') ;
class c_page_ajax extends c_page_XML_AJAX
{

    function med_org_create()
    {
        FORM()->form_open() ;
        FORM()->row_open() ;
        FORM()->section_input(['name'=>'name','title'=>'Наименование мед.организации:','col_count'=>'12','required'=>1]) ;
        FORM()->row_close() ;
        FORM()->button(['title'=>'Добавить','cmd'=>'medical_organizations/med_org_create_apply']);
        FORM()->button(['title'=>'Отмена','class'=>'cancel']);
        FORM()->form_close() ;


        $this->add_element('show_modal_window','HTML',array('title'=>'Добавление медицинской организации','_after_close_update_page'=>1,'_width'=>1000)) ;
    }

    function med_org_create_apply()
    {
        $result=ESMO()->med_org_create($_POST) ;
        if ($result['status']=='success') $this->update_page=1 ;
        else
        { damp_array($result,1,-1);
          $this->add_element('show_modal_window','HTML',array('title'=>'Добавление медицинской организации','_after_close_update_page'=>1,'_width'=>1000)) ;
        }
    }

    function med_org_edit_apply()
    {
        $result=\cab\aspmo\i_med_org::save($_POST['med_org_id'],$_POST) ;
        if ($result['result']) $this->update_page=1 ;
        else
        { damp_array($result,1,-1);
          $this->add_element('show_modal_window','HTML',array('title'=>'Редактирование медицинской организации','_after_close_update_page'=>1,'_width'=>1000)) ;
        }
        /*$rec_med_org=ESMO()->get_med_org_by_id($_POST['med_org_id']) ;
        if ($rec_med_org['pkey'])
        {   if ($_POST['inn'] and GENERAL()->enabled['dadata'])
						{
								include_once(_DIR_TO_MODULES.'/plugins/i_dadata.php') ;
								$result=DADATA()->get_org_rekvesit($_POST['inn']) ;
								$org_info=$result['suggestions'][0] ; damp_array($org_info) ;
							  $_POST['name']=$org_info['value'] ;
							  $_POST['full_name']=$org_info['data']['name']['full_with_opf'] ;
							  $_POST['ogrn']=$org_info['data']['ogrn'] ;
							  $_POST['ogrn_date']=$org_info['data']['ogrn_date'] ;
							  $_POST['okpo']=$org_info['data']['okpo'] ;
							  $_POST['okato']=$org_info['data']['okato'] ;
							  $_POST['oktmo']=$org_info['data']['oktmo'] ;
							  $_POST['okogu']=$org_info['data']['okogu'] ;
							  $_POST['okfs']=$org_info['data']['okfs'] ;
							  $_POST['okved']=$org_info['data']['okved'] ;
							  $_POST['okved_type']=$org_info['data']['okved_type'] ;
							  $_POST['adres1']=$org_info['data']['address']['unrestricted_value'] ;

						}
					  if (!$rec_med_org['uid']) $_POST['uid']=ENGINE()->generate_UID() ;
					  $result=ESMO()->med_org_save($rec_med_org['pkey'],$_POST) ;
            if ($result['status']=='success') $this->update_page=1 ;
            else
            { damp_array($result,1,-1);
              $this->add_element('show_modal_window','HTML',array('title'=>'Редактирование медицинской организации','_after_close_update_page'=>1,'_width'=>1000)) ;
            }
        }
        else if (!$rec_med_org['pkey']) $this->add_element('show_notife','Некорректный med_org ID',array('type'=>'error')) ;
      */

    }

    function med_org_delete()
    {   $rec_med_org=ESMO()->get_med_org_by_id($_POST['med_org_id']) ;
        if ($rec_med_org['pkey']/* and (($rec_med_org['client_id']==MEMBER()->client_id) or MEMBER()->rol==1)*/)
        {   ?><p>Подтвержите удаление медицинской организации</p><?
            FORM()->form_open() ;
            FORM()->button(['title'=>'Удалить','cmd'=>'medical_organizations/med_org_delete_apply','med_org_id'=>$rec_med_org['pkey']]);
            FORM()->button(['title'=>'Отмена','class'=>'cancel']);
            FORM()->form_close() ;
            $this->add_element('show_modal_window','HTML',array('title'=>'Удаление медицинской организации','_after_close_update_page'=>1,'_width'=>1000)) ;
        }
        else if (!$rec_med_org['pkey']) $this->add_element('show_notife','Некорректный med_org ID',array('type'=>'error')) ;
        else $this->add_element('show_notife','Доступ запрещен',array('type'=>'error')) ;
    }

    function med_org_delete_apply()
    {   $rec_med_org=ESMO()->get_med_org_by_id($_POST['med_org_id']) ;
        if ($rec_med_org['pkey'] /*and (($rec_med_org['client_id']==MEMBER()->client_id) or MEMBER()->rol==1)*/)
        {   $result=ESMO()->med_org_delete($rec_med_org['pkey']) ;
            if ($result['status']=='success') $this->go_url='/cab/medical_organizations/' ;
            else
            { damp_array($result,1,-1);
              $this->add_element('show_modal_window','HTML',array('title'=>'Удаление медицинской организации','_after_close_update_page'=>1,'_width'=>1000)) ;
            }
        }
        else if (!$rec_med_org['pkey']) $this->add_element('show_notife','Некорректный med_org ID',array('type'=>'error')) ;
        else $this->add_element('show_notife','Доступ запрещен',array('type'=>'error')) ;
    }

    function med_org_toogle_enabled()
    {
        $rec_med_org=ESMO()->get_med_org_by_id($_POST['med_org_id']) ;
        if ($rec_med_org['pkey']/* and (($rec_med_org['client_id']==MEMBER()->client_id) or MEMBER()->rol==1)*/)
        {   $form_data['enabled']=($_POST['checked'])? 1:0 ;
            $result=ESMO()->med_org_save($rec_med_org['pkey'],$form_data) ;
            if ($result['status']=='success') $this->add_element('show_notife','Сохранено',array('type'=>'info')) ;
            else
            { damp_array($result,1,-1);
              $this->add_element('show_modal_window','HTML',array('title'=>'Изменение медицинской организации','_after_close_update_page'=>1,'_width'=>1000)) ;
            }
        }
        else if (!$rec_med_org['pkey']) $this->add_element('show_notife','Некорректный med_org ID',array('type'=>'error')) ;
        else $this->add_element('show_notife','Доступ запрещен',array('type'=>'error')) ;
    }






}
