import { memberType } from '@/constant'
import request from '@/helpers/request'
import { orderStatus } from '@/views/order-detail/orderStatus'
import dayjs from 'dayjs'
// LIVE: '直播课',
// PRACTICE: '陪练课',
// VIDEO: '视频课',
// VIP: '开通会员',
// MUSIC: '单曲点播'
interface IAmount {
  couponAmount: number
  discountPrice: number
}
export const formatOrderDetail = async (item: any, amount?: IAmount) => {
  const type = item.goodType
  let tempList: any = {}
  switch (type) {
    case 'PIANO_ROOM':
      {
        try {
          const res = await getPinaoDetail(item.bizId)
          console.log('res', res)
          tempList = {
            orderType: item.goodType,
            goodsName: item.goodName,
            ...res
          }
        } catch (e: any) {
          throw new Error(e.message)
        }
      }
      break
    case 'VIP':
      try {
        const res = await getVipDetail(item.id)
        tempList = {
          orderType: item.goodType,
          goodName: item.goodName,
          id: item.id,
          title: memberType[res.period] || '',
          // price: res.salePrice || item.actualPrice,
          // 判断是否有优惠金额
          price: amount?.couponAmount
            ? Number(
              (
                res.salePrice -
                amount.couponAmount +
                amount.discountPrice
              ).toFixed(2)
            )
            : res.salePrice || item.actualPrice,
          startTime: dayjs(res.startTime).format('YYYY-MM-DD'),
          endTime: dayjs(res.endTime).format('YYYY-MM-DD')
        }
      } catch (e: any) {
        throw new Error(e.message)
      }
      break
    case 'MUSIC':
      {
        try {
          const res = await getMusicDetail(item.bizId)
          tempList = {
            orderType: item.goodType,
            goodName: item.goodName,
            ...res
          }
        } catch (e: any) {
          throw new Error(e.message)
        }
      }
      break
    case 'ALBUM':
      {
        try {
          const res = await getAlbumDetail(item.bizId)
          tempList = {
            orderType: item.goodType,
            goodName: item.goodName,
            ...res
          }
        } catch (e: any) {
          throw new Error(e.message)
        }
      }
      break
  }
  tempList.orderType = type
  tempList.goodName = item.goodName
  orderStatus.orderObject.orderList.push(tempList)
}

// 获取时长详情失败
export const getPinaoDetail = async (id: any) => {
  try {
    const res = await request.get(`/api-teacher/pianoRoomSettings/detail/${id}`)
    return res.data
  } catch {
    throw new Error('获取时长详情失败')
  }
}

// 获取会员详情
export const getVipDetail = async (id: any) => {
  try {
    const setting = await request.get('/api-teacher/vipCardRecord/detail/' + id)
    return setting.data || []
  } catch {
    throw new Error('获取会员详情失败')
  }
}

// 获取曲目详情
export const getMusicDetail = async (id: any) => {
  try {
    const res = await request.get(`/api-teacher/music/sheet/detail/${id}`)
    return res.data
  } catch {
    throw new Error('获取曲目详情失败')
  }
}

// 获取专辑详情
export const getAlbumDetail = async (id: any) => {
  try {
    const res = await request.post(`/api-teacher/music/album/detail`, {
      data: { id }
    })
    return res.data
  } catch {
    throw new Error('获取专辑详情失败')
  }
}

export const tradeOrder = (result: any, callBack?: any) => {
  const {
    orderNo,
    actualPrice,
    orderDesc,
    orderName,
    orderType,
    orderDetailList,
    couponAmount,
    discountPrice
  } = result
  orderStatus.orderObject.orderType = orderType
  orderStatus.orderObject.orderName = orderName
  orderStatus.orderObject.orderDesc = orderDesc
  orderStatus.orderObject.orderNo = orderNo
  orderStatus.orderObject.actualPrice = actualPrice
  orderStatus.orderObject.discountPrice = discountPrice
  orderStatus.orderObject.orderList = []

  console.log(result)
  try {
    orderDetailList.forEach(async (item: any) => {
      await formatOrderDetail(item, {
        couponAmount,
        discountPrice
      })
    })
    callBack && callBack()
  } catch {
    //
  }
}
