package cn.bunny.mvc.controller;

import cn.bunny.mvc.dao.dto.BillDto;
import cn.bunny.mvc.dao.entity.Bill;
import cn.bunny.mvc.service.ThymeleafService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@RequestMapping("thymeleaf")
@Controller
public class ThymeleafController {

    private final ThymeleafService thymeleafService;

    public ThymeleafController(ThymeleafService thymeleafService) {
        this.thymeleafService = thymeleafService;
    }

    @Operation(summary = "查看账单信息")
    @RequestMapping("bill/{page}/{limit}")
    public String bill(Model model,
                       @PathVariable Long page,
                       @PathVariable Long limit,
                       BillDto dto) {
        IPage<Bill> billIPage = new Page<>(page, limit);
        if (!StringUtils.hasText(dto.getDescription())) {
            dto.setDescription(null);
        }
        List<Bill> billList = thymeleafService.bill(billIPage, dto);
        model.addAttribute("billList", billList);

        return "thymeleaf/bill";
    }

    // 不能包含age，header必须匹配header
    @GetMapping(value = "href-test", params = {"!age"}, headers = {"host=localhost:8080"})
    public String hrefTest() {
        return "thymeleaf/href-test";
    }

    // ?表示可以任意字符
    @RequestMapping("a?e")
    public String test() {
        return "test/test";
    }
}
