<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Auth\Permission;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\InfoBox;
use Illuminate\Support\Facades\Artisan;
use Modules\Mall\Models\Order;
use Modules\Mall\Models\OrderItem;
use Modules\User\Models\User;
use Modules\User\Models\UserStock;

class HomeController extends Controller
{
    public $content;

    /**
     * Notes   : 数据看板
     *
     * @Date   : 2021/3/10 5:12 下午
     * @Author : < Jason.C >
     * @param  Content  $content
     * @return Content
     */
    public function index(Content $content)
    {
        $this->content = $content->title('数据看板')->description('Description...');
        $admin         = Admin::user();
        if ($admin->id == 1) {
            $this->getUserData();
            $this->getUserStockData();
            $this->getUserStockOrderData();
        } else {
            $this->content->row($this->setDivider('您没有权限查看数据'));
        }

        return $this->content;
    }

    /**
     * Notes: 获取用户数据
     *
     * @Author: 玄尘
     * @Date  : 2021/11/17 11:24
     */
    public function getUserData()
    {
        $this->content->row($this->setDivider('用户统计'));

        $users = [
            'all' => [
                'name'  => '用户总数',
                'color' => 'blue',
                'count' => User::count()
            ],
            'ty'  => [
                'name'  => '月卡',
                'color' => 'green',
                'count' => User::query()
                    ->whereHas('identities', function ($q) {
                        $q->where('id', 2);
                    })->count()
            ],
            'jk'  => [
                'name'  => '季卡用户数',
                'color' => 'red',
                'count' => User::query()
                    ->whereHas('identities', function ($q) {
                        $q->where('id', 3);
                    })->count()
            ],
            'nk'  => [
                'name'  => '年卡用户数',
                'color' => 'yellow',
                'count' => User::query()
                    ->whereHas('identities', function ($q) {
                        $q->where('id', 4);
                    })->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/users',
                        $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' => UserStock::query()->sum('stock')
            ],
            'stock' => [
                'name'  => '已提货数',
                'color' => 'green',
                'count' => UserStock::query()->sum('hold')
            ],
            'sy'    => [
                'name'  => '待提货数',
                'color' => 'green',
                'count' => $sy
            ],
        ];

        $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/stocks',
                        $user['count'],
                    ));

                });
            }
        });

        return $this->content;
    }

    /**
     * Notes: 提货订单数量
     *
     * @Author: 玄尘
     * @Date: 2022/9/1 13:51
     */
    public function getUserStockOrderData()
    {
        $this->content->row($this->setDivider('会员提货订单'));


        $deliver = OrderItem::query()
            ->whereHas('order', function ($q) {
                $q->paid();
            })->sum('qty');

        $deliverd = OrderItem::query()
            ->whereHas('order', function ($q) {
                $q->whereIn('state', [
                    Order::STATUS_SIGNED,
                    Order::STATUS_DELIVERED,
                ]);
            })->sum('qty');

        $users = [
            'all'      => [
                'name'  => '订单总数',
                'color' => 'blue',
                'count' => Order::query()->where('type', Order::TYPE_SAMPLE)->count()
            ],
            'deliver'  => [
                'name'  => '待发货',
                'color' => 'green',
                'count' => Order::query()
                    ->where('type', Order::TYPE_SAMPLE)
                    ->paid()
                    ->count()
            ],
            'deliverd' => [
                'name'  => '已发货',
                'color' => 'green',
                'count' => Order::query()
                    ->where('type', Order::TYPE_SAMPLE)
                    ->whereIn('state', [
                        Order::STATUS_DELIVERED,
                    ])
                    ->count(),
            ],
            'signed'   => [
                'name'  => '已签收',
                'color' => 'green',
                'count' => Order::query()
                    ->where('type', Order::TYPE_SAMPLE)
                    ->whereIn('state', [
                        Order::STATUS_SIGNED,
                    ])
                    ->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'],
                        'goods',
                        $user['color'],
                        '/admin/stocks',
                        $user['count'],
                    ));

                });
            }
        });

        return $this->content;
    }

    /**
     * Notes   : 清理模型缓存
     *
     * @Date   : 2021/6/8 10:51 上午
     * @Author : < Jason.C >
     * @return string
     */
    public function cleanCache(): string
    {
        Artisan::call('modelCache:clear');

        return '缓存清理成功';
    }

    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;
    }

}
