<?php

require_once __DIR__ . '/core.php';

if ($con->request->isAjax()) {$con->json(['msg' => json_encode($con->request->post())]);
    switch ($con->request->post('action')) {
        case 'logout':
            break;
        default:break;
    }
} else {
    // 拼接菜单
    $roles = $con->db->table('role')->find('id, pid, r_url, r_name')->where('r_on = ?', [1])->all();
    $roles = $hel->subTree($roles, 1);
    $slide = '';
    $path = $hel->path();
    $i = 1;
    foreach ($roles as $role) {
        if (in_array('1', $user_role) || in_array($role['id'], $user_role)) {
            $slide .= '<ul class="layui-nav layui-nav-tree"><li class="layui-nav-item"><a href="'.$role['r_url'].'"><img src="'.$path.'/img/'.$i.'.png">'.$role['r_name'].'</a>';
            if (isset($role['sub'])) {
                foreach ($role['sub'] as $r) {
                    $slide .= '<dl class="layui-nav-child"><dd><a href="'.$path.'/'.$r['r_url'].'">'.$r['r_name'].'</a></dd></dl>';
                }
            }
            $slide .= '</li></ul>';
        } else {
            if (isset($role['sub'])) {
                $sub_slide = '';
                foreach ($role['sub'] as $r) {
                    $flag = false;
                    if (in_array($r['id'], $user_role)) {
                        $flag = true;
                    }
                    if (isset($r['sub'])) {
                        foreach ($r['sub'] as $_) {
                            if (in_array($_['id'], $user_role)) {
                                $flag = true;
                            }
                        }
                    }
                    if ($flag) {
                        $sub_slide .= '<dl class="layui-nav-child"><dd><a href="'.$path.'/'.$r['r_url'].'">'.$r['r_name'].'</a></dd></dl>';
                    }
                }
                if ('' != $sub_slide) {
                    $slide .= '<ul class="layui-nav layui-nav-tree"><li class="layui-nav-item layui-nav-itemed"><a href="'.$role['r_url'].'">'.$role['r_name'].'</a>'.$sub_slide.'</li></ul>';
                }
            }
        }
        $i++;
    }
    unset($roles, $user_role);

    $con->render('index', [
        'host'    => $hel->url(),
        'path'    => $path,
        'slide'   => $slide,
        'version' => $version,
        'err'     => $con->session->remove('err')
    ]);
}
