<?
class report_pp_journal extends c_report
{
    var $title='Журнал измерений' ;
    var $allow_export_to_csv=1 ;
    public $use_fusioncharts=1;

    function setting()
    {
        if (!$this->filter) {
            $ts=getdate();
            $time=mktime(0,0,0,$ts['mon'],$ts['mday']-7,$ts['year']) ;
            $this->filter['data_from']=date('d.m.Y H:i',$time);;
        }
        ?><div id="panel_filter">
        <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="<?echo $this->url_report?>">
            <fieldset>
                <div class="row">
                    <section class="col col-3">
                        <label class="label">ФИО:</label>
                        <label class="input"><input type="text" name="filter[name]" value="<?echo $this->filter['name']?>" id="personal_name" SetAutoComplete cmd="autocomplette_personal_name"></label>
                    </section>
                    <section class="col col-3">
                        <label class="label">От:</label>
                        <label class="input"><input type="text" name="filter[data_from]" class="datetimepicker" data-required value="<?echo $this->filter['data_from']?>"></label>
                    </section>
                    <section class="col col-3">
                        <label class="label">До:</label>
                        <label class="input"><input type="text" name="filter[data_to]" class="datetimepicker" data-required value="<?echo $this->filter['data_to']?>"></label>
                    </section>
                </div>
                <div class="row">

                </div>
            </fieldset>
            <?$this->panel_buttons();?>
        </form>
        </div>
        <?
    }

    function create_usl_select_obj($_params,&$options)
    {
//        damp_array($this->filter,1,-1) ;
        $usl=array() ;
        $usl[]=300;
        if ($this->filter['time_from'])     $usl[]='t1.c_data>='.$this->filter['time_from'] ;
        if ($this->filter['time_to'])       $usl[]='t1.c_data<='.$this->filter['time_to'] ;
        if ($this->filter['data_from'])     $usl[]='t1.c_data>='.strtotime($this->filter['data_from']) ;
        if ($this->filter['data_to']) {
            $arr = explode(' ', trim($this->filter['data_to']));
            $time_to = (!$arr[1]) ? strtotime($this->filter['data_to'] . ' 23:59:59') : strtotime($this->filter['data_to']);
            $usl[] = 't1.c_data<=' . $time_to;
        }


//        if ($this->filter['pkey'])              $usl[]='pkey='.$this->filter['pkey'] ;
        if ($this->filter['name'])              $usl[]='t2.obj_name like "'.$this->filter['name'].'%"' ;
//        if ($this->filter['org_id'])            $usl[]='t1._org_id="'.$this->filter['org_id'].'"' ;
//        if ($this->filter['otdel_id'])          $usl[]='t1._otdel_id in ('.$this->filter['otdel_id'].')' ;
//        if ($this->filter['dep_id'])          $usl[]='parent='.$this->filter['dep_id'] ;
//        if ($this->filter['uid'])              $usl[]='uid='.$this->filter['uid'] ;
//        if ($this->filter['ids'])              $usl[]='pkey in ('.implode(',',array_keys($this->filter['ids'])).')' ;
//        if ($this->filter['propusk'])          $usl[]='propusk="'.$this->filter['propusk'].'"' ;
//        if ($this->filter['working_id'])          $usl[]='working="'.$this->filter['working_id'].'"' ;
//        if ($this->filter['working'])   {
//            $keys=array_keys($this->filter['working']);
//            $usl[]='t1.working in ('.implode(",",$keys).')';
//        }
//        if ($this->filter['ext']==1)          $usl[]='ext=1' ; else $usl[]='(ext=0 or ext is null)' ;
//        // выбор сотрудников по ID группы
//        if ($this->filter['personal_ids'])    $usl[]='pkey in ('.$this->filter['personal_ids'].')' ;

        $usl_res=implode(' and ',$usl) ;

//        damp_array($usl,1,-1) ;
        return($usl_res) ;
    }

