import request from '@/utils/request-kip.js'
const DEFAULT_CONFIG = {
	refreshTokenUrl: `/oauth/v1/oauth/renewToken?refreshToken=`,
}

const POINTS_PREFIX = '/points/v1';
// 微信授权手机号登录，注册
export function kipPhoneLogin(params) {
	const url = `/oauth/v1/auth/phone-login`;
	return request.post(url, params, { contentType: 'form', ...DEFAULT_CONFIG, noToken: true });
}

// 手机号+验证码登录，微信公众号内H5页面用
/*

params : {
	phoneNumber: string;
	authCode: string;
	verifyCode: string;
}
: Promise<{ code: string, data: Token }>
*/
export function loginWeixinH5(params) {
	return request.post(`/oauth/v1/auth/subscription-phone-login`, params, { isAuthRequest: true, isHandleError: false });
}

// 登录、注册时发送的验证码
export function kipSendRegisterSMS(params) {
	const url = `/profile/v1/send/sms/register`;
	return request.post(url, params, { ...DEFAULT_CONFIG, noToken: true });
}

// 获取微信会员信息
export function kipGetWxMember() {
	const url = `/profile/v1/customer/users/current`;
	return request.get(url, null, { ...DEFAULT_CONFIG });
}

// 通过jsCode登录
export function kipOpenidLogin(params) {
	// 如果当前用户没有注册，则只返回needLogin，如果当前用户已注册，则返回access_token，isNewUser等信息
	const url = `/oauth/v1/auth/openid-login`;
	return request.post(url, params, { contentType: 'form', ...DEFAULT_CONFIG, noToken: true });

}

// 更新微信会员信息 - 会员注册成功之后将微信收信息传给KIP（第一次保存会员信息）
export function kipUpdateWxMember(params) {
	const url = `/oauth/v1/customer/users/info`;
	return request.put(url, params, { ...DEFAULT_CONFIG });
}

// 退出
export function kipLogout() {
	const url = `/oauth/v1/auth/logout`;
	const params = {
		removeBinding: 1
	}
	return request.post(url, params, { contentType: 'form', ...DEFAULT_CONFIG });
}

// 手机号验证码登录
export function kipWxAuthLogin(params) {
	const url = `/oauth/v1/auth/authorized-phone-login`;
	return request.post(url, params, { contentType: 'form', ...DEFAULT_CONFIG, noToken: true });
}

// 发送注销验证码接口
export function kipSendInvalidSMS() {
	const url = `/profile/v1/send/sms/invalid`;
	return request.get(url, null, { ...DEFAULT_CONFIG });
}

// 会员注销（删除会员）
export function kipUserInvalid(params) {
	const url = `/profile/v1/customer/users/invalid`;
	return request.delete(url, params, { ...DEFAULT_CONFIG });
}

// 完善会员信息-注册成功后弹出完善信息页面使用
export function kipCompleteUser(params) {
	const url = `/profile/v1/customer/crm/perfect`;
	return request.put(url, params, { ...DEFAULT_CONFIG });
}

// 获取会员详细信息用于会员注册后完善信息操作，在obj.extend属性中，外层是基础会员信息
export function kipGetUserDetail(params) {
	const url = `/profile/v1/customer/crm/current`;
	return request.get(url, params, { ...DEFAULT_CONFIG });
}

// 检查token是否过期，如果没有过期则返回用户信息，与其他接口格式不一样，如果过期了则http的statusCode为401
export function kipCheckToken(token) {
	if (!token) {
		return;
	}
	// console.log(848484, token)
	const url = `/oauth/v1/customer/users/checkToken?token=${token}`;
	return request.get(url, null, { ...DEFAULT_CONFIG, noToken: true })
}

// 修改手机号的短信发送
export function kipSendSMSByModifyPhone(params) {
	const url = `/profile/v1/send/sms/modify/phone`;
	return request.post(url, params, { ...DEFAULT_CONFIG });
}

