<?php

namespace App\Admin\Controllers;

use App\Models\Product;
use App\Models\ProductCategory;
use App\Models\ProductHot;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ProductHotController extends AdminController
{
    const TYPE_normal = 'normal'; // 普通用户
    const TYPE_VIP = 'vip'; // VIP/设计师

    protected $types = ['normal' => 1, 'vip' => 2];

    protected $type = 1;

    public function __construct()
    {
        $route = \request()->route();
        $arr = explode("/",$route->uri);
        $this->type = $this->types[$arr[2]]??1;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(ProductHot::with(['product']), function (Grid $grid) {
            $grid->model()->orderByDesc('sort');
            $grid->model()->where('type', $this->type);
            $grid->column('id')->sortable();
            $grid->column('cover_img')->image('',80);
            $grid->column('product_id')->display(function (){
                return $this->product->name;
            });
            $grid->column('is_opened')->switch();
            $grid->column('sort')->editable();
            $grid->column('created_at');

//            $grid->filter(function (Grid\Filter $filter) {
//                $filter->equal('id');
//
//            });
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ProductHot(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('product_id');
            $show->field('sort');
            $show->field('is_opened');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ProductHot(), function (Form $form) {
            $form->display('id');
            $form->hidden('type')->value($this->type);
            $cates = Product::select(['id','name'])->where('is_opened',1)->get()->toArray();
            $form->select('product_id')
                ->options(array_column($cates,'name','id'))
                ->required();
            $form->image('cover_img')->saveFullUrl()
                ->uniqueName()->autoUpload()
                ->autoSave(false)
                ->removable(false)
                ->width(4)
                ->required();
            $form->radio('is_opened')->options(config('global.bool_status'))->default(1);
            $form->number('sort');


            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableListButton();
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->disableCreatingCheck();
        });
    }
}
