package lakala

import (
	"encoding/json"
	"fmt"
	"git.rosy.net.cn/baseapi/utils"
	"net/http"
	"testing"
	"time"
)

func TestApplyElectronicContract(t *testing.T) {
	//api.ApplyElectronicContract(&ApplyContractParam{
	//	OrderNo:             "9839312025071609240676528932",
	//	OrgId:               983931,
	//	EcTypeCode:          "EC008",
	//	CertType:            "RESIDENT_ID",
	//	CertName:            "石锋",
	//	CertNo:              "610126198012230014",
	//	Mobile:              "18048531223",
	//	BusinessLicenseNo:   "915101003431062533",
	//	BusinessLicenseName: "成都若溪科技有限公司",
	//	OpenningBankCode:    "305651000261",
	//	OpenningBankName:    "中国民生银行股份有限公司成都金牛支行",
	//	AcctTypeCode:        "58",
	//	AcctNo:              "6226192004500005",
	//	AcctName:            "石锋",
	//	EcContentParameters: "{\"A1\":\"成都若溪科技有限公司\",\"A29\":\"0.2\",\"A30\":\"0.2\",\"A31\":\"0.2\",\"A32\":\"0.2\",\"A33\":\"0.2\",\"A34\":\"0.2\",\"A88\":\"0.2\",\"A96\":\"D1\",\"A97\":\"开通\",\"A100\":\"不开通\",\"A101\":\"中国境内\",\"A104\":2025,\"A105\":7,\"A106\":16,\"A107\":2025,\"A108\":7,\"A109\":16}",
	//})
	reqParameter := map[string]interface{}{
		"req_data": map[string]interface{}{
			"order_no":              "9839312025071616403365650684",
			"org_id":                983931,
			"ec_type_code":          "EC008",
			"cert_type":             "RESIDENT_ID",
			"cert_name":             "石锋",
			"cert_no":               "610126198012230014",
			"mobile":                "18048531223",
			"business_license_no":   "915101003431062533",
			"business_license_name": "成都若溪科技有限公司",

			"openning_bank_code":    "308651020187",
			"openning_bank_name":    "招商银行成都红牌楼支行",
			"acct_type_code":        "58",
			"acct_no":               "6214830283341583",
			"acct_name":             "招商银行",
			"ec_content_parameters": "1",
			"agent_tag":             0,
		},
		"version":  Version3,
		"req_time": utils.Time2TimeStrByFormat(time.Now(), TimeFormat),
	}
	result, err := api.AccessAPISign(OrderProdUrl, OrderEcApplyActive, http.MethodPost, "", reqParameter)
	if err != nil {
		fmt.Println(err)
		return
	}

	if result["code"].(string) != Success {
		fmt.Println(fmt.Errorf(result["msg"].(string)))
		return
	}

	bodyResult, err := json.Marshal(result["resp_data"].(map[string]interface{}))
	if err != nil {
		fmt.Println(err)
		return
	}

	resp := &ApplyContractResp{}
	if err = json.Unmarshal(bodyResult, resp); err != nil {
		fmt.Println(err)
		return
	}

	return
}

func TestCreateOrder(t *testing.T) {
	api.CreateOrder(&CreateOrderReq{
		OutOrderNo:           "KFPT20220714160009228907288",
		MerchantNo:           "8222900701106PZ",
		TotalAmount:          1,
		OrderEfficientTime:   "20220714170009",
		OrderInfo:            "",
		VposId:               "587305941625155584",
		ChannelId:            "2021052614391",
		NotifyUrl:            "http://run.mocky.io/v3/b02c9448-20a2-4ff6-a678-38ecab30161d",
		SupportCancel:        0,
		SupportRefund:        1,
		SupportRepeatPay:     1,
		OutUserId:            "",
		CallbackUrl:          "",
		TermNo:               "",
		SplitMark:            "",
		SettleType:           "",
		OutSplitInfo:         nil,
		CounterParam:         "",
		CounterRemark:        "",
		BusiTypeParam:        "",
		SgnInfo:              nil,
		ProductId:            "",
		GoodsMark:            "",
		GoodsField:           "",
		OrderSceneField:      nil,
		AgeLimit:             "",
		RepeatPayAutoRefund:  "",
		RepeatPayNotify:      "",
		CloseOrderAutoRefund: "",
		ShopName:             "",
		InteRouting:          "",
		DiscountCode:         "",
	})
}

func TestCloseOrder(t *testing.T) {
	api.CloseOrder(&CloseOrderReq{
		MerchantNo: "8222900701105HK",
		OutOrderNo: "KFPT2022071415575938951152",
		PayOrderNo: "",
		ChannelId:  "",
	})
}

func TestQueryOrder(t *testing.T) {
	api.QueryOrder("88491738418134", "822651059990E2S")
}

func TestRefundOrderQuery(t *testing.T) {
	api.RefundOrderQuery(&RefundOrderQueryReq{
		MerchantNo:       "8222900701105HK",
		TermNo:           "A0073841",
		OutTradeNo:       "54F73F36356D4E4E83A86FD8658BEB51",
		OriginTradeDate:  "20210930",
		OriginBizType:    "3",
		OriginTradeNo:    "2021093066210003980394",
		OriginTradeRefNo: "",
		OriginOutTradeNo: "",
	})
}

func TestRefundOrder(t *testing.T) {
	param := &RefundOrderReq{
		MerchantNo:      "822651059990E2S",
		TermNo:          "M3595962",
		OutTradeNo:      "88492359533733-1",
		RefundAmount:    "100",
		OriginBizType:   "3",
		OriginTradeDate: "20250808",
		OriginLogNo:     "66222344989495",
		OriginTradeNo:   "",
		OriginCardNo:    "",
		LocationInfo:    nil,
		RefundType:      "",
	}
	api.RefundOrder(param)
}

func TestQueryElectronicContract(t *testing.T) {
	api.QueryElectronicContract(GetOrderNumber(8), 983931, "1013456771342118912")
}

func TestQueryElectronic(t *testing.T) {
	resp, err := api.QueryElectronic("202508181038577028422164", 983931, "1009773397658267648")
	fmt.Println(resp)
	fmt.Println(err)
}
