<?php

namespace App\Admin\Metrics;

use App\Models\UserRechargeRecord;
use App\Models\UserVipRecord;
use Carbon\Carbon;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Round;
use Illuminate\Database\Query\Builder;
use Illuminate\Http\Request;

class Recharge extends Round
{
    protected $labels = ['VIP充值金额', '金币充值金额'];

    private $vipRecharge   = 0;
    private $goldRecharge  = 0;
    private $totalRecharge = 0;

    /**
     * 初始化卡片内容.
     */
    protected function init()
    {
        parent::init();

        $color  = Admin::color();
        $colors = [$color->primary(), $color->alpha('blue2', 0.5)];
        $this->height(300);
        $this->chartHeight(300);
        $this->chartRadialBarSize(1);

        $this->title('VIP充值/金币充值金额');
        $this->chartLabels($this->labels);
        // 设置图表颜色
        $this->chartColors($colors);

        $this->dropdown([
            '1'  => '当日新增充值金额',
            '7'  => '7天新增充值金额',
            '30' => '当月新增充值金额',
            '-1' => '总充值金额',
        ]);
    }

    public function handle(Request $request)
    {
        switch ($request->get('option')) {
            case '-1':
                $this->getVipRecharge();
                $this->getGoldRecharge();
                break;
            case '7':
                $startAt = Carbon::now()->subDays(7)->toDateString() . ' 23:59:59';
                $endAt   = Carbon::now()->toDateString() . ' 23:59:59';
                $this->getVipRecharge($startAt, $endAt);
                $this->getGoldRecharge($startAt, $endAt);
                break;
            case '30':
                $startAt = Carbon::now()->subDays(30)->toDateString() . ' 23:59:59';
                $endAt   = Carbon::now()->toDateString() . ' 23:59:59';
                $this->getVipRecharge($startAt, $endAt);
                $this->getGoldRecharge($startAt, $endAt);
                break;
            case '1':
            default:
                $startAt = Carbon::now()->toDateString() . ' 00:00:0';
                $endAt   = Carbon::now()->toDateString() . ' 23:59:59';
                $this->getVipRecharge($startAt, $endAt);
                $this->getGoldRecharge($startAt, $endAt);
                break;
        }
        $this->totalRecharge = $this->vipRecharge + $this->goldRecharge;
        $this->withContent();

        // 图表数据
        $vipPercent  = $this->totalRecharge ? $this->vipRecharge  / $this->totalRecharge : 0;
        $goldPercent = $this->totalRecharge ? $this->goldRecharge / $this->totalRecharge : 0;
        $data        = [
            \round($vipPercent * 100, 2),
            \round($goldPercent * 100, 2),
        ];
        $this->withChart($data);
        $this->chartTotal('总充值金额', $this->totalRecharge);
    }

    private function getVipRecharge($statAt = '', $endAt = '')
    {
        $this->vipRecharge = UserVipRecord::withSum('pay', 'order_fee')
            ->whereHas('pay', function ($query) use ($statAt, $endAt) {
                /* @var Builder $query */
                $query->where('status', 1)
                ->when($statAt, function ($query) use ($statAt) {
                    /* @var Builder $query */
                    $query->where('created_at', '>', $statAt);
                })->when($statAt, function ($query) use ($endAt) {
                    /* @var Builder $query */
                    $query->where('created_at', '<=', $endAt);
                });
            })
            ->get()
            ->sum('pay_sum_order_fee');
    }

    private function getGoldRecharge($statAt = '', $endAt = '')
    {
        $this->goldRecharge = UserRechargeRecord::withSum('pay', 'order_fee')
            ->whereHas('pay', function ($query) use ($statAt, $endAt) {
                /* @var Builder $query */
                $query->where('status', 1)
                    ->when($statAt, function ($query) use ($statAt) {
                        /* @var Builder $query */
                        $query->where('created_at', '>', $statAt);
                    })->when($statAt, function ($query) use ($endAt) {
                        /* @var Builder $query */
                        $query->where('created_at', '<=', $endAt);
                    });
            })
            ->get()
            ->sum('pay_sum_order_fee');
    }

    /**
     * 设置图表数据.
     *
     * @return $this
     */
    public function withChart($data): Recharge
    {
        return $this->chart([
            'series' => $data,
        ]);
    }

    /**
     * 设置卡片头部内容.
     *
     * @return $this
     */
    protected function withContent(): Recharge
    {
        $blue = Admin::color()->alpha('blue2', 0.5);

        $style      = 'margin-bottom: 8px';
        $labelWidth = 120;

        return $this->content(
            <<<HTML
<div class="d-flex pl-1 pr-1 pt-1" style="{$style}">
    <div style="width: {$labelWidth}px">
        <i class="fa fa-circle text-success"></i> 总充值金额
    </div>
    <div>{$this->totalRecharge}</div>
</div>
<div class="d-flex pl-1 pr-1" style="{$style}">
    <div style="width: {$labelWidth}px">
        <i class="fa fa-circle text-primary"></i> {$this->labels[0]}
    </div>
    <div>{$this->vipRecharge}</div>
</div>
<div class="d-flex pl-1 pr-1" style="{$style}">
    <div style="width: {$labelWidth}px">
        <i class="fa fa-circle" style="color: $blue"></i> {$this->labels[1]}
    </div>
    <div>{$this->goldRecharge}</div>
</div>
HTML
        );
    }
}
