import request from '@/utils/request';
import { encrypt } from '@/utils/encrypt';

export function getList(data) {
  let param = '?page=' + data.page + '&size=' + data.size;
  if (data.userName != '') {
    param += '&userName=' + data.userName;
  }
  if (data.nickName != '') {
    param += '&nickName=' + data.nickName;
  }
  if (data.phone != '') {
    param += '&phone=' + data.phone;
  }
  if (data.email != '') {
    param += '&email=' + data.email;
  }
  if (data.deptId != '') {
    param += '&deptId=' + data.deptId;
  }
  return request({
    url: '/sysUser/select' + param,
    method: 'get',
    data,
  });
}

export async function doEdit(data) {
  if (data.password != null) {
    data.password = await encrypt(data.password);
  }
  return request({
    url: '/sysUser/update',
    method: 'post',
    data,
  });
}

export function doDelete(data) {
  return request({
    url: '/sysUser/remove',
    method: 'post',
    data,
  });
}

export async function doAdd(data) {
  if (data.password != '') {
    data.password = await encrypt(data.password);
  }
  return request({
    url: '/sysUser/insert',
    method: 'post',
    data,
  });
}

export function getProjectIdByUserId(data) {
  return request({
    url: '/sysUser/getProjectIdByUserId?userId=' + data.userId,
    method: 'get',
    data,
  });
}

export function assignRole(data) {
  return request({
    url: '/sysUser/assignRole',
    method: 'post',
    data,
  });
}

export function assignProject(data) {
  return request({
    url: '/sysUser/assignProject',
    method: 'post',
    data,
  });
}
//为用户分配小程序角色
export function assignMiniRole(data) {
  return request({
    url: '/sysUser/assignMiniRole',
    method: 'post',
    data,
  });
}
//查询所有项目
export function getAllProjects(data) {
  return request({
    url: '/sysUser/getAllProjects',
    method: 'get',
  });
}
