<?
// вниамение!!!
// во всех фильтрах:
// data_from, data_to - дата в текстовом формате
// time_from, time_to - дата в UNIX формате

class report_lamp_in_hand extends c_report
{
    var $title='Выданные лампы' ;
    var $view_only_model=1 ;
    var $view_by_space_filter=1 ;

    function setting($options=array())
    {
        $arr_groups=ACCOUNTS()->get_arr_groups_to_cur_account() ;
        $account_recs=mine()->get_lamp_accounts();

        ?><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_select('group_id',$GLOBALS['LS_person_group_title'].':',2,$arr_groups) ;
                        $this->filter_section_select('account_id','Ламповщик:',2,$account_recs) ;
                        $this->filter_section_input_text('name','ФИО:',3,array('auto_complete'=>"autocomplette_personal_name")) ;
                        $this->filter_section_input_text('lamp_number','Номер лампы:',2) ;
                        ?>
                    </div>
                </fieldset>
                <?if ($options['panel_button_apply']) $this->panel_button_apply(); else $this->panel_buttons();?>
            </form>
         </div>
        <?
    }

    function panel_info_filter()
    {
        $_str=array() ; $info=array() ; $title='' ;
        if ($this->filter['name'])                            $info['ФИО']='"'.$this->filter['name'].'"' ;
        if ($this->filter['lamp_number'])                     $info['Номер лампы']='"'.$this->filter['lamp_number'].'"' ;
        if ($this->filter['data_from'])                       $info['c']=$this->filter['data_from'] ;
        if ($this->filter['data_to'])                         $info['по']=$this->filter['data_to'] ;
        if ($this->filter['group_id'])                        $info['Группа']=ACCOUNTS()->get_member_name_by_id($this->filter['group_id'])  ;
        if ($this->filter['account_id'])                      $info['Ламповщик']=ACCOUNTS()->get_member_name_by_id($this->filter['account_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>' ;
        $this->report_info=$info ;
        return $title ;
    }


    function prepare_data() {
        $_usl=[]; $usl='';
        $_usl[]='t1.status=1';
        if ($this->filter['group_id']) $_usl[]='t1.group_id='.$this->filter['group_id'];
        if ($this->filter['account_id']) $_usl[]='t2.account_id='.$this->filter['account_id'];
        if ($this->filter['name']) {
            $_usl[]='t3.obj_name like "%'.addslashes($this->filter['name']).'%"';
        }
        if ($this->filter['lamp_number']) $_usl[]='t1.obj_name='.$this->filter['lamp_number'];

        if (_sizeof($_usl)) $usl=implode(' and ',$_usl);

        if ($this->filter['order_by']) switch ($this->filter['order_by']){
            case 'number_up':      $order = ' order by number_lamp,'; break;
            case 'number_down':    $order = ' order by number_lamp desc,'; break;
            case 'data_up':      $order = ' order by t2.c_data,'; break;
            case 'data_down':    $order = ' order by t2.c_data desc,'; break;
            case 'fio_up':      $order = ' order by fio,'; break;
            case 'fio_down':    $order = ' order by fio desc,'; break;
        }
        if (!$order) $order = ' order by ';
        if ($this->filter['order']) $order.=$this->filter['order']; else $order.='t1.last_event_id desc';

        //переводим строку в число t1.obj_name+0 as number_lamp для корректной фильтрации по номеру лампы
        $recs=execSQL('select t1.pkey,t1.obj_name,t1.group_id,t1.status,t2.c_data,t2.account_id,t2.personal_id, t1.obj_name+0 as number_lamp,t3.obj_name as fio
                       from obj_site_rml_lamp t1
                       left join obj_site_rml_lamp_events t2
                            on t2.pkey=t1.last_event_id
                       left join obj_site_personals t3
                            on t2.personal_id=t3.pkey
                       where '.$usl.$order);

        return $recs;
    }

    function get_filter() {
        $usl='';
        foreach ($this->filter as $key=>$value) {
            if ($key != 'order_by') $usl.=','.$key.'='.$value;
        }
        return $usl;
    }

    function HTML_report_body($options=array()) {
        if (!$GLOBALS['LS_lamp_counting']) return;
        $list_recs = $this->prepare_data();

        ?>
        <div id="reports_mo">
          <table class="basic fz_small full auto"  <?if ($options['rml_view']) {?>style="font-size: 20px;"<?}?>>
              <tr class="<?if ($options['rml_view']) echo 'class=small'; else echo 'big';?>">
                  <th>№</th>
                  <th>
                      Номер лампы<?if ($options['rml_view']) echo '<br>';?> <? if ($this->filter['order_by']=='number_down') {?> <a class="filter" href="<?echo _CUR_REPORT_DIR?>[order_by=number_up<?if ($this->filter) echo $this->get_filter()?>]/"><i class="fas fa-sort-up <?if ($this->filter['order_by']=='number_down') echo 'active';?>"></i></a> <?} else {?> <a class="filter" href="<?echo _CUR_REPORT_DIR?>[order_by=number_down<?if ($this->filter) echo $this->get_filter()?>]/"><i class="fas fa-sort-down <?if ($this->filter['order_by']=='number_up') echo 'active';?>"></i><?}?>
                  </th>
                  <th>
                      Дата<?if ($options['rml_view']) echo '<br>';?> <? if ($this->filter['order_by']=='data_down') {?> <a class="filter" href="<?echo _CUR_REPORT_DIR?>[order_by=data_up<?if ($this->filter) echo $this->get_filter()?>]/"><i class="fas fa-sort-up <?if ($this->filter['order_by']=='data_down') echo 'active';?>"></i></a> <?} else {?> <a class="filter" href="<?echo _CUR_REPORT_DIR?>[order_by=data_down<?if ($this->filter) echo $this->get_filter()?>]/"><i class="fas fa-sort-down <?if ($this->filter['order_by']=='data_up') echo 'active';?>"></i><?}?>
                  </th>
                  <?
                  if (!$options['rml_view']) {
                      ?>
                      <th>Группа</th>
                      <th>Тип</th>
                      <th>Ламповщик</th>
                      <?
                  }
                  ?>
                  <th>
                      Сотрудник<?if ($options['rml_view']) echo '<br>';?> <? if ($this->filter['order_by']=='fio_down') {?> <a class="filter" href="<?echo _CUR_REPORT_DIR?>[order_by=fio_up<?if ($this->filter) echo $this->get_filter()?>]/"><i class="fas fa-sort-up <?if ($this->filter['order_by']=='fio_down') echo 'active';?>"></i></a> <?} else {?> <a class="filter" href="<?echo _CUR_REPORT_DIR?>[order_by=fio_down<?if ($this->filter) echo $this->get_filter()?>]/"><i class="fas fa-sort-down <?if ($this->filter['order_by']=='fio_up') echo 'active';?>"></i><?}?>
                  </th>
                  <?
                  if ($options['rml_view'] and $GLOBALS['LS_lamp_show_accept_button_rml']) {
                      ?>
                      <th>Действия</th>
                      <?
                  }
                  ?>
              </tr>
              <?
              $i=1 ;
              if (_sizeof($list_recs)) foreach($list_recs as $rec)
              {
                  if (!isset($recs_account[$rec['account_id']])) $recs_account[$rec['account_id']]=ACCOUNTS()->get_member_name_by_id($rec['account_id']) ;
                  ?>
                  <tr class="item <?if (time()-$rec['c_data']>60*60*8) echo 'red_backgroup'?>" id="<?echo 'lamp_'.$rec['pkey']?>">
                      <td><?echo $i; $i++?></td>
                      <td><?echo $rec['obj_name']?></td>
                      <td><?echo date('d.m.Y H:i:s',$rec['c_data'])?></td>
                      <?
                      if (!$options['rml_view']) {
                          ?>
                          <td><?echo ACCOUNTS()->get_group_name($rec['group_id'])?></td>
                          <td><?echo $_SESSION['ARR_lamp_events'][$rec['status']]?></td>
                          <td><?echo $recs_account[$rec['account_id']]?></td>
                          <?
                      }
                      ?>
                      <td><?echo $rec['fio'];/*$recs_personals[$rec['personal_id']]['obj_name']*/?></td>
                      <?
                      if ($options['rml_view'] and $GLOBALS['LS_lamp_show_accept_button_rml']) {
                          ?>
                          <td>
                              <button class="v2" lamp_id="<?echo $rec['pkey']?>" personal_id="<?echo $rec['personal_id']?>" cmd="aspmo_rml/accept_lamp_via_id" onclick="javascript:$j('tr#lamp_<? echo $rec['pkey']?>')[0].hide();">Возврат</button>
                          </td>
                          <?
                      }
                      ?>
                  </tr>
                  <?
              }
              ?>
          </table>
        </div>
        <style type="text/css">
            #reports_mo table tr.red_backgroup {background-color: #ffbbbb}
            table.basic .fas{font-size:20px;margin:0 5px;color:black}
            table.basic .big a.filter{position:relative;}
            table.basic .big .fas{font-size:20px;margin:0 5px;position:absolute;color:black}
            table.basic .fas.active{color:#2a679e}
            table.basic .fas.fa-sort-up{top:2px;}
            table.basic .fas.fa-sort-down{bottom:2px;}
        </style>
        <?
    }


}
?>
