<?php

namespace App\Admin\Controllers\Area;

use App\Admin\Actions\Area\AddStock;
use App\Admin\Actions\Area\SubStock;
use App\Admin\Selectable\AreaStockAble;
use App\Admin\Selectable\ClerksSelectAble;
use App\Models\Area;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Exception;
use Modules\Mall\Models\Region;

class IndexController extends AdminController
{

    protected $title = '地区管理';

    /**
     * Notes:
     *
     * @Author: 玄尘
     * @Date: 2023/1/11 13:44
     * @return Grid
     */
    public function grid(): Grid
    {
        $grid = new Grid(new Area());

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->add(new AddStock());
            $actions->add(new SubStock());

        });

        $grid->filter(function (Grid\Filter $filter) {
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->like('title', '地区名称');
            });

            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->equal('user_id', '管理员')->select()->ajax(route('admin.user.users.ajax'));
            });
        });

        $grid->model()->with(['user.info']);

        $grid->column('id', '序号');
        $grid->column('title', '地区名称');
        $grid->column('mobile', '联系方式');
        $grid->column('stock', '库存')
            ->modal('变动记录', AreaStockAble::class);
//        $grid->column('user.username', '管理员')
//            ->display(function () {
//                return $this->user->username."({$this->user->info->nickname})";
//            });
        $grid->column('all_address', '地址')
            ->display(function () {
                return $this->getFullAddress();
            });

        $grid->column('clerks', '操作员')
            ->belongsToMany(ClerksSelectAble::class);
        $grid->column('created_at', '创建时间');


        return $grid;
    }

    /**
     * Notes   : 编辑表单
     *
     * @Date   : 2021/7/15 5:09 下午
     * @Author : <Jason.C>
     * @return Form
     * @throws Exception
     */
    public function form(): Form
    {

        $form = new Form(new Area());

        $form->text('title', '地区名称')->required();
        $form->text('mobile', '联系方式')->required();

//        $form->select('user_id', '管理员')
//            ->options(function ($userId) {
//                $user = User::find($userId);
//                if ($user) {
//                    return [$user->id => $user->username.' ['.$user->info->nickname.']'];
//                }
//            })
//            ->value(request()->user_id ?? '')
//            ->ajax(route('admin.user.users.ajax'))
//            ->required();

        $form->belongsToMany('clerks', ClerksSelectAble::class, '关联操作员');

        if ($form->isCreating()) {
            $form->number('stock', '库存（份）')->default(100)->help('1份2箱');
        }

        $form->select('province_id', '省份')
            ->options(Region::where('parent_id', 1)->pluck('name', 'id'))
            ->load('city_id', route('admin.mall.regions.region'))
            ->required();
        $form->select('city_id', '城市')
            ->options(function ($option) {
                $parent = Region::where('id', $option)->value('parent_id');

                return Region::where(['parent_id' => $parent])->pluck('name', 'id');
            })
            ->load('district_id', route('admin.mall.regions.region'))
            ->required();
        $form->select('district_id', '区/县')
            ->options(function ($option) {
                $parent = Region::where('id', $option)->value('parent_id');

                return Region::where(['parent_id' => $parent])->pluck('name', 'id');
            })
            ->required();
        $form->text('address', '详细地址')
            ->required();
        $form->switch('status', '状态')
            ->default(1);
        return $form;
    }
}
