<?php

namespace addons\webman\controller;

use addons\webman\model\PlayerDeliveryRecord;
use ExAdmin\ui\component\common\Html;
use ExAdmin\ui\component\common\Icon;
use ExAdmin\ui\component\grid\avatar\Avatar;
use ExAdmin\ui\component\grid\grid\Actions;
use ExAdmin\ui\component\grid\grid\Filter;
use ExAdmin\ui\component\grid\grid\FilterColumn;
use ExAdmin\ui\component\grid\grid\Grid;
use ExAdmin\ui\component\grid\tag\Tag;
use ExAdmin\ui\support\Container;
use ExAdmin\ui\support\Request;

/**
 * 账变记录
 * @group channel
 */
class ChannelPlayerDeliveryRecordController
{
    protected $model;

    public function __construct()
    {
        $this->model = plugin()->webman->config('database.player_delivery_record_model');
    }

    /**
     * 玩家账变
     * @group channel
     * @auth true
     */
    public function index(): Grid
    {
        $lang = Container::getInstance()->translator->getLocale();
        return Grid::create(new $this->model(), function (Grid $grid) use ($lang) {
            $grid->title(admin_trans('player_delivery_record.title'));
            $grid->model()->with(['player'])->orderBy('created_at', 'desc');
            $grid->autoHeight();
            $grid->bordered(true);
            $exAdminFilter = Request::input('ex_admin_filter', []);
            if (isset($exAdminFilter['created_at_start']) && !empty($exAdminFilter['created_at_start'])) {
                $grid->model()->where('created_at', '>=', $exAdminFilter['created_at_start']);
            }
            if (isset($exAdminFilter['created_at_end']) && !empty($exAdminFilter['created_at_end'])) {
                $grid->model()->where('created_at', '<=', $exAdminFilter['created_at_end']);
            }
            if (isset($exAdminFilter['search_source']) && !empty($exAdminFilter['search_source'])) {
                $searchSource = $exAdminFilter['search_source'];
                $grid->model()->where(function ($query) use ($searchSource) {
                    $query->where(function ($query) use ($searchSource) {
                        $query->where([
                            ['source', 'like', '%' . $searchSource . '%', 'and'],
                        ]);
                    });
                });
            }
            $grid->column('id', admin_trans('player_delivery_record.fields.id'))->align('center');
            $grid->column('player.uuid', admin_trans('player.fields.uuid'))->align('center');
            $grid->column('player.name', admin_trans('player.fields.name'))->display(function ($val, PlayerDeliveryRecord $data) {
                $image = $data->player->avatar ? Avatar::create()->src(is_numeric($data->player->avatar) ? config('def_avatar.' . $data->player->avatar) : $data->player->avatar) : Avatar::create()->icon(Icon::create('UserOutlined'));
                return Html::create()->content([
                    $image,
                    Html::div()->content($data->player->name),
                ]);
            })->align('center')->filter(
                FilterColumn::like()->text('player.phone')
            );
            $grid->column('source', admin_trans('player_delivery_record.fields.source'))->display(function ($val, PlayerDeliveryRecord $data) use ($lang) {
                switch ($data->type) {
                    case PlayerDeliveryRecord::TYPE_MODIFIED_AMOUNT_ADD:
                    case PlayerDeliveryRecord::TYPE_MODIFIED_AMOUNT_DEDUCT:
                    case PlayerDeliveryRecord::TYPE_RECHARGE:
                    case PlayerDeliveryRecord::TYPE_WITHDRAWAL:
                    case PlayerDeliveryRecord::TYPE_WITHDRAWAL_BACK:
                        return Tag::create(trans($val, [], 'message', $lang))->color('red');
                    case PlayerDeliveryRecord::TYPE_REGISTER_PRESENT:
                        return Tag::create(trans($val, [], 'message', $lang))->color('blue');
                    case PlayerDeliveryRecord::TYPE_COMMISSION:
                    case PlayerDeliveryRecord::TYPE_GAME_OUT:
                        return Tag::create(trans($val, [], 'message', $lang))->color('purple');
                    case PlayerDeliveryRecord::TYPE_SIGN:
                    case PlayerDeliveryRecord::TYPE_GAME_IN:
                    case PlayerDeliveryRecord::TYPE_BET_REBATE:
                    case PlayerDeliveryRecord::TYPE_DAMAGE_REBATE:
                    case PlayerDeliveryRecord::TYPE_RECHARGE_REWARD:
                    case PlayerDeliveryRecord::TYPE_PROFIT:
                        return Tag::create(trans($val, [], 'message', $lang))->color('orange');
                    default:
                        return '';
                }
            })->align('center');
            $grid->column('type', admin_trans('player_delivery_record.fields.type'))
                ->display(function ($value) {
                    switch ($value) {
                        case PlayerDeliveryRecord::TYPE_MODIFIED_AMOUNT_ADD:
                            $tag = Tag::create(admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_MODIFIED_AMOUNT_ADD))->color('#2db7f5');
                            break;
                        case PlayerDeliveryRecord::TYPE_RECHARGE:
                            $tag = Tag::create(admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_RECHARGE))->color('#3C87C9');
                            break;
                        case PlayerDeliveryRecord::TYPE_WITHDRAWAL:
                            $tag = Tag::create(admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_WITHDRAWAL))->color('#C98341');
                            break;
                        case PlayerDeliveryRecord::TYPE_MODIFIED_AMOUNT_DEDUCT:
                            $tag = Tag::create(admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_MODIFIED_AMOUNT_DEDUCT))->color('#108ee9');
                            break;
                        case PlayerDeliveryRecord::TYPE_WITHDRAWAL_BACK:
                            $tag = Tag::create(admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_WITHDRAWAL_BACK))->color('#CC6600');
                            break;
                        case PlayerDeliveryRecord::TYPE_COMMISSION:
                            $tag = Tag::create(admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_COMMISSION))->color('#3C87C9');
                            break;
                        case PlayerDeliveryRecord::TYPE_REGISTER_PRESENT:
                            $tag = Tag::create(admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_REGISTER_PRESENT))->color('#3C87C9');
                            break;
                        case PlayerDeliveryRecord::TYPE_SIGN:
                            $tag = Tag::create(admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_SIGN))->color('#CC6600');
                            break;
                        case PlayerDeliveryRecord::TYPE_GAME_IN:
                            $tag = Tag::create(admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_GAME_IN))->color('#CC6600');
                            break;
                        case PlayerDeliveryRecord::TYPE_GAME_OUT:
                            $tag = Tag::create(admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_GAME_OUT))->color('#3C87C9');
                            break;
                        case PlayerDeliveryRecord::TYPE_BET_REBATE:
                            $tag = Tag::create(admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_BET_REBATE))->color('#C98341');
                            break;
                        case PlayerDeliveryRecord::TYPE_DAMAGE_REBATE:
                            $tag = Tag::create(admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_DAMAGE_REBATE))->color('#3C87C9');
                            break;
                        case PlayerDeliveryRecord::TYPE_RECHARGE_REWARD:
                            $tag = Tag::create(admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_RECHARGE_REWARD))->color('#3C87C9');
                            break;
                        case PlayerDeliveryRecord::TYPE_PROFIT:
                            $tag = Tag::create(admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_PROFIT))->color('#3C87C9');
                            break;
                        default:
                            $tag = '';
                    }
                    return Html::create()->content([
                        $tag
                    ]);
                })->align('center')->sortable();
            $grid->column('amount', admin_trans('player_delivery_record.fields.amount'))->display(function ($val, PlayerDeliveryRecord $data) {
                switch ($data->type) {
                    case PlayerDeliveryRecord::TYPE_WITHDRAWAL:
                    case PlayerDeliveryRecord::TYPE_MODIFIED_AMOUNT_DEDUCT:
                        return Html::create()->content(['-' . $val])->style(['color' => '#cd201f']);
                    default:
                        return Html::create()->content(['+' . $val])->style(['color' => 'green']);
                }
            })->align('center');
            $grid->column('amount_after', admin_trans('player_delivery_record.fields.amount_after'))->align('center');
            $grid->column('amount_before', admin_trans('player_delivery_record.fields.amount_before'))->align('center');
            $grid->column('user_name', admin_trans('player_delivery_record.fields.user_name'))->display(function ($val, PlayerDeliveryRecord $data) {
                $name = '玩家';
                if (in_array($data->type, [PlayerDeliveryRecord::TYPE_MODIFIED_AMOUNT_ADD, PlayerDeliveryRecord::TYPE_MODIFIED_AMOUNT_DEDUCT])) {
                    $name = $data->user_name ?? '管理员';
                }
                return Html::create()->content([
                    Html::div()->content($name),
                ]);
            });
            $grid->column('created_at', admin_trans('player_delivery_record.fields.created_at'))->sortable()->align('center');
            $grid->hideDelete();
            $grid->hideSelection();
            $grid->actions(function (Actions $actions) {
                $actions->hideDel();
                $actions->hideEdit();
            });
            $grid->filter(function (Filter $filter) {
                $filter->like()->text('player.uuid')->placeholder(admin_trans('player.fields.uuid'));
                $filter->like()->text('player.name')->placeholder(admin_trans('player.fields.name'));
                $filter->like()->text('search_source')->placeholder(admin_trans('player_delivery_record.fields.source'));
                $filter->eq()->select('type')
                    ->placeholder(admin_trans('player_delivery_record.fields.type'))
                    ->showSearch()
                    ->style(['width' => '200px'])
                    ->dropdownMatchSelectWidth()
                    ->options([
                        PlayerDeliveryRecord::TYPE_MODIFIED_AMOUNT_ADD => admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_MODIFIED_AMOUNT_ADD),
                        PlayerDeliveryRecord::TYPE_RECHARGE => admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_RECHARGE),
                        PlayerDeliveryRecord::TYPE_WITHDRAWAL => admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_WITHDRAWAL),
                        PlayerDeliveryRecord::TYPE_MODIFIED_AMOUNT_DEDUCT => admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_MODIFIED_AMOUNT_DEDUCT),
                        PlayerDeliveryRecord::TYPE_WITHDRAWAL_BACK => admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_WITHDRAWAL_BACK),
                        PlayerDeliveryRecord::TYPE_REGISTER_PRESENT => admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_REGISTER_PRESENT),
                        PlayerDeliveryRecord::TYPE_COMMISSION => admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_COMMISSION),
                        PlayerDeliveryRecord::TYPE_SIGN => admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_SIGN),
                        PlayerDeliveryRecord::TYPE_GAME_OUT => admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_GAME_OUT),
                        PlayerDeliveryRecord::TYPE_GAME_IN => admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_GAME_IN),
                        PlayerDeliveryRecord::TYPE_BET_REBATE => admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_BET_REBATE),
                        PlayerDeliveryRecord::TYPE_DAMAGE_REBATE => admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_DAMAGE_REBATE),
                        PlayerDeliveryRecord::TYPE_RECHARGE_REWARD => admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_RECHARGE_REWARD),
                        PlayerDeliveryRecord::TYPE_PROFIT => admin_trans('player_delivery_record.type.' . PlayerDeliveryRecord::TYPE_PROFIT),
                    ]);
                $filter->form()->hidden('created_at_start');
                $filter->form()->hidden('created_at_end');
                $filter->form()->dateTimeRange('created_at_start', 'created_at_end', '')->placeholder([admin_trans('public_msg.created_at_start'), admin_trans('public_msg.created_at_end')]);
            });
            $grid->quickSearch();
        });
    }
}
