package controllers

import (
	"XT_New/service"
	"strconv"

	"XT_New/models"
	"XT_New/utils"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/jinzhu/gorm"
	"time"
)

type DoctorsApiController struct {
	BaseAuthAPIController
}

func DoctorApiRegistRouters() {
	beego.Router("/api/alldoctors", &DoctorsApiController{}, "get:GetAllDoctorAndNurse")
	beego.Router("/api/admin/users", &DoctorsApiController{}, "get:GetAllAdminUsers")
	beego.Router("/api/patient/getdryweightdata", &DoctorsApiController{}, "Get:GetDryWeightData")
	beego.Router("/api/patient/getAllDoctor", &DoctorsApiController{}, "Get:GetAllDoctor")
	beego.Router("/api/patient/updatedryweightdata", &DoctorsApiController{}, "Post:UpdatedDryWeightData")
	beego.Router("/api/patient/getalldata", &DoctorsApiController{}, "Get:GetAllData")
	beego.Router("/api/paients/getdryweightdetail", &DoctorsApiController{}, "Get:GetDryWeightDetail")
	beego.Router("/api/patients/modifydryweightdata", &DoctorsApiController{}, "Get:ModifydryWeightData")
	beego.Router("/api/patient/deletedryweight", &DoctorsApiController{}, "Delete:DeleteDryWeight")
}

