import React, { useState, useEffect } from 'react';
import axios from 'axios';
import PhoneInput from 'react-phone-number-input';
import { API_URL} from '@/Constants';  
import SuccessMessage from './SuccessMessage';

 

type FormDataType = {
    name: string;
    email: string;
    ph: string;
    sect: string; // Convert to string for URLSearchParams compatibility
    category: string; // Convert to string for URLSearchParams compatibility
    address: string;
};
interface Option {
    id: string; // or number, based on your data
    name: string;
}
const ListYourProperty = () => {  
      
        const handlePhoneChange = (value: string) => {
            formData.ph = value;  
        };
    const [formData, setFormData] = useState<FormDataType>({
        name: '',
        email: '',
        ph: '',
        sect: '', // Convert sect to string
        category: '', // Convert sect to string
        address: '',
    });
 
  

    const [loading, setLoading] = useState(false);
    const [responseMessage, setResponseMessage] = useState('');
    const [successNotification, setSuccessNotification] = useState('');
    const [errors, setErrors] = useState<Record<string, string>>({}); // For form validation
    const [options, setOptions] = useState<Option[]>([]); // State to hold the options fetched from the API

    // Fetch data when the component mounts
    useEffect(() => {
        const fetchOptions = async () => {
            try {
                const response = await fetch(`${API_URL}listing_category`); // Replace with your API URL
                const data: Option[] = await response.json();
                setOptions(data); // Assuming the response data is an array of options
            } catch (error) {
                console.error('Error fetching options:', error);
            }
        };

        fetchOptions();
    }, []);
    const handleInputChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>) => {
        const { name, value } = e.target;
        setSuccessNotification('');
        setFormData((prevData) => ({
            ...prevData,
            [name]: value,
        }));
        setErrors((prevErrors) => ({
            ...prevErrors,
            [name]: '', // Set the error for the specific field to an empty string
        }));
    };

    const validateForm = () => {
        const formErrors: Record<string, string> = {};
        if (!formData.name.trim()) formErrors.name = 'Name is required';
        if (!formData.email.trim()) formErrors.email = 'Email is required';
        if (!formData.ph.trim()) formErrors.ph = 'Phone is required'; 
        if (!formData.sect.trim()) formErrors.sect = 'Section is required'; 
        if (!formData.category.trim()) formErrors.category = 'Category is required'; 
        if (!formData.address.trim()) formErrors.address = 'Property Address is required';

        setErrors(formErrors);
        return Object.keys(formErrors).length === 0;
    };

    const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
        e.preventDefault();

        if (!validateForm()) {
            console.log("ERROR")
            return;
        }
        console.log("WER")
        setLoading(true);
        try {
            const params = new URLSearchParams({
                name: formData.name,
                email: formData.email,
                ph: formData.ph,
                sect: formData.sect, // Ensure sect is a string
                category: formData.category, // Ensure sect is a string
                address: formData.address
            });

            const response = await axios.get(`${API_URL}send_list_your_property`, { params });

            if (response.data.status === 'success') {
                setSuccessNotification(response.data.message);
                setFormData({
                    name: '',
                    email: '',
                    ph: '',
                    sect: '', // Reset sect
                    category: '', // Reset sect
                    address: ''
                });
            } else {
                setResponseMessage('There was an error with your submission.');
                if (response.data.errors) {
                    setErrors(response.data.errors);
                }
            }
        } catch (error) {
            setResponseMessage('Network error occurred. Please try again later.');
        } finally {
            setLoading(false);
        }
    };

    return (
        <>
        <div className="row ">
                <div className="col-sm-12  ">
                     
                    <div className=" ">
                        <div className="row d-flex">
                            <div className="col-sm-6 left-list-property">
                                <form
                                    className='pure-form pure-form-aligned main-enquiry-form at-contact-us lis-propert'

                                    onSubmit={handleSubmit}>
                                    <fieldset className="d-block">
                                        <legend>

                                            <div className="featured-title-hold text-center clearfix">
                                                <h3 className="site-h1 animate mt-3 mb-3">
                                                    <div className="line-container-parent">
                                                        <div className="lines-container flex-end">
                                                            <div className="line small-line"></div>
                                                            <div className="line big-line"></div>
                                                        </div>
                                                        <h5 className="line-text">list Your Property</h5>
                                                        <div className="lines-container">
                                                            <div className="line small-line"></div>
                                                            <div className="line big-line"></div>
                                                        </div>
                                                    </div>
                                                </h3>
                                            </div>


                                        </legend>
                                        <div className="division">
                                            {responseMessage && 
                                            (
                                                <p>{responseMessage}</p>

                                               
                                            )}
                                            {successNotification && 
                                            (
                                                <SuccessMessage responseMessage={successNotification} />
                                               
                                            )}

                                             

                                            <div className="pure-control-group form-icon form-icon-person">
                                                <input
                                                    className="pure-input-1"
                                                    placeholder="Name"
                                                    name="name"
                                                    type="text"
                                                    value={formData.name}
                                                    onChange={handleInputChange}
                                                />
                                                {errors.name && <div className="errorMessage">{errors.name}</div>}
                                            </div>
                                            <div className="pure-control-group form-icon form-icon-mail">
                                                <input
                                                    className="pure-input-1"
                                                    placeholder="Email"
                                                    name="email"
                                                    type="text"
                                                    value={formData.email}
                                                    onChange={handleInputChange}
                                                />
                                                {errors.email && <div className="errorMessage">{errors.email}</div>}
                                            </div>
                                            <div className="pure-control-group form-icon form-icon-mobile">
                                                <PhoneInput
                                                    international
                                                    defaultCountry="AE"
                                                    className="pure-input-1"
                                                    name="ph"
                                                    value={formData.ph}
                                                    onChange={handlePhoneChange}
                                                    placeholder="Phone"
                                                />
                                                {errors.ph && <div className="errorMessage">{errors.ph}</div>}
                                            </div>
                                        </div>

                                        <div className="division pt0">
                                            <div className="row row-select-category">
                                                <div className="col-sm-5">
                                                    <div className="pure-control-group form-icon form-icon-mail">
                                                    <select
                                                        className="pure-input-1"
                                                        name="sect"
                                                       
                                                        value={formData.sect}
                                                        onChange={handleInputChange}
                                                    >
                                                        <option value="">Select Option</option>
                                                        <option value="1">For Sale</option>
                                                        <option value="2">For Rent</option>
                                                    </select>
                                                    
                                                    {errors.sect && <div className="errorMessage">{errors.sect}</div>}
                                                    </div>
                                                </div>
                                                <div className="col-sm-7">
                                                    <div className="pure-control-group form-icon form-icon-mail">
                                                        <select
                                                            className="pure-input-1"
                                                            name="category"
                                                             
                                                            value={formData.category}
                                                            onChange={handleInputChange}
                                                        >
                                                            <option value="">Please Category</option>
                                                            {options.map((option) => (
                                                                <option key={option.id} value={option.id}>
                                                                    {option.name}
                                                                </option>
                                                            ))}
                                                        </select>

                                                        {errors.category && <div className="errorMessage">{errors.category}</div>}
                                                    </div>
                                                </div>
                                            </div>
                                            <div className="pure-control-group  ">
                                                <textarea
                                                    className="pure-input-1"
                                                    placeholder="Property Address"
                                                    name="address"
                                                    value={formData.address}
                                                    onChange={handleInputChange}
                                                />
                                                {errors.address && <div className="errorMessage">{errors.address}</div>}
                                            </div>
                                            <div className="pure-control-group">
                                                <button className="button button-primary" type="submit">
                                                    {loading ? 'Processing...' : <><span className="icon-mail-3"></span> Submit  Details</>}
                                                </button>
                                            </div>
                                        </div>
                                    </fieldset>
                                </form>
                            </div>
                            <div className="col-sm-6 right-list-property">

                            </div>
                        </div>
                        
                       
                    </div>
                     
            </div>
        </div>
        
        </>
    );
};

export default ListYourProperty;
