<?

class report_previous_to extends c_report
{
    var $title='Предыдущие проверки ТО' ;
    var $view_only_model=1 ;
    var $mo_sort='pkey' ;
    public $view_by_space_filter=1 ;
    var $table_name = 'obj_site_cars_to';

    function init($options=array())
    {
        if (!$this->filter['group_id']) $this->filter['group_id']=MEMBER()->cur_group_id ;
    }

    function setting()
    {
        $arr_accounts=ACCOUNTS()->get_list_members(array('group_id'=>MEMBER()->cur_group_id,'rol'=>'1,5')) ; ?>
        <div id="panel_filter">
            <form id="sky-form" class="sky-form">
                <input type="hidden" name="filter_dir" value="<?echo _CUR_REPORT_DIR?>">
                <input type="hidden" name="report_code" value="<?echo 'report_previous_to';?>">
                <fieldset>
                    <div class="row">
                        <?
                        $this->filter_section_input_text('name','ФИО водителя:',4,array('auto_complete'=>"autocomplette_personal_name")) ;
                        $this->filter_section_input_text('car','Гос.номер автомобиля:',2,array('auto_complete'=>"autocomplette_car_number_to")) ;
                        ?>
                    </div>
                    <div class="row">
                        <?
                        $this->filter_section_select('engineer', 'Допустил:', 2, $arr_accounts, array('space_element' => 1));
                        $this->filter_section_input_text('data_from','От:',2,array('class'=>'datetimepicker')) ;
                        $this->filter_section_input_text('data_to','До:',2,array('class'=>'datetimepicker')) ;
                        ?>
                    </div>
                    <div class="row">
                        <section class="col col-3">
                            <label class="label">Направление:</label>
                            <label class="radio"><input type="radio" name="filter[direction]" value="0" <?if (!$this->filter['direction']) echo 'checked'?>><i></i>Не важно</label>
                            <label class="radio"><input type="radio" name="filter[direction]" value="entry" <?if ($this->filter['direction']=='entry') echo 'checked'?>><i></i>Въезд</label>
                            <label class="radio"><input type="radio" name="filter[direction]" value="exit" <?if ($this->filter['direction']=='exit') echo 'checked'?>><i></i>Выезд</label>
                        </section>
                    </div>
                </fieldset>
                <?$this->panel_buttons();?>
            </form>
        </div>
        <style>
            @media (max-width: 1400px) {
                .sky-form .col-2 {
                    width: 33.33%; /*col-4*/
                }
                .sky-form .col-4 {
                    width: 66.67%; /*col-8*/
                }
            }
        </style>
        <?
    }

