<?php

namespace addons\webman\controller;

use addons\webman\model\Player;
use addons\webman\model\PlayerRechargeRecord;
use addons\webman\model\PlayerTag;
use ExAdmin\ui\component\common\Html;
use ExAdmin\ui\component\common\Icon;
use ExAdmin\ui\component\detail\Detail;
use ExAdmin\ui\component\grid\avatar\Avatar;
use ExAdmin\ui\component\grid\card\Card;
use ExAdmin\ui\component\grid\grid\Actions;
use ExAdmin\ui\component\grid\grid\Editable;
use ExAdmin\ui\component\grid\grid\Filter;
use ExAdmin\ui\component\grid\grid\Grid;
use ExAdmin\ui\component\grid\statistic\Statistic;
use ExAdmin\ui\component\grid\tag\Tag;
use ExAdmin\ui\component\layout\layout\Layout;
use ExAdmin\ui\component\layout\Row;
use ExAdmin\ui\response\Response;
use ExAdmin\ui\support\Request;
use Illuminate\Support\Str;
use support\Cache;

/**
 * 充值记录
 */
class RechargeRecordController
{
    protected $model;

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

    /**
     * 充值
     * @auth true
     */
    public function index(): Grid
    {
        return Grid::create(new $this->model(), function (Grid $grid) {
            $grid->title(admin_trans('player_recharge_record.title'));
            $grid->model()->with(['player', 'channel', 'channel_recharge_setting', 'player.player_extend'])->orderBy('created_at', 'desc');
            $exAdminFilter = Request::input('ex_admin_filter', []);
            if (!empty($exAdminFilter)) {
                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['finish_time_start']) && !empty($exAdminFilter['finish_time_start'])) {
                    $grid->model()->where('finish_time', '>=', $exAdminFilter['finish_time_start']);
                }
                if (isset($exAdminFilter['finish_time_end']) && !empty($exAdminFilter['finish_time_end'])) {
                    $grid->model()->where('finish_time', '<=', $exAdminFilter['finish_time_end']);
                }
                if (!empty($exAdminFilter['player']['uuid'])) {
                    $grid->model()->whereHas('player', function ($query) use ($exAdminFilter) {
                        $query->where('uuid', 'like', '%' . $exAdminFilter['player']['uuid'] . '%');
                    });
                }
                if (!empty($exAdminFilter['player']['name'])) {
                    $grid->model()->whereHas('player', function ($query) use ($exAdminFilter) {
                        $query->where('name', 'like', '%' . $exAdminFilter['player']['name'] . '%');
                    });
                }
                if (!empty($exAdminFilter['department_id'])) {
                    $grid->model()->where('department_id', $exAdminFilter['department_id']);
                }
                if (!empty($exAdminFilter['type'])) {
                    $grid->model()->where('type', $exAdminFilter['type']);
                }
                if (isset($exAdminFilter['status']) && (!empty($exAdminFilter['status']) || $exAdminFilter['status'] === 0)) {
                    $grid->model()->where('status', $exAdminFilter['status']);
                }
                if (!empty($exAdminFilter['tradeno'])) {
                    $grid->model()->where('tradeno', $exAdminFilter['tradeno']);
                }
            }
            $query = clone $grid->model();
            $totalData = $query->selectRaw(
                "ifNull(sum(IF(type = 4, money,0)), 0) as total_artificial_money,
                           ifNull(sum(IF(type = 1, money,0)), 0) as total_espay_money,
                           ifNull(sum(IF(payment_method = 'DUITNOWP2P', money,0)), 0) as total_espay_duitnow_money,
                           ifNull(sum(IF(payment_method = 'P2PDEPOSIT', money,0)), 0) as total_espay_deposit_money,
                           ifNull(sum(IF(payment_method = 'duitnowqr', money,0)), 0) as total_onepay_duitnow_money,
                           ifNull(sum(IF(payment_method = 'online_banking', money,0)), 0) as total_onepay_deposit_money,
                           ifNull(sum(IF(payment_method = 'QR', money,0)), 0) as total_skl_duitnow_money,
                           ifNull(sum(IF(payment_method = 'P2P', money,0)), 0) as total_skl_deposit_money"
            )->first();
            $layout = Layout::create();
            $layout->row(function (Row $row) use ($totalData) {
                $row->gutter([10, 0]);
                $row->column(
                    Card::create([
                        Row::create()->column(Statistic::create()->title(admin_trans('player_recharge_record.total_data.total_artificial_money'))
                            ->value(!empty($totalData['total_artificial_money']) ? floatval($totalData['total_artificial_money']) : 0)->style([
                                'font-size' => '15px',
                                'text-align' => 'center'
                            ])),
                    ])->bodyStyle([
                        'display' => 'flex',
                        'align-items' => 'center',
                        'height' => '72px'
                    ])->hoverable()->headStyle(['height' => '0px', 'border-bottom' => '0px', 'min-height' => '0px'])
                    , 8);
                $row->column(
                    Card::create([
                        Row::create()->column(Statistic::create()->title(admin_trans('player_recharge_record.total_data.total_espay_money'))
                            ->value(!empty($totalData['total_espay_money']) ? floatval($totalData['total_espay_money']) : 0)->style([
                                'font-size' => '15px',
                                'text-align' => 'center'
                            ])),
                    ])->bodyStyle([
                        'display' => 'flex',
                        'align-items' => 'center',
                        'height' => '72px'
                    ])->hoverable()->headStyle(['height' => '0px', 'border-bottom' => '0px', 'min-height' => '0px'])
                    , 8);
                $row->column(
                    Card::create([
                        Row::create()->column(Statistic::create()->title(admin_trans('player_recharge_record.total_data.total_espay_inmoney'))
                            ->value(bcadd(bcadd(
                                bcadd(bcmul($totalData['total_espay_duitnow_money'], 0.97, 3), bcmul($totalData['total_espay_deposit_money'], 0.985, 3), 3),
                                bcadd(bcmul($totalData['total_onepay_duitnow_money'], 0.984, 3), bcmul($totalData['total_onepay_deposit_money'], 0.986, 3), 3),
                                3), bcadd(bcmul($totalData['total_skl_duitnow_money'], 0.987, 3), bcmul($totalData['total_skl_deposit_money'], 0.989, 3), 3), 3))
                            ->style([
                                'font-size' => '15px',
                                'text-align' => 'center'
                            ])),
                    ])->bodyStyle([
                        'display' => 'flex',
                        'align-items' => 'center',
                        'height' => '72px'
                    ])->hoverable()->headStyle(['height' => '0px', 'border-bottom' => '0px', 'min-height' => '0px'])
                    , 8);
            })->style(['background' => '#fff']);
            $grid->header($layout);
            $grid->bordered(true);
            $grid->autoHeight();
            $grid->column('id', admin_trans('player_recharge_record.fields.id'))->align('center')->fixed(true);
            $grid->column('player.name', admin_trans('player.fields.name'))->display(function ($val, PlayerRechargeRecord $data) {
                $image = (isset($data->player->avatar) && !empty($data->player->avatar)) ? Avatar::create()->src($data->player->avatar) : Avatar::create()->icon(Icon::create('UserOutlined'));
                return Html::create()->content([
                    $image,
                    Html::div()->content($val)
                ])->style(['cursor' => 'pointer'])->modal($this->playerDetail([
                    'phone' => $data->player->phone ?? '',
                    'name' => $data->player->name ?? '',
                    'address' => $data->player->player_extend->address ?? '',
                    'email' => $data->player->player_extend->email ?? '',
                    'line' => $data->player->player_extend->line ?? '',
                    'created_at' => $data->player->created_at ? date('Y-m-d H:i:s', strtotime($data->player->created_at)) : '',
                ]));
            })->align('center')->fixed(true);
            $grid->column('player.uuid', admin_trans('player.fields.uuid'))->display(function ($val, PlayerRechargeRecord $data) {
                return $data->player->uuid;
            })->align('center')->fixed(true);
            $grid->column('tradeno', admin_trans('player_recharge_record.fields.tradeno'))->copy();
            $grid->column('channel.name', admin_trans('player_recharge_record.fields.department_id'))->align('center');
            $grid->column('type', admin_trans('player_recharge_record.fields.type'))->display(function ($val) {
                switch ($val) {
                    case PlayerRechargeRecord::TYPE_REGULAR:
                        return Tag::create(admin_trans('player_recharge_record.type.' . $val))
                            ->color('#55acee');
                    case PlayerRechargeRecord::TYPE_ACTIVITY:
                        return Tag::create(admin_trans('player_recharge_record.type.' . $val))
                            ->color('#3b5999');
                    case PlayerRechargeRecord::TYPE_ARTIFICIAL:
                        return Tag::create(admin_trans('player_recharge_record.type.' . $val))
                            ->color('#cd201f');
                    default:
                        return '';
                }
            })->align('center');
            $grid->column('status', admin_trans('player_recharge_record.fields.status'))->display(function ($val, PlayerRechargeRecord $data) {
                switch ($val) {
                    case PlayerRechargeRecord::STATUS_WAIT:
                        return Tag::create(admin_trans('player_recharge_record.status_wait'))
                            ->color('#108ee9');
                    case PlayerRechargeRecord::STATUS_RECHARGING:
                        return Tag::create(admin_trans('player_recharge_record.status_examine'))
                            ->color('#3b5999');
                    case PlayerRechargeRecord::STATUS_RECHARGED_SUCCESS:
                        return Tag::create(admin_trans('player_recharge_record.status_success'))
                            ->color('#87d068');
                    case PlayerRechargeRecord::STATUS_RECHARGED_FAIL:
                        return Tag::create(admin_trans('player_recharge_record.status_fail'))
                            ->color('#f50');
                    case PlayerRechargeRecord::STATUS_RECHARGED_CANCEL:
                        return Tag::create(admin_trans('player_recharge_record.status_cancel'))
                            ->color('#2db7f5');
                    case PlayerRechargeRecord::STATUS_RECHARGED_REJECT:
                        return Tag::create(admin_trans('player_recharge_record.status_reject'))
                            ->color('#2db7f5');
                    case PlayerRechargeRecord::STATUS_RECHARGED_SYSTEM_CANCEL:
                        return Tag::create(admin_trans('player_recharge_record.status_system_cancel'))
                            ->color('#2db7f5');
                    default:
                        return '';
                }
            })->align('center');
            $grid->column('money', admin_trans('player_recharge_record.fields.money'))->display(function ($val, PlayerRechargeRecord $data) {
                return bcdiv($val,$data->rate, 2) . ' ' . ($data->currency);
            })->align('center');
            $grid->column('inmoney', admin_trans('player_recharge_record.fields.inmoney'))->display(function ($val, PlayerRechargeRecord $data) {
                if ($data->payment_method == 'DUITNOWP2P') {
                    $ratio = 0.97;
                } elseif ($data->payment_method == 'P2PDEPOSIT') {
                    $ratio = 0.985;
                } elseif ($data->payment_method == 'duitnowqr') {
                    $ratio = 0.984;
                } elseif ($data->payment_method == 'online_banking') {
                    $ratio = 0.986;
                } elseif ($data->payment_method == 'P2P') {
                    $ratio = 0.989;
                } elseif ($data->payment_method == 'QR') {
                    $ratio = 0.987;
                } else {
                    $ratio = 1;
                }
                if ($data->currency == 'USDT') {
                    return bcdiv($val,$data->rate, 2) . ' ' . ($data->currency);
                }
                return $data->money * $ratio . ' ' . ($data->currency);
            })->align('center');
            $grid->column('coins', admin_trans('player_recharge_record.fields.coins'))->align('center')->sortable();
            $grid->column('player_tag', admin_trans('player_recharge_record.fields.player_tag'))
                ->display(function ($value) {
                    return $this->handleTagIds($value);
                })
                ->editable(
                    Editable::checkboxTag()
                        ->options($this->getPlayerTagOptionsFilter())
                )->width('150px');
            $grid->column('remark', admin_trans('player_recharge_record.fields.remark'))->display(function ($value) {
                return Str::of($value)->limit(20, ' (...)');
            })->editable(
                (new Editable)->textarea('remark')
                    ->showCount()
                    ->rows(5)
                    ->rule(['max:255' => admin_trans('player_recharge_record.fields.remark')])
            )->width('150px')->align('center');
            $grid->column('finish_time', admin_trans('player_recharge_record.fields.finish_time'))->sortable()->align('center');
            $grid->column('created_at', admin_trans('player_recharge_record.fields.created_at'))->sortable()->align('center')->fixed('right');
            $grid->hideDelete();
            $grid->hideSelection();
            $grid->expandFilter();
            $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->eq()->select('department_id')
                    ->showSearch()
                    ->style(['width' => '200px'])
                    ->dropdownMatchSelectWidth()
                    ->placeholder(admin_trans('player_recharge_record.fields.department_id'))
                    ->remoteOptions(admin_url(['addons-webman-controller-ChannelController', 'getDepartmentOptions']));
                $filter->eq()->select('type')
                    ->showSearch()
                    ->style(['width' => '200px'])
                    ->dropdownMatchSelectWidth()
                    ->placeholder(admin_trans('player_recharge_record.fields.type'))
                    ->options([
                        PlayerRechargeRecord::TYPE_REGULAR => admin_trans('player_recharge_record.type.' . PlayerRechargeRecord::TYPE_REGULAR),
                        PlayerRechargeRecord::TYPE_ARTIFICIAL => admin_trans('player_recharge_record.type.' . PlayerRechargeRecord::TYPE_ARTIFICIAL),
                    ]);
                $filter->eq()->select('status')
                    ->showSearch()
                    ->style(['width' => '200px'])
                    ->dropdownMatchSelectWidth()
                    ->placeholder(admin_trans('player_recharge_record.fields.status'))
                    ->options([
                        PlayerRechargeRecord::STATUS_WAIT => admin_trans('player_recharge_record.status.' . PlayerRechargeRecord::STATUS_WAIT),
                        PlayerRechargeRecord::STATUS_RECHARGING => admin_trans('player_recharge_record.status.' . PlayerRechargeRecord::STATUS_RECHARGING),
                        PlayerRechargeRecord::STATUS_RECHARGED_SUCCESS => admin_trans('player_recharge_record.status.' . PlayerRechargeRecord::STATUS_RECHARGED_SUCCESS),
                        PlayerRechargeRecord::STATUS_RECHARGED_FAIL => admin_trans('player_recharge_record.status.' . PlayerRechargeRecord::STATUS_RECHARGED_FAIL),
                        PlayerRechargeRecord::STATUS_RECHARGED_CANCEL => admin_trans('player_recharge_record.status.' . PlayerRechargeRecord::STATUS_RECHARGED_CANCEL),
                        PlayerRechargeRecord::STATUS_RECHARGED_REJECT => admin_trans('player_recharge_record.status.' . PlayerRechargeRecord::STATUS_RECHARGED_REJECT),
                        PlayerRechargeRecord::STATUS_RECHARGED_SYSTEM_CANCEL => admin_trans('player_recharge_record.status.' . PlayerRechargeRecord::STATUS_RECHARGED_SYSTEM_CANCEL),
                    ]);
                $filter->like()->text('tradeno')->placeholder(admin_trans('player_recharge_record.fields.tradeno'));
                $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')]);
                $filter->form()->hidden('finish_time_start');
                $filter->form()->hidden('finish_time_end');
                $filter->form()->dateTimeRange('finish_time_start', 'finish_time_end', '')->placeholder([admin_trans('player_recharge_record.fields.finish_time'), admin_trans('player_recharge_record.fields.finish_time')]);

            });
        });
    }

    /**
     * 处理标签
     * @param array $value
     * @return Html
     */
    public function handleTagIds(array $value): Html
    {
        $options = $this->getPlayerTagOptions($value);
        $html = Html::create();
        foreach ($options as $option) {
            $html->content(
                Tag::create($option)
                    ->color('success')
            );
        }
        return $html;
    }

    /**
     * 获取玩家标签选项(筛选id)
     * @param array $ids
     * @return array
     */
    public function getPlayerTagOptions(array $ids = []): array
    {
        $idsStr = json_encode($ids);
        $cacheKey = md5("player_tag_options_ids_$idsStr");
        if (Cache::has($cacheKey)) {
            return Cache::get($cacheKey);
        } else {
            if (!empty($ids)) {
                $data = (new PlayerTag())->whereIn('id', $ids)->select(['name', 'id'])->get()->toArray();
                $data = $data ? array_column($data, 'name', 'id') : [];
                Cache::set($cacheKey, $data, 24 * 60 * 60);

                return $data;
            }
            return [];
        }
    }

    /**
     * 获取玩家标签(筛选id)
     * @return array
     */
    public function getPlayerTagOptionsFilter(): array
    {
        $cacheKey = "doc_player_tag_options_filter";
        if (Cache::has($cacheKey)) {
            return Cache::get($cacheKey);
        } else {
            $data = (new PlayerTag())->select(['name', 'id'])->get()->toArray();
            $data = $data ? array_column($data, 'name', 'id') : [];
            Cache::set($cacheKey, $data, 24 * 60 * 60);

            return $data;
        }
    }

    /**
     * 筛选玩家下拉
     * @return mixed
     */
    public function getPlayerOptions()
    {
        $request = Request::input();
        $player = Player::orderBy('created_at', 'desc')
            ->forPage(1, 20);
        if (!empty($request['search'])) {
            $player->where('phone', 'like', '%' . $request['search'] . '%');
        }
        $playerList = $player->get();
        $data = [];
        /** @var Player $player */
        foreach ($playerList as $player) {
            $data[] = [
                'value' => $player->id,
                'label' => $player->phone,
            ];
        }
        return Response::success($data);
    }

    /**
     * 玩家详情
     * @param array $data
     * @return Detail
     */
    public function playerDetail(array $data): Detail
    {
        return Detail::create($data, function (Detail $detail) {
            $detail->item('name', admin_trans('player.fields.name'));
            $detail->item('address', admin_trans('player_extend.fields.address'));
            $detail->item('email', admin_trans('player_extend.fields.email'));
            $detail->item('phone', admin_trans('player.fields.phone'));
            $detail->item('line', admin_trans('player_extend.fields.line'));
            $detail->item('created_at', admin_trans('player.fields.created_at'));
        })->layout('vertical');
    }
}
