<?php

namespace Modules\User\Http\Controllers\Admin;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Modules\User\Http\Controllers\Admin\Actions\AllotCard;
use Modules\User\Models\UserInvite;
use Vinkla\Hashids\Facades\Hashids;

class InviteController extends AdminController
{
    protected $title = '激活码管理';

    protected function grid()
    {
        $grid = new Grid(new UserInvite());
        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->disableColumnSelector();

        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new AllotCard());
        });

        $grid->header(function ($query) {
            $form = new Form();
            $form->action(admin_url('/users/invites/createCard'));
//            $form->hidden('_token')->default(csrf_token());
            $form->disableReset();
            $form->text('num', '生成数量')->rules('required|integer|min:1');

            $box = new Box('生成激活码', $form->render());
            $box->collapsable();
            $box->style('success');
            $box->solid();
            return $box->render();
        });


        $grid->model()->orderBy('id', 'desc');

        $grid->column('id', '序号')->sortable();
//        $grid->column('number', '编号')->sortable();
        $grid->column('归属')->display(function () {
            return ($this->user->username ?? '---')."<br>".($this->user->info->nickname ?? '---');
        });

        $grid->column('code', '激活码');
        $grid->column('status', '状态')
            ->using(UserInvite::STATUS)
            ->label([
                1 => 'warning',
                2 => 'success',
                3 => 'info',
            ]);

        $grid->column('激活用户')->display(function () {
            return ($this->activeUser->username ?? '---')."<br>".($this->activeUser->info->nickname ?? '---');

        });
        $grid->column('actived_at', '激活时间');
        $grid->column('created_at', '创建时间');

        $grid->filter(function (Grid\Filter $filter) {
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->like('code', '激活码');
            });
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->like('user.username', '归属');
            });
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->equal('status', '状态')->select(UserInvite::STATUS);
            });

        });

        return $grid;
    }

    public function createCard(Request $request)
    {
        $request->validate([
            'num' => 'required|integer|min:1',
        ], [
            'num.required' => '数量必须填写',
            'num.integer'  => '数量必须是整数',
            'num.min'      => '数量最小为1',
        ]);

        $num       = $request->num;
        $codeStart = (UserInvite::max('id') ?? 0) + 1;

        $data = [];
        while ($num > 0) {
            $data[] = [
                'number'     => sprintf("%'.08d", $codeStart++),
                'code'       => Str::random(14),
                'status'     => UserInvite::STATUS_INIT,
                'created_at' => now(),
                'updated_at' => now(),
            ];
            --$num;
        }
        UserInvite::insert($data);
        admin_success('成功生成', $request->num.'个码已经生成。');
        return back();
    }

}
