package com.cym.controller.page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cym.ext.AdminExt;
import com.cym.model.Admin;
import com.cym.service.AdminExampleService;
import com.cym.utils.BaseController;
import com.cym.utils.JsonResult;

import cn.hutool.core.lang.UUID;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

/**
 * 登录页
 * 
 * @author Administrator
 *
 */
@Slf4j
@RequestMapping("/")
@Controller
public class LoginExampleController extends BaseController {
	@Autowired
	AdminExampleService adminExampleService;


	@RequestMapping("index")
	public ModelAndView admin(ModelAndView modelAndView, HttpServletRequest request,  String adminId) {

		modelAndView.setViewName("/loginExample/index");
		return modelAndView;
	}

	@RequestMapping("loginExample/login")
	@ResponseBody
	public JsonResult submitLogin(String name, String pass,  HttpServletRequest request) {

		getHttpSession().removeAttribute("adminLogin");

		// 用户名密码
		Admin admin = adminExampleService.login(name, pass);
		if (admin == null) {
			return renderError("用户名密码错误"); // 用户名密码错误
		}

		// 登录成功
		admin.setAutoKey(UUID.randomUUID().toString()); // 生成自动登录code
		jpaHelper.updateById(admin);

		// 读取菜单
		AdminExt adminExt = new AdminExt();

		getHttpSession().setAttribute("adminLogin", adminExt);


		return renderSuccess(admin);
	}



}
