import React from 'react';
import Request from '../utils/request';
export const ScheduleStaus = {
  0: '未开工',
  1: '已完成',
  2: '进行中',
};
export interface IscheduleProps {
  expect_end_time: string;
  expect_start_time: string;
  reality_end_time: string;
  name: string;
  status: 0 | 1 | 2 | 3;
  id: number;
}
/**
 * @description 获取工地进度
 */
export const useProjectSchedule = () => {
  const [state, setState] = React.useState<{
    loading: boolean;
    data: {
      project_plan: string;
      list: IscheduleProps[];
      start_days: number;
      total_days: number;
    };
  }>({
    loading: false,
    data: {
      project_plan: '',
      list: [],
      start_days: 0,
      total_days: 0,
    },
  });
  const request = () => {
    setState((preState) => ({ ...preState, loading: true }));
    Request.sendRequest({
      url: '/v1/project/progress_list',
    })
      .then((data: any) => {
        setState((preState) => ({
          ...preState,
          loading: false,
          data: { ...data, list: data.list || [] },
        }));
      })
      .catch((error) => {
        setState((preState) => ({ ...preState, loading: false }));
      });
  };
  return [state, request] as const;
};

/**
 * @description 新增项目进度
 */
export const useAddProcess = () => {
  const [state, setState] = React.useState({ loading: false });
  const request = (data: {
    expectEndTime: number;
    expectStartTime: number;
    name: string;
  }) => {
    setState((preState) => ({ ...preState, loading: true }));
    return new Promise((resolve, reject) => {
      Request.sendRequest({
        url: '/v1/project/progress',
        method: 'POST',
        data,
      })
        .then((data: any) => {
          resolve();
        })
        .catch((error) => {
          reject(error);
        })
        .finally(() => {
          setState((preState) => ({ ...preState, loading: false }));
        });
    });
  };
  return { state, request } as const;
};
/**
 * @description 删除进度
 */
export const useDeleteProcess = () => {
  const [state, setState] = React.useState({ loading: false });
  const request = (data: { id: number }) => {
    setState((preState) => ({ ...preState, loading: true }));
    return new Promise((resolve, reject) => {
      Request.sendRequest({
        url: `/v1/project/progress/${data.id}`,
        method: 'DELETE',
      })
        .then((data: any) => {
          resolve();
        })
        .catch((error) => {
          reject(error);
        })
        .finally(() => {
          setState((preState) => ({ ...preState, loading: false }));
        });
    });
  };
  return { state, request } as const;
};
export const useUpdateProcess = () => {
  const [state, setState] = React.useState({ loading: false });
  const request = async (data: { id: number; image?: string;reality_end_time:number;reality_start_time:number }) => {
    setState((preState) => ({ ...preState, loading: true }));
    await Request.sendRequest({
      url: `/v1/project/progress`,
      method: 'PUT',
      data,
    }).finally(() => {
      setState((preState) => ({ ...preState, loading: false }));
    });
  };
  return { state, request } as const;
};
