package com.backendsys.modules.cms.views;

import cn.hutool.core.convert.Convert;
import com.backendsys.modules.cms.page.entity.Page;
import com.backendsys.modules.cms.page.service.PageService;
import com.backendsys.modules.common.aspect.Pages;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Controller
public class PageViewController {

    @Autowired
    private PageService pageService;

    @Pages
    @Operation(summary = "单页详情 (视图)")
    @GetMapping({"/page/{page_sign}"})
    public String page(Model model, @PathVariable("page_sign") String page_sign) {

        String lang = Convert.toStr(model.getAttribute("lang"));

        // [Get] 获取单页详情 ----------------------------------------------
        Page pageDetail = pageService.selectPage(lang, page_sign);
        if (pageDetail == null) return "error";
        model.addAttribute("pageDetail", pageDetail);

        // -- Layout ---------------------------------------------
        model.addAttribute("title", pageDetail.getTitle());
        model.addAttribute("layout", "page");
        return "layout/layout";
    }

}
