import * as React from 'react';
import Request from '../utils/request';
export interface WarnRuleProps {
	alarm_count: number; //连续报警次数
	continue_period: number; //持续周期
	id: number;
	is_on: boolean; //是否启用
	is_on_text: string; // 是否启用文字
	project_id: number; //项目id
	silence_period: number; //静默时间,发送间隔（单位分）
	sn: string; //设备唯一编码
}
/**
 * @description 告警规则列表
 */
export const useWarnRules = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		dataSource: WarnRuleProps[];
		page: number;
		total: number;
		error?: { code: number; message: string };
	}>({
		loading: false,
		dataSource: [],
		page: 1,
		total: 0,
	});
	const request = (params: { sn: string }) => {
		setState((preState) => ({ ...preState, loading: true }));
		Request.sendRequest({
			url: '/v1/alarm/rule_list',
			method: 'GET',
			params,
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					...data,
					loading: false,
					dataSource: data.list.map((element) => {
						return {
							...element,
							is_on_text: element.is_on ? '启用' : '未启用',
						};
					}),
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false, error: error }));
			});
	};
	return { state, request } as const;
};
/**
 * @deprecated 删除告警规则
 */
export const useDeleteRule = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (data: { id: number }) => {
		return new Promise((resolve, reject) => {
			setState((preState) => ({ ...preState, loading: true }));
			Request.sendRequest({
				url: `/v1/alarm/rule_delete/${data.id}`,
				method: 'DELETE',
			})
				.then((data) => {
					resolve(data);
				})
				.catch((error) => {
					reject(error);
				})
				.finally(() => {
					setState((preState) => ({ ...preState, loading: false }));
				});
		});
	};
	return { state, request } as const;
};
/**
 * @deprecated 添加规则
 */
export const useAddRule = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (data: {
		alarm_count: number;
		is_on: boolean;
		silence_period: number;
		sn: string;
		continue_period: number;
	}) => {
		return new Promise((resolve, reject) => {
			setState((preState) => ({ ...preState, loading: true }));
			Request.sendRequest({
				url: `/v1/alarm/rule`,
				method: 'POST',
				data,
			})
				.then((data) => {
					resolve(data);
				})
				.catch((error) => {
					reject(error);
				})
				.finally(() => {
					setState((preState) => ({ ...preState, loading: false }));
				});
		});
	};
	return { state, request } as const;
};
export const useSwitchRuleStatus = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (data: { id: number; is_on: boolean }) => {
		return new Promise((resolve, reject) => {
			setState((preState) => ({ ...preState, loading: true }));
			Request.sendRequest({
				url: `/v1/alarm/rule_on_off`,
				method: 'PUT',
				data,
			})
				.then((data) => {
					resolve(data);
				})
				.catch((error) => {
					reject(error);
				})
				.finally(() => {
					setState((preState) => ({ ...preState, loading: false }));
				});
		});
	};
	return { state, request } as const;
};

export const useUpdateRules = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (data: {
		alarm_count: number;
		continue_period: number;
		id: number;
		is_on: boolean;
		silence_period: number;
		sn: string;
	}) => {
		return new Promise((resolve, reject) => {
			setState((preState) => ({ ...preState, loading: true }));
			Request.sendRequest({
				url: `/v1/alarm/rule`,
				method: 'PUT',
				data,
			})
				.then((data) => {
					resolve(data);
				})
				.catch((error) => {
					reject(error);
				})
				.finally(() => {
					setState((preState) => ({ ...preState, loading: false }));
				});
		});
	};
	return { state, request } as const;
};
export interface ConcatProps {
	email: string;
	phone: string;
	id: number;
}
/**
 * @description 告警联系人列表
 */
export const useWarnConcatList = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		dataSource: ConcatProps[];
	}>({
		loading: false,
		dataSource: [],
	});
	const request = () => {
		setState((preState) => ({ ...preState, loading: true }));
		Request.sendRequest({
			url: `/v1/alarm/contact_list`,
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					loading: false,
					dataSource: data.list,
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return { state, request } as const;
};
/**
 * @description 删除告警联系人
 */
export const useDeleteConcat = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (data: { id: number }) => {
		return new Promise((resolve, reject) => {
			setState((preState) => ({ ...preState, loading: true }));
			Request.sendRequest({
				url: `/v1/alarm/contact_delete/${data.id}`,
				method: 'DELETE',
			})
				.then((data) => {
					resolve(data);
				})
				.catch((error) => {
					reject(error);
				})
				.finally(() => {
					setState((preState) => ({ ...preState, loading: false }));
				});
		});
	};
	return { state, request } as const;
};
/**
 * @description 新增告警联系人
 */
export const useAddWarnConcat = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (data: { email: string; phone: string }) => {
		return new Promise((resolve, reject) => {
			setState((preState) => ({ ...preState, loading: true }));
			Request.sendRequest({
				url: `/v1/alarm/contact`,
				method: 'POST',
				data,
			})
				.then((data) => {
					resolve(data);
				})
				.catch((error) => {
					reject(error);
				})
				.finally(() => {
					setState((preState) => ({ ...preState, loading: false }));
				});
		});
	};
	return { state, request } as const;
};
/**
 * @description 修改告警联系人
 */
export const useChangeWarnConcat = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (data: { email: string; phone: string; id: number }) => {
		return new Promise((resolve, reject) => {
			setState((preState) => ({ ...preState, loading: true }));
			Request.sendRequest({
				url: `/v1/alarm/contact`,
				method: 'PUT',
				data,
			})
				.then((data) => {
					resolve(data);
				})
				.catch((error) => {
					reject(error);
				})
				.finally(() => {
					setState((preState) => ({ ...preState, loading: false }));
				});
		});
	};
	return { state, request } as const;
};
