<?php

namespace Modules\Mall\Http\Controllers\Admin;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Http\Request;
use Modules\Mall\Models\Tag;

class TagController extends AdminController
{

    use WithShop;

    protected $title = '商品标签';

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

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

        $grid->column('shop.name', '所属店铺');
        $grid->column('name', '标签名称');

        return $grid;
    }

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

        $this->shop($form)->required();
        $form->text('name', '标签名称')
             ->required();

        return $form;
    }

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

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

}