import $http from '@/common/request.js'
import config from '@/config'
import store from '@/store/index.js'

function storeUser() {
  return {
    tag: '20247ac1b24e82760a733485969ef7ee',
    userId: '9a1ecfbacb6b4f249bf2dd3ec7793ead',
    phone: '17611228068',
    userName: '安小霞'
  }
}

function getProjectId() {
  return 'Pj1101080259'
}

function commonParams() {
  return `openid=${storeUser().userId}&pubname=${
    config.brsgServer.pubname
  }&projectId=${getProjectId()}&userName=${storeUser().userName}&userPhone=${
    storeUser().phone
  }&userId=${storeUser().userId}`
}

// 获取地暖
function querySapceFloorHeating(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    isNotShowErrorToast: true,
    url: `/duoduo-service/duoduoenv-service/ctrl/querySapceFloorHeating?${commonParams()}&spaceId=${params.spaceId}`,
    errorSave: true,
    method: 'get'
  })
}

//  设置设备
function setEquipeHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/object/eqp/infocode/set?${commonParams()}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify(params)
  })
}

// 获取设备
function getObjectDataEqpGroupHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/object/objectDataEqpGroup/query?${commonParams()}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify({ ...params })
  })
}

// 改变场景
function switchoverSceneHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/meeting/room/scene/switchover?${commonParams()}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify({ ...params, projectId: getProjectId() })
  })
}
// 获得空间当前会议室场景
function getMeetingSceneHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/meeting/room/scene/getMeetingScene/V2?${commonParams()}&spaceId=${
      params.spaceId
    }`,
    isNotShowErrorToast: true,
    errorSave: true,
    method: 'get'
  })
}
// 获得空间场景
function querySpaceSceneConfigHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/duoduo-service/setup-service/scene/querySpaceSceneConfig?${commonParams()}&spaceId=${
      params.spaceId
    }`,
    isNotShowErrorToast: true,
    errorSave: true,
    method: 'get'
  })
}

// 设备
function getEquipmentListHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/object/equipment/findForServe?${commonParams()}&spaceId=${
      params.spaceId
    }`,
    isNotShowErrorToast: true,
    errorSave: true,
    method: 'POST'
  })
}
// set 设备
function setEquipmentHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/object/equipment/setRunStatus?${commonParams()}`,
    isNotShowErrorToast: true,
    errorSave: true,
    method: 'POST',
    data: JSON.stringify({ ...params, projectId: getProjectId() })
  })
}

// 详情页 面积图
function getChartListHTTP(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    isNotShowErrorToast: true,
    errorSave: true,
    url: `/server/spaceAdjust/queryPropertyData?${commonParams()}&spaceId=${
      params.spaceId
    }&funcid=${params.funcid}`,
    method: 'get'
  })
}
// 办公空间
function getSpaceAdjustList(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    // serverSp: 'http://192.168.0.47:52009',
    url: `/server/space/property?${commonParams()}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify({
      ...params,
      criteria: { ...params.criteria, projectId: getProjectId() }
    })
  })
}

//  温度上下限
function getTempRangeHttp() {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/space/getTempRange?${commonParams()}`,
    isNotShowErrorToast: true,
    errorSave: true,
    method: 'get'
  })
}

// 天气预报
function getWeather() {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    isNotShowErrorToast: true,
    url: `/duoduo-service/object-service/weather/getCurrentWeather?${commonParams()}`,
    errorSave: true,
    method: 'get'
  })
}
//  空调 - 调节文案/空调信息
function getFeedbackDocumentsHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/custom/feedback/documents?${commonParams()}`,
    isNotShowErrorToast: true,
    errorSave: true,
    method: 'POST',
    data: JSON.stringify(params)
  })
}

//  空调 调节
function changeTempHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/custom/feedbackCreate/v2?${commonParams()}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify({ ...params, projectId: getProjectId() })
  })
}

//  手动控制 下发指令
function changeManualTempHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/ctrl/set?${commonParams()}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify(params)
  })
}

// 查询空间空调信息
function querySpaceConditioners(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    // serverSp: 'http://192.168.0.47:52009',
    url: `/server/ctrl/querySpaceConditioners?projectId=${params.projectId}&spaceId=${
      params.spaceId
    }`,
    isNotShowErrorToast: true,
    errorSave: true,
    method: 'GET'
  })
}

// // 空调 调节
// function changeTempHttp(params) {
//   return $http({
//     serverSp: config.brsgServer.duoduoUrl,
//     url: `/server/custom/feedbackCreate?pubname=${config.brsgServer.pubname}&openid=${config.brsgServer.openid}&userName=${storeUser().userName}&userPhone=${storeUser().phone}`,
//     method: 'POST',
//     data: JSON.stringify(params)
//   });
// }
/*       灯的接口     */

// 查询定制时间
function getSetTimeHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/customScene/queryCustom?${commonParams()}&spaceId=${
      params.spaceId
    }`,
    isNotShowErrorToast: true,
    errorSave: true,
    method: 'GET'
  })
}

// 更改 定制时间
function changeSetTimeHttp(params) {
  params = { ...params, projectId: getProjectId() }
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/customScene/create?${commonParams()}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify(params)
  })
}

