<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Course\CourseHoles;
use App\Models\Game;
use App\Models\GameType;
use App\Models\GameUser;
use App\Models\GameUserScore;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Log;

class GameUserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new GameUser());
        $grid->model()->orderByDesc('id');

        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->disableActions();

        $grid->column('id')->sortable();
        $grid->column('game_id')->display(function ($game_id) {
            $game = Game::query()->find($game_id);
            if ($game) {
                return '<span class="label" style="background:#21b978;line-height: 40px;">' . $game->name . '</span>';
            } else {
                return "";
            }
        });
        $grid->column('user_id')->display(function ($user_id) {
            $user = User::query()->find($user_id);
            $str = "";
            if ($user) {
                $str .= "<div style='margin-right:10px;display: flex;align-items: center'>";
                $str .= '<img data-action="preview-img" src="' . $user->avatar . '" style="max-width:50px;max-height:50px;cursor:pointer;margin-right:5px;" class="img img-thumbnail">';
                $str .= '<div>';
                $str .= '<p style="margin-bottom: 2px">ID：' . $user->id . '</p>';
                $str .= '<p>' . trans('user.fields.name') . '：' . $user->name . '</p>';
                $str .= "</div>";
                $str .= "</div>";
            }
            return $str;
        });

        $grid->column('level')->using([
                1 => trans('game-user.fields.red'),
                2 => trans('game-user.fields.white'),
                3 => trans('game-user.fields.blue'),
                4 => trans('game-user.fields.gold')])
            ->label(['gray', 'red', 'white', 'blue', 'gold']);

        $grid->column('type')->using([
                1 => trans('game-user.fields.normal'),
                2 => trans('game-user.fields.fictitious')])
            ->label(['gray', 'green']);

        $grid->column('scores')->display(function ($res) {

            $game_user_score = (new GameUserScore())->setTable('gu')->from('game_user_scores as gu')
                ->where('gu.game_user_id', $this->id)
                ->leftJoin('course_holes as ch', 'ch.id', '=', 'gu.hole_id')
                ->select('gu.score', 'ch.sort', 'ch.par', 'ch.course_id')
                ->orderBy('sort', 'asc')
                ->get()
                ->toArray();

            //总标杆
            $par_total = 0;
            foreach ($game_user_score as $key => $val) {
                $par_total += $val['par'];
            }
            //总成绩
            $game_user_score_total = GameUserScore::where('game_user_id', $this->id)->sum('score');
            //前九标杆
            $bf9_par_total = 0;
            foreach ($game_user_score as $key => $val) {
                if($val['sort'] <= 9){
                    $bf9_par_total += $val['par'];
                }
            }
            //前九成绩
            $bf9_score = 0;
            foreach ($game_user_score as $key => $val) {
                if($val['sort'] <= 9){
                    $bf9_score += $val['score'];
                }
            }
            //球洞
            $hole_con = '';
            foreach ($game_user_score as $key => $val) {
                $hole_con .= "<td>" . ($key + 1) . "</td>";
                if($val['sort'] == 9){
                    $hole_con .= "<td>" . trans('game-user.fields.before9') . "</td>";
                }
            }
            //标杆
            $par_con = '';
            foreach ($game_user_score as $key => $val) {
                $par_con .= "<td>" . $val['par'] . "</td>";
                if($val['sort'] == 9){
                    $par_con .= "<td>" . $bf9_par_total . "</td>";
                }
            }
            //成绩
            $score_con = '';
            foreach ($game_user_score as $key => $val) {
                $score_con .= "<td>" . $val['score'] . "</td>";
                if($val['sort'] == 9){
                    $score_con .= "<td>" . $bf9_score . "</td>";
                }
            }
            $str = "<style>
                        .scores_table td{
                            border: 1px solid #ccc!important;
                            width: 40px;
                        }
                    </style>
                    <table class='scores_table' style='text-align: center'>
                        <tr>
                            <td>" . trans('game-user.fields.hole') . "</td>
                            " . $hole_con . "
                            <td>" . trans('game-user.fields.Total_rod') . "</td>
                        </tr>
                        <tr>
                            <td>" . trans('game-user.fields.par') . "</td>
                            " . $par_con . "
                            <td>" . $par_total . "</td>
                        </tr>
                        <tr>
                            <td>" . trans('game-user.fields.rod') . "</td>
                            " . $score_con . "
                            <td>" . $game_user_score_total . "</td>
                        </tr>
                    </table>";

            return $str;
        });


        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id');
            $game_type = Game::query()->get();
            $game_type_arr = array();
            foreach ($game_type as $v) {
                $game_type_arr[$v['id']] = $v['name'];
            }
            $filter->equal('game_id')->select($game_type_arr);
            $user = User::query()->where('status', 1)->get()->toArray();
            $user_arr = array();
            foreach ($user as $v) {
                $user_arr[$v['id']] = $v['name'];
            }
            $filter->equal('user_id')->select($user_arr);
            $filter->equal('type')->select([1 => trans('game-user.fields.normal'), 2 => trans('game-user.fields.fictitious')]);
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GameUser(), function (Show $show) {
            $show->field('id');
            $show->field('game_id');
            $show->field('user_id');
            $show->field('level');
            $show->field('type');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GameUser(), function (Form $form) {
            $form->display('id');
            $form->text('game_id');
            $form->text('user_id');
            $form->text('level');
            $form->text('type');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
