package cn.reghao.devops.web.admin.sys.controller.page;

import cn.reghao.devops.web.admin.sys.db.repository.EmailAccountRepository;
import cn.reghao.devops.web.admin.sys.model.po.EmailAccount;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2024-01-26 20:52:54
 */
@Api(tags = "邮件帐号管理页面")
@Controller
@RequestMapping("/sys/notify/email")
public class EmailPageController {
    private final EmailAccountRepository emailAccountRepository;

    public EmailPageController(EmailAccountRepository emailAccountRepository) {
        this.emailAccountRepository = emailAccountRepository;
    }

    @GetMapping("")
    public String emailIndex(Model model) {
        PageRequest pageRequest = PageRequest.of(0, 100);
        Page<EmailAccount> page = emailAccountRepository.findAll(pageRequest);
        page.getContent().forEach(emailAccount -> {
            emailAccount.setPassword("******");
        });

        model.addAttribute("page", page);
        model.addAttribute("list", page.getContent());
        return "/admin/notify/email";
    }

    @GetMapping("/add")
    public String emailAdd(Model model) {
        return "/admin/notify/emailadd";
    }

    @GetMapping("/edit/{username}")
    public String emailEdit(@PathVariable("username") String username, Model model) throws Exception {
        List<EmailAccount> list = emailAccountRepository.findAll();
        model.addAttribute("emailAccount", list.get(0));
        return "/admin/notify/emailadd";
    }
}
