<?php

namespace App\Admin\Controllers\Platform;

use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\InfoBox;
use Illuminate\Routing\Controller;
use Modules\Mall\Models\Order;
use Modules\Mall\Models\OrderItem;
use Modules\User\Models\User;
use Modules\User\Models\UserStock;

class DashboardController extends Controller
{
    public function index(Content $content): Content
    {
        $this->content = $content->title('数据看板')->description('Description...');
        $this->getVipUserData();
        $this->getUserStockData();
        return $this->content;

    }

    /**
     * Notes: 获取用户数据
     *
     * @Author: 玄尘
     * @Date  : 2021/11/17 11:24
     */
    public function getVipUserData(): Content
    {
        $this->content->row($this->setDivider('会员统计'));
        $users = [
            'all'      => [
                'name'  => '总会员量',
                'color' => 'blue',
                'count' => User::query()
                    ->whereHas('identities', function ($q) {
                        $q->where('id', '>', 1);
                    })->count()
            ],
            'free_vip' => [
                'name'  => '免费会员',
                'color' => 'green',
                'count' => User::query()
                    ->whereHas('identities', function ($q) {
                        $q->FreeVip();
                    })->count()
            ],
            'yk'       => [
                'name'  => '月卡用户数',
                'color' => 'red',
                'count' => User::query()
                    ->whereHas('identities', function ($q) {
                        $q->Yk();
                    })->count()
            ],
            'jk'       => [
                'name'  => '季卡用户数',
                'color' => 'red',
                'count' => User::query()
                    ->whereHas('identities', function ($q) {
                        $q->Jk();
                    })->count()
            ],
            'nk'       => [
                'name'  => '年卡用户数',
                'color' => 'yellow',
                'count' => User::query()
                    ->whereHas('identities', function ($q) {
                        $q->Nk();
                    })->count(),
            ],
            'onlone'   => [
                'name'  => '在线会员数',
                'color' => 'red',
                'count' => User::query()
                    ->whereHas('identities', function ($q) {
                        $q->where('id', '>', 2);
                    })
                    ->where('status', User::STATUS_INIT)
                    ->count(),
            ],
            'refund'   => [
                'name'  => '退费会员数',
                'color' => 'maroon',
                'count' => User::query()
                    ->whereHas('identities', function ($q) {
                        $q->where('id', '>', 2);
                    })
                    ->where('status', User::STATUS_REFUND)
                    ->count(),
            ],
        ];

        $this->content->row(function (Row $row) use ($users) {
            foreach ($users as $user) {
                $row->column(2, function (Column $column) use ($user) {
                    $column->append(new InfoBox(
                        $user['name'],
                        'users',
                        $user['color'],
                        '/admin/platform/vips',
                        $user['count'],
                    ));

                });
            }
        });

        return $this->content;
    }


    /**
     * Notes: 用户水数量
     *
     * @Author: 玄尘
     * @Date: 2022/9/1 13:35
     * @return mixed
     */
    public function getUserStockData()
    {
        $this->content->row($this->setDivider('会员库存数'));

        $all   = UserStock::query()->sum('stock');
        $holds = UserStock::query()->sum('hold');
        $sy    = bcsub($all, $holds);

        $users = [
            'all'     => [
                'name'  => '累计总箱数',
                'color' => 'blue',
                'count' => $all
            ],
            'stock'   => [
                'name'  => '累计提货数',
                'color' => 'green',
                'count' => $holds
            ],
            'sy'      => [
                'name'  => '累计剩余',
                'color' => 'green',
                'count' => $sy
            ],
            'online'  => [
                'name'  => '线上发货',
                'color' => 'green',
                'count' => OrderItem::query()
                    ->whereHas('order', function ($q) {
                        $q->whereIn('state', [
                            Order::STATUS_PAID,
                            Order::STATUS_DELIVERED,
                            Order::STATUS_SIGNED,
                        ])->where('channel', Order::CHANNEL_USER);
                    })
                    ->sum('qty')
            ],
            'offline' => [
                'name'  => '线下发货',
                'color' => 'green',
                'count' => OrderItem::query()
                    ->whereHas('order', function ($q) {
                        $q->whereIn('state', [
                            Order::STATUS_PAID,
                            Order::STATUS_DELIVERED,
                            Order::STATUS_SIGNED,
                        ])->where('channel', Order::CHANNEL_SYSTEM);
                    })
                    ->sum('qty')
            ],
        ];

        $this->content->row(function (Row $row) use ($users) {
            foreach ($users as $user) {
                $row->column(2, function (Column $column) use ($user) {
                    $column->append(new InfoBox(
                        $user['name'],
                        'goods',
                        $user['color'],
                        '/admin/users/stocks',
                        $user['count'],
                    ));

                });
            }
        });

        return $this->content;
    }

    public function setDivider($title)
    {
        return <<<HTML
<div style="height: 20px; border-bottom: 1px solid #eee; text-align: center;margin-top: 20px;margin-bottom: 20px;">
  <span style="font-size: 18px;  padding: 0 10px;">
    {$title}
  </span>
</div>
HTML;
    }

}