<?php
include_once(_DIR_TO_CLASS."/c_page.php");

class c_page_cab_esmo_setting_trassir extends c_page
{
    public $h1='Настройка камер trassir' ;

    function second_menu_config() {
        $this->second_menu=$this->menu['/cab/setting/']['items']['/cab/setting/members/']['items'] ;
    }



    function block_main() {
        // описываем меню
        $current_page_menu['button_trassir_1']=array('title'=>'Привязка камер к группам/терминалам','panel'=>'menu_panel_trassir_1','class'=>'button_panel_change') ;
        $current_page_menu['button_trassir_2']=array('title'=>'Список камер','panel'=>'menu_panel_trassir_2','class'=>'button_panel_change') ;
        // формируем меню
        if (_sizeof($current_page_menu)) foreach($current_page_menu as $id_button=>$item) { $this->current_page_menu($id_button,$item) ; }
        ?>
        <button onclick="window.open('<?echo $GLOBALS['LS_trassir_url']?>')">Настройка Trassir Gateway</button>
        <div class=clear></div>

        <div id="tab_panels">
            <div id="menu_panel_trassir_1" class="panel"><? $this->panel_trassir_groups_terminals() ; ?></div>
            <div id="menu_panel_trassir_2" class="panel" hidden="true"><? $this->panel_trassir_cameras() ; ?></div>
        </div>

        <script type="text/javascript">
            $j('.button_panel_change').click(function()
            {
                console.log($j(this).attr('panel'))
                let id=$j(this).attr('panel');
                $j('div#tab_panels div.panel').hide() ;
                $j('div#tab_panels div#'+id).show() ;
                return(false) ;
            }) ;
        </script>
        <?
    }

