<?
include_once(_DIR_EXT.'/HTML/c_page_TXT.php') ;
class c_page_ajax extends c_page_TXT
{   public $res ;
    public $form_data  ;
    public $element_tags  ;

    function show()
    { $find=$_POST['query'] ;
      if (!$find) return ;
      $this->form_data=array() ;
      $this->element_tags=$_POST['element'] ;
      if (_sizeof($_POST['form'])) foreach($_POST['form'] as $rec) $this->form_data[$rec['name']]=$rec['value'] ;

      $this->res = new stdClass() ;
      $this->res->query=$find ;

      switch($this->element_tags['cmd'])
      { case 'autocomplette_otdel':   $this->autocomplette_otdel($find) ; break ;
        case 'autocomplette_working': $this->autocomplette_working($find) ; break ;
        case 'autocomplette_personal_name':   $this->autocomplette_personal_name($find) ; break ;
        //case 'autocomplette_personal_name2':   $this->autocomplette_personal_name2($find) ; break ;
        case 'autocomplette_person_org_name':   $this->autocomplette_person_org_name($find) ; break ;
        case 'autocomplette_person_dep_name':   $this->autocomplette_person_dep_name($find) ; break ;
        case 'autocomplette_person_with_mo_name':   $this->autocomplette_person_with_mo_name($find) ; break ;
        case 'autocomplette_driver_name':   $this->autocomplette_driver_name($find) ; break ;
        case 'autocomplette_car_number':   $this->autocomplette_car_number($find) ; break ;
        case 'autocomplette_car_number_to':   $this->autocomplette_car_number_to($find) ; break ;
        //default:                              $this->res->suggestions[]=$this->element_tags['cmd'] ;
      }

      // удаляем дубликаты слов
      if (is_array($this->res->suggestions)) $this->res->suggestions=array_values(array_unique($this->res->suggestions)) ;
      // выдаем результат в формате JSON
      echo json_encode($this->res) ;
    }

    //  autocomplette для personal_name
    function autocomplette_personal_name($find)
    {   $usl=array() ; $usl_search=array() ;
        $usl[]='clss=211' ;
	    $find=str_replace(array('[',']',':',',','.','?','*'),'',$find) ;

        $usl_search[]='UPPER(obj_name) REGEXP UPPER("[[:<:]]'.addslashes($find).'")' ;
        $usl_search[]='UPPER(propusk) like UPPER("'.addslashes($find).'")' ;
        //$usl_search[]='UPPER(driving) like UPPER("'.addslashes($find).'")' ;
        //$usl_search[]='UPPER(tab_number) like UPPER("'.addslashes($find).'")' ;
        $usl[]='('.implode(' or ',$usl_search).')' ;

        // если пользователь не администратор, и в группе пользователя пристствуют киенты - показывать
        // сотрудников только этих клиентов
			  // это использовать только если используется модуль клиенты!!!!
        //if (MEMBER()->rol!=1)
        //{ $arr_clients=execSQL_line('select pkey from obj_site_account where parent='.MEMBER()->cur_group_id.' and clss=210 and rol=6 and enabled=1') ;
        //  if (_sizeof($arr_clients)==1) $usl[]='client_id='.$arr_clients[0];
        //  elseif (_sizeof($arr_clients)>1) $usl[]='client_id in ('.implode(',',$arr_clients).')';
        //}

        $select_usl=implode(' and ',$usl) ;


        $sql='select  pkey,obj_name from obj_site_personals where '.$select_usl.' limit 10' ;
       //echo $sql ;
       $list_recs=execSQL($sql) ;
        $this->res->sql=$sql ;

       //$list_recs=execSQL('select  pkey,obj_name from obj_site_personals where clss=211 and ((obj_name) rlike ("%'.addslashes($find).'%") or (propusk) rlike ("'.addslashes($find).'") or (driving) rlike ("'.addslashes($find).'") or (tab_number) rlike ("'.addslashes($find).'")) limit 10') ;
       if (_sizeof($list_recs)) foreach($list_recs as $rec) $this->res->suggestions[]=$rec['obj_name'] ;
    }

