<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Users\UserCourse;
use App\Models\CourseUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class CourseUserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CourseUser());
        $grid->model()->with(['user:id,name,avatar','course:id,name'])->orderByDesc('id');

        $grid->column('id')->sortable();
        $grid->column('course.name',trans('course-user.fields.course_id'));
        $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('user.fields.name').'：'.$this->user->name.'</p>';
                $str.="</div>";
                $str.="</div>";
                return $str;
            }else{
                return '';
            }

        });
        $grid->column('type')->using([1=>trans('course-user.fields.home'),2=>trans('course-user.fields.vice')])->label(['','green','blue']);
        $grid->column('member_name');
        $grid->column('member_no');
        $grid->column('whs');
        $grid->column('status')->using([0=>trans('course-user.fields.check_pending'),1=>trans('course-user.fields.pass'),2=>trans('course-user.fields.refuse')])->label(['yellow','green','danger']);
        $grid->column('created_at');
        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id');

        });

        //操作管理
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();
            if ($actions->row->status == 0) {
                $actions->append(new UserCourse());
            }
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CourseUser(), function (Show $show) {
            $show->field('id');
            $show->field('course_id');
            $show->field('user_id');
            $show->field('type');
            $show->field('member_name');
            $show->field('member_no');
            $show->field('whs');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CourseUser(), function (Form $form) {
            $form->display('id');
            $form->text('course_id');
            $form->text('user_id');
            $form->text('type');
            $form->text('member_name');
            $form->text('member_no');
            $form->text('whs');
            $form->text('status');

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