package mobile_api_controllers

import (
	"Xcx_New/enums"
	"Xcx_New/models"
	"Xcx_New/service"
	"time"
)

type CheckApiController struct {
	MobileBaseAPIAuthController
}

func (c *CheckApiController) GetInspectionReference() {
	patient, _ := c.GetInt64("patient")
	if patient <= 0 {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	adminUserInfo := c.GetMobileAdminUserInfo()

	references, err := service.GetInspectionReference(adminUserInfo.Org.Id)
	if err != nil {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	}

	counts, err := service.GetPatientInspectionProjectCount(adminUserInfo.Org.Id, patient)
	if err != nil {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	}
	countsMap := make(map[int64]int64, 0)
	for _, count := range counts {
		countsMap[count.ProjectId] = count.Count
	}

	referenceMap := make(map[string]*models.InspectionReferenceMap, 0)

	for _, reference := range references {
		if _, exist := referenceMap[reference.Project]; !exist {
			referenceMap[reference.Project] = new(models.InspectionReferenceMap)
			referenceMap[reference.Project].Project = reference.Project
			referenceMap[reference.Project].ProjectId = reference.ProjectId
			referenceMap[reference.Project].ProjectName = reference.ProjectName
			if _, cexit := countsMap[reference.ProjectId]; cexit {
				referenceMap[reference.Project].Count = countsMap[reference.ProjectId]
			}
			referenceMap[reference.Project].InspectionReference = make([]models.InspectionReference, 0)
		}
		referenceMap[reference.Project].InspectionReference = append(referenceMap[reference.Project].InspectionReference, *reference)

	}

	reference := make([]*models.InspectionReferenceMap, 0)
	for _, item := range referenceMap {
		reference = append(reference, item)
	}

	rl := len(reference)
	for index := 0; index < rl-1; index++ {
		for jndex := 0; jndex < rl-1-index; jndex++ {
			if reference[jndex].ProjectId > reference[jndex+1].ProjectId {
				var item models.InspectionReferenceMap
				item = *reference[jndex]
				reference[jndex] = reference[jndex+1]
				reference[jndex+1] = &item
			}
		}
	}

	c.ServeSuccessJSON(map[string]interface{}{
		"reference": reference,
	})
	return

}

func (c *CheckApiController) GetInspections() {
	patient, _ := c.GetInt64("patient_id", 0)
	projectId, _ := c.GetInt64("project_id", 0)
	if patient <= 0 || projectId <= 0 {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	page, _ := c.GetInt64("page", 0)
	if page <= 0 {
		page = 1
	}

	adminUserInfo := c.GetMobileAdminUserInfo()

	inspections, _, dateTime, err := service.GetPatientInspections(adminUserInfo.Org.Id, patient, projectId, page)
	if err != nil {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	}
	date := ""
	if len(inspections) > 0 {
		date = time.Unix(dateTime, 0).Format("2006-01-02")
	}

	c.ServeSuccessJSON(map[string]interface{}{
		"inspections": inspections,
		"date":        date,
	})
	return
}
