import request, { tenantRequest } from '@/utils/request'
import {
  send,
  messageSend,
  confirmAndSend,
  del,
  resolve,
  reject,
  addScope,
  addStatusScope,
  addOrg
} from './base'

// 发布
export function publish (deviceIds, target, options) {
  return messageSend({
    url: '/orchestration/calendarReleaseScheduling',
    method: 'POST',
    data: addOrg({
      deviceIds,
      target: JSON.stringify(target),
      ...options
    })
  }, '发布', tenantRequest)
}

export function getPublishes (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return tenantRequest({
    url: '/orchestration/calendarReleaseSchedu/page',
    method: 'GET',
    params: addStatusScope({
      pageIndex, pageSize,
      ...params
    })
  })
}

export function resolvePublish ({ id, name }) {
  return resolve({
    url: `/orchestration/calendarReleaseSchedu/${id}/approval`,
    method: 'POST',
    data: { remark: '' }
  }, name)
}

export function rejectPublish ({ id, name }, remark) {
  return reject({
    url: `/orchestration/calendarReleaseSchedu/${id}/reject`,
    method: 'POST',
    data: { remark }
  }, name)
}

export function getPublishHistory (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return tenantRequest({
    url: '/orchestration/calendarReleaseHis/page',
    method: 'GET',
    params: addScope({
      pageIndex, pageSize,
      ...params
    })
  })
}

// 日志
export function getLogs (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return tenantRequest({
    url: '/sysLog/list',
    method: 'GET',
    params: addScope({
      pageIndex, pageSize,
      ...params
    })
  })
}

export function getLog (id) {
  return send({
    url: `/sysLog/${id}`,
    method: 'GET'
  })
}

export function getBusiness () {
  return request({
    url: '/sysLog/business',
    method: 'GET'
  })
}

// 升级
export function getApks (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return request({
    url: '/apkUpgradeFile/queryListPage',
    method: 'GET',
    params: {
      pageIndex, pageSize,
      ...params
    }
  })
}

export function addApk (data, onUploadProgress) {
  return request({
    url: '/apkUpgradeFile/add',
    method: 'POST',
    timeout: 0,
    data, onUploadProgress
  })
}

export function enableApk ({ id, name }) {
  return confirmAndSend('启用', name, {
    url: `/apkUpgradeFile/enable/${id}`,
    method: 'GET'
  })
}

export function disableApk ({ id, name }) {
  return confirmAndSend('禁用', name, {
    url: `/apkUpgradeFile/disable/${id}`,
    method: 'GET'
  })
}

export function delApk ({ id, name }) {
  return del({
    url: '/apkUpgradeFile/delBatchByIds',
    method: 'POST',
    data: [id]
  }, name)
}

export function getVersions (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return request({
    url: '/apkUpgradePolicy/queryListPage',
    method: 'GET',
    params: {
      pageIndex, pageSize,
      ...params
    }
  })
}

export function deployVersion (version) {
  return confirmAndSend('发布', version.name, {
    url: '/apkUpgradePolicy/add',
    method: 'POST',
    data: version
  })
}

export function delVersion ({ id, deviceName, versionName, status }) {
  return confirmAndSend('删除', `对${deviceName}的${versionName}升级${status === 1 ? '' : '记录'}`, {
    url: '/apkUpgradePolicy/delBatchByIds',
    method: 'POST',
    data: [id]
  })
}
