import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  Patch,
  Query,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { TokenData } from 'src/common/decorator/token-data.decorator';
import { UserDto } from '../dto/user.dto';
import { TokenDataEntity } from '../../common/entities/token-data.entity';
import { UserService } from '../service/user.service';
import { PageInfo } from 'src/common/decorator/query-page-info.decorator';
import { PageInfoEntity } from 'src/common/entities/page-info.entity';
import { UpdatePasswordDto } from '../dto/update-password.dto';
import { NoAuthToken } from 'src/common/decorator/no-token.decorator';

@ApiTags('系统管理 - 组织机构')
@ApiBearerAuth()
@Controller('system/user')
export class UserController {
  constructor(private readonly userService: UserService) {}
  @Post('batchCreate')
  async batchCreate(@TokenData() token: TokenDataEntity, @Body() userList) {
    console.log('6666666666666666666666');
    return this.userService.batchCreate(userList, token);
  }
  @Post()
  async create(@Body() userDto: UserDto, @TokenData() token: TokenDataEntity) {
    userDto.creator = userDto.updater = token?.userId;
    return this.userService.create(userDto);
  }
  @Post('register')
  @NoAuthToken()
  async register(@Body() userDto: UserDto) {
    userDto.creator = userDto.updater = 'resgister';
    userDto.roleId = 3;
    return this.userService.create(userDto);
  }
  @Get()
  async findAll(@PageInfo() pageInfo: PageInfoEntity, @Query() query) {
    return this.userService.findAll(pageInfo, query);
  }

  @Patch(':id')
  async update(
    @Param('id') id: string,
    @TokenData() token: TokenDataEntity,
    @Body() userDto: UserDto,
  ) {
    userDto.updater = token.userId;
    return this.userService.update(id, userDto);
  }

  @Post('updatePassword')
  async updatePassword(
    @TokenData() token: TokenDataEntity,
    @Body() updatePasswordDto: UpdatePasswordDto,
  ) {
    updatePasswordDto.updater = token.userId;
    return this.userService.updatePassword(updatePasswordDto);
  }

  @Patch('resetPassword/:id')
  async resetPassword(
    @Param('id') id: string,
    @TokenData() token: TokenDataEntity,
  ) {
    return this.userService.resetPassword(id, token.userId);
  }

  @Delete()
  async removes(@Query('ids') ids: string) {
    return this.userService.removes(ids.split(','));
  }
  @NoAuthToken()
  @Get('findAllTeacher')
  async findAllTeacher() {
    return this.userService.findAllTeacher();
  }

  @NoAuthToken()
  @Get('/role/:role')
  @ApiOperation({ summary: '获取用户列表ByRole' })
  async findSomeUserByRole(@Param('role') role: number) {
    return this.userService.findSomeUserByRole(+role);
  }

  @Get('findStudentByOrgId')
  async findStudentByOrgId(@Query('orgId') orgId: string) {
    return this.userService.findStudentByOrgId(+orgId);
  }

  @Get('findStudentByTeacher')
  async findStudentByTeacher(
    @Query('teacherId') teacherId: string,
    @TokenData() token: TokenDataEntity,
  ) {
    return this.userService.findStudentByTeacher(teacherId || token.userId);
  }
}
