package common.controller;

import common.model.Menu;
import common.model.User;
import common.service.MenuService;
import common.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import security.annotation.CurrentAccount;
import util.ResultModel;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by mxq on 2017/12/21.
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;


    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public ResultModel create(@CurrentAccount User loginAccount, @RequestBody User resource) {
        userService.create(loginAccount, resource);
        return ResultModel.OK();
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public ResultModel update(@CurrentAccount User loginAccount, @RequestBody User resource) {
        userService.create(loginAccount, resource);
        return ResultModel.OK();
    }

    @RequestMapping(value = "/find", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel find(@CurrentAccount User loginAccount, @RequestBody User resource) {
        userService.create(loginAccount, resource);
        return ResultModel.OK();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel delete(@CurrentAccount User loginAccount, @RequestBody User resource) {
        userService.create(loginAccount, resource);
        return ResultModel.OK();
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public ResultModel updateName(@CurrentAccount User loginAccount, @RequestParam String name, @PathVariable Long id) {
        userService.updateName(loginAccount, id, name);
        return ResultModel.OK();
    }

    @RequestMapping(value = "/update/{id}/pwd", method = RequestMethod.PUT)
    @ResponseBody
    public ResultModel updatePwd(@CurrentAccount User loginAccount, @RequestParam String pwd, @PathVariable Long id) {
        userService.updatePwd(loginAccount, id, pwd);
        return ResultModel.OK();
    }

    @RequestMapping(value = "/update/pwd", method = RequestMethod.PUT)
    @ResponseBody
    public ResultModel updatePwd(@RequestParam String email, @RequestParam String pwd) {

        return ResultModel.OK(userService.updatePwd(email, pwd));
    }

    @RequestMapping(value = "forget/", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel sengCode(@RequestParam String email) {

        return ResultModel.OK(userService.sendCode(email));
    }

    @RequestMapping(value = "valid/code", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel validCode(@RequestParam String email, @RequestParam String code) {
        userService.validCode(email, code);
        return ResultModel.OK();
    }
}
