<?php

namespace Modules\Mall\Http\Controllers\Admin;

use App\Admin\Traits\WithUploads;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Http\Request;
use Modules\Mall\Models\Brand;

class BrandController extends AdminController
{

    use WithUploads,
        WithShop;

    protected $title = '品牌管理';

    public function grid(): Grid
    {
        $grid = new Grid(new Brand());

        $grid->model()->with(['shop']);

        $grid->column('cover', 'LOGO')->image('', 80);
        $grid->column('shop.name', '所属店铺');
        $grid->column('name', '品牌名称');
        $grid->column('status', '状态')->bool();
        $grid->column('created_at', '创建时间');

        return $grid;
    }

    public function form(): Form
    {
        $form = new Form(new Brand());

        $this->shop($form)->required();
        $form->text('name', '品牌名称')
             ->required();
        $this->cover($form);
        $form->textarea('description', '品牌简介');
        $form->switch('status', '状态')->default(1);

        return $form;
    }

    public function ajax(Request $request)
    {
        $key = $request->q;

        return Brand::where('name', 'like', "%$key%")->paginate(null, ['id', 'name as text']);
    }

}