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 {  useGetMeal, useUpdateMeal } from '../../../api/Meal';
import useNavigateOnSuccess from '../../../Hooks/useNavigateOnSuccess';
import Form from './EditForm';
import { useParams } from 'react-router-dom';
import { isItArrayOfString } from '../../../utils/Array/isItArrayOfString';

const EditPage = () => {
  const { setObjectToEdit, objectToEdit } = usePageState()
  const { t } = useTranslation();
  const {id} = useParams()
  const { data,isRefetching } = useGetMeal({
    show:id
  })
  const { mutate, isSuccess,isLoading:IsloadingButton } = useUpdateMeal()

 

  const handleSubmit = (values: any) => {
      const data_to_send = {...values}
      
    if( typeof values?.video === "string" ){
      delete data_to_send["video"]
    }
    if(isItArrayOfString(values?.images)){
      delete data_to_send["images"]
    }

    mutate(data_to_send);
}


  useNavigateOnSuccess(isSuccess, '/Meal')


  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