package controllers

import (
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/middleware/jwts"
	"LAPP_LF_MOM_BACKEND/web/models"
	"LAPP_LF_MOM_BACKEND/web/supports"
	"fmt"
	"github.com/kataras/iris/v12"
	"time"
)

func GetPmWoMonthList(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/PmWo_controller.go"
	logs.Level = "info"
	logs.Function = "GetPmWoMonthList"
	logs.Message = "维护工单日历查看"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()
	var data models.PmWo
	var err error
	var pageSize = 10
	var pageIndex = 1

	if size := ctx.URLParam("pageSize"); size != "" {
		pageSize = utils.ValueToInt(size, 0)
	}

	if index := ctx.URLParam("pageIndex"); index != "" {
		pageIndex = utils.ValueToInt(index, 0)
	}
	data.Finr = user.Pid
	data.Status, _ = ctx.URLParamInt("status")
	data.Assetid, _ = ctx.URLParamInt("assetid")
	nowtime := ctx.URLParam("month")
	result, count, err := data.GetMonthPage(pageSize, pageIndex, nowtime)
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	res := make(map[string]interface{})
	res["data"] = result
	res["count"] = count
	res["pageIndex"] = pageIndex
	res["pageSize"] = pageSize
	supports.Ok(ctx, supports.OptionSuccess, res)
}

func GetPmWoList(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/PmWo_controller.go"
	logs.Level = "info"
	logs.Function = "GetPmWoList"
	logs.Message = "维护工单查看"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()
	var data models.PmWo
	var err error
	var pageSize = 10
	var pageIndex = 1

	if size := ctx.URLParam("pageSize"); size != "" {
		pageSize = utils.ValueToInt(size, 0)
	}

	if index := ctx.URLParam("pageIndex"); index != "" {
		pageIndex = utils.ValueToInt(index, 0)
	}


	data.Finr = user.Pid
	data.Status, _ = ctx.URLParamInt("status")
	data.Assetid, _ = ctx.URLParamInt("assetid")
	data.Descr = ctx.URLParam("assetname")
	searchtime := ctx.URLParam("searchtime")
	mainttype := ctx.URLParam("mainttype")
	result, count, err := data.GetPage(pageSize, pageIndex, searchtime, mainttype, user.Userid)
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	res := make(map[string]interface{})
	res["data"] = result
	res["count"] = count
	res["pageIndex"] = pageIndex
	res["pageSize"] = pageSize
	supports.Ok(ctx, supports.OptionSuccess, res)
}

func GetPmWo(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	var me models.PmWo
	me.Finr = user.Pid
	me.Maintwoid = ctx.URLParam("maintwoid")
	result, err := me.SelectOne()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}
func UpdatePmWo(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/PmWo_controller.go"
	logs.Level = "info"
	logs.Function = "UpdatePmWo"
	logs.Message = "更新维护工单"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()
	data := new(models.PmWo)
	if err := ctx.ReadJSON(data); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	data.Lastuser = user.Userid
	data.Finr = user.Pid
	err := data.Update()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "更新失败", nil)
		return
	}

	supports.Ok(ctx, "修改成功", "")
}
func GetPmWoAll(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/PmWo_controller.go"
	logs.Level = "info"
	logs.Function = "GetPmWoAll"
	logs.Message = "维护工单查询"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()

	me := new(models.PmWo)
	me.Finr = user.Pid
	me.Status, _ = ctx.URLParamInt("status")
	me.Assetid, _ = ctx.URLParamInt("assetid")
	nowtime := ctx.URLParam("month")
	result, err := me.SelectPmWoAll(nowtime)
	if err != nil {
		fmt.Printf("err  is :%v", err)
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}

// 手动创建维护工单接口
func InsertOnePMWO(ctx iris.Context)  {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/PmWo_controller.go"
	logs.Level = "info"
	logs.Function = "InsertOne"
	logs.Message = "创建维护工单"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()
	 pmOrder := new(models.PmWo)
	if err := ctx.ReadJSON(pmOrder); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "解析参数错误:" + err.Error(), nil)
		return
	}
	pmOrder.Finr = user.Pid
	pmOrder.Lastuser = user.Userid
	err := pmOrder.InsertOne()
	if err != nil {
		supports.Error(ctx, iris.StatusInternalServerError, "创建维护工单失败,错误: " + err.Error(), nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, nil)
}