<?php

namespace App\Admin\Controllers\Activity;

use App\Models\ActivityCoupon;
use App\Models\ActivityRule;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;

class CouponController extends AdminController
{

    protected $title = '卡券列表管理';

    protected function grid(): Grid
    {
        $grid = new Grid(new ActivityCoupon);
        $grid->model()->with(['outlet'])->latest();
        $grid->disableActions();
        $grid->disableCreateButton();

        $grid->filter(function ($filter) {
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('status', '状态')->select(ActivityCoupon::STATUS);
                $filter->between('used_at', '核销时间')->datetime();
                $filter->where(function ($query) {
                    $query->whereHas('outlet', function ($query) {
                        $query->whereHas('info', function ($query) {
                            $query->where('nickname', 'like', "%{$this->input}%");
                        });
                    });
                }, '网点名称', 'outlet_name');

                $filter->between('created_at', '发券时间')->datetime();

            });
            $filter->column(1 / 2, function ($filter) {
                $filter->like('code', '卡券编号');
                $filter->like('mobile', '手机号');
                $filter->where(function ($query) {
                    $query->whereHas('activity', function ($query) {
                        $query->whereHas('rule', function ($query) {
                            $query->where('code', $this->input);
                        });
                    });
                }, '优惠政策', 'rule_code')->select(ActivityRule::where('status', 1)->pluck('title', 'code'));

                $users = User::query()
                    ->whereHas('identity', function ($query) {
                        $query->where('identity_id', 1);
                    })
                    ->get()
                    ->pluck('nickname', 'id');
                $filter->where(function ($query) {
                    $query->whereHas('outlet', function ($query) {
                        $query->whereHas('parent', function ($query) {
                            $query->where('id', $this->input);
                        });
                    });
                }, '渠道', 'parent_id')->select($users);;
            });
        });

        $grid->column('id', '#ID#');
        $grid->column('activity.title', '卡券名称');
        $grid->column('mobile', '手机号');
        $grid->column('优惠政策')->display(function () {
            return $this->activity->rule->title;
        });

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

        $grid->column('code', '卡券编号');
        $grid->column('status', '状态')
            ->using(ActivityCoupon::STATUS)
            ->label([
                1 => 'default',
                2 => 'warning',
                3 => 'info',
            ]);

        $grid->column('used_at', '核销时间');
        $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 $value."\t";
            });

            $export->filename($this->title.date("YmdHis"));
        });

        return $grid;
    }

}
