<?php
include_once("c_rmt_setting.php");
class c_page_cab_rmt_setting_to_in extends c_page_cab_rmt_setting
{
  public $h1='Настройки ЭСМО - ТО - въезд' ;
  public $arr_types=array(1=>'чекбокс (да/нет)',
                          2=>'строка',
                          3=>'текстовое поле'
          );
  var $access=0 ;

 function block_main()
 {
     $this->page_title() ;

     if ($_GET['car_type']) $car_type=$_GET['car_type'];

     if (!$GLOBALS['LS_TO_use_diff_TO_car_type']) {
         $recs_params=execSQL('select * from obj_site_to_params where access='.$this->access.' and car_type is null and clss=361 order by indx') ;
         $this->panel_select_themes($recs_params) ;
     } else {
         if ($car_type) {
             if ($car_type=='null') {
                 $recs_params=execSQL('select * from obj_site_to_params where access='.$this->access.' and car_type is null and clss=361 order by indx') ;
             } else {
                 $recs_params=execSQL('select * from obj_site_to_params where access='.$this->access.' and car_type="'.$car_type.'" and clss=361 order by indx') ;
             }
             $this->panel_select_themes($recs_params,$car_type) ;
         } else {
             $this->list_to_group_params();
         }
     }
 }

 function list_to_group_params()
 {
     $TO_param_groups = execSQL('select distinct(type) from obj_site_personals_cars',0,1);
     $TO_params = execSQL('select car_type,count(pkey) as cnt from obj_site_to_params where access='.$this->access.' and car_type is not null group by car_type');
     $TO_params_default = execSQL_van('select car_type,count(pkey) as cnt from obj_site_to_params where access='.$this->access.' and car_type is null');
     ?>
     <table class="basic fz_small">
         <tr class="TO_group" car_type="null">
             <td>По умолчанию</td>
             <td>
                 <?if ($TO_params_default['cnt']) echo 'Задано '.$TO_params_default['cnt'].' параметра'; else echo 'Не заданы параметры';?>
             </td>
         </tr>
         <?
         foreach ($TO_param_groups as $TO_param_group)
         {
            ?>
             <tr class="TO_group" car_type="<?echo $TO_param_group['type']?>">
                 <td>
                     <?echo $TO_param_group['type']?>
                 </td>
                 <td>
                     <?if ($TO_params[$TO_param_group['type']]['cnt']) echo 'Задано '.$TO_params[$TO_param_group['type']]['cnt'].' параметра'; else echo 'Не заданы параметры';?>
                 </td>
             </tr>
             <?
         }
         ?>
     </table>
     <style>
         tr.TO_group {cursor: pointer;}
         tr.TO_group:hover {
             background-color: #2da5da;
             color: #fff;
         }
     </style>
     <script type="text/javascript">
         $j('tr.TO_group').click(function()
         {
             let carTypeUrl = '<?echo _CUR_PAGE_DIR?>' + '?car_type=' + $j(this).attr('car_type');
             //console.log('<?//echo _CUR_PAGE_DIR?>//' + '?car_type=' + $j(this).attr('car_type'));
             $j(location).attr('href',carTypeUrl);
         });
     </script>
     <?
 }



