<?
include_once(_DIR_EXT.'/aspmo_pp/i_source.php') ;
class report_pp_results extends c_report
{
    var $title='Результаты пупиллометрии (сервисный)' ;

    function setting()
    {
        $now=getdate() ;
        if (!$this->filter['data_from']) $this->filter['data_from']=date('d.m.Y',mktime(0,0,0,$now['mon'],1,$now['year']));
        if (!$this->filter['data_to'])   $this->filter['data_to']=date('d.m.Y',mktime(0,0,0,$now['mon']+1,1,$now['year'])-1);

        ?><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">
                    <section class="col col-2">
                        <label class="label">От:</label>
                        <label class="input"><input type="text" name="filter[data_from]" class="datepicker" data-required value="<?echo $this->filter['data_from']?>"></label>
                    </section>
                    <section class="col col-2">
                        <label class="label">До:</label>
                        <label class="input"><input type="text" name="filter[data_to]" class="datepicker" data-required value="<?echo $this->filter['data_to']?>"></label>
                    </section>
                    <section class="col col-2">
                        <label class="label">Результат:</label>
                        <label class="select">
                            <select name="filter[pp_result]"><option value="0">Все</option><?
                                if (_sizeof(PP()->pp_result_name)) foreach(PP()->pp_result_name as $id=>$name) {?><option value="<?echo $id+1?>" <?if($this->filter['pp_result']==($id+1)) echo 'selected'?>><?echo $name?></option><?}?>
                            </select>
                        </label>
                    </section>
                </div>
            </fieldset>
            <?$this->panel_buttons();?>
        </form>
        </div>
        <?
    }

    function panel_info_filter()
    { $_str=array() ; $info=array() ; $title='' ;
        //if ($this->filter['member'])                        $info['механик']=ACCOUNTS()->get_member_name_by_id($this->filter['member'] )  ;
        if ($this->filter['data_from'])                       $info['c']=$this->filter['data_from'] ;
        if ($this->filter['data_to'])                         $info['по']=$this->filter['data_to'] ;
        if ($this->filter['pp_result'])                       $info['Тип результата']=$GLOBALS['pp_result'][$this->filter['pp_result']-1]  ;
        //if (_sizeof($info)) foreach($info as $title=>$value) $_str[]=$title.': <strong>'.$value.'</strong>' ;
        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='<div class=center>'.implode(' ',$_str).'</div>' ;
        return $title ;
    }

    function create_usl_select_obj($_params,&$options)
    {
//        damp_array($this->filter,1,-1) ;
        $usl=array() ;
        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['pp_result'])     $usl[]='t1.result_code='.($this->filter['pp_result']-1) ;
        $usl_res=implode(' and ',$usl) ;

        return($usl_res) ;
    }

    function get_cnt_items($usl,$options=array())
    {
        if ($usl) $usl=' where '.$usl;
        $cnt=execSQL_value('select count(t1.pkey) from obj_site_narko_data t1'.$usl,1);

        return $cnt;
    }

    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' ;
        if ($usl) $usl=' where '.$usl;
        $list_rec=execSQL('select t1.*,t2.terminal_id as mo_terminal_id,t2.terminal_pkey as mo_terminal_pkey
                        from obj_site_narko_data t1 
                            left join obj_site_mo_session t2
                            on t1.id_mo = t2.pkey
                        '.$usl.$order.$limit,$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>terminal_id</th>
            <th>id MO</th>
            <th>Время</th>
            <th>Сотрудник</th>
            <th>Результат</th>
        </tr><? ;
        $i=$this->first_obj ;
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        {
            $person=PERSONAL()->get_personal_by_id($rec['personal_id']);
            PERSONAL()->prepare_href_info_to_personal($person);
            ?><tr class=item>
                <td><?echo $i; $i++;?></td>
                <td><?echo $rec['mo_terminal_id']?></td>
                <td><a href="/cab/mo/<?echo $rec['id_mo']?>/"><?echo $rec['id_mo']?></a></td>
                <td><?echo date('d.m.Y H:i:s',$rec['c_data'])?></td>
                <td><a href="<?echo  $person['__href_window']?>" class="new-window"><?echo $person['obj_name']?></a></td>
                <td><?echo $rec['result']?></td>
            </tr><?
        }
        ?></table><?
        //damp_array($rec) ;
    }
}
?>
