package history

import (
	"Wine-Server/utils"
	"Wine-Server/utils/tables"
	"github.com/gorilla/websocket"
)

func QueryWorker(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	var param queryWorkerParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryQueryWorkerRes", utils.Fail("参数错误")))
		return
	}
	total, workers, err := tables.WorkerQueryForHistory(manager.Id, param.Cond, param.Limit, param.Page)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryQueryWorkerRes", utils.Fail("查询信息失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent(
		"adminWineHistoryQueryWorkerRes",
		utils.Success(utils.JsonType{"total": total, "list": workers}),
	))
}

func QueryOrder(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	var param queryOrderParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryQueryOrderRes", utils.Fail("参数错误")))
		return
	}
	total, orders, err := tables.ChangesQuery(manager.Id, param.Worker, param.Cond, param.Deal, param.Limit, param.Page)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryQueryOrderRes", utils.Fail("查询信息失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent(
		"adminWineHistoryQueryOrderRes",
		utils.Success(utils.JsonType{"total": total, "list": orders}),
	))
}

func OrderUpdate(conn *websocket.Conn, data any) {
	var param updateParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryOrderUpdateRes", utils.Fail("参数错误")))
		return
	}
	change := tables.ChangeTable{Id: param.Id}
	err = change.Get()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryOrderUpdateRes", utils.Fail("订单不存在")))
		return
	}
	if change.Deal {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryOrderUpdateRes", utils.Fail("不可修改已完成的订单")))
		return
	}
	change.New, change.Publish = param.New, utils.TimeNow()
	err = change.UpdateSelf()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryOrderUpdateRes", utils.Fail("更新失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryOrderUpdateRes", utils.Success(nil)))
}

func OrderDelete(conn *websocket.Conn, data any) {
	id, ok := data.(uint32)
	if !ok {
		_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryOrderDeleteRes", utils.Fail("参数错误")))
		return
	}
	change := tables.ChangeTable{Id: id}
	err := change.Get()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryOrderDeleteRes", utils.Fail("订单不存在")))
		return
	}
	if change.Deal {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryOrderDeleteRes", utils.Fail("不可删除已完成的订单")))
		return
	}
	warn := tables.WarnTable{Id: change.Wid}
	err = warn.Get()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryOrderDeleteRes", utils.Fail("报警信息错误")))
		return
	}
	warn.Deal = false
	err = warn.UpdateSelf()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryOrderDeleteRes", utils.Fail("重置报警信息失败")))
		return
	}
	err = change.Delete()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryOrderDeleteRes", utils.Fail("删除失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("adminWineHistoryOrderDeleteRes", utils.Fail("删除成功")))
}
