package com.contract.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.contract.entity.AppHeader;
import com.contract.entity.resp.MessageResult;
import com.contract.enums.MessageCode;
import com.contract.service.AppHeaderService;
import com.contract.util.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app-header")
public class AppHeaderController {

    @Autowired
    private AppHeaderService appHeaderService;

    /**
     * 分页查询
     */
    @PostMapping("/page-query")
    public MessageResult pageQuery(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String title,
            HttpServletRequest request) {
        String lang = RequestUtils.getLangFromHeader(request);
        Page<AppHeader> page = appHeaderService.pageQuery(pageNum, pageSize, status, title);
        return MessageResult.successLang(lang, page);
    }

    /**
     * 查询详情
     */
    @GetMapping("/detail")
    public MessageResult detail(@RequestParam Long id, HttpServletRequest request) {
        String lang = RequestUtils.getLangFromHeader(request);
        AppHeader header = appHeaderService.getById(id);
        if (header == null) {
            return MessageResult.failLang(lang, MessageCode.HEADER_NOT_FOUND);
        }
        return MessageResult.successLang(lang, header);
    }

    /**
     * 查询App端生效的Header列表
     */
    @GetMapping("/active-list")
    public MessageResult activeList(HttpServletRequest request) {
        String lang = RequestUtils.getLangFromHeader(request);
        return MessageResult.successLang(lang, appHeaderService.findActiveHeaders());
    }
}
