import { useState } from 'react';
import Request from '../utils/request';
import { PARTICIPATION_TYPE } from '../contants';
export type UnitRecordProps = {
	area_code: string;
	corp_type: keyof typeof PARTICIPATION_TYPE;
	id: number;
	name: string;
	register_date: number;
	social_code: string;
};
/**
 * @description 获取参建单位列表
 */
export const useUnitList = () => {
	const [state, setState] = useState<{
		loading: boolean;
		dataSource: UnitRecordProps[];
	}>({ loading: false, dataSource: [] });
	const request = () => {
		setState((preState) => ({ ...preState, loading: true }));
		Request.sendRequest({
			url: '/v1/project/corp_list',
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					loading: false,
					dataSource: data.list,
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
				return Promise.reject(error);
			});
	};
	return { state, request } as const;
};
/**
 * @description 更新建设单位
 */
export const useUpdateUnit = () => {
	const [state, setState] = useState({
		loading: false,
	});
	const request = (data: UnitRecordProps) => {
		return Request.sendRequest({
			url: '/v1/project/corp',
			method: 'PUT',
			data,
		}).finally(() => {
			setState((preState) => ({ ...preState, loading: false }));
		});
	};
	return { state, request } as const;
};
/**
 * @description 新增参建单位
 */
export const useAddUnit = () => {
	const [state, setState] = useState({
		loading: false,
	});
	const request = (data: Omit<UnitRecordProps, 'id'>) => {
		return Request.sendRequest({
			url: '/v1/project/corp',
			method: 'post',
			data,
		}).finally(() => {
			setState((preState) => ({ ...preState, loading: false }));
		});
	};
	return { state, request } as const;
};
/**
 * @description 删除参建单位
 */
export const useRemoveUnit = () => {
	const [state, setState] = useState({
		loading: false,
	});
	const request = (data: { id: number }) => {
		return Request.sendRequest({
			url: `/v1/project/corp/${data.id}`,
			method: 'DELETE',
		}).finally(() => {
			setState((preState) => ({ ...preState, loading: false }));
		});
	};
	return { state, request } as const;
};
