package new_mobile_api_controllers

import (
	"Xcx_New/enums"
	"Xcx_New/service/management_service"
	service "Xcx_New/service/print_data_service/schedule_dialysis"
	"Xcx_New/utils"
	"time"
)

type ManagementAnalyseApiController struct {
	NewMobileBaseAPIAuthController
}

func (this *ManagementAnalyseApiController) StockRecordPrintData() {
	types, _ := this.GetInt("type", 0)
	start_time := this.GetString("start_time")
	end_time := this.GetString("end_time")
	adminUserInfo := this.GetMobileAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var startTime int64
	if len(start_time) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
		if err != nil {
			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		startTime = theTime.Unix()
	}
	var endTime int64
	if len(end_time) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		endTime = theTime.Unix()
	}

	list, err := service.FindPrintStockGoodInfoByType(types, startTime, endTime, adminUserInfo.Org.Id)

	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
	} else {
		this.ServeSuccessJSON(map[string]interface{}{
			"list": list,
			"type": types,
		})
	}
}
func (this *ManagementAnalyseApiController) StatisticsPatientChart() {
	start_time := this.GetString("start_time")
	end_time := this.GetString("end_time")
	statistics_type, _ := this.GetInt("type", 0)

	adminUserInfo := this.GetMobileAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var startTime int64
	if len(start_time) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
		if err != nil {
			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		startTime = theTime.Unix()
	}
	var endTime int64
	if len(end_time) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		endTime = theTime.Unix()
	}
	data, total_one, _ := management_service.GetPatientChartData(adminUserInfo.Org.Id, startTime, endTime, statistics_type)
	total, _ := management_service.GetLapsetoPatientTotal(adminUserInfo.Org.Id, statistics_type)

	total_three, _ := management_service.GetLapsetoPatientTotal(adminUserInfo.Org.Id, 2)

	this.ServeSuccessJSON(map[string]interface{}{
		"chart_data":  data,
		"total_one":   total_one,
		"total":       total,
		"total_three": total + total_three,
	})

}
func (this *ManagementAnalyseApiController) StatisticsPatientTable() {
	start_time := this.GetString("start_time")
	end_time := this.GetString("end_time")
	statistics_type, _ := this.GetInt("type", 0)
	page, _ := this.GetInt64("page", 0)
	limit, _ := this.GetInt64("limit", 0)

	if page == 0 {
		page = 1
	}
	if limit == 0 {
		limit = 20
	}

	adminUserInfo := this.GetMobileAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var startTime int64
	if len(start_time) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
		if err != nil {
			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		startTime = theTime.Unix()
	}
	var endTime int64
	if len(end_time) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		endTime = theTime.Unix()
	}
	data, total, _ := management_service.GetPatientTableData(adminUserInfo.Org.Id, page, limit, startTime, endTime, statistics_type)

	this.ServeSuccessJSON(map[string]interface{}{
		"data":  data,
		"total": total,
	})

}
