import {http2} from "@/utils/request.js";
/**获取消息列表 */
export const getMessageList = (data) => {
  return http2.get(`/acs/v1/messages/get_message_list?page=${data.page}&count=${data.count}&message_type=${data.message_type}`)
}
/**获取单条消息 */
export const getMessage = (data) => {
  return http2.get(`/acs/v1/messages/get_message?message_id=${data}`)
}
/**添加消息 */
export const addMessage = (data) => {
  return http2.post(`/acs/v1/messages/add_message`, data,{
headers: {
'Content-Type': 'multipart/form-data'
}
})
}
/**删除消息 */
export const deleteMessage = (data) => {
  return http2.post(`/acs/v1/messages/delete_message`, data,{
headers: {
'Content-Type': 'multipart/form-data'
}
})
}
/**修改消息 */
export const editMessage = (data) => {
   return http2.post(`/acs/v1/messages/update_message`, data,{
headers: {
'Content-Type': 'multipart/form-data'
}
})
}
/**获取附件列表 */
export const getFileList = (data) => {
  return http2.get(`/acs/v1/attachment/get_attachment_list?page=${data.page}&count=${data.count}&key=${data.key}&user_type=${data.user_type}`)
}
/**上传附件 */
export const uploadFile = (data) => {
   return http2.post(`/acs/v1/attachment/add_attachment`, data,{
headers: {
'Content-Type': 'multipart/form-data'
}
})
}
/**删除附件 */
export const deleteFile = (data) => {
  return http2.get(`/acs/v1/attachment/delete_attachment?aid=${data}`)
}
/**用户获取消息列表 */
export const getUserMessageList = (data) => {
  return http2.get(`/acs/v1/messages/get_message_list?page=${data.page}&count=${data.count}&message_type=${data.message_type}`)
}
/**用户获取单条消息 */
export const getUserMessage = (data) => {
  return http2.get(`/acs/v1/messages/get_message?message_id=${data}`)
}

/**获取消息详情 */
export const getMessageDetail = (data) => {
  return http2.get(`/acs/v1/messages/get_message?message_id=${data.message_id}`)
}
/**修改图片大小 */
export const compressAndConvertFileToBase64=async(file)=> {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    
    reader.onload = function(e) {
      const img = new Image();
      img.src = e.target.result;
      
      img.onload = function() {
        const canvas = document.createElement('canvas');
        const MAX_WIDTH = 300; // 压缩的最大宽度
        const MAX_HEIGHT = 200; // 压缩的最大高度
        
        let width = img.width;
        let height = img.height;
        
        // 计算压缩比例
        if (width > height) {
          if (width > MAX_WIDTH) {
            height *= MAX_WIDTH / width;
            width = MAX_WIDTH;
          }
        } else {
          if (height > MAX_HEIGHT) {
            width *= MAX_HEIGHT / height;
            height = MAX_HEIGHT;
          }
        }
        
        canvas.width = width;
        canvas.height = height;
        
        const ctx = canvas.getContext('2d');
        ctx.drawImage(img, 0, 0, width, height);
        
        // 将canvas内容转换为jpeg并压缩质量
        const dataUrl = canvas.toDataURL('image/jpeg', 0.8); // 0.8是压缩质量，范围0-1
        
        resolve(dataUrl);
      };
      
      img.onerror = function(error) {
        reject(error);
      };
    };
    
    reader.onerror = function(error) {
      reject(error);
    };
    
    reader.readAsDataURL(file);
  });
}