import * as React from 'react';

import Request from '../utils/request';
/**
 * @description 用户登录
 */
export const useUserLogin = () => {
	const [state, setState] = React.useState({ loading: false });
	const request = (
		data: {
			password: string;
			user: string;
			rember_password: boolean;
		},
		header?: { token: string }
	) => {
		return new Promise<{
			uid: string;
			token: string;
			refresh_token: string;
			username: string;
		}>((resolve, reject) => {
			setState((preState) => ({ ...preState, loading: true }));
			Request.sendRequest({
				url: '/v1/user/login_front',
				method: 'POST',
				headers: header,
				data,
			})
				.then((res: any) => {
					resolve(res);
				})
				.catch((error) => {
					reject(error);
				})
				.finally(() => {
					setState((preState) => ({ ...preState, loading: false }));
				});
		});
	};
	return [state, request] as const;
};
/**
 * @description 用户权限
 */
export const useUserPermission = () => {
	const [state, setState] = React.useState({
		loading: false,
		data: { admin: true, user_name: '' },
	});
	const request = () => {
		setState((preState) => ({ ...preState, loading: true }));
		return new Promise((resolve, reject) => {
			Request.sendRequest({
				url: '/v1/user/permission',
			})
				.then((data: any) => {
					setState((preState) => ({
						...preState,
						loading: false,
						data: { admin: data.is_admin, user_name: data.user_name },
					}));
					resolve(data);
				})
				.catch((error) => {
					setState((preState) => ({ ...preState, loading: false }));
					reject(error);
				});
		});
	};
	return { state, request } as const;
};
/**
 * @description 修改邮箱
 */
export const useChangeEmail = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (data: { email: string; password: string }) => {
		return Request.sendRequest({
			url: '/v1/user/verify_mail',
			method: 'PUT',
			data,
		}).finally(() => {
			setState((preState) => ({ ...preState, loading: false }));
		});
	};
	return { state, request } as const;
};
/**
 * @description 发送验证码
 */
export const useSendPhoneCode = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (params: { phone: string }) => {
		return Request.sendRequest({
			url: '/v1/vcode',
			params,
		}).finally(() => {
			setState((preState) => ({ ...preState, loading: false }));
		});
	};
	return { state, request } as const;
};
/**
 * @description 修改手机号
 */
export const useChangePhone = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (data: { phone: string; vcode: number }) => {
		return Request.sendRequest({
			url: '/v1/user/phone',
			method: 'PUT',
			data,
		}).finally(() => {
			setState((preState) => ({ ...preState, loading: false }));
		});
	};
	return { state, request } as const;
};
/**
 * @description 获取用户信息
 */
export const useGetUrerInfo = () => {
	const [state, setState] = React.useState({
		loading: false,
		data: { email: '', phone: '' },
		error: null,
	});
	const request = () => {
		setState((preState) => ({ ...preState, loading: true }));
		Request.sendRequest({
			url: '/v1/user/info',
		})
			.then((data: any) => {
				setState((preState) => ({ ...preState, loading: false, data }));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false, error }));
			});
	};
	return { state, request } as const;
};
/**
 * @description 重置用户密码
 */
export const useChangePassword = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (data: { new: string; old: string }) => {
		setState((preState) => ({ ...preState, loading: true }));
		return Request.sendRequest({
			url: '/v1/user/change_passwd',
			data,
			method: 'PUT',
		})
			.then((data: any) => {
				setState((preState) => ({ ...preState, loading: false, data }));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false, error }));
			});
	};
	return { state, request } as const;
};
