<?php
class report_pl_clients extends c_report
{
 var $title='Справочник клиентов ПЛ' ;
 var $view_by_space_filter=1 ;
 var $allow_org_ids=1 ;
 var $usl_select_org='' ;
 var $allow_export_to_csv=1 ;
 var $group_ids=0 ;
 var $arr_status=[] ;

 function setting($options=array())
 {   ?><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('obj_name','Наименование:',2,array('_auto_complete'=>"autocomplette_personal_name")) ;
                             $this->filter_section_input_text('email','Email:',2) ;
                             $this->filter_section_input_text('phone','Телефон:',2) ;
                             $this->filter_section_input_text('inn','ИНН:',2) ;
                             $this->filter_section_input_text('address','Адрес:',2) ;
                          ?>
                        </div>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }

  function create_usl_select_obj($_params=array(),&$options=array())
  {  //damp_array($this->filter,1,-1) ;
     $usl=array() ;
     $usl[]='true' ;
     //damp_array($this->arr_status);
     if (_sizeof($this->arr_status)) $usl[]='status in ('.implode(',',$this->arr_status).')' ;
     //$usl[]='skd_id>0' ;
     if ($this->filter['obj_name'])          $usl[]='t1.obj_name like "%'.$this->filter['obj_name'].'%"' ;
     if ($this->filter['inn'])           $usl[]='t1.inn="'.$this->filter['inn'].'"' ;
     if ($this->filter['email'])           $usl[]='t1.email like "%'.$this->filter['email'].'%"' ;
     if ($this->filter['phone'])           $usl[]='t1.phone like "%'.$this->filter['phone'].'%"' ;
     if ($this->filter['address'])           $usl[]='t1.client_info like "%'.$this->filter['address'].'%"' ;
     $usl_res=implode(' and ',$usl) ;
     //damp_array($usl,1,-1) ;
     return($usl_res) ;
  }

 function get_arr_info_filters()
 { $info=parent::get_arr_info_filters() ;
   if ($this->filter['obj_name'])                 $info['Наименование']=$this->filter['obj_name'] ;
   if ($this->filter['inn'])                 $info['ИНН']=$this->filter['inn'] ;
   if ($this->filter['email'])                 $info['Email']=$this->filter['email'] ;
   if ($this->filter['phone'])                 $info['Телефон']=$this->filter['phone'] ;
   if ($this->filter['address'])                 $info['Адрес']=$this->filter['address'] ;
   return $info ;
 }

  function get_cnt_items($usl,$options=array())
  {  $cnt=execSQL_value('select count(pkey)
                         from  fr_clients t1
                         where '.$usl,$options) ;
    return($cnt) ;
  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
    $order=' order by t1.pkey desc' ;
    //$options['debug']=1 ;
// 	$list_rec=execSQL('select *
//                       from  clients
//                       where '.$usl.$order.$limit,$options) ;
      $list_rec=execSQL('select t1.*, t2.obj_name as ext_info 
                       from  fr_clients t1
                       left join fr_clients_ext_info t2 on t2.client_id=t1.pkey
                       where '.$usl.$order.$limit,$options) ;
    if (_sizeof($list_rec)) foreach($list_rec as $id=>$rec) {
        PL()->prepare_public_info_to_client($list_rec[$id]) ;
        PL()->prepare_ext_info_client($list_rec[$id]);
    }
    return($list_rec) ;
  }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  { $id=($options['id'])? 'id='.$options['id']:'' ;


    ?><table class="basic fz_small full auto" <?echo $id?>>
        <tr><th>ID</th><th>Статус</th><th>Клиент</th><th>ИНН</th><th>Email</th><th>Телефон</th><th></th></tr><?
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        {   ?><tr class=item>
                <td><?echo $rec['pkey']?></td>
                <td>
                    <label class="c-switch c-switch-label c-switch-pill c-switch-success" style="margin-bottom:0;">
                        <input class="c-switch-input v2" type="checkbox" <?if ($rec['enabled'] and $rec['_active_accounts']) echo 'checked=""' ; ?> cmd="pl/client_activate" client_id="<?echo $rec['pkey']?>">
                        <span class="c-switch-slider" data-checked="On" data-unchecked="Off"></span>
                    </label>
                </td>
                <td class="left"><a href="/window/pl/clients/<?echo $rec['pkey']?>/" class="new_window"><?echo $rec['_ext_info']['dadata']['suggestions'][0]['data']['name']['short_with_opf']?></a>
                    <br><?echo $rec['_ext_info']['dadata']['suggestions'][0]['data']['address']['unrestricted_value']?>
                </td>
                <td class="dr"><?echo $rec['inn']?></td>
                <td class="dr"><?echo $rec['email']?></td>
                <td class="dr"><?echo $rec['phone']?></td>
                <td><button class="v1" href="/cab/pl/clients/card/<?echo $rec['pkey']?>/">Карточка клиента</button></td>

            </tr><?
        }
      ?></table><?
  }

}

?>
