<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\common\Util;
use support\exception\BusinessException;
use support\Request;
use support\Response;
use Exception;
use support\think\Db;
use Workerman\Worker;

class IndexController extends Base
{

    /**
     * 无需登录的方法
     * @var string[]
     */
    protected $noNeedLogin = ['index'];

    /**
     * 不需要鉴权的方法
     * @var string[]
     */
    protected $noNeedAuth = ['dashboard','upload'];

    /**
     * 后台主页
     * @param Request $request
     * @return Response
     * @throws BusinessException|Exception
     */
    public function index(Request $request): Response
    {
        clearstatcache();
        $admin = admin();
        if (!$admin) {
            $title = config('site.name') ?? 'admin';
            $logo = cdnurl(config('site.admin_logo') ?? admin_path().'/images/logo.png');
            return view('account/login',['logo'=>$logo,'title'=>$title]);
        }
        //缓存
        $list = (new AdminRuleController())->get($request)->rawBody();
        //return $this->success($list);
        $list = json_decode($list,true);
        $menu = $list['data'];
        return view('index/index',[
            'menu'  =>  $menu,
            'user'  =>  admin()
        ]);
    }

    /**
     * 仪表板
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function dashboard(Request $request): Response
    {
        // 今日新增充值
        //$today_user_recharge_sum = Recharge::where('status',2)->whereTime('created_at', 'today')->sum('amount');
        // 7天内新增充值
        $day7_user_recharge_sum = 0;
        for ($i=7; $i >= 0; $i--) { 
            $date = date('Y-m-d',strtotime('-'.$i.' days'));
            $day7_user_recharge_sum +=cache('statistics_recharge_amount_'.$date);
        }
        //$day7_user_recharge_sum = Recharge::where('status',2)->whereTime('created_at', '-7 days')->sum('amount');
        // 总用户数
        $user_count = \app\model\User::where('status',1)->count('id');
        $recharge_total = 0;//\app\model\Recharge::where('status',\app\enum\RechargeStatus::COMPLETE->value)->sum('amount');
        // mysql版本
        $withdrawl_total = \app\model\Withdrawl::where('status',\app\enum\WithdrawlStatus::COMPLETE->value)->sum('recive_amount');
        // mysql版本
        $version = Db::query('select VERSION() as version');
        $mysql_version = $version[0]['version'] ?? 'unknown';

        // $recharge = \app\model\Recharge::where('status',2)->field("FROM_UNIXTIME(created_at, '%Y-%m-%d') AS label, sum(amount) AS value")
        //     ->limit(0,30)->group("label")
        //     ->select()->toArray();
        // $withdrawl = \app\model\Withdrawl::where('status',2)->field("FROM_UNIXTIME(created_at, '%Y-%m-%d') AS label, sum(recive_amount) AS value")
        //     ->limit(0,30)->group("label")
        //     ->select()->toArray();
        // // if(!cache('last_jiaquan_time')){
        //     cache('last_jiaquan_time',strtotime('2025-02-27 00:51:00'));
        // }

        return view('index/dashboard', [
            'today_user_recharge_sum'  => formatAmount(cache('statistics_recharge_amount_'.date('Y-m-d')),0),
            'day7_user_recharge_sum'   => formatAmount($day7_user_recharge_sum,0),
            'user_count'        => $user_count,
            //'recharge'          => $recharge,
            //'withdrawl'         => $withdrawl,
            'recharge_total'    => formatAmount($recharge_total,0),
            'withdrawl_total'   => formatAmount($withdrawl_total,0),
            'user_score_total'  => formatAmount(\app\model\User::sum('score')),
            'user_money_total'  => formatAmount(\app\model\User::sum('money')),
            
            'php_version'       => PHP_VERSION,
            'workerman_version' =>  Worker::VERSION,
            'webman_version'    => Util::getPackageVersion('workerman/webman-framework'),
            'admin_version'     => config('plugin.admin.app.version'),
            'mysql_version'     => $mysql_version,
            'os'                => PHP_OS,
        ]);
    }
    function clean(){
        return $this->success('');
    }
    function role_buy_lines()
    {
        $res = [];
        for ($i=7; $i >= 0; $i--) { 
            $date = date('Y-m-d',strtotime('-'.$i.' days'));
            $res[$date] = [
                'amount'    =>  cache('role_buy_amount_total_'.$date)?:0,
                'reward'    =>  cache('role_buy_reward_total_'.$date)?:0,
                'residual'    =>  cache('role_buy_residual_total_'.$date)?:0,
            ];
        }
        return $this->success('ok',$res);
    }
    function recharge_lines()
    {
        $res = [];
        for ($i=7; $i >= 0; $i--) { 
            $date = date('Y-m-d',strtotime('-'.$i.' days'));
            $res[$date] = [
                'amount'    =>  cache('statistics_recharge_amount_'.$date)?:0,
            ];
        }
        return $this->success('ok',$res);
    }
    function withdrawl_lines()
    {
        $res = [];
        for ($i=7; $i >= 0; $i--) { 
            $date = date('Y-m-d',strtotime('-'.$i.' days'));
            $res[$date] = [
                'amount'    =>  cache('statistics_withdrawl_amount_'.$date)?:0,
            ];
        }
        return $this->success('ok',$res);
    }
    function money_lines()
    {
        $res = [];
        for ($i=7; $i >= 0; $i--) { 
            $date = date('Y-m-d',strtotime('-'.$i.' days'));
            $res[$date] = [
                'withdrawl'    =>  cache('statistics_withdrawl_amount_'.$date)?:0,
                'recharge'    =>  cache('statistics_recharge_amount_'.$date)?:0,
            ];
        }
        return $this->success('ok',$res);
    }

}
