package cn.reghao.tnb.search.app.controller;

import cn.reghao.tnb.search.app.EsSearch;
import cn.reghao.tnb.search.app.model.po.Wenshu;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author reghao
 * @date 2025-05-02 16:44:46
 */
@Controller
public class SearchController {
    private EsSearch esSearch;

    // 裁判文书搜索页面
    @GetMapping("/wenshu")
    public String wenshuSearchPage(@RequestParam(value = "search", required = false) String search,
                                   @RequestParam(value = "searchType", required = false) String searchType,
                                   ModelMap model) {
        Pageable pageable = PageRequest.of(0, 10, Sort.by(Sort.Direction.ASC, "createTime"));
        Page<Wenshu> page;
        if (search == null) {
            page = Page.empty();
        } else {
            page = esSearch.search(search, pageable);
        }

        if (searchType == null) {
            searchType = "";
        }

        model.put("page", page);
        model.put("search", search);
        model.put("searchType", searchType);
        return "/classic/wenshu/wenshu_search";
    }

    // 裁判文书详情页面
    @GetMapping("/wenshu/{id}")
    public String wenshuPage(@PathVariable("id") String id, ModelMap model) {
        Wenshu wenshuResult = esSearch.getById(id);
        model.put("wenshu", wenshuResult);
        return "/classic/wenshu/wenshu";
    }
}
