<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Product;
use App\Admin\Repositories\ProductType;
use App\Models\Msg;
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('user:id,name,email,member_type')->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('user.name',admin_trans_field('user_id'));
            $grid->column('user.email',admin_trans_field('email'));
            $grid->column('user.member_type_text',admin_trans_field('member_type'));
            $grid->column('name');
            $grid->column('content');
            $grid->column('image')->image('',40);
            $grid->column('url')->link();
            $grid->column('type',admin_trans_field('type'))->display(function (){
                $arr = \App\Models\ProductType::query()->whereIn('id',$this->type)->pluck('zh_name');
                    return $arr;
            })->label();

            $grid->column('status')->switch();
            $grid->column('created_at');
            $grid->disableCreateButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('name')->width(3);
                $filter->where('type',function ($query){
                    $query->whereJsonContains('type',[intval($this->input)]);
                })->select(\App\Models\ProductType::selectOptions())->width(4);

            });
        });
    }

    /**
     * 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->multipleSelect('type')
            ->options(\App\Models\ProductType::selectOptions())
                ->saving(function ($value) {
                    // 转化成json字符串保存到数据库
                    return json_encode($value);
                });

//            $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->url('url')->width(4);
            $form->switch('status')->default(1);
            $form->saving(function (Form $form) {
                if($form->status == "0" && $form->isEditing()){
                    $id = $form->getKey();
                    $product = \App\Models\Product::query()->where('id',$id)->first();
                    $msg = [
                        'type' => 4,//下架通知
                        'title' => "下架通知",
                        'content' => "很抱歉的告诉您，您上传的内容涉及敏感内容，我们已处理下架",
                        'user_id' => $product->user_id,
                        'to_user_id' => $product->user_id,
                    ];
                    Msg::query()->create($msg); // 添加通知消息
                }
            });
        });
    }
}
