package mobile_api_controllers

import (
	"Xcx_New/enums"
	"Xcx_New/models"
	"Xcx_New/service"
	"Xcx_New/utils"
	"fmt"
)

type DoctorAdviceAPIController struct {
	MobileBaseAPIAuthController
}

// /m/api/schedule/advices [get]
// @param date:string (yyyy-mm-dd)
// @param advice_type:int 1长期医嘱 3临时医嘱 其他:全部
func (this *DoctorAdviceAPIController) ScheduleAdvices() {
	schedualDate := this.GetString("date")
	adviceType, _ := this.GetInt("advice_type")
	patientType, _ := this.GetInt("patient_type")
	delivery_way := this.GetString("delivery_way")
	if adviceType != 1 && adviceType != 3 && adviceType != 2 {
		adviceType = 0
	}

	if patientType != 1 && patientType != 2 {

		patientType = 0
	}

	date, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", schedualDate)
	if parseDateErr != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	adminInfo := this.GetMobileAdminUserInfo()
	orgID := adminInfo.Org.Id

	scheduals, err := service.MobileGetScheduleDoctorAdvices(orgID, date.Unix(), adviceType, patientType, adminInfo.AdminUser.Id, delivery_way)
	//获取his医嘱数据
	fmt.Println("date.un222222", date.Unix())
	hisAdvices, _ := service.GetHisDoctorAdvices(orgID, date.Unix(), delivery_way, patientType, adminInfo.AdminUser.Id)

	project, _ := service.GetMobileHisPrescriptionProject(orgID, date.Unix(), delivery_way, patientType, adminInfo.AdminUser.Id)
	config, _ := service.GetHisDoctorConfig(orgID)
	project_config, _ := service.GetHisProjectConfig(orgID)

	adminUser, _ := service.GetAllAdminUsers(orgID, adminInfo.App.Id)
	if err != nil {
		this.ErrorLog("获取排班信息失败:%v", err)
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
	} else {
		filtedScheduals := []*service.MScheduleDoctorAdviceVM{}
		for _, schedual := range scheduals {
			if len(schedual.DoctorAdvices) > 0 {
				filtedScheduals = append(filtedScheduals, schedual)
			}
		}
		this.ServeSuccessJSON(map[string]interface{}{
			"scheduals":      filtedScheduals,
			"adminUser":      adminUser,
			"hisAdvices":     hisAdvices,
			"config":         config,
			"project":        project,
			"project_config": project_config,
		})
	}
}

func (c *DoctorAdviceAPIController) GetAdviceConfigs() {
	advice_type, _ := c.GetInt64("type", 0)
	adminUserInfo := c.GetMobileAdminUserInfo()
	var drugs []models.DrugDic
	drugways, _, _ := service.GetDrugWayDics(adminUserInfo.Org.Id)
	efs, _, _ := service.GetExecutionFrequencyDics(adminUserInfo.Org.Id)
	adviceTemplates, _ := service.FindAllAdviceTemplate(adminUserInfo.Org.Id, advice_type)
	c.ServeSuccessJSON(map[string]interface{}{
		"drugs":            drugs,
		"drugways":         drugways,
		"efs":              efs,
		"advice_templates": adviceTemplates,
	})
}
