package new_mobile_api_controllers

import (
	"Xcx_New/controllers/mobile_api_controllers"
	"Xcx_New/enums"
	"Xcx_New/models"
	"Xcx_New/service"
	"fmt"
	"strings"
	"time"
)

type StaffScheduleApiController struct {
	mobile_api_controllers.MobileBaseAPIAuthController
}

func (this *StaffScheduleApiController) GetMobileDoctorList() {

	orgId := this.GetMobileAdminUserInfo().Org.Id
	appId := this.GetMobileAdminUserInfo().App.Id
	staffList, err := service.GetDoctorList(orgId, appId)
	//获取所有的医生
	doctorList, err := service.GetAllDoctorList(orgId, appId)

	//获取所有的护士
	nurseList, err := service.GetAllNurseList(orgId, appId)
	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}

	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}

	this.ServeSuccessJSON(map[string]interface{}{
		"staffList":  staffList,
		"doctorList": doctorList,
		"nurseList":  nurseList,
	})
}

func (this *StaffScheduleApiController) GetMobileStaffScheduleList() {

	orgId := this.GetMobileAdminUserInfo().Org.Id
	start_time, _ := this.GetInt64("start_time")
	fmt.Println("staft_time", start_time)
	end_time, _ := this.GetInt64("end_time")
	staffList, err := service.GetStaffScheduleList(orgId, start_time, end_time)
	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}

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

func (this *StaffScheduleApiController) GetMobileNextWeekScheduleLIst() {

	orgId := this.GetMobileAdminUserInfo().Org.Id
	start_time, _ := this.GetInt64("start_time")
	end_time, _ := this.GetInt64("end_time")

	staffList, err := service.GetStaffScheduleList(orgId, start_time, end_time)
	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}

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

func (this *StaffScheduleApiController) GetScheduleByUserType() {
	doctor_id, _ := this.GetInt64("doctor_id")
	start_time, _ := this.GetInt64("start_time")
	end_time, _ := this.GetInt64("end_time")
	orgId := this.GetMobileAdminUserInfo().Org.Id
	staffList, err := service.GetScheduleByDoctorId(doctor_id, start_time, end_time, orgId)
	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}

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

func (this *StaffScheduleApiController) GetSearchPatientByDoctorId() {

	orgId := this.GetMobileAdminUserInfo().Org.Id
	user_name := this.GetString("user_name")
	start_time, _ := this.GetInt64("start_time")
	end_time, _ := this.GetInt64("end_time")
	staffList, err := service.ToSearchSeacheduleList(user_name, start_time, end_time, orgId)
	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}

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

func (this *StaffScheduleApiController) GetMobileSchedulelist() {

	orgId := this.GetMobileAdminUserInfo().Org.Id
	list, err := service.GetScheduleList(orgId)
	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}

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

func (this *StaffScheduleApiController) GetMobileScheduleListTotal() {

	start_time, _ := this.GetInt64("start_time")
	end_time, _ := this.GetInt64("end_time")
	orgId := this.GetMobileAdminUserInfo().Org.Id

	//统计班次
	scheudletotal, err := service.GetScheduleListTotal(orgId, start_time, end_time)
	//统计总工时
	list, err := service.GetTotalMinutes(orgId, start_time, end_time)
	//统计出勤天数
	workDay, err := service.GetTotalWorkDay(orgId, start_time, end_time)
	//统计缺勤天数
	noWorkDay, err := service.GetTotalNoWorkDay(orgId, start_time, end_time)
	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"scheudletotal": scheudletotal,
		"list":          list,
		"workDay":       workDay,
		"noWorkDay":     noWorkDay,
	})
}

func (this *StaffScheduleApiController) GetMySchedule() {
	start_time, _ := this.GetInt64("start_time")
	end_time, _ := this.GetInt64("end_time")
	orgId := this.GetMobileAdminUserInfo().Org.Id
	creator, _ := this.GetInt64("creator")
	schedule, err := service.GetMySchedule(start_time, end_time, orgId, creator)
	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"schedule": schedule,
	})
}

func (this *StaffScheduleApiController) GetMobileChartList() {

	start_time, _ := this.GetInt64("start_time")
	end_time, _ := this.GetInt64("end_time")
	orgId := this.GetMobileAdminUserInfo().Org.Id
	//统计总工时
	list, err := service.GetTotalMinutesOne(orgId, start_time, end_time)
	//统计出勤的总工时
	workDaylist, err := service.GetTotalMinutesTwo(orgId, start_time, end_time)
	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"list":        list,
		"workDaylist": workDaylist,
	})
}

