import React, { useEffect, useState } from 'react'
import { getInitialValues } from '../formUtil'
import { Tab, TabList, TabPanel as TabBody, Tabs } from 'react-tabs'
import { MdLanguage } from 'react-icons/md'
import ViewPage from '../../../Layout/Dashboard/ViewPage';
import { Rate, Spin } from 'antd';
import { usePageState } from '../../../lib/statemangment/LayoutPagestate';
import LoadingPage from '../../../Layout/app/LoadingPage';
import { useTranslation } from 'react-i18next';
import {  useGetOffer, useUpdateOffer } from '../../../api/Offer';
import useNavigateOnSuccess from '../../../Hooks/useNavigateOnSuccess';
import Form from './EditForm';
import { useParams } from 'react-router-dom';

const EditPage = () => {
  const { setObjectToEdit, objectToEdit } = usePageState()
  const { t } = useTranslation();
  const {id} = useParams()
  const { data,isRefetching } = useGetOffer({
    show:id
  })
  const { mutate, isSuccess,isLoading:IsloadingButton } = useUpdateOffer()
  const handleSubmit = (values: any) => {
    if( typeof values?.image === "string" ){
      delete values["image"]
    }
    mutate({...values, _method : "put"});
}


  useNavigateOnSuccess(isSuccess, '/Offer')


  useEffect(() => {

    setObjectToEdit(data?.data);

  }, [data]);


  const getValidationSchema = () => {
    return null
  };
  if (   !objectToEdit || isRefetching) {
    return <Spin />
  }



  const ViewProps = { getInitialValues, getValidationSchema, handleSubmit,IsloadingButton };


  return (
    <div className='ViewPage'>
      {objectToEdit && data ?
        <ViewPage {...ViewProps}>
          <Tabs>
            <TabList>
              <Tab><div className='Tab_Info_Container'><span className='SignleDriverInfoIcon'><MdLanguage size={20} /></span> <h6 className='Tab_Info'>{t("BasicInfo")}</h6></div></Tab>

            </TabList>
            <TabBody >
              <div className=" mt-4"><Form /></div>
            </TabBody>


          </Tabs>
        </ViewPage>
        : <LoadingPage />}


    </div>
  )

}

export default EditPage