import { AddReq, DelReq, EditReq, InfoReq, UserPageQuery } from '@fast-crud/fast-crud';
import Cookies from 'js-cookie';
import { request } from '/@/utils/service';
export const apiPrefix = '/api/system/device/';

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

export function getBorrowRecords(id: number) {
	return request({
		url: `/api/system/device/${id}/borrow_records/`,
		method: 'get'
	});
  }
  
  export function getMaintenanceHistory(id: number) {
	return request({
		url: `/api/system/device/${id}/maintenance_history/`,
		method: 'get'
	});
  }

export function getMaintenanceRecords(id: number) {
	return request({
		url: `/api/system/device/${id}/maintenance_records/`,
		method: 'get'
	});
}

export function getDeviceStatistics(id: number) {
	return request({
		url: `/api/system/device/${id}/statistics/`,
		method: 'get'
	});
}

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

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

export function UpdateObj(obj: EditReq) {
	return request({
		url: apiPrefix + obj.id + '/',
		method: 'put',
		data: obj
	});
}


export function UpdateObjStatus(obj: EditReq) {
	return request({
		url: apiPrefix + obj.id + '/change_status/',
		method: 'post',
		data: obj
	});
}

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

export function getDeviceCategoryList() {
	return request({
		url: apiPrefix + 'category/',
		method: 'get'
	});
}


// export function getDownloadTemplate() {
// 	return request({
// 		url: '/api/system/device/download_template/',
// 		method: 'get',
// 		responseType: 'blob'
// 	});
// }


export function uploadFile(formData: FormData) {
	const token = Cookies.get('token');
	return request({
		url:  '/api/system/device/upload-image/',
		method: 'post',
		data: formData,
		headers:{
					'Content-Type': 'multipart/form-data',
					'Authorization': token ? `JWT ${token}` : ''
				},
		timeout:600000

	});
	// return axios.post('/api/system/device/upload-image/', formData, {
	// 	headers: {
	// 		'Content-Type': 'multipart/form-data'
	// 	}
	// });
}

// export async function uploadImage(formData: FormData) {
// 	return await axios.post('/api/system/device/upload-image/', formData, {
// 	headers: {
// 		'Content-Type': 'multipart/form-data',
// 		// 'Authorization': auth.token
// 	}
// 	});
//   }
  

// export function GetPermission() {
// 	return request({
// 		url: apiPrefix + 'field_permission/',
// 		method: 'get'
// 	});
// }