
import request from './request.js'
import { CHAT_TYPE, PAY_TYPE } from '@/common/enum.js'
const FormData = require('./formData.js')

/**
 * @code {string} 微信授权code
 * @shareId {string} 推荐人id
 */
export function wxlogin(code , shareId = '') {
	return request({
		url: 'wx-mp/login',
		method: 'POST',
		data: {
			"code": code,
			"shareId": shareId == 'undefined' ? '' : shareId
		}
	})
}

/**
 * 获取用户信息
 */
export function getUserinfo() {
	return request({
		url: 'user/info',
		method: 'GET'
	})
}

/** 
 * 更新用户头像
 * @param {String} avatar 头像地址
 */
export function updateAvatar(avatar) {
	return request({
		url: 'user/update',
		method: 'POST',
		data: {
			"avatarUrl": avatar
		}
	})
}

/** 
 * 更新用户昵称
 * @param {String} name 昵称
 */
export function updateUserName(name) {
	return request({
		url: 'user/update',
		method: 'POST',
		data: {
			"nickname": name
		}
	})
}

/** 
 * 获取用户的手机号
 * @param {String} code 加密code，后端解析
 */
export function getPhoneNumber(code) {
	return request({
		url: 'wx-mp/getPhoneNumber',
		method: 'POST',
		data: {
			code
		}
	})
}



/**
 * 提问接口
 * @param {String} 内容
 */
export function ask(data) {
	return request({
		url: 'chat/ask',
		method: 'POST',
		data
	})
}

/**
 * 根据问题id查询结果
 */
export function getAnswer(questionId, timeout = 3000) {
	return request({
		url: `chat/check?questionId=${questionId}`,
		method: 'GET',
		timeout: timeout,
	})
}

/**
 * 点赞
 */
export function like(questionId) {
	return request({
		url: `chat/like`,
		method: 'POST',
		data: {
			"questionId": questionId
		}
	})
}

/**
 * 点灭
 */
export function dislike(questionId) {
	return request({
		url: `chat/dislike`,
		method: 'POST',
		data: {
			"questionId": questionId
		}
	})
}


/**
 * 分享成功
 */
export function shareSuccess() {
	return request({
		url: `user/share`,
		method: 'POST'
	})
}

/**
 * 观看广告成功
 */
export function adSuccess() {
	return request({
		url: `user/ad`,
		method: 'POST'
	})
}

/**
 * 签到
 */
export function checkInSuccess() {
	return request({
		url: `user/checkIn`,
		method: 'POST'
	})
}

/**
 * 查询历史聊天记录
 */
export function chatHistory(sessionId = '', size = 10, cursor = 0) {
	// cursor先不传
	return request({
		url: `chat/history?cursor=&size=${size}&sessionId=${sessionId}`,
		method: 'GET'
	})
}

/**
 * 查询历史聊天记录
 */
export function chatSessionList(page = 1, size = 10, type = CHAT_TYPE.NORMAL) {
	return request({
		url: `chat/sessions?page=${page}&size=${size}&type=${type}`,
		method: 'GET'
	})
}

/**
 * 新建会话
 */
export function createChatSession(type = CHAT_TYPE.NORMAL, name = '') {
	return request({
		url: `chat/session/create`,
		method: 'POST',
		data: {
			type,
			name
		}
	})
}

/**
 * 修改会话名称
 */
export function updateChatSession(id, name) {
	return request({
		url: `chat/session/update`,
		method: 'POST',
		data: {
			sessionId: id,
			name
		}
	})
}

/**
 * 删除会话
 */
export function delChatSession(id) {
	return request({
		url: `chat/session/delete`,
		method: 'POST',
		data: {
			sessionId: id
		}
	})
}

export function uploadImage(file) {
	let formData = new FormData();
	// http://tmp/glVuGzCe080R9ca2ce1e365d83505481117c93cc2137.jpeg
	let name = file.length > 10 ? file.substring(file.length - 9, file.length) : file;
	formData.appendFile("file", file, name);
	const data = formData.getData()
	// console.log(data)
	return request({
		url: `image`,
		method: 'POST',
		header: {
			'content-type': data.contentType
		},
		data: data.buffer
	})
}

/**
 * 支付接口
 * @param {Object} type MONTHLY:包月;SEASONALLY:包季;YEARLY:包年
 */
export function payOrder(type = PAY_TYPE.MONTHLY) {
	return request({
		url: `wx-mp/preOrder`,
		method: 'POST',
		data: {
			type: type
		}
	})
}

/** 
 * 获取支付订单列表
 */
export function getOrders(page = 1, size = 100) {
	///user/orders?page=&size=
	// cursor先不传
	return request({
		url: `user/orders?page=${page}&size=${size}`,
		method: 'GET'
	})
}
/** 
 * 获取产品列表
 */
export function getProducts() {
	return request({
		url: `user/products`,
		method: 'GET'
	})
}

/**
 * 获取二维码
 * @param {Object} data, 参数格式如下
 *  "page": "pages/index/index",
	 "scene": "a=1",
	 "check_path": true,
	 "env_version": "release",
	 "width":430,
	 "auto_color":false,
	 "line_color":{
		"r":255,
		"g":255,
		"b":255
	 },
	 "is_hyaline":false
 */
export function getQrcode(data) {
	return request({
		url: `wx-mp/getQRCode`,
		header: {
			'content-type': 'application/json',
		},
		method: 'POST',
		data
	})
}