<?php

namespace app\admin\controller;

use app\admin\AdminBaseController;
use app\common\model\AdminModel;
use app\common\model\FeedbackModel;
use app\common\model\MenuModel;
use app\common\model\UserModel;

class Home extends AdminBaseController
{

    public function index()
    {
        $access_admin = session('access_admin');
        $admin        = AdminModel::find($access_admin['id']);
        $where        = [
            ['mtype', '=', 'admin'],
            ['status', '=', 1],
        ];
        if ($access_admin['id'] != 1) {
            $where[] = ['id', 'in', $admin->powerids];
        }
        $menu_list = MenuModel::where(['mtype' => 'admin', 'status' => 1])
            ->where($where)
            ->order(['pid' => 'asc', 'priority' => 'asc', 'id' => 'asc'])
            ->select()
            ->toArray();
        $powerarr  = [];
        if (!empty($menu_list)) {
            foreach ($menu_list as $k => $v) {
                if ($v['pid'] == 0) {
                    $v['children']      = [];
                    $powerarr[$v['id']] = $v;
                } else {
                    $powerarr[$v['pid']]['children'][] = $v;
                }
            }
        }
        return view('', [
            'access_admin' => $access_admin,
            'powerarr'     => $powerarr,
        ]);
    }

    public function console()
    {
        //用户统计
        $user_count              = [];
        $user_count['today']     = UserModel::whereDay('create_time')->count();
        $user_count['yesterday'] = UserModel::whereDay('create_time', 'yesterday')->count();
        $user_count['week']      = UserModel::whereWeek('create_time')->count();
        $user_count['month']     = UserModel::whereMonth('create_time')->count();
        $user_count['total']     = UserModel::count();

        //意见反馈
        $feedback = FeedbackModel::where('status', FeedbackModel::STATUS_UNREAD)
            ->limit(5)
            ->order('create_time', 'desc')
            ->select();

        return view('home/console', [
            'access_admin' => session('access_admin'),
            'user_count'   => $user_count,
            'feedback'     => $feedback,
        ]);
    }


}