package controllers

import (
	"FollowUp_Notice/conf"
	"FollowUp_Notice/lib"
	"FollowUp_Notice/models/Account"
	"FollowUp_Notice/models/Surgical"
	"FollowUp_Notice/models/System"
	beego "github.com/beego/beego/v2/server/web"
	"math"
	"strconv"
)

type SurgicalController struct {
	beego.Controller
	User Account.User
}

func (c *SurgicalController) Prepare() {
	c.User = *Account.User_r
}

// 疾病
func (c *SurgicalController) Surgical_List() {

	// 分页参数 初始化
	page, _ := c.GetInt("page")
	if page < 1 {
		page = 1
	}
	page_z, _ := c.GetInt("page_z")
	if page_z < 1 {
		page_z = conf.Page_size
	}

	// 查询
	T_name := c.GetString("T_name")

	R_List, R_cnt := Surgical.Read_Surgical_List(c.User.Id, T_name, page, page_z)

	var r_jsons lib.R_JSONS
	r_jsons.Num = R_cnt
	r_jsons.Data = R_List
	r_jsons.Page = page
	r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))

	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
	c.ServeJSON()
	return
}
func (c *SurgicalController) Surgical_Add() {

	T_name := c.GetString("T_name")
	T_sort, _ := c.GetInt("T_sort")

	var_ := Surgical.Surgical{
		T_name:  T_name,
		T_sort:  T_sort,
		T_State: 1,
		T_uid:   c.User.Id,
	}

	Id, err := Surgical.Add_Surgical(var_)
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
		c.ServeJSON()
		return
	}

	System.Add_UserLogs_T(c.User.T_uuid, "术式", "添加", var_)

	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
	c.ServeJSON()
	return
}
func (c *SurgicalController) Surgical_Edit() {
	T_id, _ := c.GetInt("T_id")
	T_name := c.GetString("T_name")
	T_sort, _ := c.GetInt("T_sort")

	Surgical_r, err := Surgical.Read_Surgical_ById(T_id)
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
		c.ServeJSON()
		return
	}

	if len(T_name) > 0 {
		Surgical_r.T_name = T_name
	}
	Surgical_r.T_sort = T_sort

	if err = Surgical.Update_Surgical(Surgical_r, "T_name", "T_sort"); err != nil {
		c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
		c.ServeJSON()
		return
	}

	System.Add_UserLogs_T(c.User.T_uuid, "术式", "修改", Surgical_r)

	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
	c.ServeJSON()
	return
}
func (c *SurgicalController) Surgical_Del() {

	T_id, _ := c.GetInt("T_id")
	Surgical_r, err := Surgical.Read_Surgical_ById(T_id)
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
		c.ServeJSON()
		return
	}

	if err = Surgical.Delete_Surgical(Surgical_r); err != nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
		c.ServeJSON()
		return
	}

	System.Add_UserLogs(c.User.T_uuid, "术式", "删除", strconv.Itoa(T_id))

	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
	c.ServeJSON()
	return
}
