<?php
class report_cars extends c_report
{
 var $title='Отчет по автомобилям' ;
 var $filter=array() ;
 var $view_by_space_filter=1;

    function setting()
    {
        $arr_orgs_ids=$this->get_org_list() ;
        ?><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">
                             <?
                             $this->filter_section_input_text('car_number','Номер машины:',2,['autocomplete'=>'off']) ;
                             $this->filter_section_select_multiple('org_ids','Владелец авто:',3,$arr_orgs_ids) ;
                             ?>
                         </div>
                     </fieldset>
                     <?$this->panel_buttons();?>
                 </form>
         </div>
        <?
    }

    function get_org_list()
    {
        $result=[];
        $result=execSQL_row('select distinct(t1.parent) as pkey,t2.obj_name 
                            from '.TO()->table_car.' t1 
                                left join obj_site_personals t2
                                    on t2.clss=207 and t1.parent=t2.pkey
                           order by obj_name') ;

        return $result;
    }

    function panel_info_filter()
    {
      $_str=array() ; $info=array() ; $title='' ;
      if ($this->filter['car_number'])                            $info['Номер авто']='"'.$this->filter['car_number'].'"' ;
      if ($this->filter['org_ids'])                         $info['Организации']=PERSONAL()->get_org_names_by_ids(array_keys($this->filter['org_ids']))  ;

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

    function create_usl_select_obj($_params, &$options)
    {
        $_usl=[];

        $_usl[]='enabled=1';
        if ($this->filter['car_number'])                $_usl[]='obj_name like "%'.$this->filter['car_number'].'%"' ;
        if ($this->filter['org_ids'])                   $_usl[]='parent in ('.implode(',',array_keys($this->filter['org_ids'])).')' ;

        $usl = implode(' and ',$_usl);
        return $usl;
    }

    function get_cnt_items($usl, $options = array())
    {
        if ($usl) $usl=' where '.$usl;
        $cnt=execSQL_value('select count(pkey) from view_personals_cars'.$usl,$options['debug']);
        return $cnt;
    }

    function get_items($usl, $options = array())
    {
        $limit=($options['limit'])? ' '.$options['limit']:'' ;
        if ($usl) $usl=' where '.$usl;
        $recs = execSQL('select * from view_personals_cars'.$usl.$limit,$options['debug']);

        return $recs;
    }

    function print_template_HTML($list_recs,$options=array())
    {
        $i=$this->first_obj ; ;

        ?>
        <table class="basic fz_small full auto">
            <tr>
                <th class="table_number">№</th>
                <th>Номер автомобиля</th>
                <th>Владелец</th>
                <th>Модель</th>
                <th>Тип</th>
            </tr>
            <?
            foreach ($list_recs as $list_rec) {
                ?>
                <tr>
                    <td><?echo $i; $i++;?></td>
                    <td class="left">
                        <?echo $list_rec['obj_name']?>
                        <br>
                        <div class="comment"><?echo $list_rec['uid']?></div>
                    </td>
                    <td class="left"><?echo $list_rec['_org_name']?></td>
                    <td><?echo $list_rec['model']?></td>
                    <td><?echo $list_rec['type']?></td>
                </tr>
                <?
            }
            ?>
        </table>
        <style>
            th.table_number {width: 20px;}
        </style>
        <?
    }
}

?>
