package com.example.monitorpatientstatus.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import com.example.monitorpatientstatus.entity.Patient;
import com.example.monitorpatientstatus.service.DoctorService;
import com.example.monitorpatientstatus.service.ManagerService;
import com.example.monitorpatientstatus.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    private ManagerService managerService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private PatientService patientService;
    @PostMapping("/login")
    //方法参数说明
    @ApiImplicitParam(name = "name", value = "用户名")
    //说明是什么方法(可以理解为方法注释)
    @ApiOperation(value = "登录", notes = "登录")
    public int login(String name,String password){
        if(managerService.login(name,password)!=0) {
            System.out.println(managerService.login(name,password));
            return managerService.login(name,password);
        }else if(doctorService.login(name,password)!=0){
            System.out.println(doctorService.login(name,password));
            return doctorService.login(name,password);
        }else  if(patientService.login(name,password)!=0){
            System.out.println(patientService.login(name,password));
            return patientService.login(name,password);
        }else {
            return 0;
        }
    }
    //登录&登出(待启用)
    @PostMapping("/login1")
    public String login1(String name, String password, HttpSession session, RedirectAttributes redirectAttributes){
        int i=login(name,password);
        if(i==0){
            redirectAttributes.addFlashAttribute("message","Wrong username or password");
            return "redirect:/admin";
        }else {
            //session.setAttribute("user", );
            return  "admin/index";
        }
    }
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "admin/login";
    }
}