 function panel_select_themes($recs_params,$car_type='null')
 {
//     damp_array(execSQL('select distinct(type) from obj_site_personals_cars'));
//
//     $recs_params=execSQL('select * from obj_site_to_params where access='.$this->access.' and car_type="" and clss=361 order by indx') ;
     ?><div id="panel_filter"><form id="sky-form" class="sky-form"><?
        ?><table class="basic fz_small full auto">
            <tr class="head">
                <th rowspan="2">Выбор</th>
                <th rowspan="2">№</th>
                <th rowspan="2">Наименование</th>
                <th rowspan="2">Группа</th>
                <th rowspan="2">Тип</th>
                <th rowspan="2">Обязательный для заполнения</th>
                <th>При выборе "НЕТ"</th>
            </tr>
            <tr class="head">
                <th>Требуется фото</th>
            </tr>

         <?
         if (_sizeof($recs_params)) foreach($recs_params as $rec)
         { ?><tr>
               <td style="width:10%;"><input type="checkbox" name="_check[]" value="<?echo $rec['pkey']?>"></td>
               <td style="width:10%;" class="indx"><i class="fa fa-chevron-circle-up v2" aria-hidden="true" param_id="<?echo $rec['pkey']?>" cmd="rmt/TO_param_up"></i>&nbsp;
                                                   <span id="param_<?echo $rec['pkey']?>"><?echo $rec['indx']?></span>&nbsp;
                                                   <i class="fa fa-chevron-circle-down v2" aria-hidden="true"  param_id="<?echo $rec['pkey']?>" cmd="rmt/TO_param_down"></i>
               </td>
               <td style="width:25%;" class="left"><textarea style="width:100%;height:100px;" name="params[<?echo $rec['pkey']?>][obj_name]"><?echo $rec['obj_name']?></textarea></td>
               <td style="width:10%;">
                   <input type="text" style="width:100%;" name="params[<?echo $rec['pkey']?>][group_name]" value="<?echo $rec['group_name']?>">
               </td>
               <td style="width:10%;">
                   <select name="params[<?echo $rec['pkey']?>][type]">
                                         <?if (_sizeof($this->arr_types)) foreach($this->arr_types as $id=>$value) {?><option value="<?echo $id?>" <?if ($rec['type']==$id) echo 'selected'?>><?echo $value?></option><?}?>
                                     </select>
               </td>
               <td style="width:10%;"><input type="hidden" name="params[<?echo $rec['pkey']?>][notnull]" value="0"><input type="checkbox" name="params[<?echo $rec['pkey']?>][notnull]" value="1" <?if ($rec['notnull']) echo 'checked'?>></td>
               <td style="width:10%;"><?if ($rec['type']==1){?><input type="hidden" name="params[<?echo $rec['pkey']?>][photo]" value="0"><input type="checkbox" name="params[<?echo $rec['pkey']?>][photo]" value="1" <?if ($rec['photo']) echo 'checked'?>><?}?></td>
             </tr>
           <?
         }
         ?>
          <tr>
              <td colspan="2">Новый параметр</td>
              <td class="left">
                  <input type="text" style="width:100%;" name="new_param[obj_name]" value="">
                  <input type="hidden" name="new_param[access]" value="<?echo $this->access?>">
              </td>
              <td>
                  <input type="text" style="width:100%;" name="new_param[group_name]" value="">
              </td>
              <td>
                  <select name="new_param[type]">
                      <?if (_sizeof($this->arr_types)) foreach($this->arr_types as $id=>$value) {?><option value="<?echo $id?>"><?echo $value?></option><?}?>
                  </select>
              </td>
              <td><input type="checkbox" name="new_param[notnull]" value="1"></td>
              <td><input type="checkbox" name="new_param[photo]" value="1"></td>
          </tr>
          </table><br>
         <div id="panel_buttons">
             <button class="button v2" car_type="<?echo $car_type?>" cmd="rmt/to_params_save">Сохранить</button><?
             if (_sizeof($recs_params)){?><button class="button v2" event_id="<?echo $this->rec_event['pkey']?>" cmd="rmt/to_params_delete">Удалить отмеченные параметры</button><?}
             ?>
         </div><br>

          <style type="text/css">
             td.indx i{display:none;}
             td.indx:hover i{display:inline;cursor:pointer;}
          </style>
          <script type="text/javascript">
              $j('i.fa-chevron-circle-up').click(function()
               { if (!$j(this).closest('tr').prev().hasClass('head')) $j(this).closest('tr').prev().before($j(this).closest('tr')) ;
               }) ;
              $j('i.fa-chevron-circle-down').click(function()
               {  $j(this).closest('tr').next().after($j(this).closest('tr')) ;
               }) ;
          </script>
         <?
       /*
     ?>
                      <fieldset>
                                 <label class="label" style="color:black;font-weight:bold;">Добавте темы занятий инструктажа, можно указать сразу несколько тем, каждая тема с новой строки</label>
                                 <label class="textarea"><textarea name="filter[new_themes]" rows="10"></textarea></label>
                      </fieldset>

                     <div id="panel_buttons">
                         <input type="submit" class="button v2" event_id=<?echo $this->rec_event['pkey']?>  cmd="rmt_instruction/event_themes_create" value="Добавить темы" validate="form">
                     </div><br>
                  </form>
           </div><br>
           <style>
               div.event_info strong{font-size:18px;font-weight:normal;}
           </style>
         <?    */
    }


}



?>
