<?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 Modules\Mall\Models\Express;

class ExpressController extends AdminController
{

    use WithUploads;

    protected $title = '物流管理';

    protected function grid(): Grid
    {
        $grid = new Grid(new Express());

        $grid->model()->withCount('shops');

        $grid->filter(function (Grid\Filter $filter) {
            $filter->scope('trashed', '回收站')->onlyTrashed();

            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->like('name', '物流名称');
            });
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->like('slug', '物流简称');
            });
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->equal('status', '状态')->select([
                    0 => '禁用',
                    1 => '正常',
                ]);
            });
        });

        $grid->column('id');
        $grid->column('cover', 'LOGO')->image('', 60);
        $grid->column('name', '物流名称');
        $grid->column('slug', '物流简称');
        $grid->column('description', '物流简介');
        $grid->column('status', '状态')->bool();
        $grid->column('shops_count', '使用商户');
        $grid->column('created_at', '创建时间');

        return $grid;
    }

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

        $form->text('name', '物流名称')
             ->required();
        $form->text('slug', '物流简称')
             ->required();
        $form->textarea('description', '物流简介');

        $this->cover($form);

        $form->switch('status', '状态')->default(1);

        return $form;
    }

}