package pposRequest

import (
	"PPOSUtils/utils"
	"encoding/json"
	"errors"
	"net/url"
)

//NewBaseAccountStadium 获取账户授权的场馆
func (that *PPOSClient) NewBaseAccountStadium() *BaseAccountStadiumRequest {
	return &BaseAccountStadiumRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/base/stadium/list",
		},
	}
}

//Response...
type BaseAccountStadiumResponse struct {
	Count int64         `json:"count"` //count
	List  []StadiumList `json:"list"`  //list
	Page  int64         `json:"page"`  //page
	Size  int64         `json:"size"`  //size
}

type StadiumList struct {
	BusinessId  int64  `json:"business_id"`
	StadiumId   int64  `json:"stadium_id"`
	StadiumName string `json:"stadium_name"`
}

//BaseAccountStadiumRequest ...
type BaseAccountStadiumRequest struct {
	Request
}

func (that *BaseAccountStadiumRequest) SetStatus(status, page, size, personnelID, businessID int64) *BaseAccountStadiumRequest {
	that.initParams()
	that.params.Set("status", utils.ToString(status))
	that.params.Set("page", utils.ToString(page))
	that.params.Set("size", utils.ToString(size))
	that.params.Set("account_id", utils.ToString(2))
	that.params.Set("status", utils.ToString(1))
	that.params.Set("personnel_id", utils.ToString(personnelID))
	that.params.Set("business_id", utils.ToString(businessID))
	return that
}

//Params ...
func (that *BaseAccountStadiumRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	//
	if that.publicParamsSlice != nil {
		for _, _v := range that.publicParamsSlice {
			if _params.Get(_v) == "" {
				return nil, errors.New(that.apiAddress + "缺少参数:" + _v)
			}
		}
	}
	return _params, nil
}

type BaseAccountStadiumStadiumResponse struct {
	StadiumId   int64  `json:"stadium_id"`
	Logo        string `json:"logo"`
	StadiumName string `json:"stadium_name"`
}

//Do ...
func (that *BaseAccountStadiumRequest) Do() (BaseAccountStadiumResponse, error) {
	_rs := BaseAccountStadiumResponse{}
	//
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return _rs, _err
	}
	//
	_slice := []string{
		"count",
		"list",
		"page",
		"size",
	}
	//
	_sliceData, _ok := _data.(map[string]interface{})
	if !_ok {
		return _rs, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	//
	for _, _val := range _slice {
		if _, _ok := _sliceData[_val]; !_ok {
			return _rs, errors.New(that.apiAddress + "请求响应参数不正确2")
		}
	}
	//
	_str, _err := json.Marshal(_sliceData)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	return _rs, nil
}