// 灯的状态
function getLampHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/light/status/query?${commonParams()}&spaceId=${
      params.spaceId
    }`,
    isNotShowErrorToast: true,
    errorSave: true,
    method: 'GET'
  })
}

// 批量 开关灯
function setallLampHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/light/onoff/set?${commonParams()}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify(params)
  })
}

// 全部关闭 关灯 走廊的灯 晚关
function specialLightSetHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/light/onoff/specialSet?${commonParams()}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify(params)
  })
}
// 开关失败时 定时查询灯状态
function getStatusHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/light/onoff/setSatus?${commonParams()}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify(params)
  })
}
// 插座-列表
function getSocketHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/duoduo-service/duoduoenv-service/ctrl/queryEquipmentStatusInfo?${commonParams()}&spaceId=${
        params.spaceId
      }&equipType=socket`,
    isNotShowErrorToast: true,
    errorSave: true,
    method: 'GET'
  })
}
// 插座-控制
function setSocketHttp(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/duoduo-service/duoduoenv-service/ctrl/set?${commonParams()}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify(params)
  })
}
// 查询是否是过渡季
function getSeasonType(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/duoduo-service/custom-service/custom/getSeasonType?projectId=${params.projectId}&date=${params.date}`,
    isNotShowErrorToast: true,
    errorSave: true,
    method: 'GET'
  })
}
// 查询所有空间设备
function queryEquipStatistics(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/duoduo-service/object-service/object/equipment/queryEquipStatistics?projectId=${params.projectId}&floorId=${params.floorId}`,
    isNotShowErrorToast: true,
    errorSave: true,
    method: 'GET'
  })
}
//  批量空调 调节
function changeAllAirHttp(params) {
  /// duoduoenv.sagacloud.cn/server/custom/feedbackCreate/v2Batch
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/custom/feedbackCreate/v2Batch?${commonParams()}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify(params)
  })
}

// 批量取消和申请加班
function saveBatchCustomScene(params) {
  let paramsArr = JSON.parse(JSON.stringify(params.historyBookList))
  paramsArr.find((item) => {
    item.customSceneEqType = 'ALL'
    if (!item.sceneEquipList) item.sceneEquipList = []
  })
  if (params.type !== 'workOff' && params.type !== 'delete') {
    if (
      params.newBooklist &&
      params.newBooklist.startTime &&
      params.newBooklist.startTime !== ''
    ) {
      params.newBooklist.projectId = getProjectId()
      params.newBooklist.sourceType = config.brsgServer.pubname
      params.newBooklist.userId = storeUser().userId
      params.newBooklist.userPhone = storeUser().phone
      params.newBooklist.userName = storeUser().userName
    }
    if (paramsArr.length > 0) {
      paramsArr.find((item) => {
        item.startDate = item.startDate.replace(/-/g, '')
        item.endDate = item.startDate
      })
    }
    params.newBooklist && paramsArr.push(params.newBooklist)
  }
  if (params.type === 'delete') {
    paramsArr.find((item) => {
      item.startDate = item.startDate.replace(/-/g, '')
      item.endDate = item.startDate
      item.customSceneEqType = 'ALL'
      item.projectId = getProjectId()
      item.sourceType = config.brsgServer.pubname
      item.userId = storeUser().userId
      item.userPhone = storeUser().phone
      item.userName = storeUser().userName
    })
  }

  console.log(paramsArr, 'paramsArrparamsArr')
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/customScene/saveBatchCustomScene?${commonParams()}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify(paramsArr)
  })
}
// 查询加班记录
function queryCustomAndScence(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/server/customScene/queryCustomAndScence?${commonParams()}&spaceId=${
      params.spaceId
    }`,
    method: 'GET',
    isNotShowErrorToast: true,
    errorSave: true
  })
}

function changeDefaultSpace(params) {
  return $http({
    url: `/user/defaultSpace?spaceId=${params}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true
  })
}
// 设备运行配置
function queryEquipmentRunConfig(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/duoduo-service/custom-service/equipmentRun/queryEquipmentRunConfig?projectId=${getProjectId()}&spaceId=${params}`,
    method: 'GET',
    isNotShowErrorToast: true,
    errorSave: true
  })
}

// ---权限控制---
// 项目控制权限
function queryProjectConfig() {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/duoduo-service/duoduoenv-service/user/permissions/queryProjectConfig?projectId=${getProjectId()}`,
    method: 'GET',
    isNotShowErrorToast: true,
    errorSave: true
  })
}
// 空间控制权限
function queryUserControlInfo() {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/duoduo-service/duoduoenv-service/user/permissions/queryUserControlInfo?${commonParams()}`,
    method: 'GET',
    isNotShowErrorToast: true,
    errorSave: true
  })
}
// 会议室控制权限
function querySpaceMeetingInfo(params) {
  return $http({
    serverSp: config.brsgServer.duoduoUrl,
    url: `/duoduo-service/duoduoenv-service/meeting/querySpaceMeetingInfo?${commonParams()}`,
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify(params)
  })
}

export {
  querySapceFloorHeating,
  querySpaceMeetingInfo,
  queryUserControlInfo,
  queryProjectConfig,
  queryEquipmentRunConfig,
  setSocketHttp,
  getSocketHttp,
  queryCustomAndScence,
  saveBatchCustomScene,
  setEquipeHttp,
  getObjectDataEqpGroupHttp,
  getFeedbackDocumentsHttp,
  getTempRangeHttp,
  getStatusHttp,
  setallLampHttp,
  specialLightSetHttp,
  getLampHttp,
  changeSetTimeHttp,
  getSetTimeHttp,
  getSpaceAdjustList,
  getWeather,
  changeTempHttp,
  getChartListHTTP,
  getEquipmentListHttp,
  setEquipmentHttp,
  getMeetingSceneHttp,
  querySpaceSceneConfigHttp,
  switchoverSceneHttp,
  getSeasonType,
  queryEquipStatistics,
  changeAllAirHttp,
  querySpaceConditioners,
  changeManualTempHttp,
  changeDefaultSpace
}
