<?php

namespace App\Admin\Controllers;

use Admin;
use App\Admin\Repositories\ServiceCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ServiceCategoryController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ServiceCategory(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('cover')->image();
            $grid->column('sort')->sortable();
            $grid->column('status')->using([0 => '关闭', 1 => '启用']);
            $grid->column('creator');
            $grid->column('updater');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            // 显示服务分类下的服务
            $grid->column('services', '服务')->display(function ($services) {
                return count($services);
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('title');
                $filter->equal('status')->select([0 => '关闭', 1 => '启用']);
            });
            // $grid->sortable();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ServiceCategory(), function (Show $show) {
            // $show->field('id');
            $show->field('title');
            $show->field('cover')->image();
            $show->field('sort');
            $show->field('status')->using([0 => '关闭', 1 => '启用']);
            $show->field('creator');
            $show->field('updater');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ServiceCategory(), function (Form $form) {
            // id在创建时不显示，并数据库自动递增
            $form->text('title');
            $form->image('cover')->uniqueName()->autoUpload(); // 自动上传
            // $form->image('cover')->uniqueName()->autoUpload()
            // ->resize(300, 200)->saveAs('thumbnails/'.$form->cover->getClientOriginalName());
            $form->number('sort');
            $statusOptions = [
                0 => '关闭',
                1 => '启用',
            ];
            $form->select('status', '状态')->options(options: $statusOptions);

            $form->saving(function (Form $form) {
                if ($form->isCreating()) {
                    $form->creator = Admin::user()->name;
                    $form->created_at = now();
                }
                $form->updater = Admin::user()->name;
                $form->updated_at = now();
            });

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