<?php

namespace App\Admin\Controllers\Finance;

use App\Admin\Actions\Coupon\Batch;
use App\Admin\Actions\Coupon\BatchProfit;
use App\Admin\Actions\Coupon\BatchProfits;
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->model()->with(['outlet', 'user', 'user.info'])->where('profit', '>', 0);
        $grid->disableCreateButton();
        $grid->disableBatchActions(false);

        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new Batch);
            $tools->batch(function ($batch) {
                $batch->disableDelete();
                $batch->add(new BatchProfits);
            });
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();
            $actions->disableDelete();
            if ($actions->row->canProfit()) {
                $actions->add(new BatchProfit);
            }
        });

        $grid->model()->where('status', 2)->orderBy('id', 'desc');

        $grid->filter(function ($filter) {
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('is_profit', '打款')->select([
                    '0' => '未打款',
                    '1' => '已打款',
                ]);
                $filter->between('created_at', '核销时间')->datetime();
                $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('状态')->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('打款')->display(function () {
            switch ($this->is_profit) {
                case 0:
                    return '<span style="color:green">未打款</span>';
                    break;
                case 1:
                    return '<span style="color:red">已打款</span>';
                    break;
                default:
                    return '未知';
                    break;
            }
        });

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

        $grid->footer(function ($query) {
            $total   = $query->count();
            $success = $query->where('status', 2)->count();
            $faield  = $total - $success;

            return '<label class="label label-success">全部:' . $total . '张</label>&nbsp;&nbsp;'
                   . '<label class="label label-success">核销金额:' . $query->sum('price') . '元</label>&nbsp;&nbsp;'
                   . '<label class="label label-success">资金通道结算:' . $query->sum('profit') . '元</label>&nbsp;&nbsp;'
                   . '<label class="label label-success">打款金额:' . $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('打款', 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('profit', function ($value, $original) {
                return strip_tags($value) . "\t";
            });
            $export->filename('平安渠道打款处理' . date("YmdHis"));
        });

        return $grid;
    }

}
