import { faArrowLeft, faSearch } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-native-fontawesome";
import { useIsFocused, useNavigation } from "@react-navigation/native";
import React, { useEffect, useRef, useState } from "react";
import {
  Image,
  KeyboardAvoidingView,
  StyleSheet,
  TextInput,
  TouchableOpacity,
  View,
} from "react-native";
import obn from "../../../assets/obn.png";
const SearchHeader = ({ unfocus, searchProduct, cat }) => {
  const [searchKeyword, setsearchKeyword] = useState("");
  const navigation = useNavigation();
  const isFocused = useIsFocused();
  useEffect(() => {
    setsearchKeyword(cat);
    // searchProduct(searchKeyword);
  }, [isFocused]);

  console.log(searchKeyword);
  const Search = (e) => {
    setsearchKeyword(e);
  };
  const textInputRef = useRef(null);
  useEffect(() => {
    focusTextInput();
  }, []);
  useEffect(() => {
    unfocusTextInput();
  }, [unfocus]);

  const focusTextInput = () => {
    if (textInputRef.current) {
      textInputRef.current.focus();
    }
  };
  const unfocusTextInput = () => {
    if (textInputRef.current) {
      textInputRef.current.blur();
    }
  };

  return (
    <View style={styles.container}>
      <View style={styles.wrapper}>
        <TouchableOpacity
          onPress={() => navigation.navigate("Home")}
          style={styles.backIcon}
        >
          <FontAwesomeIcon icon={faArrowLeft} color={"#d4c600"} size={25} />
        </TouchableOpacity>
        <TextInput
          style={styles.input}
          ref={textInputRef}
          autoFocus={true}
          placeholder="Search product or vendorss"
          placeholderTextColor="#a5a5a5"
          onChangeText={(e) => Search(e)}
          defaultValue={cat ?? ""}
          onSubmitEditing={() => searchProduct(searchKeyword)}
        />
        <TouchableOpacity
          onPress={() => searchProduct(searchKeyword)}
          style={styles.backIcon}
        >
          <FontAwesomeIcon icon={faSearch} color={"#888888"} size={25} />
        </TouchableOpacity>
      </View>
    </View>
  );
};
const styles = StyleSheet.create({
  container: {
    // flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
    width: "100%",
    // top: 0,
    // position: "fixed",
    paddingVertical: 15,
    paddingTop: 5,
    // height:80
    //   boxShadow: '0px 0px 4px 0px rgba(0, 0, 0, 0.25)'
  },
  wrapper: {
    flexDirection: "row",
    width: "95%",
    alignItems: "center",
    justifyContent: "space-around",
    margin: "auto",
  },
  button: {
    padding: 15,
  },
  imgLogo: {
    height: 30,
    width: 30,
  },
  input: {
    backgroundColor: "#f5f5f5dd",
    padding: 10,
    paddingHorizontal: 20,
    borderRadius: 10,
    width: "70%",
  },
});
export default SearchHeader;
