<?php
include_once("c_setting_members.php");
class c_page_cab_setting_members_rol_card extends c_page_cab_setting_members
{
  public $h1='Карточка роли ЭСМО' ;
  public $rec_rol=[] ;

function select_obj_info()
 { $this->rec_rol=$_SESSION['ARR_roll'][_CUR_ID] ;
   if (!$this->rec_rol['pkey']) $this->result=404 ;
   else
   { $this->h1='Карточка роли "'.$this->rec_rol['obj_name'].'"' ;
     $this->rec_rol['__href_window']='/window/setting/members/rol/card/'.$this->rec_rol['id'].'/' ;
   }
 }


function menu_config()
 { if ($this->rec_rol['pkey'])
   { $this->menu[$this->rec_rol['__href_window']]=array('title'=>'Карточка роли "'.$this->rec_rol['obj_name'].'"','_class'=>'fa fa-user-md') ;
     $this->menu[$this->rec_rol['__href_window'].'accounts/']=array('title'=>'Аккаунты','_class'=>'fa fa-user-md') ;
     $this->menu[$this->rec_rol['__href_window'].'personals/']=array('title'=>'Персонал группы','_class'=>'fa fa-user-md') ;
     $this->menu[$this->rec_rol['__href_window'].'personals_set/']=array('title'=>'Выбор персонала','_class'=>'fa fa-user-md') ;
     $this->menu[$this->rec_rol['__href_window'].'db/']=array('title'=>'Дамп БД','_class'=>'fa fa-user-md') ;
   }
 }

 function block_main()
 {
     $this->page_title() ;
   //$this->panel_list_edit() ;
 }



 function panel_list_edit()
{ $recs_account=execSQL('select pkey,obj_name,rol,enabled from obj_site_account where clss=210 and rol>0') ;
  $recs_rol_by_account=array() ;
  $recs_rol=execSQL('select * from '.TM_ROLES.' order by id') ;
  if (_sizeof($recs_account)) foreach($recs_account as $rec_account)
  {   if (!$rec_account['enabled']) $rec_account['enabled']=0 ;
      $recs_rol_by_account[$rec_account['rol']][$rec_account['enabled']][]='<a class="new_window" target="_blank" title="window_member" href="/window/setting/member/'.$rec_account['pkey'].'/">'.$rec_account['obj_name'].'</a>' ;
  }
  $arr_reports=execSQL_row('select code,obj_name from reports where type=1') ;
  //damp_array($arr_reports,1,-1);
  //damp_array($recs_rol_by_account);
  ?><form>
    <button class="v2" cmd="aspmo_setting/rol_create">Создать новую роль</button> <button class="v2" cmd="aspmo_setting/save_rol">Сохранить изменения</button> <button class="v2" cmd="aspmo_setting/export_rols">Выгрузить роли</button> <button class="v2" cmd="aspmo_setting/upload_rols_json">Загрузить роли</button>
    <table class="basic fz_small"><tr><th>Выбор<br><input type="checkbox" name="check_all"></th><th>ID</th><th>Наименование</th><th>Активных аккаунтов</th><th>Заблокированных аккаунтов</th><th>Стартовая страница кабинета</th><th>Работа<br>по<br>сменам</th><th>Одна<br>смена<br>в группе</th><th>Шаблон отчета на закрытие смены</th><th>Срок жизни сессии, секунд</th><th>Операции</th></tr>
     <?if ($recs_rol) foreach($recs_rol as $rec)
       {   $id=$rec['id'] ;
           ?><tr><td><?if ($id!=1) {?><input type="checkbox" name="_check[<?echo $rec['pkey']?>]" value="<?echo $id?>"><?}?></td>
               <td class="left"><?echo $id?><input type="hidden" name="roles[<?echo $rec['pkey']?>][id]" value="<?echo $rec['id']?>"></td>
               <td class="left"><? FORM()->element_input(['name'=>'roles['.$rec['pkey'].'][name]','title'=>'','style'=>'width:auto;','value'=>htmlspecialchars($rec['obj_name'])]); ?></td>
               <td class="center"><? $this->show_account_info($id,'only_enabled',$recs_rol_by_account[$id][1]) ; ?></td>
               <td class="center"><? $this->show_account_info($id,'only_disabled',$recs_rol_by_account[$id][0]) ; ?></td>
               <td class="left"><? FORM()->element_input(['name'=>'roles['.$rec['pkey'].'][index_page]','title'=>'','style'=>'width:auto;','value'=>htmlspecialchars($rec['index_page'])]);?></td>
               <td class="center"><?if ($id<100) echo ($rec['use_auch_as_smena'])? 'Да':'' ; ?></td>
               <td class="center"><?if ($id<100) echo ($rec['smena_monopole_mode'])? 'Да':'' ; ?></td>
               <td class="left"><?if ($id<100 and $rec['use_auch_as_smena']) FORM()->element_select(['name'=>'roles['.$rec['pkey'].'][report_template]','title'=>'','recs'=>$arr_reports,'value'=>$rec['report_template']]);  ?></td>
               <td class="left"><? FORM()->element_input(['name'=>'roles['.$rec['pkey'].'][autologout]','title'=>'','data-mask'=>'00000000','style'=>'width:auto;','value'=>htmlspecialchars(($rec['autologout'])?:0)]); ?></td>
               <td class="left"><?if (!_sizeof($recs_rol_by_account[$id]) && $id>=100){?><button class="v2" cmd="aspmo_setting/rol_delete" rol_id="<?=$id?>">Удалить</button><?}?></td>
          </tr><?
       }?>
   </table>
    <script type="text/javascript">
        $j('table input[name="check_all"]').on('click',onclick_check_all);
        function onclick_check_all()
        { if ($j(this).is(':checked')) $j(this).closest('table').find('input[type="checkbox"][name^="_check"]').attr('checked',true)  ;
        else                         $j(this).closest('table').find('input[type="checkbox"][name^="_check"]').removeAttr('checked')  ;
        }
    </script>
   </form>
    <?
}

function show_account_info($rol_id,$status,$recs)
{
   $cnt=_sizeof($recs) ;
   if ($cnt)
   { echo '<a target="_blank"  href="/cab/setting/members/[rol='.$rol_id.',props=('.$status.')]/">'.$cnt.' '.get_case_by_count_1_2_5($cnt,'аккаунт, аккаунта, аккаунтов').'</a><br>' ;
     /*if ($cnt<=3) echo implode('<br>',$recs) ;
     else {?><div style="height:100px;overflow-y: scroll;"><? echo implode('<br>',$recs)?></div><?}*/
   }
}


}



?>