    //  autocomplette для personal_name
    function autocomplette_personal_name2($find)
    {   $find=str_replace(array('[',']',':',',','.','?','*'),'',$find) ;
    	$sql='select  pkey,obj_name,parent from obj_site_personals where clss=211 and '.
            //'UPPER(obj_name) REGEXP UPPER("[[:<:]]'.addslashes($find).'") '.   // поиск по началу слова, без учета регистра
            'UPPER(obj_name) like UPPER("'.addslashes($find).'%") '.   // поиск по началу слова, без учета регистра
            ' limit 10' ;
       //echo $sql ;
       $list_recs=execSQL($sql) ;
        $this->res->sql=$sql ;

       //$list_recs=execSQL('select  pkey,obj_name from obj_site_personals where clss=211 and ((obj_name) rlike ("%'.addslashes($find).'%") or (propusk) rlike ("'.addslashes($find).'") or (driving) rlike ("'.addslashes($find).'") or (tab_number) rlike ("'.addslashes($find).'")) limit 10') ;
       if (_sizeof($list_recs))
           foreach($list_recs as $rec)
               $this->res->suggestions[]=$rec['obj_name'].'/'.$rec['pkey'].'/'.
                       htmlspecialchars(PERSONAL()->tree[PERSONAL()->tree[$rec['parent']]->parent]->name). '/'.
                       htmlspecialchars(PERSONAL()->tree[$rec['parent']]->name).'/'.
                       PERSONAL()->tree[$rec['parent']]->parent.'/'.$rec['parent'] ;
    }

    //  autocomplette для working
    function autocomplette_working($find)
    {  // 32 - id спика "Должности компании"
       $list_recs=execSQL('select id,obj_name from '.TM_LIST.' where clss=20 and parent='._IL('IL_works')->list_id.' and enabled=1 and UPPER(obj_name) rlike UPPER("'.$find.'") order by obj_name limit 10') ;
       if (_sizeof($list_recs)) foreach($list_recs as $rec) $this->res->suggestions[]=$rec['obj_name'] ;
    }

    function autocomplette_otdel($find)
    {  if (!$this->form_data['org_id']) return ;
      $list_recs=execSQL('select pkey,obj_name from '.PERSONAL()->table_name.' where clss=208 and parent='.$this->form_data['org_id'].' and enabled=1 and UPPER(obj_name) rlike UPPER("'.$find.'") order by obj_name limit 10') ;
      if (_sizeof($list_recs)) foreach($list_recs as $rec) $this->res->suggestions[]=$rec['obj_name'] ;

    }

    function autocomplette_person_org_name($find)
    { $list_recs=execSQL_line('select distinct(obj_name) from '.PERSONAL()->table_name.' where clss=207 and enabled=1 and UPPER(obj_name) rlike UPPER("'.addslashes($find).'") order by obj_name limit 50') ;
      if (_sizeof($list_recs)) foreach($list_recs as $name) $this->res->suggestions[]=htmlspecialchars($name) ;

    }

    function autocomplette_person_dep_name($find)
    { $list_recs=execSQL_line('select distinct(obj_name) from '.PERSONAL()->table_name.' where clss=208 and enabled=1 and UPPER(obj_name) rlike UPPER("'.addslashes($find).'") order by obj_name limit 10') ;
      if (_sizeof($list_recs)) foreach($list_recs as $name) $this->res->suggestions[]=htmlspecialchars($name) ;

    }

    function autocomplette_person_with_mo_name($find)
    {
        $usl=[];

        $usl[]='t1.c_data>'.(time()-60*60*$GLOBALS['LS_doctor_workspace_name_filter_hour_before']);

        $find=str_replace(array('[',']',':',',','.','?','*'),'',$find) ;
        $usl[]='UPPER(t2.obj_name) REGEXP UPPER("[[:<:]]'.addslashes($find).'")' ;

        //Поиск по группе сотрудника
        if (MEMBER()->cur_group_id and MEMBER()->cur_group_id!=1) $usl[]='t1.group_id='.MEMBER()->cur_group_id;

        $select_usl=implode(' and ',$usl) ;

        $sql='
            select t1.member_id,t2.obj_name
                from obj_site_mo_session t1
                     left join obj_site_personals t2
                           on t2.pkey=t1.member_id and t2.clss=211
                where '.$select_usl.'
                group by t1.member_id 
                limit 10
        ';
        $list_recs=execSQL($sql) ;
        $this->res->sql=$sql ;

        if (_sizeof($list_recs)) foreach($list_recs as $rec) $this->res->suggestions[]=$rec['obj_name'] ;
    }

