<?
include_once(_DIR_EXT.'/HTML/c_page_XML_AJAX.php') ;
class c_page_ajax extends c_page_XML_AJAX
{
    function search_cars()
      {  //$this->add_element('show_modal_window','HTML') ;
         //damp_array($_POST,1,-1) ;
         $car_ids='' ; $options=array() ;

         if ($_POST['search_text'])
         { $arr_ids=PERSONAL()->search_cars($_POST['search_text']) ;
           if (_sizeof($arr_ids)) $options['car_id']=implode(',',$arr_ids) ;
           else                  $options['car_id']=-1 ;
           $options['cars_id_search']=$_POST['search_text'] ;
         }

         $options['client_id']=$_POST['client_id'] ;
         $options['view_ext']=$_POST['view_ext'] ;
         if ($_SESSION['list_cars_filter_org'])  $options['org_id']=$_SESSION['list_cars_filter_org'] ;
         if ($_SESSION['list_cars_filter_park']) $options['park_id']=$_SESSION['list_cars_filter_park'] ;
         if ($car_ids)                         $options['car_id']=$car_ids ;

         ob_start() ;
           include_once(_DIR_EXT.'/aspmo_client/panel_list_cars.php') ;
           $cnt=panel_list_cars($options) ;
         $this->add_element('panel_list_cars.HTML',ob_get_clean()) ;

         if ($cnt) $this->add_element('show_notife','Найдено '.$cnt.' автомобилей',array('type'=>'info')) ;
         else      $this->add_element('show_notife','Автомобилей не найдено',array('type'=>'info')) ;
      }

      function change_car_sort()
      {  $_SESSION['list_cars_sort_mode']=$_POST['sort_mode'] ;
         $this->add_element('show_notife','Сортировка изменена',array('type'=>'info')) ;
         $this->search_cars() ;
      }

      function set_filter_park()
      {  $_SESSION['list_cars_filter_park']=($_POST['value']==-1)? 0:$_POST['value'] ;
         $this->search_cars() ;
      }

      function set_filter_org()
      {  $_SESSION['list_cars_filter_org']=($_POST['value']==-1)? 0:$_POST['value'] ;
         $this->search_cars() ;
      }



  //==========================================================================================================================================================================
  // Работа с сотрудниками предприятия
  //==========================================================================================================================================================================

 function get_panel_car_create()
 { include(_DIR_EXT.'/aspmo_car/panel_car_edit.php') ;
   if (!$_POST['to_extlist']) { panel_car_edit(array('save_cmd'=>'car_create','to_extlist'=>0)) ; $title='Добавление нового сотрудника' ; }
   else                       { panel_car_edit(array('save_cmd'=>'car_create','to_extlist'=>1)) ; $title='Добавление в дополнительный список' ; }
   $this->add_element('show_modal_window','HTML',array('title'=>$title)) ;
 }

 function get_panel_car_edit_info()
 {

   $rec_car=PERSONAL()->get_car_by_id($_POST['reffer']) ;
   if ($rec_car['pkey'])
   { include(_DIR_EXT.'/aspmo_car/panel_car_edit.php') ;
     panel_car_edit(array('rec'=>$rec_car,'save_cmd'=>'car_save')) ;
     $this->add_element('show_modal_window','HTML',array('title'=>'Редактирование данных сотрудника')) ; ;
   }
   else $this->add_element('show_notife','Сотрудник не найден или нет прав доступа',array('type'=>'error')) ;
 }

 function car_create()
 {
   $reffer=ESMO()->car_create($_POST) ;
   $this->add_element('close_mBox_window','all') ;
   if ($reffer)     { $rec_car=PERSONAL()->get_car_by_id($reffer) ;
                      switch($_POST['after_create'])
                       { case 'list': if (!$_POST['to_extlist']) $this->add_element('go_url','/cab/personal/') ;
                                      else                       $this->add_element('go_url','/cab/personal/extlist/') ;
                                      $_SESSION['after_car_create']='list' ;
                                      break ;
                         case 'card': ob_start() ;
                                      ?><script type="text/javascript">var newWin=window.open("<?echo $rec_car['__href']?>",'win_second','status=1,dependent=1,width=1920,height=1000,left=1920,top=200, resizable=1, scrollbars=yes') ;newWin.focus() ;</script><?
                                      $_SESSION['after_car_create']='' ;
                                      $this->add_element('JSCODE',ob_get_clean()) ;
                                      break ;
                         case 'photo': if ($rec_car['pkey'])
                                        { include(_DIR_EXT.'/webcamera_capture/panel_photo_capture.php') ;
                                          panel_photo_capture(array('title'=>'Фото сотрудника','save_cmd'=>'save_car_photo','rec'=>$rec_car)) ;
                                          $this->add_element('show_modal_window','HTML',array('width'=>'700px','after_close_func'=>'stop_video_capture')) ;
                                        }
                                       $_SESSION['after_car_create']='photo' ;
                                       break ;
                       }
                    }
    else $this->add_element('show_notife','Не удалось добавить нового сотрудника',array('type'=>'error')) ;

 }

