<?php if (!defined('ROOT')){ echo 'Deny ...'; exit; } extract($data); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>MAIN</title>
        <link href="<?php echo $base_url; ?>static/css/style.css" rel="stylesheet" type="text/css" />
        <link href="<?php echo $base_url; ?>static/css/select.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="<?php echo $base_url; ?>static/js/jquery.js"></script>
        <script type="text/javascript" src="<?php echo $base_url; ?>static/js/select-ui.min.js"></script>
        <script type="text/javascript" src="<?php echo $base_url; ?>static/js/common.js"></script>
    </head>
    <body>
        <div class="place">
            <span>位置：</span>
            <ul class="placeul">
                <li><a href="<?php echo $base->get_url(array('c' => 'frame', 'm' => 'main')); ?>">首页</a></li>
                <li>权限节点</li>
            </ul>
        </div>
        <div class="formbody">
            <div class="usual">
                <div class="itab" style="margin: 12px 0px;">
                    <ul>
                        <?php
                        $channels = service::channels();
                        ksort($channels);
                        foreach ($channels as $c) {
                        ?>
                        <li><a href="<?php echo $base->get_url(array('c' => 'node', 'cid' => $c['id'])); ?>" class="<?php if ($c['id'] == $cid) { ?>selected<?php } ?>"><?php echo $c['name']; ?></a></li> 
                        <?php } ?>
                    </ul>
                </div> 
                <ul class="seachform">
                    <?php
                        $node = service::common_index_by_id('AdminNode', array('where' => 'pid=0 and model_id = ?', 'param' => array($cid)));
                        echo form::start('form1');
                        echo form::nonce($admin['id'], ($db ? 'editNode' : 'createNode'));
                        echo form::input_hidden('cid', $cid);
                        if ($db){
                            echo form::input_hidden('id', $db['id']);
                        }
                        echo form::select('父级节点', 'pid', 0, $node, 'id::name', 120, ($db ? $db['pid'] : ''));
                        echo form::input(array(
                            'title' => '节点名称',
                            'name' => 'name',
                            'must' => 1,
                            'placeholder' => '中文',
                            'style' => 'width: 120px;',
                            'value' => ($db ? $db['name'] : ''),
                            'readonly' => 0
                        ));
                        echo form::input(array(
                            'title' => '控制器',
                            'name' => 'controller',
                            'must' => 1,
                            'placeholder' => '字母',
                            'style' => 'width: 120px;',
                            'value' => ($db ? $db['controller'] : ''),
                            'readonly' => 0
                        ));
                        echo form::input(array(
                            'title' => '方法',
                            'name' => 'method',
                            'must' => 0,
                            'placeholder' => '字母',
                            'style' => 'width: 120px;',
                            'value' => ($db ? $db['method'] : ''),
                            'readonly' => 0
                        ));
                        echo form::submit('保存节点', 'btn88');
                        $_save_action = $db ? 'editAJAX' : 'createAJAX';
                        $_jump_action = array('c' => 'node', 'cid' => $cid);
                        if ($db && $db['pid']){
                            $_jump_action['_pid'] = $db['pid'];
                        }
                        echo form::onclick('form1', 'btn88', '保存节点', $base->get_url(array('c' => 'node', 'm' => $_save_action)), $base->get_url($_jump_action));
                    ?>
                </ul>
                <table class="tablelist">
                    <thead>
                        <tr>
                            <th>节点ID</th>
                            <th>权限名称</th>
                            <th>控制器</th>
                            <th>方法</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if ($result['lists']) {
                            foreach ($result['lists'] as $item) {
                        ?>
                        <tr class="node-<?php echo $item['id'];?>">
                            <td><?php echo $item['id'];?></td>
                            <td>
                                <a href="javascript:;" onclick="_find(<?php echo $item['id'];?>);">
                                    <?php echo $item['name'];?>
                                </a>
                            </td>
                            <td><?php echo $item['controller'];?></td>
                            <td>--</td>
                            <td>
                                <a href="<?php echo $base->get_url(array('c' => 'node', 'cid' => $cid, 'id' => $item['id']));?>" class="tablelink">[编辑]</a>
                                <a href="javascript:;" class="tablelink" onclick="_delete(<?php echo $item['id'];?>, '<?php echo $item['name'];?>');">[删除]</a>
                            </td>
                        </tr>
                        <?php
                            }
                        }
                        ?>
                    </tbody>
                </table>
                <div style="margin-top: 12px;"><?php echo $result['pager']; ?><br style="clear: both;"/></div>
            </div>
        </div>   
        <script>
            function _find(id){
                $('.child_node').remove();
                $.get('<?php echo $base->get_url(array('c' => 'node', 'm' => 'child'));?>', {id: id, cid: <?php echo $cid;?>}, function (ret){
                    if (ret){
                        $('.node-' + id).after(ret);
                        $('.tablelist tbody tr:odd').addClass('odd');
                    }
                });
            }
            $(function(){
                $('.tablelist tbody tr:odd').addClass('odd');
            });
        </script>
        <?php
        $_reload = $db && $db['pid'] ? $db['pid'] : (isset($_GET['_pid']) ? intval($_GET['_pid']) : 0);
        if ($_reload){
        ?>
        <script>
            $(function(){
                _find(<?php echo $_reload;?>);
            });
        </script>
        <?php
        }
        ?>
        <?php echo form::alert('deleteNode', $base_url);?>
        <?php echo form::alert_confirm('deleteNode', $base->get_url(array('c' => 'node', 'm' => 'delete')), form::create_nonce($admin['id'], 'deleteNode'));?>
        <script>
            function _delete(id, name){
                $('.deleteNode .tipright p').html('确认要删除 <font color="red">' + name + '</font> 吗?');
                $(".deleteNode").fadeIn(200);
                $('#_id').val(id);                
            } 
        </script>
    </body>
</html>