	//  autocomplette для personal_name
	    function autocomplette_driver_name($find)
	    {   $usl=array() ; $usl_search=array() ;
		    $find=str_replace(array('[',']',':',',','.','?','*'),'',$find) ;

	        $usl_search[]='UPPER(obj_name) REGEXP UPPER("[[:<:]]'.addslashes($find).'")' ;
	        $usl_search[]='uid_1c!=""' ;
		    $usl_search[]='skd_id>0 and not skd_id is null' ;
	        //$usl_search[]='UPPER(propusk) like UPPER("'.addslashes($find).'")' ;
	        //$usl_search[]='UPPER(driving) like UPPER("'.addslashes($find).'")' ;
	        //$usl_search[]='UPPER(tab_number) like UPPER("'.addslashes($find).'")' ;
	        $usl[]='('.implode(' and ',$usl_search).')' ;

	        $select_usl=implode(' and ',$usl) ;


	        $sql='select  pkey,obj_name,dr,skd_id from drivers where '.$select_usl.' limit 10' ;
	       //echo $sql ;
	       $list_recs=execSQL($sql) ;
	        $this->res->sql=$sql ;

	       //$list_recs=execSQL('select  pkey,obj_name from obj_site_personals where clss=211 and ((obj_name) rlike ("%'.addslashes($find).'%") or (propusk) rlike ("'.addslashes($find).'") or (driving) rlike ("'.addslashes($find).'") or (tab_number) rlike ("'.addslashes($find).'")) limit 10') ;
	       if (_sizeof($list_recs)) foreach($list_recs as $rec) $this->res->suggestions[]=$rec['obj_name'].', '.$rec['dr'].', '.$rec['skd_id'] ;
	    }

	    function autocomplette_car_number($find)
	    {   $usl=array() ; $usl_search=array() ;
		    $find=str_replace(array('[',']',':',',','.','?','*'),'',$find) ;

	        $usl_search[]='UPPER(obj_name) REGEXP UPPER("[[:<:]]'.addslashes($find).'")' ;
		    $usl_search[]='uid_1c!=""' ;
		    $usl_search[]='skd_id>0 and not skd_id is null' ;
	        //$usl_search[]='UPPER(propusk) like UPPER("'.addslashes($find).'")' ;
	        //$usl_search[]='UPPER(driving) like UPPER("'.addslashes($find).'")' ;
	        //$usl_search[]='UPPER(tab_number) like UPPER("'.addslashes($find).'")' ;
	        $usl[]='('.implode(' and ',$usl_search).')' ;

	        $select_usl=implode(' and ',$usl) ;


	        $sql='select  pkey,obj_name,model from obj_site_cars where '.$select_usl.' limit 10' ;
	       //echo $sql ;
	       $list_recs=execSQL($sql) ;
	        $this->res->sql=$sql ;

	       //$list_recs=execSQL('select  pkey,obj_name from obj_site_personals where clss=211 and ((obj_name) rlike ("%'.addslashes($find).'%") or (propusk) rlike ("'.addslashes($find).'") or (driving) rlike ("'.addslashes($find).'") or (tab_number) rlike ("'.addslashes($find).'")) limit 10') ;
	       if (_sizeof($list_recs)) foreach($list_recs as $rec) $this->res->suggestions[]=$rec['obj_name'].', '.$rec['model'] ;
	    }

	    function autocomplette_car_number_to($find)
        {
            $usl=array() ; $usl_search=array() ;
            $find=str_replace(array('[',']',':',',','.','?','*'),'',$find) ;

            $usl_search[]='UPPER(obj_name) REGEXP UPPER("'.addslashes($find).'")' ;
            $usl_search[]='enabled=1' ;

            $usl[]='('.implode(' and ',$usl_search).')' ;

            $select_usl=implode(' and ',$usl) ;


            $sql='select  pkey,obj_name,model from obj_site_personals_cars where '.$select_usl.' limit 10' ;
            //echo $sql ;
            $list_recs=execSQL($sql) ;
            $this->res->sql=$sql ;

            if (_sizeof($list_recs)) foreach($list_recs as $rec) $this->res->suggestions[]=$rec['obj_name'] ;
        }



}
