import * as React from 'react';
import Request from '../utils/request';
import { globalColorConfigs } from '../contants';
interface basicHooksStateprops {
	loading: boolean;
}
/**
 * @description 获取人员类型
 */
export const usePersonTypes = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		dataSource: {
			total_attendance: number;
			total_in: number;
			list: {
				attendance: number;
				staff_type: string;
				total: number;
				type_code: number;
			}[];
		};
	}>({
		loading: false,
		dataSource: { list: [], total_in: 0, total_attendance: 0 },
	});
	const request = () => {
		setState((preState) => ({ ...preState, loading: true }));
		Request.sendRequest({
			url: '/v1/staff/type_statistics',
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					loading: false,
					dataSource: {
						...data,
					},
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return [state, request] as const;
};
/**
 * @description 获取最新考勤数据
 */
export const usePersonLastAttendance = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		dataSource: {
			group: string;
			in_out: number;
			name: string;
			photo: string;
			recog_time: string;
			work_no: string;
			work_type: string;
		};
	}>({
		loading: false,
		dataSource: {
			group: '',
			in_out: 0,
			name: '',
			photo: '',
			recog_time: '',
			work_no: '',
			work_type: '',
		},
	});
	const request = () => {
		setState((preState) => ({ ...preState, loading: true }));
		Request.sendRequest({
			url: '/v1/staff/last',
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					loading: false,
					dataSource: {
						...data,
					},
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return [state, request] as const;
};
/**
 * @description 出勤工种分布
 */
export const useAttendancetype = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		dataSource: any[];
	}>({
		loading: false,
		dataSource: [],
	});
	const request = () => {
		setState((preState) => ({ ...preState, loading: true }));
		Request.sendRequest({
			url: '/v1/staff/work_type_statistics',
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					loading: false,
					dataSource: data.list
						.map((type, index) => {
							return {
								...type,
								color: globalColorConfigs[index],
								text: type.work_type,
								current: type.attendance,
							};
						})
						.filter((type) => !!type.total),
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return [state, request] as const;
};

/**
 * @deprecated 劳务公司出勤人员
 */
export const useServicePersonDetail = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		dataSource: {
			labor_company: string;
			labor_attendance: number;
			manager_attendance: number;
			total: number;
			attendance: number;
		}[];
	}>({
		loading: false,
		dataSource: [],
	});
	const request = () => {
		setState((preState) => ({ ...preState, loading: true }));
		Request.sendRequest({
			url: '/v1/staff/company_statistics',
		})
			.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 useStaffList = () => {
	const [state, setState] = React.useState({
		loading: false,
		dataSource: [],
		total: 0,
		page: 1,
	});
	const request = (params: { page: number; filter: string }) => {
		setState((preState) => ({ ...preState, loading: true }));
		Request.sendRequest({
			url: '/v1/staff/list',
			params,
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					...data,
					loading: false,
					dataSource: data.list,
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return { state, request } as const;
};
