<script type="text/javascript" src="./app/member/assets/access.js"></script>
<script type="text/javascript">
    var appAccess = <?php echo $appAccess; ?>;
    $(function() {
        inputChecked(appAccess, '#access-menu');
        inputChecked(appAccess, '#access-app');
    });
</script>

<input name="akey" type="hidden" value="admincp" />
<div id="access-menu" class="tab-pane active">
    <div class="form-group">
        <div class="input-group">
            <div class="input-group-prepend">
                <label class="input-group-text" for="checkAllMenu">全选</label>
                <div class="input-group-text">
                    <div class="custom-control custom-checkbox">
                        <input type="checkbox" class="custom-control-input" id="checkAllMenu" data-toggle="checkAll" data-target="#access-menu" />
                        <label class="custom-control-label" for="checkAllMenu"></label>
                    </div>
                </div>
            </div>
            <div class="input-group-append">
                <button class="btn btn-primary" type="submit"><i class="fa fa-fw fa-check"></i> 提交</button>
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="input-group">
            <div class="input-group-prepend">
                <label class="input-group-text" for="checkbox-admincp">允许登录后台</label>
            </div>
            <div class="input-group-append">
                <div class="input-group-text">
                    <div class="custom-control custom-checkbox">
                        <input type="checkbox" class="custom-control-input" id="checkbox-admincp" name="access[menu][]" value="ADMINCP" />
                        <label class="custom-control-label" for="checkbox-admincp"></label>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="alert alert-danger">注:此处权限设置为后台的菜单权限是否显示,设置后还要设置具体的相关应用权限,否刚有可能出错</div>

    <table class="table table-hover table-sm table-borderless">
        <thead class="bg-light text-black">
            <tr>
                <th>菜单</th>
            </tr>
        </thead>
        <tbody>
            <?php
            function menu_access($M, $level, $child, $pid)
            {
                $id = md5($M['id'] . $M['access'] . uniqid(true));
                $ltag = ($level == '1' ? "" : "├ ");
                $name = $M['children'] ? '<b>' . $M['caption'] . '</b>' : $M['caption'];
                echo '<tr>';
                echo '<td class="p-0">';
                echo str_repeat("│　", $level - 1) . $ltag;
                echo '<div class="custom-control custom-checkbox custom-control-inline">
                        <input type="checkbox" class="custom-control-input menu_access"
                        id="' . $id . '"
                        mid="' . $M['id'] . '"
                        pid="' . $pid . '"
                        name="access[menu][]"
                        value="' . $M['access'] . '"/>
                        <label class="custom-control-label" for="' . $id . '">' . $name . '</label>
                    </div>';
                echo '</td>';
                echo '</tr>';
            }
            Menu::$callback['func'] = 'menu_access';
            Menu::callfunc();
            ?>
        </tbody>
    </table>
