<?php

namespace App\Admin\Controllers\Finance;

use App\Admin\Exporters\CouponProfitExport;
use App\Models\Coupon;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;

class LogController extends AdminController
{

    protected $title = '已打款记录';

    /**
     * Notes:
     * @Author: <C.Jason>
     * @Date  : 2019/9/18 14:50
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Coupon);
        $grid->model()->where('profit', '>', 0);

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

        $grid->model()->where('is_profit', 1)->orderBy('id', 'desc');

        $grid->filter(function ($filter) {
            $filter->column(1 / 2, function ($filter) {
                $filter->between('paid_at', '打款时间')->datetime();
                $users = User::query()
                             ->whereHas('identity', function ($query) {
                                 $query->where('identity_id', 1);
                             })
                             ->get()
                             ->pluck('nickname', 'id');
                $filter->equal('user_id', '渠道')->select($users);
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->like('redemptionCode', '平安券编号');
                $filter->where(function ($query) {
                    $query->whereHas('outlet', function ($query) {
                        $query->whereHas('info', function ($query) {
                            $query->where('nickname', 'like', "%{$this->input}%");
                        });
                    });
                }, '网点名称');
            });
        });

        $grid->column('id', '#ID#');
        $grid->column('渠道')->display(function () {
            return $this->user->nickname;
        });

        $grid->column('网点名称/编号')->display(function () {
            return $this->outlet ? $this->outlet->nickname : $this->outletId;
        });

        $grid->column('redemptionCode', '平安券编号');
        $grid->column('couponName', '优惠政策');
        $grid->column('price', '核销金额');
        $grid->column('profit', '资金通道结算');

        $grid->column('paid_at', '打款时间');

        $grid->footer(function ($query) {
            $all    = $query->get();
            $pass   = $query->where('status', 2)->get();
            $reject = $query->where('status', 3)->get();

            return '<label class="label label-success">全部:' . $all->count() . '张</label>&nbsp;&nbsp;'
                   . '<label class="label label-success">核销金额:' . $pass->sum('price') . '元</label>&nbsp;&nbsp;'
                   . '<label class="label label-success">资金通道结算:' . $pass->sum('profit') . '元</label>&nbsp;&nbsp;'
                   . '<label class="label label-success">打款金额:' . $pass->where('is_profit', 1)
                                                                       ->sum('profit') . '元</label>&nbsp;&nbsp;';
        });
        $grid->disableExport(false);

        $grid->export(function ($export) {
            $export->column('状态', function ($value, $original) {
                return strip_tags($value);
            });
            $export->column('打款', function ($value, $original) {
                return strip_tags($value);
            });
            $export->column('redemptionCode', function ($value, $original) {
                return $value . "\t";
            });
            $export->column('网点名称/编号', function ($value, $original) {
                return $value . "\t";
            });
            $export->column('price', function ($value, $original) {
                return $value . "\t";
            });
            $export->column('profit', function ($value, $original) {
                return strip_tags($value) . "\t";
            });
            $export->filename('已打款记录' . date("YmdHis"));
        });

        return $grid;
    }

}
