import { axios } from '@/utils/request'
import { SERVICE_PLATFORM_POWER_BASE, UPCLOUD_RDC_TMS_MAINDATA_SERVICE,SERVICE_CTRL_TOWER } from '@/config/serviceSettings.js'

import qs from 'qs'

const baseUrl = SERVICE_PLATFORM_POWER_BASE
// 列表页
export const listRemoteUrl = {
  url: baseUrl + '/userpower/getEnterpriseUsers',
  method: 'get'
}

// 新增
export const add = data => {
  delete data.roles
  return axios.post(baseUrl + '/userpower/addEnterpriseUser', qs.stringify(data), {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  }).then(res => {
    if (res.success) {
      if (data.userCategory ==='S'&&data.companyId) {
        axios.post(UPCLOUD_RDC_TMS_MAINDATA_SERVICE + '/carrier/modify_sys_flag', { carrierId: data.companyId });
      }
    }

    return res
  })
}
// 获取详情
export const getDetail = params => {
  return axios.get(baseUrl + '/userpower/getEnterpriseUserByUserId', { params })
}
// 修改
export const update = data => {
  delete data.roles
  return axios.post(baseUrl + '/userpower/updateEnterpriseUser', qs.stringify(data), {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  }).then(res => {
    if (res.success) {
      if (data.userCategory ==='S'&&data.companyId) {
        axios.post(UPCLOUD_RDC_TMS_MAINDATA_SERVICE + '/carrier/modify_sys_flag', { carrierId: data.companyId });
      }
    }
    return res
  })
}
// 删除
export const del = data => {
  return axios.post(baseUrl + '/userpower/delEnterpriseUser', qs.stringify(data), {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}
// 重置密码
export const resetPwd = data => {
  return axios.post(baseUrl + '/userpower/updatePassword', qs.stringify(data), {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

export function userDataAuthAll(parameter) {
  // 根据用户ID查询关联权限列表
  return axios({
    url: SERVICE_CTRL_TOWER + '/userDataAuth/listAllDataAuthByUserId',
    method: 'get',
    params: parameter
  })
}

export function saveDataAuthUser(parameter) {
  // 保存角色授权
  return axios({
    url: SERVICE_CTRL_TOWER + '/userDataAuth/saveDataAuthUser',
    method: 'post',
    data: qs.stringify(parameter)
  })
}

export function userDataAuth(parameter) {
  // 根据用户ID查询关联权限列表
  return axios({
    url: SERVICE_CTRL_TOWER + '/userDataAuth/listDataAuthByUserId',
    method: 'post',
    data: qs.stringify(parameter)
  })
}