import React, { useState } from "react";
import {
  View,
  Text,
  TextInput,
  TouchableOpacity,
  StyleSheet,
  Alert,
} from "react-native";
import { Ionicons } from "@expo/vector-icons"; // Import Ionicons untuk ikon

export default function InputContact({ route, navigation }) {
  const { method } = route.params; // Mendapatkan 'method' yang diteruskan dari halaman sebelumnya
  const [contactInfo, setContactInfo] = useState(""); // Input untuk nomor WhatsApp atau email

  const handleSubmit = async () => {
    // Validasi input berdasarkan metode
    if (!contactInfo) {
      Alert.alert(
        "Peringatan",
        "Nomor WhatsApp atau Email tidak boleh kosong!"
      );
      return;
    }

    if (method === "whatsapp" && !/^\d+$/.test(contactInfo)) {
      Alert.alert("Peringatan", "Masukkan nomor WhatsApp yang valid!");
      return;
    }

    if (method === "email" && !/\S+@\S+\.\S+/.test(contactInfo)) {
      Alert.alert("Peringatan", "Masukkan email yang valid!");
      return;
    }

    try {
      // Kirim nomor WhatsApp atau email ke API untuk verifikasi (sesuaikan dengan backend)
      // await sendContactInfo(contactInfo, method);

      // Navigasi ke halaman verifikasi kode
      navigation.navigate("VerifyCode"); // Pastikan halaman "VerifyCode" sudah terdaftar dalam navigator
    } catch (error) {
      Alert.alert("Error", "Terjadi kesalahan saat mengirim data.");
    }
  };

  return (
    <View style={styles.container}>
      {/* Header dengan tombol kembali */}
      <View style={styles.header}>
        <TouchableOpacity
          onPress={() => navigation.goBack()}
          style={styles.backButton}
        >
          <Ionicons name="arrow-back" size={24} color="#000" />
        </TouchableOpacity>
        <View style={styles.headerTextContainer}>
          <Text style={styles.title}>LUPA KATA SANDI</Text>
        </View>
      </View>

      <Text style={styles.subtitle}>
        Masukkan {method === "whatsapp" ? "Nomor WhatsApp" : "Email"}
      </Text>

      <TextInput
        style={styles.input}
        placeholder={method === "whatsapp" ? "Nomor WhatsApp" : "Email"}
        value={contactInfo}
        onChangeText={setContactInfo}
        keyboardType={method === "whatsapp" ? "numeric" : "email-address"}
      />

      <TouchableOpacity
        style={styles.button}
        onPress={() => navigation.navigate("VerifyCode")}
      >
        <Text style={styles.buttonText}>KIRIM KODE</Text>
      </TouchableOpacity>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "flex-start",
    alignItems: "center",
    backgroundColor: "#FFF",
  },
  header: {
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "flex-start",
    width: "100%",
    padding: 10,
    backgroundColor: "#FFF",
    borderBottomWidth: 0,
    borderBottomColor: "#e0e0e0",
    marginTop: 30,
  },
  backButton: {
    padding: 10,
  },
  headerTextContainer: {
    flex: 1,
    alignItems: "flex-start",
    justifyContent: "center",
  },
  title: {
    fontSize: 20,
    fontWeight: "bold",
    color: "#000",
  },
  subtitle: {
    fontSize: 16,
    marginVertical: 20,
    color: "#8e8e8e",
  },
  input: {
    width: "90%",
    padding: 15,
    marginBottom: 20,
    borderWidth: 1,
    borderRadius: 15,
    borderColor: "#8e8e8e",
    fontSize: 16,
  },
  button: {
    width: "90%",
    padding: 15,
    backgroundColor: "#2D572C", // Warna hijau untuk tombol kirim
    alignItems: "center",
    borderRadius: 15,
  },
  buttonText: {
    color: "#fff",
    fontSize: 16,
    fontWeight: "bold",
  },
});
