package report

import (
	"net/http"

	"in-server/app/main/api/internal/logic/report"
	"in-server/app/main/api/internal/svc"
	"in-server/app/main/api/internal/types"
	"in-server/common/result"

	"github.com/zeromicro/go-zero/rest/httpx"
)

func DownloadReportPdfHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.DownloadReportPdfReq
		if err := httpx.Parse(r, &req); err != nil {
			result.ParamErrorResult(r, w, err)
			return
		}

		l := report.NewDownloadReportPdfLogic(r.Context(), svcCtx)
		resp, err := l.DownloadReportPdf(&req)
		if err != nil {
			result.HttpResult(r, w, nil, err)
			return
		}

		// 直接以文件流形式返回，前端更易处理下载
		w.Header().Set("Content-Type", "application/pdf")
		w.Header().Set("Content-Disposition", "attachment; filename=\""+resp.FileName+"\"")
		_, _ = w.Write(resp.Content)
	}
}

