import http from '@/utils/http'

// const appId = uni.getAccountInfoSync().miniProgram.appId

export function loginByCode(data) {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success: function (res) {
				http.post('wxMini/login', {
					code: res.code,
				}, {
					isAuth: false,
					showLoading: false
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			}
		})
	})
}
export function login(data) {
	return http.post('user/login', data)
}
export function wxLogin(data) {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success: function (res) {
				http.post('wxMini/authLogin', {
					encryptedData: data.encryptedData,
					ivStr: data.iv,
					userInfo: JSON.stringify(data.userInfo),
					code: res.code,
				}, {
					isAuth: false,
					loadingTitle: '登录中...'
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			}
		})
	})
}
export function logout(data) {
	return http.post('user/logout', data)
}


export function miniAppLogin() {
	return new Promise((resolve, reject) => {
		// uni.login({
		// 	provider: 'weixin',
		// 	success: function (res) {
		// 		http.post('common/user/login',
		// 			{ jsCode: res.code },
		// 			{
		// 				isAuth: false,
		// 				showLoading: true,
		// 				loadingTitle: '登录中...',
		// 				header: { 'content-type': 'application/json' },
		// 			}).then(res => {
		// 				resolve(res)
		// 			}).catch(err => {
		// 				reject(err)
		// 			})
		// 	},
		// 	fail: function (err) {}
		// })
		resolve({
			success: true,
			token: 'token',
			roles: [{ name: 'admin' }],
			openId: 'openId',
			user: { name: 'admin', mobile: '13700594535', photo: '', sex: 1 },
		})
	})
}


export function getSchoolInfo(schoolId) {
	return http.get(`schools/${schoolId}`, {}, { isAuth: false })
}

//获取学生详情
export function getStudentInfo(studentId) {
	return http.get(`students/${studentId}`, {}, { isAuth: false })
}



export function getSchoolGrades(schoolId) {
	return http.get(`schools/${schoolId}/grades`, {}, { isAuth: false })
}

export function getGradeClasses(schoolId, grade) {
	return http.get(`schools/${schoolId}/grades-with-classes`, { grade }, { isAuth: false })
}
export function addAdvice(data) {
	return http.post('user/advice', data)
}
// 意见反馈列表
export function getAdviceList() {
	return http.get('user/advice/list')
}
// 版本信息
export function getVersionList(data) {
	return http.get('user/version', data)
}
// 协议、隐私
export function getPolicy() {
	return http.get('user/privacyPolicy')
}
export function getAgreement() {
	return http.get('user/userServiceAgreement')
}

export function addStudent(data) {
	return http.post('students', data, { isAuth: false })
}

export function getIncompleteVisionStudents(classId, params = {}) {
	return http.get(`classes/${classId}/incomplete-vision-students`, params, { isAuth: false })
}

export function inputVisionData(data) {
	return http.post('vision/input-vision', data, { isAuth: false })
}

export function inputRefractionData(data) {
	return http.post('vision/input-refraction', data, { isAuth: false })
}

export function getStudentVisionData(studentId) {
	return http.get(`students/${studentId}/vision-data`, {}, { isAuth: false })
}

export function updateVisionData(visionDataId, data) {
	return http.put(`vision-data/${visionDataId}`, data, { isAuth: false })
}
