import axiosInstance from '@/utils/axios'

// 产品编码：4 一元购，5抽奖
export type ProductCode = '4' | '5'

/**
 * 获取登录地址
 */
export const getLoginUrl = async (): Promise<BackendResponse<string>> => {
  try {
    const response = await axiosInstance.get<BackendResponse<string>>('/api/login/get_login_url')
    return response.data
  } catch (error) {
    throw error
  }
}

interface LoginRequestData {
  code: string
  state: string
  amp: string
}

interface LoginResponseData {
  token: string
}

/**
 * 登录
 * @returns Promise<LoginResponseData>
 * @param requestData
 */
export const login = async (requestData: LoginRequestData): Promise<BackendResponse<LoginResponseData>> => {
  try {
    const response = await axiosInstance.post<BackendResponse<LoginResponseData>>('/api/login/authorize', requestData)
    return response.data
  } catch (error) {
    throw error
  }
}

interface QueryUserOrderResponse {
  orderId: string
  lotteryOrderId: string
}

interface QueryUserOrderRequestData {
  productCode: ProductCode
}

export const queryUserOrder = async (requestData: QueryUserOrderRequestData): Promise<BackendResponse<QueryUserOrderResponse>> => {
  try {
    const response = await axiosInstance.post<BackendResponse<QueryUserOrderResponse>>('/api/order/getUserOrder', requestData)
    return response.data
  } catch (error) {
    throw error
  }
}

export interface QueryBranchListResponseData {
  branchCode: string
  branchName: string
  showOrder: 1
}

/**
 * 获取网点列表
 */
export const queryBranchList = async (): Promise<BackendResponse<QueryBranchListResponseData[]>> => {
  try {
    const response = await axiosInstance.get<BackendResponse<QueryBranchListResponseData[]>>('/api/branch/query_list')
    return response.data
  } catch (error) {
    throw error
  }
}

export interface CreateOrderRequestData {
  branchName: string
  branchCode: string
  productCode: ProductCode
  number: number
}

/**
 * 创建订单
 * @param requestData
 */
export const createOrder = async (requestData: CreateOrderRequestData): Promise<BackendResponse<string>> => {
  try {
    const response = await axiosInstance.post<BackendResponse<string>>('/api/order/create_order', requestData)
    return response.data
  }catch (error) {
    throw error;
  }
}

interface GetPayUrlRequestData {
  orderId: string
}

/**
 * 查询支付地址
 * @param requestData
 */
export const queryPayUrl = async (requestData: GetPayUrlRequestData): Promise<BackendResponse<string>> => {
  try {
    const response = await axiosInstance.post<BackendResponse<string>>('/api/pay', requestData)
    return response.data
  } catch (error) {
    throw error;
  }
}

/**
 * 查询支付配置
 */
export const queryPayConfig = async <T>(): Promise<BackendResponse<T>> => {
  try {
    const response = await axiosInstance.get<BackendResponse<T>>('/api/pay/pay_config');
    return response.data;
  } catch (error) {
    throw error;
  }
}


interface QueryOrderInfoResponseOrderDetail {
  branchCode: string
  branchName: string
  mobile: string
  orderAmount: string
  payStatus: string
  payTime: string
}

interface QueryOrderInfoResponseProductDetailInfo {
  productCode: string
  productName: string
}

export interface QueryOrderInfoResponse {
  orderDetail: QueryOrderInfoResponseOrderDetail
  productDetailInfo: QueryOrderInfoResponseProductDetailInfo
}

/**
 * 查询订单详情
 * @param orderId
 */
export const queryOrderInfo = async (orderId: string): Promise<BackendResponse<QueryOrderInfoResponse>> => {
  try {
    const response = await axiosInstance.get<BackendResponse<QueryOrderInfoResponse>>(`/api/order/detail/${orderId}`)
    return response.data
  } catch (error) {
    throw error
  }
}


interface SubmitDrawRequestData {
  activityCode: string
  branchName: string;
  branchCode: string;
}
export interface SubmitDrawResponse {
  iconUrl: string;
  orderId: string;
  prizeCode: string;
  prizeName: string;
  prizeType: string;
  prizeValue: number;
  prizeValueType: string;
  receiveDeadlineTime: string;
  showOrder: number;
}
/**
 * 提交抽奖请求
 * @param requestData
 */
export const submitDraw = async (requestData: SubmitDrawRequestData): Promise<BackendResponse<SubmitDrawResponse>> => {
  try {
    const response = await axiosInstance.post<BackendResponse<SubmitDrawResponse>>('/api/activity/submitDraw', requestData)
    return response.data
  } catch (error) {
    throw error;
  }
}


interface QueryMyPrizeListRequestData {
  activityCode: string;
  pageNum: number;
  pageSize: number;
}

export interface QueryMyPrizeListResponse {
  orderId: string;
  // 状态 01：待领取 02：已到账 03：已过期 04：发送失败 05：领取中
  orderStatus: string;
  prizeCode: string;
  prizeName: string;
  prizeValue: number;
  // 奖品价值类型01：积分 02：立减金券
  prizeValueType: string;
  receiveDeadlineTime: string;
  useDeadlineTime: string;
}

/**
 * 查询我的中奖列表
 * @param requestData
 */
export const queryMyPrizeList = async (requestData: QueryMyPrizeListRequestData): Promise<BackendResponse<RecordsData<QueryMyPrizeListResponse>>> => {
  try {
    const response = await axiosInstance.post<BackendResponse<RecordsData<QueryMyPrizeListResponse>>>('/api/activity/queryMyPrizeList', requestData)
    return response.data
  } catch (error) {
    throw error;
  }
}

interface ReceivePrizeRequestData {
  orderId: string;
}

interface ReceivePrizeResponse {
  method: string;
  miniProgramType: string;
  orderId: string;
  path: string;
  username: string;
}

/**
 * 用户领取奖品接口
 * @param requestData
 */
export const receivePrize = async (requestData: ReceivePrizeRequestData): Promise<BackendResponse<ReceivePrizeResponse>> => {
  try {
    const response = await axiosInstance.post<BackendResponse<ReceivePrizeResponse>>('/api/activity/receivePrize', requestData)
    return response.data
  } catch (error) {
    throw error;
  }
}