package submail

import (
	"FollowUp_Notice/conf"
	"FollowUp_Notice/lib"
	"FollowUp_Notice/logs"
	"encoding/json"
	"github.com/go-resty/resty/v2"
)

type VoiceXSendRes struct {
	Status  string  `json:"status"`
	Send_id string  `json:"send_id"`
	Fee     float64 `json:"fee"`
	Msg     string  `json:"msg"`
	Code    string  `json:"code"`
}

// 语音模板发送
func VoiceXSend(to, hospital, name, date string) (VoiceXSendRes, error) {

	type Vars struct {
		Hospital string `json:"hospital"`
		Name     string `json:"name"`
		Date     string `json:"date"`
	}

	vars := Vars{Hospital: hospital, Name: name, Date: date}

	b, _ := json.Marshal(vars)

	client := resty.New()
	resp, err := client.R().
		SetHeader("Content-Type", "application/x-www-form-urlencoded").
		SetFormData(map[string]string{
			"appid":     conf.SUBMAIL_Voice_Appid,
			"signature": conf.SUBMAIL_Voice_Signature,
			"to":        to,
			"project":   conf.Voice_Project,
			"vars":      string(b),
		}).
		SetResult(&VoiceXSendRes{}).
		Post("https://api-v4.mysubmail.com/voice/xsend.json")

	if err != nil {
		return VoiceXSendRes{}, err
	}

	temp := VoiceXSendRes{}
	if err = json.Unmarshal(resp.Body(), &temp); err != nil {
		logs.Error(lib.FuncName(), err)
		return VoiceXSendRes{}, err
	}
	return temp, nil
}
