<?php

namespace app\controller;

use app\BaseController;
use app\model\MerchantStatisModel;
use app\model\UserBehaviorLogModel;
use app\common\CommonUtils;
use app\model\GameBetGameModel;
use app\model\MerchantsUserModel;
use think\facade\Request;

class Overview extends BaseController
{
    /**
     * 仪表盘 - 获取概览统计数据
     */
    public function Dashboard()
    {
        $userInfo = $this->getUserInfo();
        
        try {
            // 获取昨、今日时间范围
            $today = date('Y-m-d');
            $filtter = [
                'start_time' => date('Y-m-d', strtotime($today) - 86400),
                'end_time' => date('Y-m-d', strtotime($today) + 86400),
            ];       
            $merchantId = $userInfo['merchant_id'];
            
            // 获取昨、今日游戏相关统计
            $dailySummary = GameBetGameModel::getGameDailySummary($merchantId, $filtter);
            // 获取玩家统计数据
            $playerSummary = MerchantsUserModel::getPlayerStatistics($merchantId, $filtter);
            $tempPlayerData = [];
            if ($playerSummary) {
                foreach ($playerSummary as $row) {
                    $date = $row['date'] == $today ? 'today' : 'yesterday';
                    $tempPlayerData[$date] = $row;
                }                
            }

            if ($dailySummary) {
                foreach ($dailySummary as $row) {

                    $date = $row['date'] == $today ? 'today' : 'yesterday';
                    $row['commission'] = CommonUtils::convertBalance($row['game_profit'] * 0.08, false);
                    $row['bet_amount'] = CommonUtils::convertBalance($row['bet_amount'], false);
                    $row['game_profit'] = CommonUtils::convertBalance($row['game_profit'], false);
                    $row['register_users'] = $tempPlayerData[$date]['register_users'] ?? 0;
                    $row['login_users'] = $tempPlayerData[$date]['login_users'] ?? 0;
                    $row['online_users'] = 0;
                    $dashboardData[$date] = $row;
                }
            }
                      
            // 在线人数、登录数据
            $dashboardData['online_data'] = [];
            $dashboardData['login_data'] = [];

            return json_success($dashboardData, '获取成功');
            
        } catch (\Exception $e) {
            return json_error([], '获取仪表盘数据失败' . $e->getMessage());
        }
    }

    /**
     * 结算报表 - 按日期查询投注数据
     */
    public function SettlementReport() 
    {
        $userInfo = $this->request->userInfo;
        
        // 获取查询参数
        $page = Request::get('page', 1, 'intval');
        $limit = Request::get('limit', 10, 'intval');

        // 筛选条件
        $filters = [
            // 时间筛选
            'start_time' => Request::get('start_time', date('Y-m-d', strtotime('-7 days')), 'trim'),
            'end_time' => Request::get('end_time', date('Y-m-d'), 'trim'),
        ];
        
        try {
            // 获取商户每日数据
            $result = MerchantStatisModel::getMerchantDailyList(
                $userInfo['merchant_id'],
                $page,
                $limit,
                $filters,                
            );
            
            // 格式化数据
            foreach ($result['list'] as &$item) {
                $item['game_profit'] = CommonUtils::convertBalance($item['game_profit'], false);
                $item['game_profit_usdt'] = $item['game_profit'];
                $item['bet_amount'] = CommonUtils::convertBalance($item['bet_amount'], false);
                $item['bet_amount_usdt'] = $item['bet_amount'];
                $item['platform_fee'] = CommonUtils::convertBalance($item['bet_amount'] * 0.08, false);
                $item['platform_fee_usdt'] = $item['platform_fee'];
                $item['region'] = '';
            }
            
            return json_success($result, '获取成功');
        } catch (\Exception $e) {
            return json_error([], '获取商户每日数据失败：' . $e->getMessage());
        }
    }

    /**
     * 导出结算报表
     */
    public function exportSettlement() {
        ;
    }
}