import { useState } from 'react';
import Request from '../utils/request';
export interface TaskRecordProps {
	id: number;
	task_type: keyof typeof TaskType;
	status: keyof typeof taskStatus;
	is_overdue: boolean;
	task_name: string;
	created_time: string;

	task_code: string;
}
export const TaskType = {
	1: '质量',
	2: '安全',
};
export enum taskStatusEnums {
	'pending_handle' = 1,
	'pending_review',
	'refused',
	'reviewed',
}

export const taskStatus = {
	[taskStatusEnums.pending_handle]: '待处理',
	[taskStatusEnums.pending_review]: '待审核',
	[taskStatusEnums.refused]: '审核退回',
	[taskStatusEnums.reviewed]: '已处理',
};
/**
 * @description 任务列表
 */
export const useTaskList = () => {
	const [state, setState] = useState<{
		loading: boolean;
		dataSource: TaskRecordProps[];
		total: number;
		page: number;
	}>({
		loading: false,
		dataSource: [],
		total: 0,
		page: 1,
	});
	const request = (params: {
		page: number;
		start_time?: number;
		end_time?: number;
		overdue: 0 | 1 | 2;
		task_type: 1 | 2 | 3;
	}) => {
		setState((preState) => ({ ...preState, loading: true }));
		return Request.sendRequest({
			url: '/v1/task/list',
			params,
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					loading: false,
					...data,
					dataSource: data.list,
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
				return Promise.reject(error);
			});
	};
	return { state, request } as const;
};
type taskDetailProps = {
	finish_date: string;
	task_code: string;
	task_name: string;
	task_type: keyof typeof TaskType;
	list: {
		approve_status: boolean;
		desc: string;
		img: string[];
		person: string;
		pipe_time: string;
		pipe_type: number;
	}[];
	status: keyof typeof taskStatus;
};
/**
 * @description 任务详情
 */
export const useTaskDetails = () => {
	const [state, setState] = useState<{
		loading: boolean;
		dataSource?: taskDetailProps;
	}>({
		loading: false,
	});
	const request = (id: number) => {
		return Request.sendRequest({
			url: `/v1/task/info/${id}`,
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					loading: false,
					dataSource: {
						...data,
						list: data.list.map((element) => {
							return { ...element, img: JSON.parse(element.img) };
						}),
					},
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return { state, request } as const;
};
export const TaskProcess = {
	1: '创建任务',
	2: '处理任务',
	3: '审核任务',
};
/**
 * @description 更新任务
 */
export const useUpdateTask = () => {
	const [state, setState] = useState<{
		loading: boolean;
		dataSource?: taskDetailProps;
	}>({
		loading: false,
	});
	const request = (data: {
		id: number;
		processing_person: string;
		processing_results: boolean;
		processing_results_img: string[];
	}) => {
		return Request.sendRequest({
			url: `/v1/task`,
			method: 'PUT',
			data,
		}).finally(() => {
			setState((preState) => ({ ...preState, loading: false }));
		});
	};
	return { state, request } as const;
};
