import { useState, useEffect, useCallback } from 'react';
import axios from 'axios';
import { DateRange } from 'react-day-picker';
import { formatDate } from 'date-fns';
import { apiConfig } from '@/config/api.config';

const API_URL = apiConfig.service_credit;

interface useCreditUpdateDataResult {
  cardData: any;
  isCardLoading: boolean;
  cardError: any;
}

const useCreditUpdateData = (data: any, isOpenUnder: boolean): any => {
  const [cardData, setCardData] = useState<[]>([]);
  const [responseData, setResponseData] = useState<any>(null);
  const updateData = useCallback(async () => {
    let dtUpdate = {
      name: data.deptor.name,
      employee_id: data.deptor.employe_id,
      phone: data.deptop.phone,
      identity_type: data.deptor.identity_type,
      identity_file: data.deptor.identity_file,
      identity_file_status: data.deptor.identity_file_status,
      type: data.deptor.type,
      marriage_type: data.deptor.marriage_type,
      marriage_file: data.deptor.marriage_file,
      marriage_file_status: data.deptor.marriage_file_status,
      family_file: data.deptor.family_file,
      family_file_status: data.deptor.family_file_status,
      spouse_type: data.deptor.spouse_type,
      spouse_file_type: data.deptor.spouse_file_type,
      spouse_file: data.deptor.spouse_file,
      spouse_file_status: data.deptor.spouse_file_status,
      photo: data.deptor.photo,
      photo_status: data.deptor.photo_status,
      spouse_photo: data.deptor.spouse_photo,
      spouse_photo_status: data.deptor.spouse_photo_status,
      description: data.deptor.description,
      form: data.form,
      account_number: data.account_number,
      amount: data.amount,
      period_type: data.period_type,
      period_amount: data.period_amount,
      application_date: data.application_date,
      companyId: data.companyId,
      chat: isOpenUnder ? '' : data.chat,
      status: data.status
    };
    const response = await axios.put(`${API_URL}/application/update/${data.id}`, { dtUpdate });
    setResponseData(response.data.data);
  }, [data, isOpenUnder]);
  useEffect(() => {
    updateData();
  }, [updateData]);

  return { doUpdate: updateData };
};

export { useCreditUpdateData };
