<?php
class report_med_orgs extends c_report
{
 var $title='Справочник медицинских организаций' ;
 var $view_by_space_filter=1 ;
 var $allow_org_ids=1 ;
 var $usl_select_org='' ;
 public $use_datepicker=1 ;

 function setting($options=array())
{   if ($this->filter['personal_id'] and !$this->filter['name']) $this->filter['name']=PERSONAL()->get_name_by_id($this->filter['personal_id']) ;
    ?><div id="panel_filter">
            <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="<?echo _CUR_REPORT_DIR?>">
                 <fieldset>
                     <div class="row" id="person_info">
                          <section class="col col-2">
                              <label class="label">Наименование</label>
                              <label class="input">
                                   <input type="text" name="filter[name]" placeholder="" value="<?echo $this->filter['name']?>">
                              </label>
                          </section>
                       </div>
                 </fieldset>
                 <?$this->panel_button_apply();?>
             </form>
      </div><br>
    <?
 }

 function panel_report_buttons($options=array())
 {
     FORM()->button(['title'=>'Добавить мед.организацию','cmd_method'=>'v2','cmd'=>'medical_organizations/med_org_create']);
 }

  function create_usl_select_obj($_params,&$options)
  {  //damp_array($this->filter,1,-1) ;
     $usl=array() ;
     $usl[]='clss=307' ;
     //$usl[]='enabled=1' ;
     if ($this->filter['name'])              $usl[]='obj_name like "%'.$this->filter['name'].'%"' ;

     $usl_res=implode(' and ',$usl) ;
     //damp_array($usl,1,-1) ;
     return($usl_res) ;
  }

 function panel_info_filter()
 { $_str=array() ; $info=array() ; $title='' ;
   //damp_array($this->filter) ;
   if ($this->filter['name'])                            $info['Организация']='"'.$this->filter['name'].'"' ;

   if (_sizeof($info)) foreach($info as $title=>$value) $_str[]=$title.': <strong>'.$value.'</strong>' ;
   if (_sizeof($_str)) $title='<p class=center>'.implode(' ',$_str).'</p>' ;
   return $title ;
 }


  function get_cnt_items($usl,$options=array())
  { $cnt=execSQL_value('select count(pkey)
                        from  catalog_med_orgs
                        where '.$usl,$options) ;
    return($cnt) ;
  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
    $order=($this->filter['order'])? ' order by '.$this->filter['order']:' order by obj_name' ;
    //echo $usl ;
    //  $options['debug']=2 ;
 	$list_rec=execSQL('select *
                       from  catalog_med_orgs
                       where '.$usl.$order.$limit,$options) ;
    if (_sizeof($list_rec))
    { //foreach($list_rec as $id=>$rec) PERSONAL()->prepare_public_info_to_personal($list_rec[$id],$options) ;
    }
    return($list_rec) ;
  }


    // шаблон - вывод ссылками без разделитея
    function print_template_HTML($list_recs,$options=array())
      { $id=($options['id'])? 'id='.$options['id']:'' ;
        ?><table class="list_personal_table_img basic fz_small full auto"<?echo $id?>>
            <tr><th>ID</th><th>Краткое наименование организации</th><th>Полное наименование организации</th><th>email</th><th>Телефон</th><th>ИНН</th><th>КПП</th><th>ОГРН</th><th>Адрес</th><th>Рабочая группа ЭСМО</th><th></th></tr><? ;
            if (_sizeof($list_recs)) foreach($list_recs as $rec)
            {   //$stats=execSQL_row('select clss,count(pkey) from obj_site_personals where parent2='.$rec['pkey'].' and enabled=1 and _enabled=1 group by clss') ;
                $arr_groups=execSQL_row('select pkey,obj_name from obj_site_account where clss=206 and med_org_id='.$rec['pkey']);
                ?><tr class=item>
                    <td><?echo $rec['pkey']?></td>
                    <td class="left"><?echo $rec['obj_name']?></td>
                    <td><?echo $rec['full_name']?></td>
                    <td><?echo $rec['email']?></td>
                    <td><?echo $rec['phone']?></td>
                    <td><?echo $rec['inn']?></td>
                    <td><?echo $rec['kpp']?></td>
                    <td><?echo $rec['ogrn']?></td>
                    <td><?echo $rec['adres1']?></td>
                    <td><? if (sizeof($arr_groups)) echo implode('<br>',$arr_groups)?></td>
                    <td>
                        <button class="v1" href="/cab/medical_organizations/card/<?echo $rec['pkey']?>/">Редактировать</button><br>
                        UID: <?echo $rec['uid']?>
                    </td>
                </tr><?
            }
          ?></table>
          <script type="text/javascript">
              $j('select.select_group').on('change',function()
              {   send_ajax_request({cmd:'aspmo_personal/set_org_group',org_id:$j(this).attr('org_id'),group_id:$j(this).val()});

              })  ;
          </script>

          <?
          //print_2x_arr($list_recs) ;
      }
}
?>
