<?php

namespace App\Admin\Controllers\Wo;

use App\Admin\Actions\Wo\Unsubscribe;
use App\Admin\Exporters\WoCouponExport;
use App\Models\User;
use App\Models\WoCoupon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Table;

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 WoCoupon);
        $grid->model()->with(['user']);
        $grid->disableCreateButton();
        $grid->disableBatchActions();
        // $grid->disableActions();

        $grid->model()->whereIn('status', [1, 2, 3])->orderBy('id', 'desc');

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();

            if ($actions->row->status == 1 && $actions->row->service == 'ticketGrant') {
                $actions->add(new Unsubscribe);
            }
        });

        $grid->filter(function ($filter) {
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('status', '状态')->select([
                    '1' => '成功',
                    '2' => '失败',
                    '3' => '退订',
                ]);
                $filter->equal('service', '业务')->select([
                    'ticketGrant'   => '发券请求',
                    'ticketBuyback' => '返销请求',
                    'ticketInvalid' => '作废请求',
                ]);
                $filter->like('mobile', '手机号');
                $filter->like('logNo', '发券流水号');
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->like('number', '工号');
                $users = User::query()
                             ->whereHas('identity', function ($query) {
                                 $query->where('identity_id', 1);
                             })
                             ->where('type', 'wo')
                             ->get()
                             ->pluck('nickname', 'id');
                $filter->equal('user_id', '渠道')->select($users);
                $filter->equal('activityId', '活动')->select([
                    '97LJ202025' => '25元兑换券',
                    '97LJ202010' => '100元兑换券',
                    '97LJ202018' => '180元话费券',
                ]);
                $filter->between('created_at', '处理时间')->datetime();

            });
        });

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

        $grid->column('number', '工号');

        $grid->column('业务')->display(function () {
            return $this->service_text;
        });

        $grid->column('活动')->display(function () {
            return $this->activity_text;
        });
        $grid->column('mobile', '手机号')->display(function ($title, $column) {
            return $this->mobile;
        })->modal(function ($model) {
            $datas = $model->logs->map(function ($data) {
                return [
                    'mobile'        => $data->mobile,
                    'service_text'  => $data->coupon->service_text ?? '---',
                    'activity_text' => $data->coupon->activity_text ?? '---',
                    'status_text'   => $data->coupon->status_text ?? '---',
                    'created_at'    => $data->created_at,
                ];
            });

            return new Table(['手机号', '业务', '活动', '结果', '时间'], $datas->toArray());

        });
        $grid->column('ticketAmt', '电子券面值');
        $grid->column('status', '状态')->display(function () {
            switch ($this->status) {
                case 1:
                    return "<span class='label label-success' >" . $this->status_text . "</span>";
                    break;
                case 2:
                    return "<span class='label label-warning' >" . $this->status_text . "</span>";
                    break;
                case 3:
                    return "<span class='label label-primary' >" . $this->status_text . "</span>";
                    break;
                default:
                    return $this->status_text;
                    break;
            }
        });
        $grid->column('remark', '处理结果');
        $grid->column('原发放流水')->display(function () {
            return $this->ologNo ?? '---';
        });
        $grid->column('logNo', '发券流水号');
        $grid->column('created_at', '处理时间');
        $grid->disableExport(false);

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

            $export->filename('沃钱包业务' . date("YmdHis"));
        });

        return $grid;
    }

}
