import { extend } from 'umi-request';

interface ILoginParams {
  name: string;
  password: string;
}

const request = extend({});

let host = "";
if (process.env.NODE_ENV === 'development') {
  host = 'http://localhost:44315'
} else {
  host = 'https://glm-mangement-api.baibaomen.com'
}

request.interceptors.request.use((url, options) => {
  const token = JSON.parse(localStorage.getItem('user') || '{}')?.token;

  let authHeader = null;
  if (url == host + "/User/excelFile"){
    authHeader= {
      Authorization: `Bearer ${token || ''}`,
      headers:{'Content-Type': 'multipart/form-data'},
      __tenant: '3a0b773f-907f-f191-74d0-48f258e57bcb',
    };
  }else{
    authHeader= {
      Authorization: `Bearer ${token || ''}`,
      'Content-Type': 'application/json', 
      __tenant: '3a0b773f-907f-f191-74d0-48f258e57bcb',
    };
  }
  return {
    url: url,
    options: { ...options, interceptors: true ,headers: authHeader},
  };
});

request.interceptors.response.use(async (response): Promise<any> => {
  if (response.status === 401) {
    localStorage.removeItem('user');
    localStorage.removeItem('expires');
    location.href = '/chat';
  } else if (response.status === 200) {
    return response;
  } else {
    return Promise.reject(response.statusText);
  }
});

export function loginChatGLM(options: ILoginParams) {
  return request(host + '/api/app/account/login', {
    method: 'POST',
    data: options,
  });
}

export interface IPromptWord {
  type: string;
  content: string;
}
export function GetChatGLMToken(): Promise<string> {
  return request(host + '/user/token', {
    method: 'POST'
  });
}

export function GetPromptList(): Promise<{ items: IPromptWord[] }> {
  return request(host + '/promptwords/page', {
    method: 'POST',
    headers: {
      Authorization: 'Bearer ' + JSON.parse(localStorage.getItem('user') || '')?.token,
    },
    body: JSON.stringify({ pageIndex: 0, pageSize: 10 }),
  });
}

export const CreatedAccount = (params: any): Promise<string> => {
  return request(host + '/User/create', {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const getRole = (params?: any): Promise<string> => {
  return request(host + '/User/roles', {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const getUserMessage = (params?: any): Promise<string> => {
  return request(host + '/User/page', {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const createPromptWords = (params: any): Promise<any> => {
  return request(host + '/PromptWords/create', {
    method: 'POST',
    data: params,
  });
};

export const updatePromptWords = (params: any): Promise<any> => {
  return request(host + '/PromptWords/update', {
    method: 'POST',
    data: params,
  });
};

export const deletePromptWords = (params: any): Promise<any> => {
  return request(host + '/PromptWords/delete', {
    method: 'POST',
    data: params,
  });
};

export const pagePromptWords = (params: any): Promise<any> => {
  return request(host + '/PromptWords/page', {
    method: 'POST',
    data: params,
  });
};

export const createPromptWordType = (params: any): Promise<any> => {
  return request(host + '/PromptWordTypes/create', {
    method: 'POST',
    data: params,
  });
};

export const updatePromptWordType = (params: any): Promise<any> => {
  return request(host + '/PromptWordTypes/update', {
    method: 'POST',
    data: params,
  });
};

export const deletePromptWordType = (params: any): Promise<any> => {
  return request(host + '/PromptWordTypes/delete', {
    method: 'POST',
    data: params,
  });
};

export const pagePromptWordType = (params: any): Promise<any> => {
  return request(host + '/PromptWordTypes/page', {
    method: 'POST',
    data: params,
  });
};

export const typePromptWords = (): Promise<any> => {
  return request(host + '/PromptWordTypes/type', {
    method: 'POST',
  });
};

export const postDeleteUser = (params?: any): Promise<string> => {
  return request(host + '/User/delete', {
    method: 'POST',
    data: params,
  });
};

export const postResetUser = (params?: any): Promise<string> => {
  return request(host + '/User/reset', {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const postEditUser = (params?: any): Promise<string> => {
  return request(host + '/User/update', {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const postExcelFile = (file) => {
  const formData = new FormData();
  formData.append('file', file);
  
  return request(host + '/User/excelFile', {
    method: 'POST',
    body: formData
  }).then(response => {
    return response;
  })
  .catch(error => {
    throw error;
  });
};

export const postDowload = () => {
  return request('Template.xlsx', {
    method: 'GET',
    responseType: 'blob',   // 将响应类型设置为二进制流
  }).then((response) => {
    const url = window.URL.createObjectURL(new Blob([response]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', 'excelTemplate.xlsx');
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  });
}

export const createSession = (params: any): Promise<any> => {
  return request(host + '/ChatSessions/create', {
    method: 'POST',
    data: params,
  });
};

export const updateSession = (params: any): Promise<any> => {
  return request(host + '/ChatSessions/update', {
    method: 'POST',
    data: params,
  });
};
