import { request } from '/@/utils/service';
import { UserPageQuery, AddReq, DelReq, EditReq, InfoReq } from '@fast-crud/fast-crud';

export const apiPrefix = '/api/system/borrow/';
export const apiPrefixDetail = '/api/system/borrow/application/';

export function GetList(query: UserPageQuery) {
	return request({
		url: apiPrefix + "application/",
		method: 'get',
		params: query
	});
}

export function GetObj(id: InfoReq) {
	return request({
		url: apiPrefix + id,
		method: 'get'
	});
}

export function AddObj(obj: AddReq) {
	return request({
		url: apiPrefix+ "application/",
		method: 'post',
		data: obj
	});
}

export function UpdateObj(obj: EditReq) {
	return request({
		url:`/api/system/borrow/application/${obj.id}/`, //+ obj.id + '/',
		method: 'put',
		data: obj
	});
}

export function DelObj(id: DelReq) {
	return request({
		url: '/api/system/borrow/application/' + id + '/',
		method: 'delete',
		data: { id }
	});
}

// 获取设备列表（用于借用时选择设备）
export function getDeviceList() {
	return request({
		url: '/api/system/device/',
		method: 'get'
	});
}

// 获取用户列表（用于选择借用人）
export function getUserList() {
	return request({
		url: '/api/system/user/',
		method: 'get'
	});
}

export function GetApplicationDetail(ids:number){
	return request({
		url: apiPrefixDetail + ids+"/",
		method: 'get'
	});
}

// 审批借用申请
export function approveBorrow(id: number, action: 'approve' | 'reject', remark?: string) {
	return request({
		url: apiPrefix + id + '/approve/',
		method: 'post',
		data: { action, remark }
	});
}

// 归还设备
export function returnDevice(id: number, remark?: string) {
	return request({
		url: apiPrefix + id + '/return_device/',
		method: 'post',
		data: { remark }
	});
}

export async function CollectEquipment(data: any) {
	return await request({
		url: `/api/system/borrow/application/${data.id}/borrow/`,
		method: 'post',
		data
	});
}

/* 归还 */
export async function ReturnEquipment(data: any) {
	return await request({
		url: `/api/system/borrow/application/${data.id}/return_device/`,
		method: 'post',
		data
	});
}

//续借
export async function RenewDevice(data: any) {
	return await request({
		url: `/api/system/borrow/application/${data.id}/renew/`,
		method: 'post',
		data
	});
}


/* 获取所有用户 */
export function GetAllUser(params:any) {
	return request({
		url: '/api/system/app_user/',
		method: 'get',
		params
	});
}

// 获取设备分类列表
export function getDeviceCategories() {
	return request({
		url: '/api/system/deviceclass/',
		method: 'get'
	});
}