package com.weavi.controller;

import com.weavi.entity.User;
import com.weavi.service.NewUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/front")
public class NewUserController {
    @Autowired
    NewUserService userService;

    @RequestMapping("/index")
    public String index(){
        return "index1";
    }

    @RequestMapping("/register")
    public String register(){
        return "_register";
    }

    @RequestMapping("/delete")
    public String delete(int id){
        userService.delete(id);
        return "null";
    }

    @RequestMapping("/doregister")
    public String register(HttpServletRequest request, User user) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String password2 = request.getParameter("password2");
        if(password.equals(password2)){
            User user1 = new User();
            user1.setUsername(username);
            user1.setPassword(password);
            userService.save(user1);
            return "_login";
        }else
            return "_register";

    }


    public boolean registerUser(String username){
        if(userService.findByName(username).isEmpty())
            return true;
        else
            return false;
    }

    @RequestMapping("/login")
    public String login(){
        return "_login";
    }

    @RequestMapping("/dologin")
    public String login(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = userService.FindNameAndPsw(username, password);
        String str = "";
        if (user != null) {
            str = "success";
        } else {
            str = "_login";
        }
        return str;
    }

}
