<?php

namespace App\Admin\Metrics\Examples;

use App\Models\User;
use App\Models\UserLock;
use Carbon\Carbon;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
class NewLock extends Line
{
    /**
     * 初始化卡片内容
     *
     * @return void
     */
    protected function init()
    {
        parent::init();

        $this->title(admin_trans('admin-home.New_Lock'));
        $this->dropdown([
            '7' => admin_trans('admin-home.Last_7_days'),
            '30' => admin_trans('admin-home.Last_month'),
//            '180' => admin_trans('admin-home.Last_half_year'),
//            '365' => admin_trans('admin-home.Last_year'),
        ]);
    }

    /**
     * 处理请求
     *
     * @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_lock("365");
                // 卡片内容
                $this->withContent($user['num']);
                // 图表数据
                $this->withChart($user['num_arr']);
                break;
            case '30':
                $user = $this->new_lock("30");
                // 卡片内容
                $this->withContent($user['num']);
                // 图表数据
                $this->withChart($user['num_arr']);
                break;
            case '180':
                $user = $this->new_lock("180");
                // 卡片内容
                $this->withContent($user['num']);
                // 图表数据
                $this->withChart($user['num_arr']);
                break;
            case '7':
            default:
            $user = $this->new_lock("7");
            // 卡片内容
            $this->withContent($user['num']);
            // 图表数据
            $this->withChart($user['num_arr']);
            break;
        }
    }

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

        if ($kind=='365'){
            $t_last = date('Y-m-d H:i:s',strtotime('-1 year'));
            $t_time = 86400*12;
            $count = 13;
        }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 = UserLock::query()->whereBetween('created_at',array($t_last,$t_now))->count();
        //计算会员增长数量
        for ($i=1 ; $i < $count; $i++) {
            $today = strtotime(date('Ymd'));
            $start_time = date('Y-m-d 23:59:59',$today-$t_time*$i);
            $end_time  = date('Y-m-d 23:59:59',$today-$t_time*($i-1));
            $inc_num = UserLock::query()->whereBetween('created_at',[$start_time,$end_time])->count();
            $arr_n[]=$inc_num;
        }
        //逆序输出数组
        //$arr_t=array_reverse($arr_t);  //时间
        $arr_n=array_reverse($arr_n);  //数量
        return array('num'=>$num,'num_arr'=>$arr_n);
    }

    /**
     * 设置图表数据.
     *
     * @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
        );
    }
}