func (this *StaffScheduleApiController) ToSearchMobileTotal() {

	start_time, _ := this.GetInt64("start_time")
	end_time, _ := this.GetInt64("end_time")
	orgId := this.GetMobileAdminUserInfo().Org.Id
	keyword := this.GetString("keyword")
	//统计表
	scheduletotal, err := service.GetSearchScheduleListTotal(orgId, start_time, end_time, keyword)
	list, err := service.GeSearchtTotalMinutes(orgId, start_time, end_time, keyword)
	workDay, err := service.GetSearchWorkDay(orgId, start_time, end_time, keyword)
	noWorkDay, err := service.GetSearchWorkNoDay(orgId, start_time, end_time, keyword)

	//统计图
	chartTotalMinute, err := service.GetChartTotalMinutesOne(orgId, start_time, end_time, keyword)
	chartWorkDayMinute, err := service.GetChartTotalMinutesTwo(orgId, start_time, end_time, keyword)
	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"scheduletotal":      scheduletotal,
		"list":               list,
		"workDay":            workDay,
		"noWorkDay":          noWorkDay,
		"chartTotalMinute":   chartTotalMinute,
		"chartWorkDayMinute": chartWorkDayMinute,
	})
}

func (this *StaffScheduleApiController) ChangeMobileOption() {

	start_time, _ := this.GetInt64("start_time")
	end_time, _ := this.GetInt64("end_time")
	orgId := this.GetMobileAdminUserInfo().Org.Id
	doctor_type, _ := this.GetInt64("doctor_type")
	//统计图
	scheduletotal, err := service.ChagneScheduleListTotal(orgId, start_time, end_time, doctor_type)
	list, err := service.ChangeScheduleMinute(orgId, start_time, end_time, doctor_type)
	workday, err := service.ChangeWorkDay(orgId, start_time, end_time, doctor_type)
	worknoday, err := service.ChangeNoWorkDay(orgId, start_time, end_time, doctor_type)

	//统计表
	chartotalminute, err := service.ChartTotalMinute(orgId, start_time, end_time, doctor_type)
	chartworkdayminute, err := service.ChartWorkDayMinute(orgId, start_time, end_time, doctor_type)
	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"scheduletotal":      scheduletotal,
		"list":               list,
		"workday":            workday,
		"worknoday":          worknoday,
		"chartotalminute":    chartotalminute,
		"chartworkdayminute": chartworkdayminute,
	})
}

func (this *StaffScheduleApiController) GetAllZone() {

	id := this.GetMobileAdminUserInfo().Org.Id
	zone, err := service.GetAllMobileZone(id)
	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"zone": zone,
	})
}

func (this *StaffScheduleApiController) GetPatientScheduleList() {
	orgid := this.GetMobileAdminUserInfo().Org.Id
	fmt.Println("orgid", orgid)
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	start_time := this.GetString("start_time")
	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()
	}
	fmt.Println("时间搓", startTime)
	zoneid, _ := this.GetInt64("zoneid")
	fmt.Println(zoneid)
	classtype, _ := this.GetInt64("classtype")
	fmt.Println("classtype", classtype)

	//获取当天排班的病人情况
	schedule, err := service.GetPatientSchedule(startTime, zoneid, classtype, orgid)

	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"schedule": schedule,
	})
}

func (this *StaffScheduleApiController) DeleteSchedule() {
	id, _ := this.GetInt64("id")
	fmt.Println("id", id)
	err := service.DeleteSchedule(id)
	fmt.Println(err)
	returnData := make(map[string]interface{}, 0)
	returnData["msg"] = "ok"
	this.ServeSuccessJSON(returnData)
	return
}

func (this *StaffScheduleApiController) SearchPatient() {
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	keyword := this.GetString("keyword")
	fmt.Println(keyword)
	orgId := this.GetMobileAdminUserInfo().Org.Id
	start_time := this.GetString("start_time")
	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()
	}
	schedule, err := service.GetSearchPatient(keyword, orgId, startTime)

	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加设备失败")
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"schedule": schedule,
	})
}

func (this *StaffScheduleApiController) UpdatePatientScheduleById() {

	ids := this.GetString("ids")
	idSplit := strings.Split(ids, ",")
	var err error
	//查询患者信息
	list, _ := service.GetStaffScheduleById(idSplit)
	if len(list) > 0 {
		for index, _ := range list {
			fmt.Println("index2322332", index)
			schedule := models.XtSchedule{
				PatientId: list[0].PatientId,
				ModeId:    list[0].ModeId,
			}
			xtSchedule := models.WeekSchedule{
				PatientId: list[1].PatientId,
				ModeId:    list[1].ModeId,
			}
			err = service.UpdateStaffSchedule(list[1].ID, schedule)
			fmt.Println("errrrrrrr2323323232323", err)
			service.UpdateStaffScheduleOne(list[0].ID, xtSchedule)
		}
	}
	fmt.Println(err)
	this.ServeSuccessJSON(map[string]interface{}{
		"msg": "切换成功",
	})
}
