package com.jwipc.sys.controller.pc;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Sets;
import com.jwipc.page.PageView;
import com.jwipc.sys.dto.LoginPageAttrDTO;
import com.jwipc.sys.logic.LoginLogic;
import com.jwipc.sys.logic.NoticeLogic;
import com.jwipc.sys.model.query.MsgNoticeQuery;
import com.jwipc.util.EncryptUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;
import java.util.Set;

import static com.jwipc.sys.constant.ModuleConst.MODULE_ROOT;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Controller
@RequiredArgsConstructor
@RequestMapping()
public class LoginPageController {
    private final LoginLogic loginLogic;
    private final NoticeLogic noticeLogic;

    @RequestMapping({"/login"})
    public String login(Model model) {
        Set<String> sysConfigKeys = Sets.newConcurrentHashSet();
        sysConfigKeys.add("loginBg");
        sysConfigKeys.add("loginLogo");
        sysConfigKeys.add("loginCopyright");
        sysConfigKeys.add("webUrlIco");
        sysConfigKeys.add("webUrlTitle");
        final LoginPageAttrDTO hanlder = loginLogic.renderHanlder(sysConfigKeys);
        final Map<String, String> configs = hanlder.getConfigs();
        if (StrUtil.isNotEmpty(hanlder.getSupplierDefaultTemplateId())) {
            String registerUl = "pc/supplier/register?type=0&templateId=" +
                    EncryptUtils.encrypt(hanlder.getSupplierDefaultTemplateId());
            model.addAttribute("registerUl", registerUl);
        }
        for (String key : sysConfigKeys) {
            model.addAttribute(key, configs.get(key));
        }
        MsgNoticeQuery query = new MsgNoticeQuery();
        query.setPage(1);
        query.setRows(10);
        query.setPublicFlag(1);
        query.setStatus(1);
        PageView pageView = noticeLogic.findByPage(query);
        model.addAttribute("noticeList",pageView.getRows());
        return MODULE_ROOT + "/login";
    }

    @GetMapping("/logout")
    public String logout() {
        StpUtil.logout();
        return "redirect:/login";
    }
}
