package admin_api_controllers

import (
	"XT_Admin_Api/enums"
	"XT_Admin_Api/models/admin_models"
	"XT_Admin_Api/service"
	"time"
)

type SystemApiController struct {
	AdminBaseAPIAuthController
}

//	func (this *SystemApiController) GetOrgs() {
//		list, _ := service.GetAllAdmin()
//		this.ServeSuccessJSON(map[string]interface{}{
//			"list": list,
//		})
//
// }
func (this *SystemApiController) GetAdminUserInfo() {
	id, _ := this.GetInt64("id")
	admin, _ := service.FindAdminById(id)
	this.ServeSuccessJSON(map[string]interface{}{
		"admin": admin,
	})
}

func (this *SystemApiController) GetUserList() {
	list, _ := service.GetAllAdmin()
	this.ServeSuccessJSON(map[string]interface{}{
		"list": list,
	})

}

func (this *SystemApiController) CreateAdminUser() {
	name := this.GetString("name")
	mobile := this.GetString("mobile")
	password := this.GetString("user_password")
	role_type, _ := this.GetInt64("type")
	remark := this.GetString("remark")

	admin := &admin_models.AdminAccount{
		Mobile:   mobile,
		Password: password,
		RoleType: role_type,
		Name:     name,
		Status:   1,
		Remark:   remark,
		IsAdmin:  2,
		Ctime:    time.Now().Unix(),
		Mtime:    time.Now().Unix(),
	}
	admins, _ := service.FindUserInfoByAccount(mobile)
	if admins.ID > 0 {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNameException)
		return
	}
	err := service.CreateAdmin(admin)
	if err == nil {
		this.ServeSuccessJSON(map[string]interface{}{
			"admin": admin,
		})
	}
}

func (this *SystemApiController) ModifyAdminUser() {
	id, _ := this.GetInt64("id")
	name := this.GetString("name")
	mobile := this.GetString("mobile")
	password := this.GetString("user_password")
	role_type, _ := this.GetInt64("type")
	remark := this.GetString("remark")

	admins, _ := service.FindAdminById(id)
	admin := &admin_models.AdminAccount{
		ID:       admins.ID,
		Mobile:   mobile,
		Password: password,
		Name:     name,
		Status:   1,
		RoleType: role_type,
		IsAdmin:  2,
		Ctime:    admins.Ctime,
		Remark:   remark,
		Mtime:    time.Now().Unix(),
	}

	if admins.Mobile != mobile {
		info, _ := service.FindUserInfoByAccount(mobile)
		if info.ID > 0 {
			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNameException)
			return
		}
	}
	err := service.UpdateAdmin(admin)
	if err == nil {
		this.ServeSuccessJSON(map[string]interface{}{
			"admin": admin,
		})
	}
}

func (this *SystemApiController) DeleteAdminUser() {
	id, _ := this.GetInt64("id")
	item_type, _ := this.GetInt64("type") //1.删除，2恢复
	if item_type == 1 {
		err := service.DeleteAdmin(id)
		if err == nil {
			this.ServeSuccessJSON(map[string]interface{}{
				"msg": "删除成功",
			})
		}
	} else {
		err := service.UpdateAdminTwo(id)
		if err == nil {
			this.ServeSuccessJSON(map[string]interface{}{
				"msg": "恢复成功",
			})
		}

	}

}

func (this *SystemApiController) GetAdminUserById() {
	id, _ := this.GetInt64("id")
	account, err := service.FindAdminById(id)
	if err == nil {
		this.ServeSuccessJSON(map[string]interface{}{
			"account": account,
		})
	}
}
