package cn.reghao.devops.mgr.admin.controller.page;

import cn.reghao.devops.mgr.admin.db.repository.NotifyReceiverRepository;
import cn.reghao.devops.mgr.admin.model.po.NotifyReceiver;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author reghao
 * @date 2024-01-26 20:32:24
 */
@Api(tags = "通知接收页面")
@Controller
@RequestMapping("/sys/notify/receiver")
public class ReceiverPageController {
    private final NotifyReceiverRepository notifyReceiverRepository;

    public ReceiverPageController(NotifyReceiverRepository notifyReceiverRepository) {
        this.notifyReceiverRepository = notifyReceiverRepository;
    }

    @ApiOperation(value = "通知接收列表页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("")
    public String receiverIndex(Model model) throws Exception {
        PageRequest pageRequest = PageRequest.of(0, 100);
        Page<NotifyReceiver> page = notifyReceiverRepository.findAll(pageRequest);
        model.addAttribute("page", page);
        model.addAttribute("list", page.getContent());
        return "/admin/notify/receiver";
    }

    @ApiOperation(value = "添加通知接收页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/add")
    public String receiverAdd(Model model) throws Exception {
        return "/admin/notify/receiveradd";
    }

    @ApiOperation(value = "更新通知接收页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/edit/{name}")
    public String receiverEdit(@PathVariable("name") String name, Model model) throws Exception {
        NotifyReceiver notifyReceiver = notifyReceiverRepository.findByName(name);
        model.addAttribute("notifyReceiver", notifyReceiver);
        return "/admin/notify/receiveredit";
    }
}
