<?php

namespace App\Store\Metrics\Examples;

use App\Models\OrderInfo as OrderInfoModel;
use Dcat\Admin\Widgets\Metrics\Card;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;

class ProductOrders extends Card
{
    /**
     * 卡片底部内容.
     *
     * @var string|Renderable|\Closure
     */
    protected $footer;

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

        $this->title('用户已购买总数');
        $this->dropdown([
            '1' => '今天',
            '7' => '最近7天',
            '30' => '最近30天',
            '365' => '最近一年',
        ]);
    }

    /**
     * 处理请求.
     *
     * @param Request $request
     *
     * @return void
     */
    public function handle(Request $request)
    {
        switch ($request->get('option')) {
            case '365':
                $data = OrderInfoModel::getYearData();
                // 卡片内容
                $this->withContent($data['total']);
                // 图表数据
                //$this->withChart($data['list']);
                break;
            case '30':
                $data = OrderInfoModel::getNumDayData(30);
                // 卡片内容
                $this->withContent($data['total']);
                // 图表数据
                //$this->withChart($data['list']);
                break;
            case '7':
                $data = OrderInfoModel::getNumDayData(7);
                // 卡片内容
                $this->withContent($data['total']);
                // 图表数据
                //$this->withChart($data['list']);
                break;
            default:
                $data = OrderInfoModel::getNumDayData(1);
                // 卡片内容
                $this->withContent($data['total']);
                // 图表数据
                //$this->withChart($data['list']);
        }
    }



    /**
     * 设置卡片底部内容.
     *
     * @param string|Renderable|\Closure $footer
     *
     * @return $this
     */
    public function footer($footer)
    {
        $this->footer = $footer;

        return $this;
    }

    /**
     * 设置卡片内容.
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
    <h2 class="ml-1 font-lg-1">{$content}</h2>
</div>
HTML
        );
    }
}