<?php
class report_org_otdels extends c_report
{
 var $title='Подразделения организации' ;
 var $view_by_space_filter=1 ;
 var $allow_org_ids=1 ;
 var $usl_select_org='' ;
 public $use_datepicker=1 ;
 public $org_id=1 ;
 public $rec_org=[] ;

 function init($create_options=array())
 {
     $this->rec_org=PERSONAL()->get_org_by_id($this->org_id) ;
 }

 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 create_usl_select_obj($_params,&$options)
  {  //damp_array($this->filter,1,-1) ;
     $usl=array() ;
     $usl[]='clss=208' ;
     $usl[]='parent='.$this->org_id ;
     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  view_deps
                        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 pkey,tkey,parent,clss,obj_name,level,
                       (select count(pkey) from obj_site_personals where parent'.($this->rec_org['level']+1).'=t1.pkey and clss=211 and enabled=1) as cnt_personals
                       from  view_deps t1
                       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?>>
            <colgroup><col id="c1"><col id="c2"><col id="c3"><col id="c4"></colgroup>
            <tr><th>ID</th><th>Наименование подразделения</th><th>Число сотрудников</th><th>Операции</th></tr><? ;
            if (_sizeof($list_recs)) foreach($list_recs as $rec)
            {   if (!$rec['obj_name']) $rec['obj_name']='Подразделение ID '.$rec['pkey'] ;
                ?><tr class=item>
                    <td><?echo $rec['pkey']?></td>
                    <td class="left"><a href="/frame/personals/otdel/card/<?echo $rec['pkey']?>/"><?echo $rec['obj_name']?></td>
                    <td><?echo ($rec['cnt_personals'])? $rec['cnt_personals']:'-'?></td>
                    <td>
                        <?if ($rec['cnt_personals'] == 0 and MEMBER()->rol==1) {?>
                            <button class="v2" cmd="aspmo_personal/after_delete_otdel" otdel_id="<?echo $rec['pkey']?>">Удалить подразделение</button>
                        <?}?>
                    </td>
                </tr><?
            }
          ?></table><?
          //print_2x_arr($list_recs) ;
      }
}
?>
