<?php

namespace App\Admin\Controllers\Coupon;

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

class SettleCouponController extends AdminController
{

    protected $title = '平安券核销列表';

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

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

        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new SettleCoupon());//上传等待校验数据
        });

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

        $grid->filter(function ($filter) {
            $filter->column(1 / 2, function ($filter) {
                $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->like('pa_order_id', '平安主订单号');
                $filter->equal('is_settle', '是否结算')->select(Coupon::SETTLES);
            });
        });

        $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('is_settle', '是否结算')->bool();

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

            return '<span style="color:red">'.$profit.'</span>';
        });
        $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) {
            $total = (clone $query)->count();
            $no    = (clone $query)->where('is_settle', 0)->count();
            $yes   = $total - $no;

            return '<label class="label label-success">全部:'.$total.'张</label>&nbsp;&nbsp;'
                .'<label class="label label-success">已结算:'.$yes.'张</label>&nbsp;&nbsp;'
                .'<label class="label label-success">未结算:'.$no.'张</label>&nbsp;&nbsp;';
        });

        $grid->disableExport(false);

        $grid->export(function ($export) {
            $export->column('type', function ($value, $original) {
                return strip_tags($value);
            });
            $export->column('redemptionCode', function ($value, $original) {
                return $value."\t";
            });

            $export->column('orderid', function ($value, $original) {
                return $value."\t";
            });

            $export->column('资金通道结算', function ($value, $original) {
                return strip_tags($value);
            });
            $export->filename($this->title.date("YmdHis"));
        });

        return $grid;
    }

}
