package com.openhis.web.document.controller;

import java.util.HashMap;
import java.util.Map;

import com.openhis.web.document.dto.DocTemplateDto;
import org.springframework.web.bind.annotation.*;

import com.core.common.core.domain.R;
import com.openhis.common.enums.DocTypeEnum;
import com.openhis.common.enums.DocUseRangeEnum;
import com.openhis.web.document.appservice.IDocTemplateAppService;
import com.openhis.web.document.util.EnumUtil;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

/**
 * 文书模板/病历模板 controller
 *
 * @author wanghaiming
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/document/template")
@Slf4j
@AllArgsConstructor
public class DocTemplateController {

    private final IDocTemplateAppService docTemplateAppService;

    /**
     * 初始化文书定义
     *
     * @return
     */
    @GetMapping("/init")
    public R<?> init() {
        Map<String, Object> map = new HashMap<>();

        // 获取文档类型枚举列表
        map.put("docTypes", EnumUtil.toMapList(DocTypeEnum.class));
        // 使用范围枚举
        map.put("useRanges", EnumUtil.toMapList(DocUseRangeEnum.class));
        return R.ok(map);
    }

    @PostMapping("/add")
    public R<?> add(@RequestBody DocTemplateDto docTemplateDto) {
        return docTemplateAppService.add(docTemplateDto);
    }

    @DeleteMapping("/delete")
    public R<?> delete(Long id) {
        return docTemplateAppService.delete(id);
    }

    @PutMapping("/update")
    public R<?> update(@RequestBody DocTemplateDto docTemplateDto) {
        return docTemplateAppService.update(docTemplateDto);
    }

    @GetMapping("/getById")
    public R<?> getById(Long id) {
        return docTemplateAppService.getById(id);
    }


    @GetMapping("/getListByDefinitionId")
    public R<?> getListByDefinitionId(Long definitionId) {
        return docTemplateAppService.getListByDefinitionId(definitionId);
    }

    @GetMapping("/getPageList")
    public R<?> getPageList(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                            @RequestParam(name = "searchKey", required = false) String searchKey, HttpServletRequest request) {
        return docTemplateAppService.getPageList(pageNo, pageSize, searchKey, request);
    }
}
