import request from '@/utils/request'
import { parseTime } from '@/utils'
import { saveAs } from 'file-saver';


/**
 * 直播间列表
 * @returns 
 */
export const getBrandListAPI = () => {
  return request({
    url: '/bi/finance/zbjType/list'
  })
}

/**
 * 口味列表
 * @returns 
 */
export const getTasteListAPI = () => {
  return request({
    url: '/bi/finance/flavor/list'
  })
}

/**
 * 规格列表
 * @returns 
 */
export const getSpecListAPI = () => {
  return request({
    url: '/bi/finance/spec/list'
  })
}

/**
 * 获取财务列表
 * @param {*} data
 * @returns 
 */
export const getFinanceListAPI = (data) => {
  return request({
    url: '/bi/finance/cost/list',
    method: 'POST',
    data: {
      zbjQdType: data.zbjQdType,
      flavor: data.flavorErp,
      specName: data.specNameErp,
      zbjQdTypeAll: data.zbjQdTypeAll,
      flavorErpAll: data.flavorErpAll,
      specNameErpAll: data.specNameErpAll,
      series: data.seriesPrdMap.map(o => o[0]),
      goodsName: data.seriesPrdMap.map(o => o[1]),
      startDate: parseTime(data.date[0], `{y}-{m}-{d}`),
      endDate: parseTime(data.date[1], `{y}-{m}-{d}`),
      pageNum: data.pageNum,
      pageSize: data.pageSize
    }
  })
}

/**
 * 获取订单详情
 * @param {*} data 
 * @returns 
 */
export const getFinanceDetailAPI = (data) => {
  return request({
    url: '/bi/finance/cost/detail/list',
    method: 'POST',
    data: {
      zbjQdType: data.zbjQdType,
      flavor: data.flavorErp,
      specName: data.specNameErp,
      zbjQdTypeAll: data.zbjQdTypeAll,
      flavorErpAll: data.flavorErpAll,
      specNameErpAll: data.specNameErpAll,
      series: data.seriesPrdMap.map(o => o[0]),
      goodsName: data.seriesPrdMap.map(o => o[1]),
      startDate: parseTime(data.date[0], `{y}-{m}-{d}`),
      endDate: parseTime(data.date[1], `{y}-{m}-{d}`),
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      // 直播间展开行里的订单详情需要带分销商（没有值参数也不会传递）
      fenxiaoId: data.fenxiaoId
    }
  })
}

/**
 * 直播间子数据列表
 * @param {*} data 
 * @returns 
 */
export const getFinanceSubListAPI = (data) => {
  console.log(data)
  return request({
    url: '/bi/finance/cost/subList',
    method: 'POST',
    data: {
      subType: 0, // 直播间的
      zbjQdType: data.zbjQdType instanceof Array ? data.zbjQdType : data.zbjQdType ? [data.zbjQdType] : [],
      flavor: data.flavorErp instanceof Array ? data.flavorErp : data.flavorErp ? [data.flavorErp] : [],
      specName: data.specNameErp instanceof Array ? data.specNameErp : data.specNameErp ? [data.specNameErp] : [],
      series: data.seriesPrdMap.map(o => o[0]),
      goodsName: data.seriesPrdMap.map(o => o[1]),
      startDate: parseTime(data.date[0], `{y}-{m}-{d}`),
      endDate: parseTime(data.date[1], `{y}-{m}-{d}`),
      pageNum: data.pageNum,
      pageSize: data.pageSize
    }
  })

}

/**
 * 下载达人数据表格
 */
export const downloadFinanceListAPI = async (data) => {
  const jsonString = JSON.stringify({
    zbjQdType: data.zbjQdType,
    flavor: data.flavorErp,
    specName: data.specNameErp,
    zbjQdTypeAll: false,
    flavorErpAll: data.flavorErpAll,
    specNameErpAll: data.specNameErpAll,
    series: data.seriesPrdMap.map(o => o[0]),
    goodsName: data.seriesPrdMap.map(o => o[1]),
    startDate: parseTime(data.date[0], `{y}-{m}-{d}`),
    endDate: parseTime(data.date[1], `{y}-{m}-{d}`),
    subType: 0, // 代表直播间的展开数据
  });

  const res = await request({
    url: '/bi/finance/cost/export',
    method: 'POST',
    responseType: 'blob',
    data: jsonString
  })
  // 保存文件到本地
  saveAs(res, '财务分析-达人数据.xlsx');
  
}
