<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Feeds\FeedAction;
use App\Admin\Actions\Feeds\ForwardAction;
use App\Admin\Actions\Feeds\LikeAction;
use App\Admin\Actions\Games\GameMembers;
use App\Admin\Actions\Games\GameUserScores;
use App\Models\Feed;
use App\Models\Team;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Modal;

class FeedsController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Feed());
        $grid->model()->with(['user:id,name,avatar'])->orderByDesc('id');
        $grid->column('id')->sortable();
//        $grid->column('user.avatar','头像')->image("",50);;
        $grid->column('user_id')->display(function (){
            if($this->user){
                $str = "";
                $str.="<div style='margin-right:10px;display: flex;align-items: center'>";
                $str.='<img data-action="preview-img" src="'.$this->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：'.$this->user->id.'</p>';
                $str.='<p>'.trans('feeds.fields.nickname').'：'.$this->user->name.'</p>';
                $str.="</div>";
                $str.="</div>";
                return $str;
            }else{
                return '';
            }

        });
        $grid->column('team_id')->display(function ($res){
            if($res>0){
                $team = Team::query()->find($res);
                if($team){
                    return '<span class="label" style="background:#586cb1">'.trans('feeds.fields.team').'</span>
                        <br>
                        <br>
                        <span class="label" style="background:#586cb1">'.$team->name.'</span>';
                }else{
                    return "";
                }

            }else{
                return '<span class="label" style="background:#21b978">'.trans('feeds.fields.personal').'</span>';
            }
        });
        $grid->column('forward_id')->display(function ($res){
            if($res>0){
                return '<span class="label" style="background:#586cb1">'.trans('feeds.fields.forward').' ID:'.$res.'</span>';
            }else{
                return '<span class="label" style="background:#21b978">'.trans('feeds.fields.original').'</span>';
            }
        });
        $grid->column('content')->limit(30);
        $grid->column('file_url')->display(function ($v){
            if(isset($v)&&!empty($v)){
                $v = json_decode($v,true);
                $str = '';
                if(!empty($v)&&count($v)>0){
                    foreach ($v as $item){
                        $str.='<img data-action="preview-img" src="'.$item.'" style="max-width:50px;max-height:200px;cursor:pointer" class="img img-thumbnail">';
                    }
                }
                return $str;
            }else{
                return "";
            }
        });
        $grid->column('status')->switch();
        $grid->column('like_num');
        $grid->column('forward_num');
        $grid->column('address_info');

        $grid->column('match_title')->display(function ($res){
            //dd($this->game_per_id);
            $game_per_id = isset($this->game_per_id)&&!empty($this->game_per_id)?$this->game_per_id:'###';
            if(!empty($res)) {
                return '<a href="/admin/game_user_scores?id='.$game_per_id.'" target="_self"><span class="label" style="background:#21b978;line-height: 40px;">' . $res . '</span>
                        <br><i class="feather icon-bar-chart-2"></i> '.trans('feeds.fields.scorecard').'</a>';
            }else{
                return '';
            }
        });

        $grid->column('created_at');
        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id');
            $filter->like('user.name');
            $team = Team::query()->select(['id','name'])->get()->toArray();
            $team_arr[0] = trans('feeds.fields.personal');
            foreach ($team as $v){
                $team_arr[$v['id']] = $v['name'];
            }
            $filter->equal('team_id')->select($team_arr);

            $filter->like('content');

        });

        //批量操作
//        $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
//            $batch->disableDelete();
//        });
        //操作管理
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();

            $actions->append(new FeedAction());
            $actions->append(new LikeAction());
            $actions->append(new ForwardAction());
        });


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Feed(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('content');
            $show->field('file_url');
            $show->field('status');
            $show->field('like_num');
            $show->field('address_info');
            $show->field('match_title');
            $show->field('game_per_id');
            $show->field('is_delete');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Feed(), function (Form $form) {
            $form->display('id');

            $user = User::query()->select(['id','name'])->get()->toArray();
            $user_arr = array();
            foreach ($user as $v){
                $user_arr[$v['id']] = $v['name'];
            }
            $form->select('user_id')->options($user_arr);
            $form->textarea('content');
            $form->text('file_url');
            $form->switch('status')->default(1);
            $form->number('like_num')->default(0);
            $form->text('address_info');
            $form->text('match_title');
            $form->text('game_per_id');
            $form->text('is_delete');

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

            $form->footer(function ($footer) {
                // 去掉`查看`checkbox
                $footer->disableViewCheck();
                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();
                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });

        });
    }
}
