<?php

namespace addons\webman\controller;

use addons\webman\Admin;
use addons\webman\model\Channel;
use addons\webman\model\DrawRecord;
use addons\webman\model\Game;
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\Filter;
use ExAdmin\ui\component\grid\grid\Grid;
use ExAdmin\ui\support\Request;


/**
 * 游戏游玩记录
 */
class PlayGameRecordController
{
    protected $model;

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

    }

    /**
     * 玩家游戏记录
     * @auth true
     */
    public function index(): Grid
    {
        return Grid::create(new $this->model, function (Grid $grid) {
            $grid->title(admin_trans('play_game_record.title'));
            $grid->model()->orderBy('created_at', 'desc');
            $exAdminFilter = Request::input('ex_admin_filter', []);
            if (isset($exAdminFilter['created_at_start']) && !empty($exAdminFilter['created_at_start'])) {
                $grid->model()->whereDate('created_at', '>=', $exAdminFilter['created_at_start']);
            }
            if (isset($exAdminFilter['created_at_end']) && !empty($exAdminFilter['created_at_end'])) {
                $grid->model()->whereDate('created_at', '<=', $exAdminFilter['created_at_end']);
            }
            $grid->autoHeight();
            $grid->bordered(true);
            $grid->hideAction();
            $grid->hideDelete();
            $grid->hideDeleteSelection();
            $grid->hideSelection();
            $grid->column('id', admin_trans('play_game_record.fields.id'))->fixed(true)->align('center');
            $grid->column('player.name', admin_trans('player.fields.name'))->display(function ($val, DrawRecord $data) {
                $image = !empty($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($val),
                ]);
            })->fixed(true)->align('center');
            $grid->column('channel.name', admin_trans('channel.fields.name'))->align('center');
            $grid->column('game_type', admin_trans('game.fields.game_type'))->display(function ($val) {
                return $val ? admin_trans('game.game_type.' . $val) : admin_trans('game.nu_set');
            })->align('center');
            $grid->column('prize_name', admin_trans('prize.fields.name'))->align('center');
            $grid->column('prize_type', admin_trans('prize.fields.type'))->display(function ($val) {
                return $val ? admin_trans('prize.prize_type.' . $val) : admin_trans('prize.nu_set');
            })->align('center');
            $grid->column('created_at', admin_trans('play_game_record.fields.create_at'))->align('center');
            $grid->filter(function (Filter $filter) {
                $filter->like()->text('player.name')->placeholder(admin_trans('player.fields.name'));
                $filter->eq()->select('game_type')
                    ->placeholder(admin_trans('play_game_record.fields.game_type'))
                    ->showSearch()
                    ->style(['width' => '200px'])
                    ->dropdownMatchSelectWidth()
                    ->options([
                        Game::GAME_TYPE_EGG => admin_trans('game.game_type.' . Game::GAME_TYPE_EGG),
                        Game::GAME_TYPE_TURNTABLE => admin_trans('game.game_type.' . Game::GAME_TYPE_TURNTABLE),
                        Game::GAME_TYPE_BLINDBOX => admin_trans('game.game_type.' . Game::GAME_TYPE_BLINDBOX),
                        Game::GAME_TYPE_TICKET => admin_trans('game.game_type.' . Game::GAME_TYPE_TICKET),
                        Game::GAME_TYPE_LOTTERY => admin_trans('game.game_type.' . Game::GAME_TYPE_LOTTERY),
                        Game::GAME_TYPE_DICE => admin_trans('game.game_type.' . Game::GAME_TYPE_DICE),
                    ]);
                $filter->eq()->select('department_id')
                    ->showSearch()
                    ->style(['width' => '200px'])
                    ->dropdownMatchSelectWidth()
                    ->placeholder(admin_trans('channel.fields.department_name'))
                    ->remoteOptions(admin_url(['addons-webman-controller-ChannelController', 'getDepartmentOptions']));
                $filter->form()->hidden('created_at_start');
                $filter->form()->hidden('created_at_end');
                $filter->form()->dateRange('created_at_start', 'created_at_end', '')->placeholder([admin_trans('public_msg.created_at_start'), admin_trans('public_msg.created_at_end')]);
            });
            $grid->quickSearch();
        });
    }
}
