import http from '@/utils/request'

// export function getOrder (params) {
//   let channel = params.channel
//   let payStr, orderStr
//   let { pageNum = 1, pageSize = 10, payStatus = [], orderStatus = [] } = params
//   if (payStatus.length > 0) {
//     payStr = payStatus.map(i => {
//       return `pay_status=${i}`
//     }).join('&')
//   }
//   if (orderStatus.length > 0) {
//     orderStr = orderStatus.map(i => {
//       return `status=${i}`
//     }).join('&')
//   }
//   return http.get(`order/?channel=${channel}&pageNum=${pageNum}&pageSize=${pageSize}&${payStr || ''}&${orderStr || ''}`)
// }
export function getOrder (params) {
  const entries = Object.entries(params).filter(arr => arr[1]).map(arr => {
    return `${arr[0]}=${arr[1]}`
  })
  const str = entries.join('&')
  return http.get(`order/?${str}`)
}

export function getOrderById (id) {
  return http.get(`order/${id}`)
}

export function cancelOrderById (id) {
  return http.get(`order/${id}/cancel`)
}

export function cancelskipota (id) {
  return http.get(`order/${id}/cancelskipota`)
}

export function queryLatestRefund (id) {
  return http.get(`jianhang/queryLatestRefund?orderId=${id}`)
}
export function refundForStatusUnKnown (id) {
  return http.post(`jianhang/doRefundForStatusUnKnown?orderId=${id}`, {})
}
export function getOrderStatisticsByType (params, group) {
  return http.post('orderstatistics', {
    // 'export': {
    //   'fileprefix': '售票统计',
    //   'type': '售票统计'
    // },
    fields: ['total_ticket_num', 'total_ticket_amount', 'checked_ticket_num', 'checked_ticket_amount', 'refunded_ticket_num', 'refunded_ticket_amount'],
    filter: params,
    groupby: group,
    orderby: [
      { left: 'ticket_type', right: 'DESC' }
    ]
  })
}

// 售票用户统计
export function getOrderStatisticsByAgency (params, group) {
  return http.post('orderstatistics/agencyUserStatistics', {
    fields: ['total_ticket_num', 'total_ticket_amount', 'checked_ticket_num', 'checked_ticket_amount', 'refunded_ticket_num', 'refunded_ticket_amount'],
    filter: params,
    groupby: group,
    orderby: [
      { left: 'ticket_type', right: 'DESC' }
    ]
  })
}

export function exportOrderStatisticsByType (params, group) {
  // 兑换票和普通票作区分
  // const fields = params.isConvertible ? ['sales_ticket_num', 'sales_ticket_amount', 'sales_ticket_commission'] : ['total_ticket_num', 'total_ticket_amount', 'checked_ticket_num', 'checked_ticket_amount', 'refunded_ticket_num', 'refunded_ticket_amount']
  const fields = ['total_ticket_num', 'total_ticket_amount', 'checked_ticket_num', 'checked_ticket_amount', 'refunded_ticket_num', 'refunded_ticket_amount']

  return http.post('orderstatistics', {
    export: {
      fileprefix: '售票统计',
      type: '售票统计'
    },
    fields: fields,
    filter: params,
    groupby: group,
    orderby: [
      { left: 'ticket_type', right: 'DESC' }
    ]
  })
}

// 获取兑换票统计数据
export function getSalesOrderStatisticsByType (params, group) {
  return http.post('orderstatistics', {
    fields: ['sales_ticket_num', 'sales_ticket_amount', 'sales_ticket_commission', 'wx_prorate_num', 'wx_prorate_amount'],
    filter: params,
    groupby: group,
    orderby: [
      { left: 'ticket_type', right: 'DESC' }
    ]
  })
}

export function exportSalesOrderStatisticsByType (params, group) {
  // 兑换票和普通票作区分
  const fields = ['sales_ticket_num', 'sales_ticket_amount', 'sales_ticket_commission', 'wx_prorate_num', 'wx_prorate_amount']

  return http.post('orderstatistics', {
    export: {
      fileprefix: '销售售票统计',
      type: '销售售票统计'
    },
    fields: fields,
    filter: params,
    groupby: group,
    orderby: [
      { left: 'ticket_type', right: 'DESC' }
    ]
  })
}

// 导出旅行社单统计
export function exportAgencyOrderStatistics (params, group) {
  // 售票数量 售票金额 退票张数 退票金额
  // const fields = ['guide_name', 'sum_count', 'sum_price', 'sum_cancel_count', 'sum_cancel_price']
  return http.post('/orderstatistics/agencyOrderStatistics', {
    export: {
      fileprefix: '旅行社单统计',
      type: '旅行社单统计'
    },
    ...params
    // fields: fields,
    // filter: params,
    // groupby: ['agency_name', 'guide_name'],
    // orderby: [
    //   { left: 'agency_name', right: 'DESC' }
    // ]
  })
}

// 批量取消订单
export function cancelOrder (params) {
  console.log(params)
  return http.post('/order/cancel', params)
}
// 部分退
export function cancelPart (params) {
  return http.post('/order/cancelpart', params)
}
// 批量审核
export function wxProrateAuth (params) {
  console.log(params)
  return http.post('/order/checkWxProrate', params)
}
// 批量取消订单任务列表
export function getOrderCancelTask (params) {
  const { pageNum = 1, pageSize = 10 } = params

  return http.get(`/ordercanceltask/?${(pageNum === 0 || pageNum) ? `pageNum=${pageNum}` : ''}&${(pageSize === 0 || pageSize) ? `pageSize=${pageSize}` : ''}`)
}

// 批量取消订单任务列表详情
export function getOrderCancelTaskById (id) {
  return http.get(`/ordercanceltask/${id}`)
}

// 改签
export function changeticket (id, tickets) {
  return http.post(`/order/${id}/changeticket`, {
    tickets
  })
}

// 获取支付方式
export function getPayWay () {
  return http.get(`/payway`)
}
// 归还库存
export function returnStock (params) {
  return http.post(`/order/returnStock`, params)
}

// 营销单审核(通过/拒绝)
export function checkConvertibleOrder (params) {
  return http.post(`/order/${params.id}/convertibleOrderAudit`, params)
}

// 微信分佣
export function wxProrate (id) {
  return http.post(`/order/${id}/wxProrate`)
}

// 获取微信分佣详情
export function getWxProrateDetail (id) {
  return http.get(`/order/${id}/wxProrateDetail`)
}

// 重试微信分佣
export function retryWxProrate (id) {
  return http.post(`/order/${id}/retryWxProrate`)
}

// 旅行社单统计
export function agencyOrderStatistics (params) {
  return http.post('/orderstatistics/agencyOrderStatistics', params)
}

// 修改门票状态
export function updateTicketStatus (params) {
  return http.post('/order/updateTicketStatus', params)
}
