package com.huiju.foyo.controller;

import com.alibaba.fastjson.JSONObject;
import com.huiju.foyo.common.BaseController;
import com.huiju.foyo.common.Md5Utils;
import com.huiju.foyo.common.ResponseBean;
import com.huiju.foyo.model.SysUser;
import com.huiju.foyo.service.ISysUserService;
import com.huiju.foyo.session.UserControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author FXF
 * @date 2019-08-21
 */
@RestController
@RequestMapping("/")
public class LoginController {

	@Autowired
	ISysUserService iSysUserService;

	@RequestMapping("/admin/toLogin")
	public ResponseBean toLogin() {
		ResponseBean responseBean = new ResponseBean();
		responseBean.addError("用户未登录");
		return responseBean;
	}

	/**
	 * 登录
	 * @param jsonString
	 * @return
	 */
	@PostMapping("/admin/login")
	public ResponseBean login(@RequestBody String jsonString){
		return iSysUserService.login(jsonString);
	}

	/**
	 * 退出登录
	 * @return
	 */
	@PostMapping("/admin/logout")
	public ResponseBean logout() {
		UserControl.removeUser();
		return new ResponseBean();
	}

	/**
	 * 获取用户信息
	 * @return
	 */
	@GetMapping("/admin/user/info")
	public ResponseBean info(HttpSession session){
		ResponseBean responseBean = new ResponseBean();

		// 判断是否登录
		SysUser user = UserControl.getUser();

		Map<String, Object> map = new HashMap<>();
		map.put("roles","['admin']");
		map.put("token",user.getToken());
		map.put("introduction","我是超级管理员");
		map.put("avatar", user.getAvatar());
		map.put("name", user.getName());

		responseBean.addSuccess(map);
		return responseBean;
	}

	/**
	 * 修改密码
	 * @param oldPass
	 * @param newPass
	 * @return
	 */
	@PutMapping("/admin/user/upPass")
	public ResponseBean updatePass(@RequestParam(value = "oldPass") String oldPass,
								   @RequestParam(value = "newPass") String newPass) {

		return iSysUserService.upPass(oldPass, newPass);
	}
}
