import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useParams } from "react-router-dom";
import { fetchPropertyById } from "../redux/features/propertySlice";
import propertydummy from "../img/propertydummy.jpg"
import Navbar from "./Navbar";
import Footer from "./Footer";



const PropertyView = () => {
  const { id } = useParams(); // Extract the property ID from the route
  const dispatch = useDispatch();
  const { selectedProperty, status, error } = useSelector((state) => state.property);
  const [loading, setLoading] = useState(true); // Loader state

  useEffect(() => {
    // Fetch the property by ID when the component loads
    if (id) {
      dispatch(fetchPropertyById(id));
      setLoading(false);
    }
  }, [id, dispatch]);

  if (status === "loading") {
    return <p>Loading property details...</p>;
  }

  if (status === "failed") {
    return <p>Error loading property: {error}</p>;
  }

//   console.log("selectedProperty", selectedProperty);

  return (
    <>

<Navbar />
      <br /> <br /> <br /> <br />
 
      <div className="container col-12">
        {loading ? (
          <div className="loader">Loading...</div> // Loader
        ) : selectedProperty ? (
          <div className="py-3 py-md-5 bg-light">
            {/* <div className="container"> */}
            <div className="card-header bg-white">
              <div className="row">
                <div className="col-md-5 mt-3">
                  <div className="bg-white border">
                  {/* {selectedProperty.images && selectedProperty.images[0] && (
                    <img
                    src={selectedProperty.images[0].file || propertydummy}
                    alt="Property Thumbnail"
                    style={{ width: "100px", height: "auto" }}
                    />
                    )} */}

                    {selectedProperty.images && selectedProperty.images[0] ? (
  <img
    src={selectedProperty.images[0].file || propertydummy}
    alt="Property Thumbnail"
    style={{
                              marginTop: "0px",
                              maxWidth: "500px",
                              maxHeight: "500px",
                            }}
  />
) : (
  <img
    src={propertydummy}
    alt="Default Property Thumbnail"
    style={{
                              marginTop: "0px",
                              maxWidth: "500px",
                              maxHeight: "500px",
                            }}
  />
)}

               
                  </div>
                </div>
                <div className="col-md-7 mt-3">
                  <div className="product-view">
                    <h4
                      className="product-name"
                      style={{ color: "#fda417", fontSize: "25px" }}
                    >
                      {selectedProperty.address}
                      <label className="label-stock bg-success">
                        Verified Property
                      </label>
                    </h4>
                    <hr />
                    <p className="product-path">
                      <span style={{ color: "#fda417", fontSize: "15px" }}>
                        city:{" "}
                      </span>{" "}
                      {selectedProperty.city} /
                      <span style={{ color: "#fda417", fontSize: "15px" }}>
                        County:{" "}
                      </span>{" "}
                      {selectedProperty.county} /
                      <span style={{ color: "#fda417", fontSize: "15px" }}>
                        State:{" "}
                      </span>{" "}
                      {selectedProperty.state} /
                      <span style={{ color: "#fda417", fontSize: "15px" }}>
                        Zipcode:
                      </span>{" "}
                      {selectedProperty.zip}
                    </p>
                    <div>
                      <span
                        className="selling-price"
                        style={{ color: "#fda417", fontSize: "15px" }}
                      >
                        Total Living Square Foot:{" "}
                      </span>
                      {selectedProperty.totallivingsqft}
                      <p></p>
                      <span
                        className=""
                        style={{ color: "#fda417", fontSize: "15px" }}
                      >
                        Cost per Square Foot:{" "}
                      </span>
                      ${selectedProperty.costpersqft}/sqft
                      <p></p>
                      <span
                        className=""
                        style={{ color: "#fda417", fontSize: "15px" }}
                      >
                        Year Built:{" "}
                      </span>
                      {selectedProperty.yearBuild}
                    </div>

                    <div className="mt-3 card bg-white">
  <h5 className="mb-0" style={{ color: "#fda417", fontSize: "15px" }}>
    Legal Description
  </h5>
  <span>
    {selectedProperty.legal
      ? selectedProperty.legal
      : "No data available"}
  </span>
</div>

                  </div>
                </div>
              </div>
              <div className="row">
                <div className="col-md-12 mt-3">
                  <div className="card">
                    <div className="card-header bg-white">
                      <h4
                        className="product-name"
                        style={{ color: "#fda417", fontSize: "25px" }}
                      >
                        Description
                      </h4>
                    </div>
                    <div className="card-body">
                      <p>
                        Lorem Ipsum is simply dummy text of the printing and
                        typesetting industry. Lorem Ipsum has been the
                        industry's standard dummy text ever since the 1500s,
                        when an unknown printer took a galley of type and
                        scrambled it to make a type specimen book. It has
                        survived not only five centuries, but also the leap into
                        electronic typesetting, remaining essentially unchanged.
                        It was popularised in the 1960s with the release of
                        Letraset sheets containing Lorem Ipsum passages, and
                        more recently with desktop publishing software like
                        Aldus PageMaker including versions of Lorem Ipsum.
                      </p>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        ) : (
          <p>No property details found.</p>
        )}
      </div>
      <Footer />

    </>
  );
};

export default PropertyView;
