import { orderType } from './../../constant/index'
import { reactive } from 'vue'
import { state } from '@/state'
import request from '@/helpers/request'
import { Dialog } from 'vant'
type orderType =
  | 'VIDEO'
  | 'LIVE'
  | 'PRACTICE'
  | 'GOODS'
  | 'VIP'
  | 'MUSIC'
  | 'PINAO_ROOM'
  | 'ACTI_REGIST'
  | ''

const original = () => {
  return {
    orderType: '' as orderType, // 购买类型
    orderInfo: {
      // 订单信息
      orderNo: '',
      actualPrice: 0,
      payStatus: true
    },
    orderObject: {
      // 订单对象
      orderNo: '',
      actualPrice: 0,
      orderName: '',
      orderDesc: '',
      orderType: '' as orderType,
      recomUserId: null as any, // 推荐人编号
      orderList: [] as Array<any>, // 商品信息
      activityId: '' as any, // 活动编号
      couponId: '' as string, // 优惠券编号
      discountPrice: 0 as number // 优惠
    }
    // orderObject: {
    //   orderNo: '',
    //   actualPrice: 28,
    //   orderName: '小酷Ai月度会员',
    //   orderDesc: '小酷Ai月度会员',
    //   orderType: 'VIP',
    //   recomUserId: 0,
    //   activityId: null,
    //   orderList: [
    //     {
    //       orderType: 'VIP',
    //       goodsName: '小酷Ai月度会员',
    //       id: 2,
    //       title: '月度会员',
    //       price: 28,
    //       startTime: '2023-02-28',
    //       endTime: '2023-03-28'
    //     }
    //   ]
    // }
  }
}

export const orderStatus = reactive(original())

// 重置对象
export const resestState = () => {
  Object.assign(orderStatus, original())
}

export const orderInfos = () => {
  // 商品列表
  const orderList = orderStatus.orderObject.orderList || []
  return orderList.map((item: any) => {
    const params = {
      goodType: item.orderType,
      goodName: item.goodsName,
      recomUserId: item.recomUserId, // 推荐人id
      bizContent: {}
    }
    if (item.orderType === 'VIDEO') {
      params.bizContent = {
        videoLessonGroupId: item.courseGroupId,
        payMoney: item.coursePrice || 0
      }
    } else if (item.orderType === 'LIVE') {
      params.bizContent = {
        groupId: item.courseGroupId
      }
    } else if (item.orderType === 'PRACTICE') {
      const tempTime = item.classTime || []
      const classCourse: any = []
      tempTime.forEach((time: any) => {
        classCourse.push({
          classDate: time.classDate,
          startTime: time.startTime,
          endTime: time.endTime
        })
      })
      params.bizContent = {
        courseGroupName: item.courseGroupName,
        courseIntroduce: item.courseIntroduce,
        subjectId: item.subjectId,
        singleCourseMinutes: item.singleCourseMinutes,
        courseNum: item.courseNum,
        coursePrice: item.coursePrice,
        teacherId: item.teacherId,
        classTime: classCourse
      }
    } else if (item.orderType === 'VIP') {
      params.bizContent = item.id
    } else if (item.orderType === 'MUSIC') {
      params.bizContent = {
        musicSheetId: item.id,
        actualPrice: item.actualPrice || 0,
        clientType: state.platformType
      }
    } else if (item.orderType === 'PINAO_ROOM') {
      params.bizContent = item.id
    } else if (item.orderType === 'ACTI_REGIST') {
      params.bizContent = {
        activityId: item.activityId
      }
    }
    return params
  })
}

/**
 * @title 0元购买
 * @param {function} callBack 回调函数
 * @returns {Promise<void>}
 */
export const onSubmitZero = async (callBack?: Function): Promise<void> => {
  // 正常支付
  try {
    const orderObject = orderStatus.orderObject
    const url =
      state.platformType === 'TEACHER'
        ? '/api-teacher/userOrder/executeOrder'
        : '/api-student/userOrder/executeOrder'
    const res = await request.post(url, {
      data: {
        orderName: orderObject.orderName,
        orderDesc: orderObject.orderDesc,
        orderType: orderObject.orderType,
        actualPrice: orderObject.actualPrice || 0,
        recomUserId: orderObject.recomUserId,
        activityId: orderObject.activityId,
        orderInfos: [...orderInfos()]
      }
    })
    const result = res.data || {}
    // 支付成功
    if (result.status == 'PAID') {
      if (callBack) {
        callBack()
      } else {
        Dialog.alert({
          message: '领取成功',
          confirmButtonText: '确定',
          confirmButtonColor: '#01C1B5'
        })
      }
    } else {
      Dialog.alert({
        message: result.msg,
        confirmButtonText: '确定',
        confirmButtonColor: '#01C1B5'
      })
    }
  } catch {
    Dialog.alert({
      title: '提示',
      message: '支付失败，请稍后重试！',
      confirmButtonText: '确定',
      confirmButtonColor: '#01C1B5'
    })
  }
}
