<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Feeds\FeedAction;
use App\Admin\Actions\Feeds\LikeAction;
use App\Models\Team;
use App\Models\TeamPhoto;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TeamPhotoController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new TeamPhoto());
        $grid->model()->with(['user:id,name','team:id,name'])->orderByDesc('id');
        $grid->column('id')->sortable();
        $grid->column('team.name',trans('team-photo.fields.team_id'));
        $grid->column('user.name',trans('team-photo.fields.user_id'));
        $grid->column('photo_urls')->display(function ($res){
            if(isset($res)&&!empty($res)){
                $res = json_decode($res,true);
                $str = '';
                if(!empty($res)&&is_array($res)&&count($res)>0){
                    foreach ($res as $item){
                        $str.='<img data-action="preview-img" src="'.$item['url'].'" style="max-width:50px;max-height:200px;cursor:pointer" class="img img-thumbnail">';
                    }
                }
                return $str;
            }else{
                return "";
            }
        });
        $grid->column('sort')->sortable();
        $grid->column('created_at');
        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id');
            $filter->like('user.name',trans('user.fields.name'));
            $team = Team::query()->select(['id','name'])->get()->toArray();
            foreach ($team as $v){
                $team_arr[$v['id']] = $v['name'];
            }
            $filter->equal('team_id',trans('team.labels.team'))->select($team_arr);
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TeamPhoto(), function (Show $show) {
            $show->field('id');
            $show->field('team_id');
            $show->field('user_id');
            $show->field('url')->image('',100);
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TeamPhoto(), function (Form $form) {
            $form->width(11, 1);
            $form->width(7, 1)->display('id');
            $team = Team::query()->select(['id','name'])->get()->toArray();
            $team_arr = array();
            foreach ($team as $v){
                $team_arr[$v['id']] = $v['name'];
            }
            $form->select('team_id')->options($team_arr);
            $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->array('photo_urls', function ($form) {
                $form->image('url', '图片')->disk("oss")->saveFullUrl()->uniqueName()->removable(false)->autoUpload();
            })->label("图片");

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

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