package mtwmapi

import (
	"fmt"
	"git.rosy.net.cn/baseapi/utils"
)

const (
	ApplyCompensationStatusOne = 110 // 第一次申请赔付
	ApplyCompensationStatusTwo = 120 // 第二次申请赔付
)

type DistributeOrderDetail struct {
	WmOrderID              int         `json:"wm_order_id"`
	WmPoiID                int         `json:"wm_poi_id"`
	LogisticsStatus        int         `json:"logistics_status"`
	LogisticsCode          string      `json:"logistics_code"`
	LogisticsID            int         `json:"logistics_id"`
	LogisticsName          string      `json:"logistics_name"`
	PoiShippingFee         float64     `json:"poi_shipping_fee"`
	TipAmount              float64     `json:"tip_amount"`
	SelfDispatch           int         `json:"self_dispatch"`
	DispatchCancelAllow    int         `json:"dispatch_cancel_allow"`
	DispatcherName         string      `json:"dispatcher_name"`
	DispatcherPhone        interface{} `json:"dispatcher_phone"`
	OrgLeaderPhone         interface{} `json:"org_leader_phone"`
	OrgEmergencyPhone      interface{} `json:"org_emergency_phone"`
	LatestDeliveryTime     int         `json:"latest_delivery_time"`
	WaitingTime            int         `json:"waiting_time"`
	IsThirdPartShipping    int         `json:"is_third_part_shipping"`
	ThirdPartShippingName  interface{} `json:"third_part_shipping_name"`
	ThirdPartShippingPhone interface{} `json:"third_part_shipping_phone"`
	IsKS                   int         `json:"isKS"`
	DispatchTeam           interface{} `json:"dispatchTeam"`
	HhKS                   bool        `json:"hhKS"`
	PreCheckJuHeMsg        interface{} `json:"preCheckJuHeMsg"`
	HasJuheAndZB           bool        `json:"hasJuheAndZB"`
	PtGray                 int         `json:"ptGray"`
	ShippingFeeDetailVo    interface{} `json:"shippingFeeDetailVo"`
	AssignTime             int         `json:"assignTime"`
	AssignDegrade          bool        `json:"assignDegrade"`
	CurrentTime            int         `json:"currentTime"`
	SubLogisticsCode       int         `json:"subLogisticsCode"`
	DelayPushSecond        int         `json:"delay_push_second"`
}

type DeliveryData struct {
	Code           int           `json:"code"`
	Title          string        `json:"title"`
	ReasonList     []*ReasonList `json:"reasonList"`
	Msg            string        `json:"msg"`
	DeliveryStatus int           `json:"deliveryStatus"`
}

type ReasonList struct {
	Code          string `json:"code"`
	Content       string `json:"content"`
	PreCancelCode int    `json:"preCancelCode"`
	PreCancelMsg  string `json:"preCancelMsg"`
}

type ResultMsg struct {
	Data  string `json:"data"`
	Error *struct {
		Code int    `json:"code"`
		Msg  string `json:"msg"`
	}
}

// ApplyCompensationRes 申请赔付
type ApplyCompensationRes struct {
	OrderId     int64   `json:"order_id"`     // 订单id
	Reason      string  `json:"reason"`       // 原因
	ApplyStatus int     `json:"apply_status"` // 申请状态类型：110-第一次申请；120-第二次申诉。
	Amount      float64 `json:"amount"`       // 申请赔付金额，单位是元
	//FoodData    []FoodDataList `json:"food_data"`    // 惨损商品列表
}
type FoodDataList struct {
	AppSpuCode string `json:"app_spu_code"` // 即商家中台系统里商品的编码
	SkuId      string `json:"sku_id"`       // SKU码(商家的规格编码)
}

// 取消跑腿配送请求参数
type CancelOrderParam struct {
	ReasonCode    string `json:"reason_code"`    // 取消原因code 请开发者一定要先调用
	DetailContent string `json:"detail_content"` // 取消原因
	AppPoiCode    string `json:"app_poi_code"`   // 门店号
	OrderId       string `json:"order_id"`       // 订单id
}

//美团骑手修改小费，单位为元，最多小数点后一位
//order/receive/processed/w/distribute/tipAmount/v2?ignoreSetRouterProxy=true
func (a *API) OrderModityTips(orderID, poiCode string, tipAmount float64) (err error) {
	params := map[string]interface{}{
		"wmOrderViewId": orderID,
		"wmPoiId":       poiCode,
		"tipAmount":     tipAmount,
	}
	_, err = a.AccessUserPage2("v2/order/receive/processed/w/distribute/tipAmount/v2", params, true)
	return err
}

//获取美团订单小费
//https://shangoue.meituan.com/v2/order/receive/processed/r/distribute/list/v2
func (a *API) GetDistributeOrderDetail(orderID, poiCode string) (distributeOrderDetail *DistributeOrderDetail, err error) {
	params1 := map[string]interface{}{
		"wmOrderViewId": orderID,
		"wmPoiId":       poiCode,
	}
	params := map[string]interface{}{
		"orderInfos": []string{string(utils.MustMarshal(params1))},
	}
	result, err2 := a.AccessUserPage2("v2/order/receive/processed/r/distribute/list/v2", params, false)
	if err2 != nil {
		return distributeOrderDetail, err
	}
	err = utils.Map2StructByJson(result[orderID], &distributeOrderDetail, true)
	return distributeOrderDetail, err
}

type RecommendSkuGet struct {
	WmPoiId            int64  `json:"wmPoiId"`  // 平台门店id
	PageNum            int64  `json:"pageNum"`  // 页数
	PageSize           int64  `json:"pageSize"` // 页码
	NeedTag            int64  `json:"needTag"`
	Name               string `json:"name"`
	BrandId            int64  `json:"brandId"`
	TagId              int64  `json:"tagId"`
	SearchWord         string `json:"searchWord"`
	State              int64  `json:"state"`
	LabelIds           int64  `json:"labelIds"`
	SaleStatus         int64  `json:"saleStatus"`
	LimitSale          int64  `json:"limitSale"`
	NeedCombinationSpu int64  `json:"needCombinationSpu"`
	NoStockAutoClear   int64  `json:"noStockAutoClear"`
	MedicareType       int64  `json:"medicareType"`
}

// GetRecommendSku 获取美团力荐商品
func (a *API) GetRecommendSku(params *RecommendSkuGet, cookie string) {
	result, err2 := a.AccessUserPage2("reuse/sc/product/retail/r/searchListPage", utils.Struct2MapByJson(params), true)
	fmt.Println(result)
	fmt.Println(err2)
}

// EcommerceOrder 获取门店当前的订单号
type EcommerceOrder struct {
	ResultCode int `json:"result_code"`
	SuccessMap struct {
		Vernier string `json:"vernier"`
		HasMore int    `json:"has_more"`
	} `json:"success_map"`
	SuccessList []struct {
		OrderId int64 `json:"order_id"`
		Status  int   `json:"status"`
	} `json:"success_list"`
	ErrorList []struct {
		Msg  string `json:"msg"`
		Code int    `json:"code"`
	} `json:"error_list"`
}
