<?php

namespace App\Admin\Controllers;

use App\Models\User;
use App\Models\UserRole;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UsersRoleController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserRole(), function (Grid $grid) {
            $grid->model()->with('userData')->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('userData.name','昵称');
            $grid->column('userData.avatar','头像')->image('',80,80);
            $grid->column('userData.mobile','手机号');
            $grid->column('name');
            $grid->column('sex');
            $grid->column('age');
            $grid->column('star');
            $grid->column('level','年级');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->disableCreateButton();
            $grid->disableViewButton();
            $grid->export() ->rows(function ($rows) {
                $exportRows = [];
                foreach ($rows as $index => $row) {
                    // 确保 $row->userData 不为 null
                    if ($row->userData !== null) {
                        $user = $row->userData;
                        $exportRow = [
                            'id' => $row->id,
                            'userData.name' => $user->name,  // Add 'userData.name' to export
                            'userData.avatar' => $user->avatar,  // Add 'userData.avatar' to export
                            'userData.mobile' => $user->mobile,  // Add 'userData.avatar' to export
                            'name' => $row->name,
                            'sex' => $row->sex,
                            'age' => $row->age,
                            'star' => $row->star,
                            'level' => $row->level,
                            'created_at' => $row->created_at,
                            'updated_at' => $row->updated_at,
                        ];
                        $exportRows[] = $exportRow;
                    }
                }
//                var_dump($exportRows);
                return collect($exportRows);
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('level')->select(UserRole::$level);
                $filter->like('name');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserRole(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('name');
            $show->field('sex');
            $show->field('age');
            $show->field('star');
            $show->field('level');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserRole(), function (Form $form) {
            $form->display('id');
//            $form->text('user_id');
            $form->text('name');
            $form->select('sex')->options(['男孩' => '男孩','女孩' => '女孩']);
            $form->number('age');
            $form->text('star');
            $form->select('level')->options(UserRole::$level);
            $form->disableViewButton();
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
