package eta_trial

import (
	"encoding/json"
	"eta/eta_mobile/models"
	"eta/eta_mobile/models/eta_trial"
	"eta/eta_mobile/utils"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

type EtaTrialQuestionnaireListResp struct {
	Code   int                                 `json:"code" description:"状态码"`
	Msg    string                              `json:"msg" description:"提示信息"`
	Data   eta_trial.EtaTrialQuestionnaireResp `json:"data" description:"返回数据"`
	ErrMsg string                              `json:"-" description:"错误信息，不用返回给前端，只是做日志记录"`
}

// GetEtaTrialQuestionnaireList CRM_ETA服务-获取问卷列表
func GetEtaTrialQuestionnaireList() (res eta_trial.EtaTrialQuestionnaireResp, err error) {
	url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta_trial/questionnaire/list")

	body := ioutil.NopCloser(strings.NewReader(""))
	client := &http.Client{}
	req, e := http.NewRequest("POST", url, body)
	if e != nil {
		err = fmt.Errorf("http create request err: %s", e.Error())
		return
	}

	contentType := "application/json;charset=utf-8"
	req.Header.Set("Content-Type", contentType)
	req.Header.Set("Authorization", utils.CrmEtaAuthorization)
	resp, e := client.Do(req)
	if e != nil {
		err = fmt.Errorf("http client do err: %s", e.Error())
		return
	}
	defer func() {
		_ = resp.Body.Close()
	}()
	b, e := ioutil.ReadAll(resp.Body)
	if e != nil {
		err = fmt.Errorf("resp body read err: %s", e.Error())
		return
	}
	if len(b) == 0 {
		err = fmt.Errorf("resp body is empty")
		return
	}
	// 生产环境解密, 注意有个坑前后的双引号
	if utils.RunMode == "release" {
		str := string(b)
		str = strings.Trim(str, `"`)
		b = utils.DesBase64Decrypt([]byte(str), utils.CrmEtaServerDes3Key)
	}

	result := new(EtaTrialQuestionnaireListResp)
	if e = json.Unmarshal(b, &result); e != nil {
		err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
		return
	}
	if result.Code != 200 {
		err = fmt.Errorf("result: %s", string(b))
		return
	}
	res = result.Data
	return
}

type EtaTrialQuestionnaireCommitReq struct {
	List []eta_trial.EtaTrialQuestionnaireRecord
}

// EtaTrialQuestionnaireCommit CRM_ETA服务-提交问卷调查
func EtaTrialQuestionnaireCommit(pars EtaTrialQuestionnaireCommitReq) (res bool, err error) {
	url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta_trial/questionnaire/commit")
	params, e := json.Marshal(pars)
	if e != nil {
		err = fmt.Errorf("data json marshal err: %s", e.Error())
		return
	}

	body := ioutil.NopCloser(strings.NewReader(string(params)))
	client := &http.Client{}
	req, e := http.NewRequest("POST", url, body)
	if e != nil {
		err = fmt.Errorf("http create request err: %s", e.Error())
		return
	}

	contentType := "application/json;charset=utf-8"
	req.Header.Set("Content-Type", contentType)
	req.Header.Set("Authorization", utils.CrmEtaAuthorization)
	resp, e := client.Do(req)
	if e != nil {
		err = fmt.Errorf("http client do err: %s", e.Error())
		return
	}
	defer func() {
		_ = resp.Body.Close()
	}()
	b, e := ioutil.ReadAll(resp.Body)
	if e != nil {
		err = fmt.Errorf("resp body read err: %s", e.Error())
		return
	}
	if len(b) == 0 {
		err = fmt.Errorf("resp body is empty")
		return
	}
	// 生产环境解密, 注意有个坑前后的双引号
	if utils.RunMode == "release" {
		str := string(b)
		str = strings.Trim(str, `"`)
		b = utils.DesBase64Decrypt([]byte(str), utils.CrmEtaServerDes3Key)
	}

	result := new(models.ResultData)
	if e = json.Unmarshal(b, &result); e != nil {
		err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
		return
	}
	if result.Code != 200 {
		err = fmt.Errorf("result: %s", string(b))
		return
	}
	res = true
	return
}
