import request from '@/utils/request'
import { AxiosPromise } from 'axios'

// apiUrl
const api = {
	page: '/sys/user/page',
	create: '/sys/user/create',
	update: '/sys/user/update',
	delete: '/sys/user/delete',
	resetPassword: '/sys/user/reset-password',
	assignRoles: '/sys/user/assign-roles',
	assignDepartment: '/sys/user/assign-departments',
	roleIds: '/sys/user/rel-ids'
}

/**
 * 用户管理 - 列表
 */
function userPageApi(data: any): AxiosPromise {
	return request({
		url: api.page,
		method: 'post',
		data
	})
}

/**
 * 用户管理 - 新增编辑保存
 */
function userAddOrEditSaveApi(data: any): AxiosPromise {
	return request({
		url: data.id ? api.update : api.create,
		method: 'post',
		data
	})
}

/**
 * 用户管理 - 删除
 */
function userDeleteApi(data: any): AxiosPromise {
	return request({
		url: api.delete,
		method: 'post',
		data
	})
}

/**
 * 用户管理 - 重置密码
 */
function userResetPasswordApi(data: any): AxiosPromise {
	return request({
		url: api.resetPassword,
		method: 'post',
		data
	})
}
/**
 * 用户管理 - 重置密码
 */
function userAssignRolesApi(data: any): AxiosPromise {
	return request({
		url: api.assignRoles,
		method: 'post',
		data
	})
}

function userAssignDepartmentApi(data: any): AxiosPromise {
	return request({
		url: api.assignDepartment,
		method: 'post',
		data
	})
}
/**
 * 用户管理 - 绑定角色
 */
function userRoleIdsApi(data: any): AxiosPromise {
	return request({
		url: `${api.roleIds}?id=${data.id}`,
		method: 'post'
	})
}

/**
 * 用户信息
 */
export function userGetApi(id: string): AxiosPromise {
	return request({
		url: `/sys/user/get?id=${id}`,
		method: 'get'
	})
}

const user = {
	userPageApi,
	userAddOrEditSaveApi,
	userDeleteApi,
	userResetPasswordApi,
	userAssignRolesApi,
	userRoleIdsApi,
	userGetApi,
	userAssignDepartmentApi
}
export default user
