import * as React from 'react';
import Request from '../utils/request';
interface basicHooksStateprops {
  loading: boolean;
}
/**
 * @description 项目信息
 */
export const useProjectInfo = () => {
  const [state, setState] = React.useState<{
    loading: boolean;
    data: {
      project_plan: "";
      project_effect_pic:"";
      name: string;
      start_day: string;
      end_day: string;
      location: string;
    };
  }>({
    loading: false,
    data: {
      project_plan: "",
      project_effect_pic: "",
      name: '',
      end_day: '',
      start_day: '',
      location: '',
    },
  });
  const request = () => {
    setState((preState) => ({ ...preState, loading: true }));
    Request.sendRequest({
      url: '/v1/project/info',
    })
      .then((data: any) => {
        setState((preState) => ({
          ...preState,
          loading: false,
          data: {
            ...data,
            project_effect_pic: JSON.parse(data.project_effect_pic).length>0?JSON.parse(data.project_effect_pic)[0]:"",
            project_plan:  JSON.parse(data.project_plan).length>0?JSON.parse(data.project_plan)[0]:"",
          },
        }));
      })
      .catch((error) => {
        setState((preState) => ({ ...preState, loading: false }));
      });
  };
  return [state, request, setState] as const;
};

export const usePutProjectInfo = () => {
  const [state, setState] = React.useState({
    loading: false,
  });
  const request = (data: {
    location: string;
    name: string;
    shortName: string;
  }) => {
    setState((preState) => ({ ...preState, loading: true }));
    return new Promise((resolve, reject) => {
      Request.sendRequest({
        url: '/v1/project/name_location',
        method: 'PUT',
        data,
      })
        .then((data: any) => {
          resolve(data);
        })
        .catch((error) => {
          reject(error);
        })
        .finally(() => {
          setState((preState) => ({ ...preState, loading: false }));
        });
    });
  };
  return [state, request] as const;
};

export const useUpdateProjectPic = () => {
  const [state, setState] = React.useState({
    loading: false,
  });
  const request = (data: { picType: 0 | 1; picUrl: string[] }) => {
    setState((preState) => ({ ...preState, loading: true }));
    return new Promise((resolve, reject) => {
      Request.sendRequest({
        url: `/v1/project/pic`,
        method: 'PUT',
        data,
      })
        .then((data: any) => {
          resolve();
        })
        .catch((error) => {
          reject(error);
        })
        .finally(() => {
          setState((preState) => ({ ...preState, loading: false }));
        });
    });
  };
  return { state, request } as const;
};
