package utils

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

type AmisResponse struct {
	Status int         `json:"status"`
	Msg    string      `json:"msg"`
	Data   interface{} `json:"data"`
}

type SelectOption struct {
	Label string `json:"label"`
	Value string `json:"value"`
}

type Options []SelectOption

func (o Options) Len() int {
	return len(o)
}

func (o Options) Swap(i, j int) {
	o[i], o[j] = o[j], o[i]
}

func (o Options) Less(i, j int) bool {
	return o[i].Label < o[j].Label
}

type SelectResponse struct {
	Options Options `json:"options"`
	Value   string  `json:"value,omitempty"`
}

type TableResponse struct {
	Items   interface{} `json:"items"`
	Total   int         `json:"total,omitempty"`
	HasNext bool        `json:"hasNext,omitempty"`
}

func AmisError(c *gin.Context, err interface{}) {
	c.JSON(http.StatusOK, AmisResponse{
		Status: -1,
		Msg:    fmt.Sprintf("%s", err),
	})
}
