<?php

namespace App\Admin\Controllers\Coupon;

use App\Admin\Actions\Coupon\RefreshPAOrder;
use App\Models\ActivityRule;
use App\Models\Coupon;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;

class IndexController 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->disableCreateButton();
        $grid->disableBatchActions();
        $grid->disableActions();

        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new refreshPAOrder());//刷新平安订单id
        });

        $grid->model()
            ->with(['outlet.province', 'outlet.city', 'outlet.district', 'user', 'user.info'])
            ->whereIn('status', [2, 3])
            ->orderBy('id', 'desc');

        $grid->filter(function ($filter) {
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('status', '状态')->select([
                    '2' => '核销成功',
                    '3' => '核销失败',
                ]);
                $filter->between('created_at', '核销时间')->datetime();
                $users = User::whereHas('identity', function ($query) {
                    $query->where('identity_id', 1);
                })->get()->pluck('nickname', 'id');

                $filter->equal('user_id', '渠道')->select($users);
                $filter->equal('thirdPartyGoodsId', '优惠政策')->select(ActivityRule::pluck('title', 'code'));
                $filter->like('couponName', '优惠政策名');
            });
            $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}%");
                        });
                    });
                }, '网点名称');

                $filter->equal('type', '类型')->select(Coupon::TYPES);
                $filter->like('pa_order_id', '平安主订单号');
                $filter->like('pa_sub_order_id', '平安子订单号');
            });
        });

        $grid->column('id', '#ID#');
        $grid->column('渠道')->display(function () {
            return $this->user->nickname;
        });
        $grid->column('type', '类型')
            ->using(Coupon::TYPES)
            ->label([
                '1' => 'info',
                '2' => 'success',
            ]);

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

        $grid->column('redemptionCode', '卡券编号');
        $grid->column('couponName', '优惠政策');
        $grid->column('price', '核销金额');
        $grid->column('total', '订单金额');
        $grid->column('orderid', '订单id');
        $grid->column('pa_order_id', '平安主订单号');
        $grid->column('pa_sub_order_id', '平安子订单号')->hide();

        $grid->column('资金通道结算')->display(function () {
            $profit = $this->status == 2 ? $this->profit : '0.00';

            return '<span style="color:red">'.$profit.'</span>';
        });
        $grid->column('状态')->display(function () {
            switch ($this->status) {
                case 2:
                    return '<span style="color:green">'.$this->status_text.'</span>';
                    break;
                case 3:
                    return '<span style="color:red">'.$this->status_text.'</span>';
                    break;
                default:
                    return $this->status_text;
                    break;
            }
        });

        $grid->column('remark', '处理结果');
        $grid->column('startTime', '起始时间')->hide();
        $grid->column('endTime', '到期时间')->hide();
        $grid->column('created_at', '核销时间');
        $grid->column('省')->display(function () {
            return ($this->outlet && $this->outlet->province) ? $this->outlet->province->name : '';
        });

        $grid->column('市')->display(function () {
            return ($this->outlet && $this->outlet->province) ? $this->outlet->city->name : '';
        });

        $grid->column('区')->display(function () {
            return ($this->outlet && $this->outlet->province) ? $this->outlet->district->name : '';
        });

        $grid->footer(function ($query) {
            // $all  = $query->get();
            // $pass = $all->where('status', 2)->all();
            // $pass = collect($pass);
            $total   = (clone $query)->count();
            $success = (clone $query)->where('status', 2)->count();
            $faield  = $total - $success;

            return '<label class="label label-success">全部:'.$total.'张</label>&nbsp;&nbsp;'
                .'<label class="label label-success">成功:'.$success.'张</label>&nbsp;&nbsp;'
                .'<label class="label label-success">失败:'.$faield.'张</label>&nbsp;&nbsp;'
                .'<label class="label label-success">核销金额:'.(clone $query)->sum('price').'元</label>&nbsp;&nbsp;'
                .'<label class="label label-success">资金通道结算:'.(clone $query)->sum('profit').'元</label>&nbsp;&nbsp;'
                .'<label class="label label-success">打款金额:'.(clone $query)->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('type', function ($value, $original) {
                return strip_tags($value);
            });
            $export->column('redemptionCode', function ($value, $original) {
                return $value."\t";
            });
            //            $export->column('price', function ($value, $original) {
            //                return $value . "\t";
            //            });
            $export->column('orderid', function ($value, $original) {
                return $value."\t";
            });
            //            $export->column('total', function ($value, $original) {
            //                return $value . "\t";
            //            });
            $export->column('资金通道结算', function ($value, $original) {
                return strip_tags($value);
            });
            $export->filename('卡券列表'.date("YmdHis"));
        });

        return $grid;
    }

}
