<?php
namespace app\admin\controller;

use think\Controller;

class Base extends Controller
{
    public function _initialize()
    {
        $admin = db('admins')->where('token',session('token'))->find();
        if (!session('user_id')) {
            $this->redirect('admin/login/index');
        }
        if (!$admin) {

            $this->redirect('admin/login/index');
        }
        $admin = db('admins')->where('id',session('user_id'))->find();
        $menu = db('menu')->alias('a')->join('admingrouppower b','a.id=b.menu_id')->field('a.id,a.name_cn,a.name_en,a.p_id,a.url,a.ico')->where('b.group_id',$admin['group_id'])->where('a.p_id',0)->order('sort','asc')->select();
        $menulist = db('menu')->alias('a')->join('admingrouppower b','a.id=b.menu_id')->field('a.id,a.name_cn,a.name_en,a.p_id,a.url,a.ico')->where('b.group_id',$admin['group_id'])->where('a.p_id','<>',0)->order('sort','asc')->select();
        for($i=0;$i<count($menu);$i++){
            for($j=0;$j<count($menulist);$j++){
                if($menu[$i]['id'] == $menulist[$j]['p_id']){
                    $menu[$i]['menulist'][$j] = $menulist[$j];
                }
            }
        }
        $settings = db('settings')->where('id',1)->find();
        if($settings['kf_white_list'] == 'on'){
            $settings['white_list'] = '关闭客服白名单';
        }else{
            $settings['white_list'] = '开启客服白名单';
        }
        if($settings['account_black_list'] == 'on'){
            $settings['black_list'] = '关闭用户黑名单';
        }else{
            $settings['black_list'] = '开启用户黑名单';
        }

        $material = db('material')->select();

        $this->assign([
            'version' => config('version'),
            'socket' =>config('socket'),
            'menu' =>$menu,
            'admin' =>$admin,
            'settings' =>$settings,
            'material' =>$material,
        ]);
    }
}