    function panel_info_filter()
    {
        $_str=array() ; $info=array() ; $title='' ;

        if ($this->filter['name'])                            $info['Водитель']='"'.$this->filter['name'].'"' ;
        if ($this->filter['engineer'])                        $info['Допустил']=ACCOUNTS()->get_member_name_by_id($this->filter['engineer']);
        if ($this->filter['car'])                             $info['Автомобиль']='"'.$this->filter['car'].'"' ;
        if ($this->filter['direction'])                       if ($this->filter['direction']=='entry') $info['Направление']='"Въезд"' ; else $info['Направление']='"Выезд"' ;
        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 (_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[]='t1.clss=360';
        if ($this->filter['name'])
        {   $arr_ids=execSQL_line('select pkey from obj_site_personals where clss=211 and obj_name like "%'.addslashes($this->filter['name']).'%"') ;
            if (_sizeof($arr_ids))  $_usl[]='t1.personal_id in ('.implode(',',$arr_ids).')' ;
            else                   $_usl[]='t1.personal_id=-1' ;
        }
        if ($this->filter['engineer'])
        {   $arr_ids=execSQL_line('select pkey from obj_site_account where clss=210 and pkey='.$this->filter['engineer']) ;
            if (_sizeof($arr_ids))  $_usl[]='t1.member_id in ('.implode(',',$arr_ids).')' ;
            else                   $_usl[]='t1.member_id=-1' ;
        }
        if ($this->filter['car'])       $_usl[]='t1.car_number="'.$this->filter['car'].'"';
        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['direction']) {
            if ($this->filter['direction']=='entry') $_usl[]='t1.access_type=0';
            else $_usl[]='t1.access_type=1';
        }

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

    function get_cnt_items($usl,$options=array())
    {
        $options['debug']=0 ;
        $this->cnt_items=execSQL_value('select count(t1.pkey) from '.$this->table_name.' t1
                   where '.$usl,$options) ;
        return($this->cnt_items) ;
    }

    function get_items($usl,$options=array())
    {
        $limit=($options['limit'])? ' '.$options['limit']:'' ;
        $order=($this->filter['skoro'])? ' order by pkey ':' order by pkey desc' ;
        if ($options['order']) $order=' order by '.$options['order'] ;
        $options['debug']=0 ;
        $recs=execSQL('select t1.*,t2.obj_name as engineer from '.$this->table_name.' t1
                       left join '.ACCOUNTS()->table_account.' t2 on t2.pkey=t1.member_id
                       where '.$usl.$order.$limit,$options) ;
        $all_information = $this->get_dop_information($recs);
        return($all_information) ;
    }

    function get_dop_information($recs)
    {
        $arr_mo=array() ;  $recs_signatures=array() ;
        $arr_personal=array() ;  $recs_drivings=array() ;
        $arr_member=array() ;  $recs_members=array() ;
        $arr_car=array() ;  $recs_cars=array() ;
        if (_sizeof($recs)) foreach($recs as $rec)
        {  if ($rec['member_id']) $arr_member[$rec['member_id']]=1 ;
            if ($rec['doctor_id']) $arr_member[$rec['doctor_id']]=1 ;
            $arr_personal[$rec['personal_id']]=1 ;
            $arr_car[$rec['car_id']]=1 ;
            if ($rec['mo_id']) $arr_mo[$rec['mo_id']]=1 ;
        }
        $debug=0 ;
        if (_sizeof($arr_mo)) $recs_signatures=execSQL_row('select parent,pkey from obj_site_mo_session_DTW where parent in ('.implode(',',array_keys($arr_mo)).')',array('debug'=>$debug)) ;
        if (_sizeof($arr_personal)) $recs_drivings=execSQL_row('select pkey,driving from '.PERSONAL()->table_personal.' where pkey in ('.implode(',',array_keys($arr_personal)).')',array('debug'=>$debug)) ;
        if (_sizeof($arr_member))   $recs_members=execSQL_row('select pkey,obj_name from '.ACCOUNTS()->table_account.' where pkey in ('.implode(',',array_keys($arr_member)).')',array('debug'=>$debug)) ;

        if (_sizeof($recs)) foreach($recs as $i=>$rec)
        { if ($rec['mo_id'] and isset($recs_signatures[$rec['mo_id']])) $recs[$i]['_personal_mo_signature']=_PATH_TO_SITE.'/services/esmo_signature/graf_signature/?mo_id='.$rec['mo_id'] ;
            //if ($rec['member_id'] and isset($recs_members[$rec['member_id']])) $recs[$i]['_member_name']=$recs_members[$rec['member_id']] ;
            if ($rec['doctor_id'] and isset($recs_members[$rec['doctor_id']])) $recs[$i]['_doctor_name']=$recs_members[$rec['doctor_id']] ;
            if ($rec['personal_id'] and isset($recs_drivings[$rec['personal_id']])) $recs[$i]['_personal_driving']=$recs_drivings[$rec['personal_id']] ;
            if ($rec['car_id'] and isset($recs_cars[$rec['car_id']])) $recs[$i]['_car_model']=$recs_cars[$rec['car_id']] ;
            $this->prepare_public_info_to_TO($recs[$i]) ;
        }
        return($recs) ;
    }

    function prepare_public_info_to_TO(&$rec,$options=array())
    {
        $rec['__href']='/cab/rmt/check/'.$rec['pkey'].'/' ;
        $cnt1=0 ; $cnt2=0 ;
        for($i=1;$i<=19;$i++) { if ($rec['i'.$i]==1) $cnt1++ ; else { $rec['i'.$i]=0 ; $cnt2++ ;} }
        $rec['_check_status']=$cnt1.' / '.$cnt2 ;
        $rec['_status']=($rec['status']==1)? 'Разрешен':'Запрещен';
        if (!$rec['_member_name']) $rec['_member_name']=ACCOUNTS()->get_member_name_by_id($rec['member_id']) ;
    }

    // шаблон - вывод ссылками без разделитея
    function print_template_HTML($list_recs,$options=array())
    {
        include_once(_DIR_EXT.'/rmt/list_previous_to.php') ;
        previous_to($list_recs,$options) ;
    }

}
?>
