<?php
/**
 * Created by PhpStorm.
 * User: 陈武杰
 * Date: 2021/1/15
 * Time: 10:31
 */

namespace App\Admin\Controllers\DataCenter;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Echarts\Echarts;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Layout\Row;
use Encore\Admin\Layout\Column;
use Encore\Admin\Widgets\Collapse;
use App\Models\Order;

class FinancialRechargeChart
{
    public  function chart(Row $row)
    {
        $row->column(12,function (Column $column){

            $form = new Form();
            $form->disableReset();
            $form->disableSubmit();
            $ry = request('year');
            $rm = request('month');
            //是否是初始化
            $isb = request('b');
//            dd($isb);
            $year = range(2000,2099);
            $month = range(1,12);
            $nowYear = date('Y');
            $nowMonth = date('m');

            //默认数据获取方式 1默认选择当前月份 2根据时间
            $type = 2;
            if(empty($ry) && empty($rm)){
                $type = 1;
            }

            //判断是否是初始化
            if(empty($isb)){
                $ry = $nowYear;
                $rm = $nowMonth;
            } else {
                $type = 2;
            }
            //生成对应年月的键名的数组
            foreach ($year as $v){
                $realYear[$v]=$v;
            }
            foreach ($month as $v){
                $realMonth[$v]=$v;
            }
            //获取数据 传入参数 真实的年、月、和类型 1默认选择当前月份 2根据时间
            $chartData =self::getData($ry,$rm,$type);

            $filter_content = new Row(function (Column $col)use ($form,$type,$realYear,$ry,$rm,$realMonth){
                $col->row(function (Row $row)use ($form,$type,$realYear,$ry,$rm,$realMonth){
                    $form->method('get');
                    $form->select('year','年份')->options($realYear)->default($ry);
                    $form->select('month','月份')->options($realMonth)->default($rm);
                    $form->hidden('b')->default(1);
                    $form->html("<button type='submit'  class='btn btn-success'>筛选</button><a style='margin-left: 20px;' href='/admin/financial_recharge_overview'  class='btn btn-danger'>重置</a>");
                    $form->action('/admin/financial_recharge_overview');
                    $row->column(6,$form->render());
                });
            });
            $col = new Collapse();
            $col->add('日期筛选',$filter_content->render());

            $column->append($col->render());
            $column->row(function (Row $row)use ($chartData){
                $row->column(12,function (Column $column)use ($chartData){
                    $echarts = (new Echarts('充值走势图', '','user_change'))
                        ->setData($chartData['d'])
                        ->bindLegend($chartData['h'])->setShowToolbox(true);
                    return $column->append(new  Box('',$echarts));
                });
            });
        });
    }

    public function getData($year,$month,$type)
    {

        //type 1默认选择当前月份 2根据时间+
        if(intval($month) < 10){
            $month  = '0'.$month;
        }

        if($type == 1){
            //查找天

            $data = Order::query()
                ->selectRaw('DATE_FORMAT(created_at,"%m-%d") as day')
                ->selectRaw('SUM(payment_amount) as sum')
                ->where('product_type',7)
                ->where('payment_status',2)
                ->where('created_at', '>=', $year.'-'.$month)
                ->where('created_at', '<', $year.'-'.($month+1))
                ->groupBy('day')
                ->orderBy('day')
                ->get()->toArray();
//            dd($data);

            $data = self::getFomateData($data,3);

            return $data;
        }

        if(empty($year) && empty($month)){
            //查找年
            $data = Order::query()
                ->selectRaw('DATE_FORMAT(created_at,"%Y") as day')
                ->selectRaw('SUM(payment_amount) as sum')
                ->where('product_type',7)
                ->where('payment_status',2)
                ->where('created_at', '>', '2000-')
                ->where('created_at', '<', '2099-')
                ->groupBy('day')
                ->orderBy('day')
                ->get()->toArray();
            $data = self::getFomateData($data,1);
            return $data;
        }

        if($year && $month){
            //查找天
            $data = Order::query()
                ->selectRaw('DATE_FORMAT(created_at,"%m-%d") as day')
                ->selectRaw('SUM(payment_amount) as sum')
                ->where('product_type',7)
                ->where('payment_status',2)
                ->where('created_at', '>=', $year.'-'.$month)
                ->where('created_at', '<', $year.'-'.($month+1))
                ->groupBy('day')
                ->orderBy('day')
                ->get()->toArray();
            $data = self::getFomateData($data,3);

            return $data;

        }

        if($year){
            //查找月
            $data = Order::query()
                ->selectRaw('DATE_FORMAT(created_at,"%m") as day')
                ->selectRaw('SUM(payment_amount) as sum')
                ->where('product_type',7)
                ->where('payment_status',2)
                ->where('created_at', '>=', $year.'-')
                ->where('created_at', '<', ($year+1).'-')
                ->groupBy('day')
                ->orderBy('day')
                ->get()->toArray();
            $data = self::getFomateData($data,2);
            return $data;
        }
        return self::getFomateData([],3);

    }

    public static function getFomateData($data,$type)
    {
        $typeArr = [1=>'年',2=>'月',3=>'日'];
        $chartData['d'] = [];
        foreach ($data as $v){
            $chartData['d'][] = ['day'=>$v['day'].$typeArr[$type],'sum'=>$v['sum']/100];
        }
        $chartData['h'] = ['day'=>$typeArr[$type],'sum'=>'充值金额'];

        return $chartData;
    }
}