// 修改会员手机号
export function kipModifyPhone(params) {
	const url = `/profile/v1/customer/users/modify/phone-number`;
	return request.put(url, params, { ...DEFAULT_CONFIG });
}

// 修改会员信息-个人信息页面使用
export function kipModifyMember(params) {
	const url = `/profile/v1/customer/crm/modify`;
	return request.put(url, params, { ...DEFAULT_CONFIG });
}

// 修改会员头像-个人信息页面使用
export function kipUploadHeadImg(file) {
	const url = `/oauth/v1/customer/users/headImg`;
	const params = {
		fileType: "image",
		file: file,
	}
	return request.uploadFile(url, params, { ...DEFAULT_CONFIG });
}

// 获取会员车牌列表
export function kipGetMemberVehicles() {
	const url = `/profile/v1/customer/vehicles`;
	return request.get(url, null, { ...DEFAULT_CONFIG });
}

// 会员添加车牌
export function kipAddMemberVehicles(params) {
	const url = `/profile/v1/customer/vehicles`;
	return request.post(url, params, { ...DEFAULT_CONFIG });
}

// 会员删除车牌
export function kipDeleteMemberVehicles(id) {
	if (!id) {
		console.error(`kipDeleteMemberVehicles id is null: ${id}`)
		return;
	}
	const url = `/profile/v1/customer/vehicles/${id}`;
	return request.delete(url, null, { ...DEFAULT_CONFIG });
}

// 会员修改车牌
export function kipModifyMemberVehicles(params) {
	if (!params || !params.id) {
		console.error(`kipAddMemberVehicles id is null: ${id}`)
		return
	}
	const url = `/profile/v1/customer/vehicles/${params.id}`;
	return request.put(url, params, { ...DEFAULT_CONFIG });
}

// 获取brand信息(logo, 会员协议)
export function kipGetBrandInfo(params) {
	const url = `/hive-view/v1/hiveas/v2/brands`;
	return request.post(url, params, { ...DEFAULT_CONFIG, noToken: true });
}

// 会员绑定商场
export function kipMemberAddBrand(params) {
	const url = `/profile/v1/customer/brands/establish`;
	return request.post(url, params, { ...DEFAULT_CONFIG });
}

// 使用refreshToken刷新accessToken
export function kipGetNewAccessTokenByRefreshToken(refreshToken) {
	if (!refreshToken) {
		return;
	}
	const url = `/oauth/v1/oauth/renewToken?refreshToken=${refreshToken}`;
	return request.post(url, null, { ...DEFAULT_CONFIG, noToken: true });
}

// 获取所有城市以及brand信息
export function kipAllCities(params) {
	const url = `/hive-view/v1/hiveas/v2/cities`;
	return request.post(url, params, { ...DEFAULT_CONFIG, noToken: true });
}

// 检查品牌是否已授权
export function kipBrandCheck(params) {
	const url = `/profile/v1/customer/brands/check`;
	return request.get(url, params, { ...DEFAULT_CONFIG });
}

// 检查当前会员是否同意协议
export function kipAgreementSave(params) {
	const url = `/profile/v1/customer/agreemen/save`;
	return request.post(url, params, { ...DEFAULT_CONFIG });
}

// 获取自助积分配置
export function kipGetPointsConfig() {
  const url = '/points/v1/auto/points/config'
  return request.get(url, null, { ...DEFAULT_CONFIG });
}

// 检查当前会员是否同意协议
export function kipAgreementCheck (params) {
  const url = `/profile/v1/customer/agreemen/checkUserAgreementVersion`;
  return request.post(url, params, {...DEFAULT_CONFIG});
}

/**
 * 补录无感积分
 * 商圈会员待积分状态: PENDING: 需要弹窗提示补录, FINISHED: 不需要提示补录
 */
export function wxEasyPointsCommitStatus(params) {
	const url = `/points/v1/auto/points/commit-status`;
	return request.get(url, params);
}

export function getVIPIntegral(){
  return request.get(`/points/v1/member/current`);
}