<?php

namespace App\Admin\Controllers;

use App\Models\Goods;
use App\Models\GoodsAttr;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class GoodsController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Goods(), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('img')->image('', 60,60);
            $grid->column('cover_imgs')->display(function ($cover_imgs) {
                return json_decode($cover_imgs, true);
            })->image('', 60, 60);
            $grid->column('specific');
            $grid->column('is_recommend')->switch();
            $grid->column('status')->switch();
            $grid->column('created_at');

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

            $grid->disableViewButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Goods(), function (Form $form) {

            $form->tab('基本信息', function (Form $form){
                $form->display('id')->width(4);
                $form->text('name')->width(4);
                $form->image('img')->saveFullUrl()->autoUpload()->width(4);
                $form->multipleImage('cover_imgs')->saveFullUrl()->autoUpload()->uniqueName()->saveAsJson();
                $form->number('integral')->min(1);
                $form->number('stock')->min(1);
                $form->switch('is_recommend');
                $form->switch('status');
                $form->editor('introduce');
                $form->display('created_at')->width(4);
                $form->display('updated_at')->width(4);
            });

            $form->tab('规格', function (Form $form){
                $form->table('specific_list', function (Form\NestedForm $table){
                    $table->text('title', '名称');
                    $table->number('integral', '积分')->min(1);
                    $table->number('stock', '库存');
                    $table->image('pic_url', '图片')->saveFullUrl()->autoUpload();
                });
            });

            $form->saving(function (Form $form) {
                if (is_array($form->specific_list)) {
                    $this->specific_list = array_values($form->specific_list);
                    $form->deleteInput('specific_list');
                }
            });

            $form->saved(function (Form $form) {
                $id = $form->getKey(); //新增数据后返回的主键 id
                if (!empty($this->specific_list) && is_array($this->specific_list)) {
                    foreach ($this->specific_list as $val) {
                        unset($val['id']);
                        unset($val['_remove_']);
                        $val['goods_id'] = $id;
                        $map = ['goods_id' => $id, 'title' => $val['title']];
                        if ($info = GoodsAttr::query()->where($map)->first()) {
                            GoodsAttr::query()->where('id', $info->id)->update($val);
                        } else {
                            GoodsAttr::query()->create($val); //模型里面要添加 fillable
                        }
                    }
                    $list = GoodsAttr::query()
                        ->where('goods_id', $id)
                        ->select('id', 'goods_id', 'title', 'integral', 'pic_url', 'stock')
                        ->orderBy('id', 'asc')
                        ->get();
                    if($list->isNotEmpty()){
                        $list = $list->toArray();
                    }else{
                        $list = false;
                    }
                    $goods = Goods::find($id);
                    $goods->specific_list = $list ? json_encode($list) : '';
                    $goods->save();
                }
                return $form->response()->success('保存成功')->redirect('goods/'.$id.'/edit');
            });

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