func (c *DoctorsApiController) GetAllDoctorAndNurse() {

	adminUserInfo := c.GetAdminUserInfo()
	doctors, nursers, _ := service.GetAllDoctorAndNurse(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)

	c.ServeSuccessJSON(map[string]interface{}{
		"doctors": doctors,
		"nursers": nursers,
	})
	return
}
func (c *DoctorsApiController) GetAllAdminUsers() {

	adminUserInfo := c.GetAdminUserInfo()
	users, _ := service.GetAllAdminUsers(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
	c.ServeSuccessJSON(map[string]interface{}{
		"users": users,
	})
	return
}

func (c *DoctorsApiController) GetDryWeightData() {
	adminUserInfo := c.GetAdminUserInfo()
	orgid := adminUserInfo.CurrentOrgId

	patientid, _ := c.GetInt64("patientid")
	fmt.Println("patientid", patientid)
	id := adminUserInfo.AdminUser.Id
	fmt.Println("id", id)
	recordDateStr := time.Now().Format("2006-01-02")
	recordDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", recordDateStr)
	fmt.Scan("parseDateErr", parseDateErr)
	nowtime := recordDate.Unix()
	fmt.Println("nowtime", nowtime)
	pre, err := service.GetDryWeightByPatientId(patientid, orgid)
	fmt.Println("错误", err)
	c.ServeSuccessJSON(map[string]interface{}{
		"pre": pre,
	})
}

func (c *DoctorsApiController) GetAllDoctor() {
	adminUserInfo := c.GetAdminUserInfo()
	orgid := adminUserInfo.CurrentOrgId
	appid := adminUserInfo.CurrentAppId
	fmt.Println("appid", appid)
	appRole, err := service.GetAllDoctor(orgid, appid)
	fmt.Println("appRole", appRole)
	fmt.Println("错误", err)
	c.ServeSuccessJSON(map[string]interface{}{
		"appRole": appRole,
	})
}

func (c *DoctorsApiController) UpdatedDryWeightData() {
	adminUserInfo := c.GetAdminUserInfo()
	orgid := adminUserInfo.CurrentOrgId
	userid := adminUserInfo.AdminUser.Id
	fmt.Println("userid", userid)
	dry_weight, _ := c.GetFloat("dry_weight")
	fmt.Println("dry_weight", dry_weight)
	doctors, _ := c.GetInt64("doctors")
	fmt.Println("doctors", doctors)
	remarks := c.GetString("remarks")
	fmt.Println("remarks", remarks)
	patientid, _ := c.GetInt64("patient_id")
	fmt.Println("patientid", patientid)
	//透前数据
	dryweight, _ := c.GetFloat("dryweight")
	fmt.Println("dryweight", dryweight)
	var weight = dryweight - dry_weight
	weights := fmt.Sprintf("%.1f", weight)
	//weights := strconv.FormatFloat(sprintf, 'E', -1, 64)

	fmt.Println("weight", weights)
	fmt.Println("weight", weight)
	var sum string
	dryWeight, errcode := service.QueryDryWeight(orgid, patientid)
	fmt.Println("errcode", errcode)
	fmt.Println("dryWeight", dryWeight)
	if errcode == gorm.ErrRecordNotFound {
		sum = "/"
		patientDryweight := models.SgjPatientDryweight{
			DryWeight:     dry_weight,
			Creator:       doctors,
			Remakes:       remarks,
			AdjustedValue: sum,
			PatientId:     patientid,
			Ctime:         time.Now().Unix(),
			UserOrgId:     orgid,
			Status:        1,
			UserId:        userid,
		}

		err := service.CreatePatientWeight(&patientDryweight)
		fmt.Println("err", err)
		c.ServeSuccessJSON(map[string]interface{}{
			"patientDryweight": patientDryweight,
		})
		return
	}
	fmt.Println("sum", sum)
	if weight == 0 {
		sum = "/"
	}
	if weight > 0 {
		sum = weights + "(" + "下调" + ")"

	}
	if weight < 0 {
		var sums = dry_weight - dryweight
		float := fmt.Sprintf("%.1f", sums)
		//float := strconv.FormatFloat(sums, 'E', -1, 64)
		sum = float + "(" + "上调" + ")"
	}

	patientDryweight := models.SgjPatientDryweight{
		DryWeight:     dry_weight,
		Creator:       doctors,
		Remakes:       remarks,
		AdjustedValue: sum,
		PatientId:     patientid,
		Ctime:         time.Now().Unix(),
		UserOrgId:     orgid,
		Status:        1,
		UserId:        userid,
	}

	err := service.CreatePatientWeight(&patientDryweight)
	//sgjPatientDryweight, _ := service.GetLastData(orgid)
	recordDateStr := time.Now().Format("2006-01-02")
	recordDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", recordDateStr)
	fmt.Scan("parseDateErr", parseDateErr)
	nowtime := recordDate.Unix()
	fmt.Println("nowtime", nowtime)
	//prescription := models.PredialysisEvaluation{
	//	DryWeight: dryweight,
	//}
	//errors := service.UpdateDialysisPrescription( sgjPatientDryweight.PatientId, sgjPatientDryweight.UserOrgId, sgjPatientDryweight.DryWeight, prescription)
	//fmt.Println("error",errors)
	fmt.Println("err", err)
	fmt.Println("patientdryweight", patientDryweight)
	c.ServeSuccessJSON(map[string]interface{}{
		"patientDryweight": patientDryweight,
	})
}

func (c *DoctorsApiController) GetAllData() {
	id, _ := c.GetInt64("id")
	page, _ := c.GetInt64("page")
	fmt.Println("page", page)
	limit, _ := c.GetInt64("limit")
	fmt.Println("limit", limit)

	fmt.Println("id", id)
	adminUserInfo := c.GetAdminUserInfo()
	orgid := adminUserInfo.CurrentOrgId
	dry, total, err := service.GetAllData(orgid, id, page, limit)
	fmt.Println("total是是多少", total)
	fmt.Println("错误是什么", err)
	fmt.Println("dry", err)
	c.ServeSuccessJSON(map[string]interface{}{
		"dry":   dry,
		"total": total,
	})
}

func (c *DoctorsApiController) GetDryWeightDetail() {

	id, _ := c.GetInt64("id")
	dryweight, _ := service.GetDryWeightDetailById(id)
	c.ServeSuccessJSON(map[string]interface{}{
		"dryweight": dryweight,
	})
}

func (c *DoctorsApiController) ModifydryWeightData() {

	adjustvalue := c.GetString("adjustvalue")

	creator, _ := c.GetInt64("creator")

	dryweight, _ := c.GetInt64("dryweight")
	dry := strconv.FormatInt(dryweight, 10)
	dry_weight, _ := strconv.ParseFloat(dry, 64)
	id, _ := c.GetInt64("id")

	remark := c.GetString("remark")

	patientDryweight := models.SgjPatientDryweight{
		AdjustedValue: adjustvalue,
		Creator:       creator,
		DryWeight:     dry_weight,
		Remakes:       remark,
	}
	service.ModifyDryWeightData(&patientDryweight, id)
	c.ServeSuccessJSON(map[string]interface{}{
		"patientDryweight": patientDryweight,
	})
}

func (c *DoctorsApiController) DeleteDryWeight() {

	id, _ := c.GetInt64("id")
	service.DeleteDryWeight(id)
	returnData := make(map[string]interface{}, 0)
	returnData["msg"] = "ok"
	c.ServeSuccessJSON(returnData)
	return
}