    function panel_trassir_groups_terminals() {
        $recs_cameras=execSQL('select * from obj_site_trassir_channels');
        ?>
        <div id="panel_filter">
            <form class="sky-form">
                <table class="basic fz_small auto">
                    <tr>
                        <th>ID</th>
                        <th>Тип</th>
                        <th>Наименование</th>
                        <th>IP</th>
                        <th>Камеры</th>
                        <th></th>
                    </tr>
                    <?
                    $recs=ESMO()->get_list_terminals(MEMBER()->cur_group_id) ;
                    $last_group=0;
                    if (_sizeof($recs)) foreach ($recs as $rec) {
                        if ($last_group!=$rec['parent']) {
                            ?>
                            <tr>
                                <td class="group_name_td" onclick="$j('.terminal_for_<?echo $rec['parent']?>').toggle('hidden')" colspan="4"><?echo $rec['_group_name']?></td>
                                <td><?$this->get_camera_panel($recs_cameras,['type'=>'group','id'=>$rec['parent']])?></td>
                                <td><button class="button v2" device_type="group" device_id="<?echo $rec['parent']?>"  cmd="esmo_setting/save_trassir_cameras">Сохранить</button></td>
                            </tr>
                            <tr class="terminal_for_<?echo $rec['parent']?>">
                                <td colspan="4">РМВ</td>
                                <td><?$this->get_camera_panel($recs_cameras,['type'=>'hand_check','id'=>$rec['parent']])?></td>
                                <td><button class="button v2" device_type="hand_check" device_id="<?echo $rec['parent']?>"  cmd="esmo_setting/save_trassir_cameras">Сохранить</button></td>
                            </tr>
                            <?
                            $last_group=$rec['parent'] ;
                        }
                        ?>
                        <tr class="terminal_for_<?echo $rec['parent']?>">
                            <td><?echo $rec['id']?></td>
                            <td><?echo $rec['terminal_type']?></td>
                            <td><?echo addslashes($rec['obj_name'])?></td>
                            <td><?echo $rec['ip']?></td>
                            <td><?$this->get_camera_panel($recs_cameras,['type'=>'terminal','id'=>$rec['pkey']])?></td>
                            <td><button class="button v2" device_type="terminal" device_id="<?echo $rec['pkey']?>" cmd="esmo_setting/save_trassir_cameras">Сохранить</button></td>
                        </tr>
                        <?
                    }
                    ?>
                </table>
            </form>
        </div>
        <script type="text/javascript">
        </script>
        <style>
            label.select label {margin-left: 10px; text-align: left;}
            td.group_name_td {font-size:18px;font-weight:bold;color:#0057aa;text-align:left;cursor: pointer;}
        </style>
        <?
    }

    function panel_trassir_cameras() {
        $recs_cameras=execSQL('select * from obj_site_trassir_channels');
        ?>
        <br><button class="v2" cmd="media_server/update_trassir_cameras_list">Обновить список камер</button>
        <?
        if (_sizeof($recs_cameras)) {
            ?>
            <table class="basic fz_small">
                <tr>
                    <th>id</th>
                    <th>GUID</th>
                    <th>Имя</th>
                    <th>data</th>
                </tr>
                <?
                foreach ($recs_cameras as $rec_camera) {
                    ?>
                    <tr>
                        <td><?echo $rec_camera['pkey']?></td>
                        <td><?echo $rec_camera['guid']?></td>
                        <td><?echo $rec_camera['obj_name']?></td>
                        <td><?damp_array(unserialize($rec_camera['data']),1,-1)?></td>
                    </tr>
                    <?
                }
                ?>
            </table>
            <?
        }
    }

    function get_camera_panel($cameras,$options=array()) {
        switch ($options['type']) {
            case 'group': $device_id_field='group_id'; break;
            case 'terminal': $device_id_field='terminal_id'; break;
            case 'hand_check': $device_id_field='hand_check_group_id'; break;
        }
        $terminal_cameras=execSQL('select camera_trassir_id,'.$device_id_field.' from obj_site_trassir_camera_terminal where '.$device_id_field.'='.$options['id']);
        if (_sizeof($cameras)) {
            ?>
                <label class="select">
                    <select class=filter multiple="multiple" name="term[<?echo $options['type']?>][<?echo $options['id']?>][trassir_cameras][]" style="width: 300px">
                        <? if (_sizeof($cameras)) foreach($cameras as $id=>$camera_rec)
                        { ?><option value="<?echo $id?>" <?if(isset($terminal_cameras[$id])) echo 'selected'?>><?echo $camera_rec['obj_name']?></option><?}
                        ?>
                    </select>
                </label>
            <?
        }
    }

    function panel_mega_menu_item2($url,$item,$i)
    {  if (!is_array($item))  $item=array('title'=>$item) ;
        if (is_int($url)) $url='' ;
        ?><li aria-haspopup="false" <?if (!$i) echo ' class=current'?>>
        <a href="<?echo $url.$this->menu_url_params?>" class="modal_window_top_menu"
            <? if (_sizeof($item)) foreach($item as $name=>$value) if ($name!='class' and $name!='title' and $name!='info_id')
            { if ($name=='cmd') echo 'class=v2 cmd="'.$value.'"' ;
            elseif ($name=='new_window') echo ' class="new_window" title="'.$value.'" ' ;
            else              echo $name.'="'.$value.'"' ;
            }
            ?>><i class="<?echo $item['class']?>"></i><?echo $item['title']?></a>
        <?if ($item['info_id']){?><span class="info" id="<?echo $item['info_id']?>"></span><?}?>
        <?if (_sizeof($item['items'])){?><div class="grid-container4"><ul><?foreach($item['items'] as $url2=>$item2) $this->panel_mega_menu_item($url2,$item2);?></ul></div><?}?>
        </li>
        <?
    }

    function current_page_menu($id_button,$item) {
        ?>
            <button class="v2 <?echo $item['class']?>" id="<?echo $id_button?>" panel="<?echo $item['panel']?>"><?echo $item['title']?></button>
        <?
    }

}
