package com.example.controller;

import com.example.common.Result;
import com.example.common.config.TokenUtils;
import com.example.entity.Account;
import com.example.entity.Patient;
import com.example.service.PatientService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 账号相关接口
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Resource
    private PatientService patientService;

    /**
     * 获取所有患者列表（仅管理员可用）
     *
     * @param name 可选的患者姓名过滤
     * @return 患者列表
     */
    @GetMapping("/listPatients")
    public Result listPatients(@RequestParam(required = false) String name) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null || !"admin".equals(currentUser.getRole())) {
            return Result.error("401", "无权限，只有管理员可以访问此接口");
        }
        
        Patient searchParam = null;
        if (name != null && !name.isEmpty()) {
            searchParam = new Patient();
            searchParam.setName(name);
        }
        
        List<Patient> patients = patientService.selectAll(searchParam);
        return Result.success(patients);
    }
} 