import { useState } from 'react';
import Request from '../utils/request';
export type MangerPersonRecordProps = {
	cert_num: string;
	cert_type: number;
	name: string;
	p_type: string;
	phone: string;
	corp_name: string;

	corp_type: number;
	id: number;
	social_code: string;
};
/**
 * @description 获取管理人员列表
 */
export const useMangerPersonList = () => {
	const [state, setState] = useState<{
		loading: boolean;
		dataSource: MangerPersonRecordProps[];
	}>({ loading: false, dataSource: [] });
	const request = (params: {
		filter: string;
		corp_id: number;
		page: number;
	}) => {
		setState((preState) => ({ ...preState, loading: true }));
		Request.sendRequest({
			url: '/v1/project/pm_list',
			params,
		})
			.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 useUpdateMangerPerson = () => {
	const [state, setState] = useState({
		loading: false,
	});
	const request = (data: MangerPersonRecordProps) => {
		setState((preState) => ({ ...preState, loading: true }));

		return Request.sendRequest({
			url: '/v1/project/pm',
			method: 'PUT',
			data,
		}).finally(() => {
			setState((preState) => ({ ...preState, loading: false }));
		});
	};
	return { state, request } as const;
};
/**
 * @description 新增管理人员
 */
export type InsertDataTye = Pick<
	MangerPersonRecordProps,
	'cert_num' | 'cert_type' | 'name' | 'p_type' | 'phone'
>;

export const useAddMangerPerson = () => {
	const [state, setState] = useState({
		loading: false,
	});
	const request = (data: InsertDataTye) => {
		setState((preState) => ({ ...preState, loading: true }));

		return Request.sendRequest({
			url: '/v1/project/pm',
			method: 'post',
			data,
		}).finally(() => {
			setState((preState) => ({ ...preState, loading: false }));
		});
	};
	return { state, request } as const;
};
/**
 * @description 删除管理人员
 */
export const useRemoveMangerPerson = () => {
	const [state, setState] = useState({
		loading: false,
	});
	const request = (data: { id: number }) => {
		setState((preState) => ({ ...preState, loading: true }));
		return Request.sendRequest({
			url: `/v1/project/pm/${data.id}`,
			method: 'DELETE',
		}).finally(() => {
			setState((preState) => ({ ...preState, loading: false }));
		});
	};
	return { state, request } as const;
};
