<?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;
use Dingo\Api\Http\Request;

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(new ProductHot(), 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 (){
                $this->products = $this->product_id;
                $names  = $this->products->pluck('name')->toArray();
                return implode('、',$names);
            });
            $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);
            $products = Product::with('cate')->select(['id','name','cate_id'])
                ->where('is_opened',1)
                ->orderBy('cate_id')
                ->get();
            $options = [];
            foreach ($products as $product){
                $options[$product->id] = $product->name."({$product->cate->name})";
            }
//            $cates = ProductCategory::select(['id','name'])->where('is_opened',1)->get();
//            $form->select('cate_id','分类')
//                ->options($cates->pluck('name','id'))
//                ->load('product_id', 'product');

            $form->multipleSelect('product_id')
                ->options($options)->required();

            $form->image('cover_img','爆款图片（宽高比 1.34:1）')->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->saving(function (Form $form){
               /* $product_ids = [];
                foreach ($form->product_id as $product){
                    $product = array_filter($product);
                    if($product){
                        $product_ids = array_merge($product_ids,$product);
                    }
                }*/
                if(isset($form->product_id)){
                    $form->product_id =  array_filter($form->product_id );
                    if(!count($form->product_id)){
                        return $form->response()->error('请选择产品');
                    }
                    $form->deleteInput(['cate_id']);
                }
                //$form->product_id = array_filter($product_ids);

            });


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