<?php

namespace App\Admin\Metrics\Examples;

use App\Models\User;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;

class NewUsers extends Line
{
    /**
     * 初始化卡片内容
     *
     * @return void
     */
    protected function init()
    {
        parent::init();

        $this->title(trans('admin-home.New_Users'));
        $this->dropdown([
            '7' => trans('admin-home.Last_7_days'),
            '30' => trans('admin-home.Last_month'),
            '180' => trans('admin-home.Last_half_year'),
            '365' => trans('admin-home.Last_year'),
        ]);
       // dd($this->new_user("365"));
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $generator = function ($len, $min = 10, $max = 300) {
            for ($i = 0; $i <= $len; $i++) {
                yield mt_rand($min, $max);
            }
        };

        switch ($request->get('option')) {
            case '365':
                $user = $this->new_user("365");
                // 卡片内容
                $this->withContent($user['num']);
                // 图表数据
                $this->withChart($user['num_arr']);
                break;
            case '180':
                $user = $this->new_user("180");
                // 卡片内容
                $this->withContent($user['num']);
                // 图表数据
                $this->withChart($user['num_arr']);
                break;
            case '30':
                $user = $this->new_user("30");
                // 卡片内容
                $this->withContent($user['num']);
                // 图表数据
                $this->withChart($user['num_arr']);
                break;
            case '7':
            default:
                $user = $this->new_user("7");
                // 卡片内容
                $this->withContent($user['num']);
                // 图表数据
                $this->withChart($user['num_arr']);
        }
    }

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

    /**
     * 设置卡片内容.
     *
     * @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>
    <span class="mb-0 mr-1 text-80">{$this->title}</span>
</div>
HTML
        );
    }



    /**
     *  新用户 一年
     *   write by wanglas
     */
    public function new_user($kind){

        if ($kind=='365'){
            $t_last = date('Y-m-d H:i:s',strtotime('-1 year'));
            $t_time = 86400*12;
            $count = 31;
        }elseif ($kind=='180'){
            $t_last = date('Y-m-d H:i:s',strtotime('-180 day'));
            $t_time = 86400*6;
            $count = 31;
        }elseif ($kind=='30'){
            $t_last = date('Y-m-d H:i:s',strtotime('-30 day'));
            $t_time = 86400;
            $count = 31;
        }elseif ($kind=='7'){
            $t_last = date('Y-m-d H:i:s',strtotime('-7 day'));
            $t_time = 86400;
            $count = 8;
        }
        //统计会员总数
        $t_now = date('Y-m-d H:i:s');
        $num=User::query()->whereBetween('created_at',array($t_last,$t_now))->whereNull('deleted_at')->count();
        //计算会员增长数量
        for ($i=1 ; $i < $count; $i++) {
            $today=strtotime(date('Ymd'));
            $now  = date('Y-m-d H:i:s',$today-$t_time*($i-1));
            $time = date('Y-m-d H:i:s',$today-$t_time*$i);
            $inc_num=User::query()->whereBetween('created_at',array($time,$now))->count();
            //$arr_t[]=$now;
            $arr_n[]=$inc_num;
        }
        //逆序输出数组
        //$arr_t=array_reverse($arr_t);  //时间
        $arr_n=array_reverse($arr_n);  //数量
        return array('num'=>$num,'num_arr'=>$arr_n);
    }
}
