package pposRequest

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

//NewMemberGetInfoRequest 会员详细信息获取
func (that *PPOSClient) NewMemberGetInfoRequest() *MemberGetInfoRequest {
	return &MemberGetInfoRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/member/member/getInfo",
			publicParamsSlice: []string{
				"member_card_ids",
			},
		},
	}
}

//
type MemberGetInfoRequest struct {
	Request
	publicParamsSlice []string //必传参数名
}

func (that *MemberGetInfoRequest) SetKeyword(value string) *MemberGetInfoRequest {
	that.initParams()
	that.params.Set("keyword", value)
	return that
}
func (that *MemberGetInfoRequest) SetMemberIds(value string) *MemberGetInfoRequest {
	that.initParams()
	that.params.Set("member_ids", value)
	return that
}
func (that *MemberGetInfoRequest) SetWithCard(value string) *MemberGetInfoRequest {
	that.initParams()
	that.params.Set("with_card", value) //1携带扩展卡信息，2不携带
	return that
}

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

type MemberGetInfoResponse struct {
	MemberId int64             `json:"member_id"`
	Sex      int64             `json:"sex"`
	Name     string            `json:"name"`
	Phone    string            `json:"phone"`
	Avatar   string            `json:"avatar"`
	Birthday string            `json:"birthday"`
	Address  string            `json:"address"`
	Remark   string            `json:"remark"`
	CardList []*MemberCardList `json:"card_list"`
}
type MemberCardList struct {
	MemberCardId      int64       `json:"member_card_id"`
	Status            int64       `json:"status"`
	CardName          string      `json:"card_name"`
	CardNum           string      `json:"card_num"`
	PCardName         string      `json:"p_card_name"`
	ValidityStartDate string      `json:"validity_start_date"`
	ValidityEndDate   string      `json:"validity_end_date"`
	Worth             interface{} `json:"worth"`
}

//Do ...
func (that *MemberGetInfoRequest) Do() ([]*MemberGetInfoResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_str, _err := json.Marshal(_data)
	if _err != nil {
		return nil, errors.New(that.apiAddress + _err.Error())
	}
	_rs := make([]*MemberGetInfoResponse, 0)
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return nil, errors.New(that.apiAddress + _err.Error())
	}
	return _rs, nil
}
