<?php

namespace App\Http\Controllers\Admin;

use App\Facade\TableName;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Storage;

class IndexController extends Controller
{
    protected $whiteList = ['全局报表', '用户单控系统', '充值排行榜', '充值渠道修改', '提现数值修改', '充值邮件记录', '绑定关系查询', '提现订单审核', '房间实时数据', '推广员奖励报表', '绑定关系查询', '房间服务端数据统计', '留存数据', '游戏日志', '充值档位修改'];
    protected $childList = ['彩金控制'];

    protected $blacklist = [88, 90, 92, 98, 2073, 2072, 2077];
    protected $publicList = [];
    protected $serviceWhiteList = [64, 59, 3102, 3108, 13, 69, 70, 77, 82, 83, 2, 40];

    public function index()
    {
        $admin = session('admin');
        $role_id = $admin->roles[0]->id;

        $menuList = $admin->getMenus();
//        dd($menuList->toArray());
        // 查找管理员拥有的菜单
//        $menuRoleArr = Cache::remember('menuRoleArr', 60 * 3, function () use ($role_id) {
        $menuRoleArr= DB::table(TableName::agent() . 'admin_menu_admin_role')
                ->where('admin_role_id', $role_id)
                ->pluck('admin_menu_id')->toArray();
//        });
        // 管理员过滤菜单
        foreach ($menuList as $key => &$val) {

//            if (in_array($val->id, $this->blacklist)) {
//                unset($menuList[$key]);
//            }
            if (!in_array($val->id, $menuRoleArr)) {
                unset($menuList[$key]);
            }
            if (!empty($val->children)) {
                foreach ($val->children as $k => &$value){
                    if (!in_array($value->id, $menuRoleArr)) {
                        unset($val->children[$k]);
                    }
                }
            }

        }

//        $hidden = session('hidden')['is_hidden'] ?? '';
//        $admin_hidden = session('admin_hidden')['is_hidden'] ?? '';

//        if (!empty($admin_hidden)) {
//            $this->hidden($menuList, ['渠道管理']);
//        }


//        switch ($hidden) {
//            case 'child':
//                $menuList = $this->hidden($menuList, $this->childList);
//                break;
//            case 'service':
////                dd($this->serviceWhiteList);
//                $menuList = $this->show($menuList, $this->serviceWhiteList);
//                break;
//        }


        return view('admin.index', ['menu' => $menuList]);
    }

    // 展示
    public function show($menuList, $whiteList)
    {
        foreach ($menuList as &$val) {
            foreach ($val->children as $k => &$v) {
                if (!in_array($v->id, $whiteList)) {
                    unset($val->children[$k]);
                }
            }
        }
        return $menuList;
    }

    // 隐藏
    public function hidden($menuList, $whiteList)
    {
        foreach ($menuList as &$val) {
            foreach ($val->children as $k => &$v) {
                if (in_array($v->id, $whiteList)) {
                    unset($val->children[$k]);
                }
            }
        }
        return $menuList;
    }

    public function console()
    {
        return view('admin.console');
    }

    /**
     * @Desc: 后台图片上传
     * @Author: woann <304550409@qq.com>
     * @param Request $request
     * @return mixed
     */
    public function upload(Request $request)
    {
        $file = $request->file('image');
        $path = $request->input('path') . '/';
        if ($file) {
            if ($file->isValid()) {
                $size = $file->getSize();
                if ($size > 5000000) {
                    return $this->json(500, '图片不能大于5M！');
                }
                // 获取文件相关信息
                $ext = $file->getClientOriginalExtension(); // 扩展名
                if (!in_array($ext, ['png', 'jpg', 'gif', 'jpeg', 'pem'])) {
                    return $this->json(500, '文件类型不正确！');
                }
                $realPath = $file->getRealPath(); //临时文件的绝对路径
                // 上传文件
                $filename = $path . date('Ymd') . '/' . uniqid() . '.' . $ext;
                // 使用我们新建的uploads本地存储空间（目录）
                $bool = Storage::disk('admin')->put($filename, file_get_contents($realPath));
                if ($bool) {
                    return $this->json(200, '上传成功', ['filename' => '/uploads/' . $filename]);
                } else {
                    return $this->json(500, '上传失败！');
                }
            } else {
                return $this->json(500, '文件类型不正确！');
            }
        } else {
            return $this->json(500, '上传失败！');
        }
    }

    /**
     * @Desc: 富文本上传图片
     * @Author: woann <304550409@qq.com>
     * @param Request $request
     */
    public function wangeditorUpload(Request $request)
    {
        $file = $request->file('wangEditorH5File');
        if ($file) {
            if ($file->isValid()) {
                // 获取文件相关信息
                $ext = $file->getClientOriginalExtension(); // 扩展名
                $realPath = $file->getRealPath(); //临时文件的绝对路径
                // 上传文件
                $filename = date('Ymd') . '/' . uniqid() . '.' . $ext;
                // 使用我们新建的uploads本地存储空间（目录）
                $bool = Storage::disk('admin')->put('/wangeditor/' . $filename, file_get_contents($realPath));
                if ($bool) {
                    echo asset('/uploads/wangeditor/' . $filename);
                } else {
                    echo 'error|上传失败';
                }
            } else {
                echo 'error|上传失败';
            }
        } else {
            echo 'error|图片类型不正确';
        }
    }

    /**
     * @Desc: 无权限界面
     * @Author: woann <304550409@qq.com>
     * @return \Illuminate\View\View
     */
    public function noPermission()
    {
        return view('base.403');
    }

    public function excelUpload(Request $request)
    {
        $file = $request->file('file');
        if ($request->isMethod('post') && $file) {
            //> 获取上传文件路径 $_FILES
            if ($_FILES['file']['error'] == 0) {
                //> 获取上传文件名称(已便于后面判断是否上传需要后缀文件)
                $name = $_FILES['file']['name'];
                //> 获取上传文件后缀 如(xls exe xlsx 等)
                $ext = strtolower(trim(substr($name, (strpos($name, '.') + 1))));
                //> 判断文件是否为指定的上传文件后缀
                if (!in_array($ext, array('xls', 'xlsx'))) {
                    //> 返回上一次请求位置,并携带错误消息
                    return redirect()->back()->withErrors('请输入xls或xlsx后缀文件')->withInput();
                }
                //> 获取文件上传路径
                $fileName = $_FILES['file']['tmp_name'];
                //> excel文件导入 上传文件
                Excel::load($fileName, function ($reader) {
                    //> 处理上传文件数据 此时 处理多个上传的 sheet 文件
                    foreach ($reader->get() as $item) {
                        //> 处理相关上传excel数据
                        dump($item);
                    }
                });
            }
            exit;
        }
    }
}
