<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Course\CourseHoles;
use App\Models\Course;
use App\Models\CourseField as CourseFieldModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

class CourseController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Course());
        $grid->model()->orderByDesc('id');
        $grid->column('id')->sortable();
        $grid->column('img')->image('', 50);
        $grid->column('name');
        $grid->column('course_field', '场地数')->display(function (){
            $fieldCount = CourseFieldModel::where('course_id', $this->id)->count();
            $url  = admin_url('/course_field?course_id='.$this->id.'');
            $html = $fieldCount.'<a href="'.$url.'"> 查看</a>';
            return $html;
        });
        $grid->column('photos')->display(function ($photos) {
            return json_decode($photos, true);
        })->image('', 60, 60);
        $grid->column('address');
        $grid->column('contacts_phone');
        $grid->column('style');
        $grid->filter(function (Grid\Filter $filter) {
            $filter->panel();
            $filter->equal('id')->width(4);
            $filter->equal('course_id')->width(4);
        });
        $grid->disableViewButton();
        return $grid;
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(Course::with('course_fileds'), function (Form $form) {

                $form->column(5, function (Form $form) {
                    $form->width(10, 2);
                    $form->display('id');
                    $form->text('name')->required();
                    $form->image('img')->removable(true)->uniqueName()->saveFullUrl()->autoUpload()->required();
                    $form->map('lat', 'lng', trans('course.fields.address'))->required();
                });
                $form->column(7, function (Form $form) {
                    $form->width(10, 2);
                    $form->text('contacts_phone');
                    $form->time('open_time')->format('HH:mm')->default('09:00', true);
                    $form->time('close_time')->format('HH:mm')->default('18:00', true);
                    $form->text('style');
                    $form->textarea('service');
                    $form->number('square');
                    $form->number('length');
                    $form->multipleImage('photos')->saveFullUrl()->autoUpload()->uniqueName()->saveAsJson();
                });


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

    }

}
