<?php

namespace App\Admin\Controllers\Finance;

use App\Models\AccountRule;
use App\Models\ActivityRule;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class CensusController extends AdminController
{

    protected $today = false;

    /**
     * Index interface.
     * @param  Content  $content
     * @return Content
     */
    public function index(Content $content): Content
    {
        return $content
            ->header('核销总数据')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * 今日核销数据
     * @param  Content  $content  [description]
     * @return \Encore\Admin\Layout\Content [type]           [description]
     */
    public function today(Content $content): Content
    {
        $this->today = true;

        return $content
            ->header('核销今日数据')
            ->description('description')
            ->body($this->grid());
    }

    protected function grid(): Grid
    {
        $grid = new Grid(new User);

        $grid->model()
             ->whereHas('identity', function ($q) {
                 $q->where('identity_id', 1);
             });

        $grid->disableCreateButton();
        $grid->disableBatchActions();
        $grid->disableActions();

        $grid->filter(function ($filter) {
            $filter->column(1 / 2, function ($filter) {
                $filter->where(function ($query) {
                    $query->whereHas('info', function ($query) {
                        $query->where('nickname', 'like', "%{$this->input}%");
                    });
                }, '渠道名称');
            });
        });

        $grid->column('渠道')->display(function () {
            return $this->info->nickname;
        });

        $today = $this->today;
        $rules = ActivityRule::get();

        foreach ($rules as $rule) {
            $grid->column(str_replace('.', '。', $rule->title))
                 ->display(function () use ($today, $rule) {
                     return $this->getCouponCount($rule->code, $today);
                 });
        }
        //        $grid->column('100元减10元优惠券')->display(function () use ($today) {
        //            return $this->getCouponCount('YSD-full100-10', $today);
        //        });
        //
        //        $grid->column('100元减25元优惠券')->display(function () use ($today) {
        //            return $this->getCouponCount('YSD-full100-25', $today);
        //        });
        //        $grid->column('100元减50元优惠券')->display(function () use ($today) {
        //            return $this->getCouponCount('YSD-full100-50', $today);
        //        });
        //
        //        $grid->column(' 200元减100元优惠券')->display(function () use ($today) {
        //            return $this->getCouponCount('YSD-full200-100', $today);
        //        });

        return $grid;
    }

}