 function car_save()
 { if (!is_member_right('car_change_propusk')) {unset($_POST['propusk']) ; $this->add_element('show_notife','Нет прав для измерения номера пропуска',array('type'=>'alert')) ; }
   $res=ESMO()->car_save($_POST['reffer'],$_POST) ;
   //$this->add_element('success','modal_window') ; ; return ;

   if ($res['update'])                       { //$this->add_element('show_notife','Сохранено',array('type'=>'info')) ;
                                               $this->update_page=1 ;
                                             }
   else if($res['found'] and !$res['update'])  { $this->add_element('show_notife','Изменений не произведено',array('type'=>'info')) ; $this->add_element('close_mBox_window','all') ; }
   else                                        { $this->add_element('show_notife','Не удалось сохранить изменения',array('type'=>'info')) ; $this->add_element('close_mBox_window','all') ; }
 }


 function add_auto_from_drivers()
 {  // получаем всех водителей клиента
   $_usl=array() ; $view_client=1 ; $title='' ; $arr_title=array() ; $cnt=0 ;
   $_usl[]='clss=211 and enabled=1' ;
   $ids_orgs=execSQL_line('select pkey from obj_site_cars where clss=207 and enabled=1 and client_id='.MEMBER()->id) ;
   $main_org=$ids_orgs[0]  ;
   if (_sizeof($ids_orgs)) foreach($ids_orgs as $org_id) $arr_parens_ids[]=PERSONAL()->tree[$org_id]->get_list_child() ;
   if (_sizeof($arr_parens_ids)) $_usl[]='parent in ('.implode(',',$arr_parens_ids).')' ;
   $usl=implode(' and ',$_usl) ;
   //echo $usl ;
   $this->add_element('show_modal_window','HTML') ;
   $recs=execSQL('select * from obj_site_cars where '.$usl.'') ;
   if (_sizeof($recs)) foreach($recs as $rec)
   { $info=unserialize($rec['ext_info']) ;
     //damp_array($info) ;
     $car_number=$info['CarDetails']['CarNumber'] ;
     $car_number=trim(str_replace(' ','',$car_number)) ;
     if ($car_number)
     {   echo $car_number ;
         $car_id=execSQL_value('select pkey from obj_site_cars_cars where obj_name like "'.$car_number.'"') ;
         if (!$car_id)
         {   adding_rec_to_table('obj_site_cars_cars',array('parent'=>$main_org,
                                                                 'clss'=>361,
                                                                 'obj_name'=>$car_number,
                                                                 'model'=>$info['CarDetails']['Marka'].' '.$info['CarDetails']['Model'],
                                                                 'e_osago'=>time()+60*60*24*30*6,
                                                                 'e_sts'=>time()+60*60*24*30*10,
                                                                 'e_to'=>time()+60*60*24*30*12,
                                                                 'ext_info'=>array('Age'=>$info['CarDetails']['Age'],
                                                                                   'Color'=>$info['CarDetails']['Color'],
                                                                                   'Uuid'=>$info['CarDetails']['Uuid'],
                                                                                   'Require'=>$info['CarDetails']['Require']
                                                                                 )
                                                                 )
                                 ) ;

             echo ' - <span class="green">Авто добавлено</span><br>' ;
         }
         else echo ' - <span class="red">Авто уже добавлено</span><br>' ;
     }
   }
   //damp_array($recs) ;


 }



    function send_post_data()
    {  ob_start() ;
        $arr_url=parse_url($_POST['server_url']) ;
        ?><h2>Отправлено на шлюз</h2><?
        echo '<strong>IP отправителя</strong>:'.$_SERVER['REMOTE_ADDR'].'<br>' ;
        echo '<strong>URL  получателя</strong>:'.$_POST['server_url'].'<br>' ;
        echo '<strong>POST DATA</strong>:'.$_POST['cont'].'<br>' ;
        ?><h2>Ответ шлюза</h2><?
        $result=IO()->doPut_v2(_MAIN_PROTOCOL,$arr_url['host'],$arr_url['path'],$arr_url['query'],$_POST['cont']) ;
        echo nl2br($result['content']) ;
       $text=ob_get_clean() ;
       $this->add_element('panel_debug.HTML',$text) ;
    }














}
