import request, { tenantRequest } from '@/utils/request'
import {
  messageSend,
  confirmAndSend,
  addTenant
} from './base'

export function addBroadcastTemplate (data) {
  return request({
    url: `/orchestration/broadcastTemplate`,
    method: 'POST',
    data
  })
}

export function updateBroadcastTemplate (data) {
  return request({
    url: `/orchestration/broadcastTemplate`,
    method: 'PUT',
    data
  })
}

export function delBroadcastTemplate (id) {
  return request({
    url: `/orchestration/broadcastTemplate/${id}`,
    method: 'DELETE'
  })
}

export function getBroadcastTemplate (id) {
  return request({
    url: `/orchestration/broadcastTemplate/${id}`,
    method: 'GET'
  })
}

export function getBroadcastTemplates (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return request({
    url: `/orchestration/broadcastTemplate/pageQuery`,
    method: 'GET',
    params: {
      pageIndex,
      pageSize,
      ...params
    }
  })
}

export function publishBroadcast (data) {
  return messageSend({
    url: `/orchestration/broadcast`,
    method: 'POST',
    data: addTenant({
      ...data
    })
  }, '发布', tenantRequest)
}

export function getBroadcasts (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return tenantRequest({
    url: `/orchestration/broadcast/pageQuery`,
    method: 'GET',
    params: addTenant({
      pageIndex,
      pageSize,
      ...params
    })
  })
}

export function disableBroadcast (id) {
  return confirmAndSend('下架', '', {
    url: `/orchestration/broadcast/disable/${id}`,
    method: 'PUT'
  })
}

export function disableDeviceRelease (data) {
  return confirmAndSend('下架', '', {
    url: `/orchestration/broadcast/deviceRelease/disable`,
    method: 'PUT',
    data
  })
}

export function getDeviceReleases ({ releaseId }) {
  return request({
    url: `/orchestration/broadcast/${releaseId}/deviceRelease`,
    method: 'GET'

  })
}
