package com.its.web.controller.admin;

import com.its.web.service.notice.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Nullable;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Slf4j
@RequiredArgsConstructor
@Controller
@RequestMapping(value="/phits")
@Api(tags = "99-2.운영자 화면 이동")
public class AdminController {
    private final NoticeService noticeService;

    @ApiOperation(value = "00.로그인")
    @GetMapping("")
    public String login(HttpSession session,
                        Model model,
                        HttpServletResponse res,
                        @Nullable @Param("LoginFail") String loginFail) throws IOException {
        model.addAttribute("loginFail", loginFail);
        return "admin/login";
    }

    @ApiOperation(value = "00.메인화면")
    @GetMapping("/main")
    public String adminMain(Model model) {
        model.addAttribute("notice", this.noticeService.findMainNotice(5));
        return "admin/main";
    }

    @ApiOperation(value = "06.운영자 - 01.공지사항 - 01.공지사항 목록")
    @GetMapping("/notice-list")
    public String adminNoticeList(Model model, @Nullable @Param("page") String page,
                                  @Nullable @Param("searchType") String searchType,
                                  @Nullable @Param("searchText")String searchText) {
        model.addAttribute("list", this.noticeService.findAllList(page, searchType, searchText));
        return "admin/notice-list";
    }

    @ApiOperation(value = "06.운영자 - 01.공지사항 - 02.공지사항 상세내용")
    @GetMapping("/notice-view/{boardNo}")
    public String adminNoticeView(Model model, @PathVariable("boardNo")String boardNo) {
        model.addAttribute("notice", this.noticeService.findNotice(boardNo));
        return "admin/notice-view";
    }

    @ApiOperation(value = "06.운영자 - 01.공지사항 - 03.공지사항 글쓰기")
    @GetMapping("/notice-write")
    public String adminNoticeWrite(Model model) {
        model.addAttribute("notice", this.noticeService.findMainNotice(5));
        return "admin/main";
    }
}
