package reports

import (
	"errors"
	"fonchain-fiee/api/reports"
	"fonchain-fiee/pkg/model/login"
	"fonchain-fiee/pkg/service"
	"fonchain-fiee/pkg/service/bundle/common"

	"github.com/gin-gonic/gin"
)

func AnnualReportCreate(ctx *gin.Context) {
	var req reports.AddAnnualReportReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	user := login.GetUserInfoFromC(ctx)
	req.Operator = user.Name
	req.OperatorId = int32(user.ID)
	resp, err := service.ReportsProvider.AddAnnualReport(ctx, &req)
	if err != nil {
		service.Error(ctx, errors.New(common.ErrorCreateReportFailed))
		return
	}
	service.Success(ctx, resp)
}

// 修改年度报告排序和状态
func AnnualReportSortAndStatus(ctx *gin.Context) {
	var req reports.SortAndStatusAnnualReportReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.ReportsProvider.SortAndStatusAnnualReport(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

// 获取年度报告信息
func AnnualReportInfo(ctx *gin.Context) {
	var req reports.GetAnnualReportInfoReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.ReportsProvider.GetAnnualReportInfo(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

// 获取年度报告列表
func AnnualReportList(ctx *gin.Context) {
	var req reports.GetAnnualReportListReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.ReportsProvider.GetAnnualReportList(ctx, &req)
	if err != nil {
		service.Error(ctx, errors.New(common.ErrorListReportFailed))
		return
	}
	service.Success(ctx, resp)
}

func AnnualReportDelete(ctx *gin.Context) {
	var req reports.DeleteAnnualReportReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.ReportsProvider.DeleteAnnualReport(ctx, &req)
	if err != nil {
		service.Error(ctx, errors.New(common.ErrorDeleteReportFailed))
		return
	}
	service.Success(ctx, resp)
}

func AnnualReportEdit(ctx *gin.Context) {
	var req reports.EditAnnualReportReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	user := login.GetUserInfoFromC(ctx)
	req.Operator = user.Name
	req.OperatorId = int32(user.ID)
	resp, err := service.ReportsProvider.EditAnnualReport(ctx, &req)
	if err != nil {
		service.Error(ctx, errors.New(common.ErrorEditReportFailed))
		return
	}
	service.Success(ctx, resp)
}

func AnnualReportDisplay(ctx *gin.Context) {
	var req reports.DisplayAnnualReportReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.ReportsProvider.DisplayAnnualReport(ctx, &req)
	if err != nil {
		service.Error(ctx, errors.New(common.ErrorDisplayReportFailed))
		return
	}
	service.Success(ctx, resp)
}

func QuarterlyReportDisplay(ctx *gin.Context) {
	var req reports.DisplayQuarterlyReportReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.ReportsProvider.DisplayQuarterlyReport(ctx, &req)
	if err != nil {
		service.Error(ctx, errors.New(common.ErrorDisplayReportFailed))
		return
	}
	service.Success(ctx, resp)
}
