import { request } from '/@/utils/service'
import { AddReq, DelReq, EditReq, InfoReq, UserPageQuery } from '@fast-crud/fast-crud'
import { storeToRefs } from 'pinia'
import { useShopInfo } from '/@/stores/shopInfo'
import { usePublicData } from '/@/stores/publicData'

const publicData = usePublicData()
const { dateRange } = storeToRefs(publicData)
const shopInfo = useShopInfo()
const { profile } = storeToRefs(shopInfo)

export const apiPrefix = '/api/ad_manage/portfolios/'
export function GetList(query: UserPageQuery) {
  const updatedQuery = { ...query, startDate: dateRange.value[0], endDate: dateRange.value[1], name: query.name }

  return request({
    url: apiPrefix,
    method: 'get',
    params: updatedQuery,
  })
}
export function GetAllPortfolios() {
  return request({
    url: apiPrefix + 'select_list',
    method: 'get',
    params: { limit: 999 },
  })
}
export function GetObj(id: InfoReq) {
  return request({
    url: apiPrefix + id,
    method: 'get',
  })
}

export function AddObj(obj: AddReq) {
  const updatedObj = { ...obj, profileId: profile.value.profile_id }
  return request({
    url: apiPrefix + 'create/',
    method: 'post',
    data: updatedObj,
  })
}

export function UpdateObj(obj: EditReq) {
  return request({
    url: apiPrefix + obj.id + '/',
    method: 'put',
    data: obj,
  })
}

export function DelObj(id: DelReq) {
  return request({
    url: apiPrefix + id + '/',
    method: 'delete',
    data: { id },
  })
}

export function getCardData(query: UserPageQuery) {
  const updatedQuery = { ...query, startDate: dateRange.value[0], endDate: dateRange.value[1], name: query.name }

  return request({
    url: apiPrefix + 'report/amount',
    method: 'GET',
    params: updatedQuery,
  })
}

export function getLineData(query: UserPageQuery) {
  const updatedQuery = { ...query, startDate: dateRange.value[0], endDate: dateRange.value[1], name: query.name }

  return request({
    url: apiPrefix + 'report/trend/daily',
    method: 'GET',
    params: updatedQuery,
  })
}

export function getLineWeekData(query: UserPageQuery) {
  const updatedQuery = { ...query, startDate: dateRange.value[0], endDate: dateRange.value[1], name: query.name }

  return request({
    url: apiPrefix + 'report/trend/weekly',
    method: 'GET',
    params: updatedQuery,
  })
}

export function getLineMonthData(query: UserPageQuery) {
  const updatedQuery = { ...query, startDate: dateRange.value[0], endDate: dateRange.value[1], name: query.name }

  return request({
    url: apiPrefix + 'report/trend/monthly',
    method: 'GET',
    params: updatedQuery,
  })
}