    function panel_info_filter()
    {
        $_str=array() ; $info=array() ; $title='' ;
      if ($this->filter['data_from'])                       $info['c']=$this->filter['data_from'] ;
      if ($this->filter['data_to'])                         $info['по']=$this->filter['data_to'] ;
      if ($this->filter['time_from'])                       $info['c']=date('d.m.Y H:i',$this->filter['time_from']);
      if ($this->filter['time_to'])                         $info['по']=date('d.m.Y H:i',$this->filter['time_to']);
//        if ($this->filter['group_id'])                        $info['Группа терминалов']=ACCOUNTS()->get_member_name_by_id($this->filter['group_id'])  ;
//      if ($this->filter['org_id'])                          $info['Организация']=PERSONAL()->get_name_by_id($this->filter['org_id'])  ;
//      if ($this->filter['org_ids'])
//      {   $arr_names=execSQL_line('select obj_name from obj_site_personals where pkey in ('.implode(',',array_keys($this->filter['org_ids'])).')') ;
//          $info['Организация']=implode(', ',$arr_names)  ;
//      }
//        if ($this->filter['working'])
//        {
//            $working_names=[];
//            foreach (array_keys($this->filter['working']) as $array_key){
//                $working_names[]=_IL('IL_works')->recs[$array_key]['obj_name'];
//            }
//            $info['Должность']=implode(', ',$working_names);
//        }
//        if ($this->filter['dep_id'])                          $info['Подразделение']=PERSONAL()->get_full_dep_name_by_dep_id($this->filter['dep_id'])  ;
        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 get_cnt_items($usl,$options=array())
    {  // !!! использовать  PERSONAL()->table_personal так как вьювер жутко тупит
//        damp_array($arr);
        $arr=execSQL('select t1.pkey
                        from obj_site_narko_data t1
                        left join '.PERSONAL()->table_personal.' t2
                            on t1.personal_id=t2.pkey
                        where '.$usl,$options);

        return(count($arr));
    }

    function get_items($usl,$options=array())
    {
        $limit=($options['limit'])? ' '.$options['limit']:'' ;
        $order=($this->filter['order'])? ' order by '.$this->filter['order']:' order by t1.pkey desc' ;
//        $list_rec=execSQL('select pkey,obj_name,age,parent,parent2,parent3,parent4,parent5,parent6,parent7,parent8,parent9,working,cnt_pp
//                                from '.PERSONAL()->table_personal.'
//                                where '.$usl.$order.$limit,$options);
        $list_rec=execSQL('select t1.pkey,t1.c_data,t1.terminal_id,t1.result,t1.comment,t1.id_mo,t2.obj_name,t2.age,t2.pol,t2.cnt_pp
                        from obj_site_narko_data t1
                        left join '.PERSONAL()->table_personal.' t2
                            on t1.personal_id=t2.pkey
                        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>№</th>
            <th>ID записи</th>
            <th>Терминал</th>
            <th>Сотрудник</th>
            <th>Возраст</th>
            <th>Пол</th>
            <th>Кол-во измерений</th>
            <th>Норма</th>
            <th>Комментарий</th>
            <th>Результат</th>
        </tr><? ;
        $i=$this->first_obj ;
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        { ?><tr class=item>
            <td><?echo $i; $i++;?></td>
            <td class="preview_graf"><div><?echo $rec['pkey'];?><div class="panel_PP_preview" pp_id="<?echo $rec['pkey']?>" id="panel_PP_preview_<?echo $rec['pkey']?>"></div></div></td>
            <td><a href="/window/mo/<?echo $rec['id_mo']?>/pp/"><?echo date('d.m.Y H:i',$rec['c_data'])?></a></td>
            <td><?echo $rec['obj_name']?></td>
            <td class="age"><?echo $rec['age']?></td>
            <td><?echo $rec['pol']?'Жен.':'Муж.'?></td>
            <td><?echo $rec['cnt_pp']?></td>
            <td></td>
            <td><?echo $rec['comment']?></td>
            <td><?echo ($rec['result']=='ok')?'Норма':$rec['result']?></td>
            </tr><?
        }
        ?></table><?
        //damp_array($rec) ;
    }

}

