<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Product;
use App\Admin\Repositories\ProductType;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ProductController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Product(), function (Grid $grid) {
            $grid->model()->with(['type:id,zh_name,ko_name','user:id,name'])->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('image')->image('',40);
            $grid->column('type.zh_name',admin_trans_field('type'));
            $grid->column('user.name',admin_trans_field('user_id'));
            $grid->column('name');
            $grid->column('content');
            $grid->column('status')->switch();
            $grid->column('created_at');
            $grid->disableCreateButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('name')->width(3);
                $filter->equal('type')->select(\App\Models\ProductType::selectOptions())->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Product(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('user_id');
            $show->field('name');
            $show->field('content');
            $show->field('image');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Product(), function (Form $form) {
            $form->display('id')->width(4);
            $form->select('type')->options(\App\Models\ProductType::selectOptions())->required()->width(4);
            $form->select('user_id')->width(4)->options(User::query()->where('status',1)->pluck('name','id'))->required();
            $form->text('name')->required()->width(4);
            $form->textarea('content')->required()->width(4);
            $form->image('image')->autoUpload()->uniqueName()->required()->width(4);
            $form->switch('status')->default(1);
        });
    }
}