</div>
<div id="access-app" class="tab-pane">
    <div class="form-group">
        <div class="input-group">
            <div class="input-group-prepend">
                <label class="input-group-text" for="checkAllApp">全选</label>
                <div class="input-group-text">
                    <div class="custom-control custom-checkbox">
                        <input type="checkbox" class="custom-control-input" id="checkAllApp" data-toggle="checkAll" data-target="#access-app" />
                        <label class="custom-control-label" for="checkAllApp"></label>
                    </div>
                </div>
            </div>
            <div class="input-group-append">
                <button class="btn btn-primary" type="submit"><i class="fa fa-fw fa-check"></i> 提交</button>
            </div>
        </div>
    </div>
    <div class="clearfloat"></div>
    <?php function app_access($accessAll, $value)
    {
        if (empty($accessAll)) return;
        $trid = $value['app'] . $value['id'] . '_access';

        $html = '<tr id="id' . $trid . '">';
        $html .= '<td>
                    <div class="custom-control custom-checkbox">
                        <input type="checkbox" class="custom-control-input" id="checkAll_' . $trid . '" data-toggle="checkAll" data-target="#' . $trid . '" />
                        <label class="custom-control-label" for="checkAll_' . $trid . '"></label>
                    </div>
                    </td>';
        // $html .= '<td>APPID:' . $value['id'] . '<br />' . $value['app'] . '<br />' . $value['name'] . '</td>';
        $html .= '<td>';
        if ($value['table']) {
            $html .= '<div class="form-group">';
            foreach ($value['ACCESC_BASE'] as $k => $label) {
                $html .= method_access($value['app'] . ".{$k}", $label . $value['title']);
            }
            $html .= '</div>';
        }
        $html .= '</td>';
        $html .= '<td><div class="form-group">';
        $access_get = $accessAll[iPHP_GET_PREFIX];
        if ($access_get) {
            ksort($access_get);
            $html .= implode('', $access_get);
        }
        $html .= '</div></td>';
        $html .= '<td><div class="form-group">';
        $access_post = $accessAll[iPHP_POST_PREFIX];
        if ($access_post) {
            ksort($access_post);
            $batchAccess = $access_post['batch:access'];
            $access_post['batch:access'] = '';
            if ($batchAccess) foreach ($batchAccess as $bkey => $bval) {
                $access_post['batch:access'] .= '│　├' . method_access($bval[0], $bval[1]);
            }
            $html .= implode('', $access_post);
        }
        $html .= '</div></td>';
        $html .= '</tr>';
        return $html;
    }
    function method_access($url, $title)
    {
        return '
                <div class="custom-control custom-checkbox custom-control-inline">
                    <input type="checkbox" class="custom-control-input" id="' . md5($url) . '" name="access[app][]" value="' . $url . '"/>
                    <label class="custom-control-label" for="' . md5($url) . '">' . $title . '</label>
                </div>
                <div class="clearfix"></div>
        ';
    }
    ?>
    <?php AppsHook::$callback['app:access'] = 'app_access'; ?>
    <?php AppsHook::$callback['method:access'] = 'method_access'; ?>
    <?php $html = AppsAccess::get($appArray); ?>
    <div class="block ">
        <ul class="nav nav-tabs nav-tabs-block " data-toggle="tabs" role="tablist">
            <?php $active = 'active';
            foreach ($appArray as $key => $apps) {
                $app = $apps[$key];
            ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo $active; ?>" href="#btabs-<?php echo $key ?>"><?php echo $app['name'] ?></a>
                </li>
            <?php
                $active = null;
            } ?>
        </ul>
        <div class="block-content tab-content p-0">
            <?php
            $active = 'active';
            foreach ($appArray as $key => $apps) {
                $app = $apps[$key];
            ?>
                <div class="tab-pane <?php echo $active; ?>" id="btabs-<?php echo $key ?>" role="tabpanel">
                    <ul class="nav nav-tabs nav-tabs-alt " data-toggle="tabs" role="tablist">
                        <?php $active1 = 'active';
                        foreach ($apps as $ak => $aa) {
                        ?>
                            <li class="nav-item">
                                <a class="nav-link <?php echo $active1; ?>" href="#btabs-<?php echo $ak ?><?php echo $aa['id'] ?>"><?php echo $aa['name'] ?></a>
                            </li>
                        <?php
                            $active1 = null;
                        } ?>
                    </ul>
                    <div class="block-content tab-content p-0">
                        <?php $active2 = 'active';
                        foreach ($apps as $ak => $aa) {
                        ?>
                            <div class="tab-pane <?php echo $active2; ?>" id="btabs-<?php echo $ak ?><?php echo $aa['id'] ?>" role="tabpanel">
                                <table class="table">
                                    <thead class="bg-light text-black">
                                        <tr>
                                            <th></th>
                                            <!-- <th>应用信息</th> -->
                                            <th>基础权限</th>
                                            <th>页面权限</th>
                                            <th>功能权限</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php echo $html[$ak]; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php
                            $active2 = null;
                        } ?>
                    </div>
                </div>
            <?php
                $active = null;
            } ?>
        </div>

    </div>
</div>